blob: 6929501c4e406253c7cdff930a743b4dfaba28f9 [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 Moolenaarbaaa7e92016-01-29 22:47:03 +010052static void screenclear2(void);
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
879#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100880static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881
882/*
Bram Moolenaar367329b2007-08-30 11:53:22 +0000883 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 */
885 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100886status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887{
888 int len = 0;
889
890#ifdef FEAT_MENU
891 int emenu = (xp->xp_context == EXPAND_MENUS
892 || xp->xp_context == EXPAND_MENUNAMES);
893
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100894 // Check for menu separators - replace with '|'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 if (emenu && menu_is_separator(s))
896 return 1;
897#endif
898
899 while (*s != NUL)
900 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +0000901 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +0000902 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100903 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 }
905
906 return len;
907}
908
909/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +0000910 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000911 * These are backslashes used for escaping. Do show backslashes in help tags.
912 */
913 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100914skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000915{
Bram Moolenaar7693ec62008-07-24 18:29:37 +0000916 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000917#ifdef FEAT_MENU
918 || ((xp->xp_context == EXPAND_MENUS
919 || xp->xp_context == EXPAND_MENUNAMES)
920 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
921#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +0000922 )
923 {
924#ifndef BACKSLASH_IN_FILENAME
925 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
926 return 2;
927#endif
928 return 1;
929 }
930 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000931}
932
933/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 * Show wildchar matches in the status line.
935 * Show at least the "match" item.
936 * We start at item 'first_match' in the list and show all matches that fit.
937 *
938 * If inversion is possible we use it. Else '=' characters are used.
939 */
940 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100941win_redr_status_matches(
942 expand_T *xp,
943 int num_matches,
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100944 char_u **matches, // list of matches
Bram Moolenaar05540972016-01-30 20:31:25 +0100945 int match,
946 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947{
948#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
949 int row;
950 char_u *buf;
951 int len;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100952 int clen; // length in screen cells
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 int fillchar;
954 int attr;
955 int i;
956 int highlight = TRUE;
957 char_u *selstart = NULL;
958 int selstart_col = 0;
959 char_u *selend = NULL;
960 static int first_match = 0;
961 int add_left = FALSE;
962 char_u *s;
963#ifdef FEAT_MENU
964 int emenu;
965#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100968 if (matches == NULL) // interrupted completion?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 return;
970
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000971 if (has_mbyte)
Bram Moolenaar964b3742019-05-24 18:54:09 +0200972 buf = alloc(Columns * MB_MAXBYTES + 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000973 else
Bram Moolenaar964b3742019-05-24 18:54:09 +0200974 buf = alloc(Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 if (buf == NULL)
976 return;
977
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100978 if (match == -1) // don't show match but original text
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 {
980 match = 0;
981 highlight = FALSE;
982 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100983 // count 1 for the ending ">"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 clen = status_match_len(xp, L_MATCH(match)) + 3;
985 if (match == 0)
986 first_match = 0;
987 else if (match < first_match)
988 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100989 // jumping left, as far as we can go
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 first_match = match;
991 add_left = TRUE;
992 }
993 else
994 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100995 // check if match fits on the screen
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 for (i = first_match; i < match; ++i)
997 clen += status_match_len(xp, L_MATCH(i)) + 2;
998 if (first_match > 0)
999 clen += 2;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001000 // jumping right, put match at the left
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 if ((long)clen > Columns)
1002 {
1003 first_match = match;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001004 // if showing the last match, we can add some on the left
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 clen = 2;
1006 for (i = match; i < num_matches; ++i)
1007 {
1008 clen += status_match_len(xp, L_MATCH(i)) + 2;
1009 if ((long)clen >= Columns)
1010 break;
1011 }
1012 if (i == num_matches)
1013 add_left = TRUE;
1014 }
1015 }
1016 if (add_left)
1017 while (first_match > 0)
1018 {
1019 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
1020 if ((long)clen >= Columns)
1021 break;
1022 --first_match;
1023 }
1024
Bram Moolenaar3633cf52017-07-31 22:29:35 +02001025 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026
1027 if (first_match == 0)
1028 {
1029 *buf = NUL;
1030 len = 0;
1031 }
1032 else
1033 {
1034 STRCPY(buf, "< ");
1035 len = 2;
1036 }
1037 clen = len;
1038
1039 i = first_match;
1040 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
1041 {
1042 if (i == match)
1043 {
1044 selstart = buf + len;
1045 selstart_col = clen;
1046 }
1047
1048 s = L_MATCH(i);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001049 // Check for menu separators - replace with '|'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050#ifdef FEAT_MENU
1051 emenu = (xp->xp_context == EXPAND_MENUS
1052 || xp->xp_context == EXPAND_MENUNAMES);
1053 if (emenu && menu_is_separator(s))
1054 {
1055 STRCPY(buf + len, transchar('|'));
1056 l = (int)STRLEN(buf + len);
1057 len += l;
1058 clen += l;
1059 }
1060 else
1061#endif
1062 for ( ; *s != NUL; ++s)
1063 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00001064 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 clen += ptr2cells(s);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001066 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 {
1068 STRNCPY(buf + len, s, l);
1069 s += l - 1;
1070 len += l;
1071 }
1072 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 {
1074 STRCPY(buf + len, transchar_byte(*s));
1075 len += (int)STRLEN(buf + len);
1076 }
1077 }
1078 if (i == match)
1079 selend = buf + len;
1080
1081 *(buf + len++) = ' ';
1082 *(buf + len++) = ' ';
1083 clen += 2;
1084 if (++i == num_matches)
1085 break;
1086 }
1087
1088 if (i != num_matches)
1089 {
1090 *(buf + len++) = '>';
1091 ++clen;
1092 }
1093
1094 buf[len] = NUL;
1095
1096 row = cmdline_row - 1;
1097 if (row >= 0)
1098 {
1099 if (wild_menu_showing == 0)
1100 {
1101 if (msg_scrolled > 0)
1102 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001103 // Put the wildmenu just above the command line. If there is
1104 // no room, scroll the screen one line up.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 if (cmdline_row == Rows - 1)
1106 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001107 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 ++msg_scrolled;
1109 }
1110 else
1111 {
1112 ++cmdline_row;
1113 ++row;
1114 }
1115 wild_menu_showing = WM_SCROLLED;
1116 }
1117 else
1118 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001119 // Create status line if needed by setting 'laststatus' to 2.
1120 // Set 'winminheight' to zero to avoid that the window is
1121 // resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 if (lastwin->w_status_height == 0)
1123 {
1124 save_p_ls = p_ls;
1125 save_p_wmh = p_wmh;
1126 p_ls = 2;
1127 p_wmh = 0;
1128 last_status(FALSE);
1129 }
1130 wild_menu_showing = WM_SHOWN;
1131 }
1132 }
1133
1134 screen_puts(buf, row, 0, attr);
1135 if (selstart != NULL && highlight)
1136 {
1137 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001138 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 }
1140
1141 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
1142 }
1143
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 vim_free(buf);
1146}
1147#endif
1148
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 * Return TRUE if the status line of window "wp" is connected to the status
1151 * line of the window right of it. If not, then it's a vertical separator.
1152 * Only call if (wp->w_vsep_width != 0).
1153 */
1154 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001155stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156{
1157 frame_T *fr;
1158
1159 fr = wp->w_frame;
1160 while (fr->fr_parent != NULL)
1161 {
1162 if (fr->fr_parent->fr_layout == FR_COL)
1163 {
1164 if (fr->fr_next != NULL)
1165 break;
1166 }
1167 else
1168 {
1169 if (fr->fr_next != NULL)
1170 return TRUE;
1171 }
1172 fr = fr->fr_parent;
1173 }
1174 return FALSE;
1175}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178/*
1179 * Get the value to show for the language mappings, active 'keymap'.
1180 */
1181 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001182get_keymap_str(
1183 win_T *wp,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001184 char_u *fmt, // format string containing one %s item
1185 char_u *buf, // buffer for the result
1186 int len) // length of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187{
1188 char_u *p;
1189
1190 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
1191 return FALSE;
1192
1193 {
1194#ifdef FEAT_EVAL
1195 buf_T *old_curbuf = curbuf;
1196 win_T *old_curwin = curwin;
1197 char_u *s;
1198
1199 curbuf = wp->w_buffer;
1200 curwin = wp;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001201 STRCPY(buf, "b:keymap_name"); // must be writable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 ++emsg_skip;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001203 s = p = eval_to_string(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 --emsg_skip;
1205 curbuf = old_curbuf;
1206 curwin = old_curwin;
1207 if (p == NULL || *p == NUL)
1208#endif
1209 {
1210#ifdef FEAT_KEYMAP
1211 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
1212 p = wp->w_buffer->b_p_keymap;
1213 else
1214#endif
1215 p = (char_u *)"lang";
1216 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02001217 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 buf[0] = NUL;
1219#ifdef FEAT_EVAL
1220 vim_free(s);
1221#endif
1222 }
1223 return buf[0] != NUL;
1224}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225
1226#if defined(FEAT_STL_OPT) || defined(PROTO)
1227/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001228 * Redraw the status line or ruler of window "wp".
1229 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001231 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001232win_redr_custom(
1233 win_T *wp,
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001234 int draw_ruler) // TRUE or FALSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235{
Bram Moolenaar1d633412013-12-11 15:52:01 +01001236 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 int attr;
1238 int curattr;
1239 int row;
1240 int col = 0;
1241 int maxwidth;
1242 int width;
1243 int n;
1244 int len;
1245 int fillchar;
1246 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00001247 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 char_u *p;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01001249 stl_hlrec_T *hltab;
1250 stl_hlrec_T *tabtab;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001251 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01001252 win_T *ewp;
1253 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001255 // There is a tiny chance that this gets called recursively: When
1256 // redrawing a status line triggers redrawing the ruler or tabline.
1257 // Avoid trouble by not allowing recursion.
Bram Moolenaar1d633412013-12-11 15:52:01 +01001258 if (entered)
1259 return;
1260 entered = TRUE;
1261
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001262 // setup environment for the task at hand
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001263 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 {
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001265 // Use 'tabline'. Always at the first line of the screen.
Bram Moolenaar362f3562009-11-03 16:20:34 +00001266 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001267 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00001268 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01001269 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001270 maxwidth = Columns;
1271# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001272 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001273# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001275 else
1276 {
Bram Moolenaar49c51b82021-04-01 16:16:18 +02001277 row = statusline_row(wp);
Bram Moolenaar3633cf52017-07-31 22:29:35 +02001278 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02001279 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001280
1281 if (draw_ruler)
1282 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00001283 stl = p_ruf;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001284 // advance past any leading group spec - implicit in ru_col
Bram Moolenaar362f3562009-11-03 16:20:34 +00001285 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001286 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00001287 if (*++stl == '-')
1288 stl++;
1289 if (atoi((char *)stl))
1290 while (VIM_ISDIGIT(*stl))
1291 stl++;
1292 if (*stl++ != '(')
1293 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001294 }
Bram Moolenaar02631462017-09-22 15:20:32 +02001295 col = ru_col - (Columns - wp->w_width);
1296 if (col < (wp->w_width + 1) / 2)
1297 col = (wp->w_width + 1) / 2;
1298 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001299 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001300 {
1301 row = Rows - 1;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001302 --maxwidth; // writing in last column may cause scrolling
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001303 fillchar = ' ';
1304 attr = 0;
1305 }
1306
1307# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001308 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001309# endif
1310 }
1311 else
1312 {
1313 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00001314 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001315 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00001316 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001317# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001318 use_sandbox = was_set_insecurely((char_u *)"statusline",
1319 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001320# endif
1321 }
1322
Bram Moolenaar53f81742017-09-22 14:35:51 +02001323 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001324 }
1325
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01001327 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001329 // Temporarily reset 'cursorbind', we don't want a side effect from moving
1330 // the cursor away and back.
Bram Moolenaar61452852011-02-01 18:01:11 +01001331 ewp = wp == NULL ? curwin : wp;
1332 p_crb_save = ewp->w_p_crb;
1333 ewp->w_p_crb = FALSE;
1334
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001335 // Make a copy, because the statusline may include a function call that
1336 // might change the option value and free the memory.
Bram Moolenaar362f3562009-11-03 16:20:34 +00001337 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01001338 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00001339 stl, use_sandbox,
Bram Moolenaar8133cc62020-10-26 21:05:27 +01001340 fillchar, maxwidth, &hltab, &tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00001341 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01001342 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001344 // Make all characters printable.
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01001345 p = transstr(buf);
1346 if (p != NULL)
1347 {
1348 vim_strncpy(buf, p, sizeof(buf) - 1);
1349 vim_free(p);
1350 }
1351
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001352 // fill up with "fillchar"
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01001353 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00001354 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 len += (*mb_char2bytes)(fillchar, buf + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 ++width;
1358 }
1359 buf[len] = NUL;
1360
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001361 /*
1362 * Draw each snippet with the specified highlighting.
1363 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 curattr = attr;
1365 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001366 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001368 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 screen_puts_len(p, len, row, col, curattr);
1370 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001371 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001373 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001375 else if (hltab[n].userhl < 0)
1376 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02001377#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02001378 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
1379 && wp->w_status_height != 0)
1380 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02001381 else if (wp != NULL && bt_terminal(wp->w_buffer)
1382 && wp->w_status_height != 0)
1383 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02001384#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00001385 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001386 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001388 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 }
1390 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001391
1392 if (wp == NULL)
1393 {
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001394 // Fill the TabPageIdxs[] array for clicking in the tab pagesline.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001395 col = 0;
1396 len = 0;
1397 p = buf;
1398 fillchar = 0;
1399 for (n = 0; tabtab[n].start != NULL; n++)
1400 {
1401 len += vim_strnsize(p, (int)(tabtab[n].start - p));
1402 while (col < len)
1403 TabPageIdxs[col++] = fillchar;
1404 p = tabtab[n].start;
1405 fillchar = tabtab[n].userhl;
1406 }
1407 while (col < Columns)
1408 TabPageIdxs[col++] = fillchar;
1409 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01001410
1411theend:
1412 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413}
1414
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001415#endif // FEAT_STL_OPT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416
1417/*
1418 * Output a single character directly to the screen and update ScreenLines.
1419 */
1420 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001421screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 char_u buf[MB_MAXBYTES + 1];
1424
Bram Moolenaar9a920d82012-06-01 15:21:02 +02001425 if (has_mbyte)
1426 buf[(*mb_char2bytes)(c, buf)] = NUL;
1427 else
Bram Moolenaar9a920d82012-06-01 15:21:02 +02001428 {
1429 buf[0] = c;
1430 buf[1] = NUL;
1431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 screen_puts(buf, row, col, attr);
1433}
1434
1435/*
1436 * Get a single character directly from ScreenLines into "bytes[]".
1437 * Also return its attribute in *attrp;
1438 */
1439 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001440screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441{
1442 unsigned off;
1443
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001444 // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
1446 {
1447 off = LineOffset[row] + col;
1448 *attrp = ScreenAttrs[off];
1449 bytes[0] = ScreenLines[off];
1450 bytes[1] = NUL;
1451
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 if (enc_utf8 && ScreenLinesUC[off] != 0)
1453 bytes[utfc_char2bytes(off, bytes)] = NUL;
1454 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
1455 {
1456 bytes[0] = ScreenLines[off];
1457 bytes[1] = ScreenLines2[off];
1458 bytes[2] = NUL;
1459 }
1460 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
1461 {
1462 bytes[1] = ScreenLines[off + 1];
1463 bytes[2] = NUL;
1464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 }
1466}
1467
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001468/*
1469 * Return TRUE if composing characters for screen posn "off" differs from
1470 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01001471 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001472 */
1473 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001474screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001475{
1476 int i;
1477
1478 for (i = 0; i < Screen_mco; ++i)
1479 {
1480 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
1481 return TRUE;
1482 if (u8cc[i] == 0)
1483 break;
1484 }
1485 return FALSE;
1486}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001487
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488/*
1489 * Put string '*text' on the screen at position 'row' and 'col', with
1490 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
1491 * Note: only outputs within one row, message is truncated at screen boundary!
1492 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
1493 */
1494 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001495screen_puts(
1496 char_u *text,
1497 int row,
1498 int col,
1499 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500{
1501 screen_puts_len(text, -1, row, col, attr);
1502}
1503
1504/*
1505 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
1506 * a NUL.
1507 */
1508 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001509screen_puts_len(
1510 char_u *text,
1511 int textlen,
1512 int row,
1513 int col,
Bram Moolenaar35d8c202022-03-03 11:46:00 +00001514 int attr_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515{
Bram Moolenaar35d8c202022-03-03 11:46:00 +00001516 int attr = attr_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 unsigned off;
1518 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02001519 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 int c;
Bram Moolenaar367329b2007-08-30 11:53:22 +00001521 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 int mbyte_blen = 1;
1523 int mbyte_cells = 1;
1524 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001525 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 int clear_next_cell = FALSE;
Bram Moolenaara12a1612019-01-24 16:39:02 +01001527#ifdef FEAT_ARABIC
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001528 int prev_c = 0; // previous Arabic character
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001529 int pc, nc, nc1;
1530 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001532 int force_redraw_this;
1533 int force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001534 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535
Bram Moolenaar0b4c9ed2019-06-03 22:04:23 +02001536 // Safety check. The check for negative row and column is to fix issue
1537 // #4102. TODO: find out why row/col could be negative.
1538 if (ScreenLines == NULL
1539 || row >= screen_Rows || row < 0
1540 || col >= screen_Columns || col < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001542 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001544 // When drawing over the right half of a double-wide char clear out the
1545 // left half. Only needed in a terminal.
Bram Moolenaar7693ec62008-07-24 18:29:37 +00001546 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaara12a1612019-01-24 16:39:02 +01001547#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00001548 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01001549#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00001550 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001551 {
1552 ScreenLines[off - 1] = ' ';
1553 ScreenAttrs[off - 1] = 0;
1554 if (enc_utf8)
1555 {
1556 ScreenLinesUC[off - 1] = 0;
1557 ScreenLinesC[0][off - 1] = 0;
1558 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001559 // redraw the previous cell, make it empty
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001560 screen_char(off - 1, row, col - 1);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001561 // force the cell at "col" to be redrawn
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001562 force_redraw_next = TRUE;
1563 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00001564
Bram Moolenaar367329b2007-08-30 11:53:22 +00001565 max_off = LineOffset[row] + screen_Columns;
Bram Moolenaara064ac82007-08-05 18:10:54 +00001566 while (col < screen_Columns
1567 && (len < 0 || (int)(ptr - text) < len)
1568 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 {
1570 c = *ptr;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001571 // check if this is the first byte of a multibyte
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 if (has_mbyte)
1573 {
zeertzjq4dc513a2022-07-25 19:42:02 +01001574 mbyte_blen = enc_utf8 && len > 0
1575 ? utfc_ptr2len_len(ptr, (int)((text + len) - ptr))
1576 : (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
1578 mbyte_cells = 1;
1579 else if (enc_dbcs != 0)
1580 mbyte_cells = mbyte_blen;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001581 else // enc_utf8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 {
zeertzjq4dc513a2022-07-25 19:42:02 +01001583 u8c = len >= 0
1584 ? utfc_ptr2char_len(ptr, u8cc, (int)((text + len) - ptr))
1585 : utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaara12a1612019-01-24 16:39:02 +01001587#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
1589 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001590 // Do Arabic shaping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
1592 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001593 // Past end of string to be displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 nc = NUL;
1595 nc1 = NUL;
1596 }
1597 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001598 {
zeertzjq4dc513a2022-07-25 19:42:02 +01001599 nc = len >= 0
1600 ? utfc_ptr2char_len(ptr + mbyte_blen, pcc,
1601 (int)((text + len) - ptr - mbyte_blen))
1602 : utfc_ptr2char(ptr + mbyte_blen, pcc);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001603 nc1 = pcc[0];
1604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 pc = prev_c;
1606 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001607 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 }
1609 else
1610 prev_c = u8c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01001611#endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01001612 if (col + mbyte_cells > screen_Columns)
1613 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001614 // Only 1 cell left, but character requires 2 cells:
1615 // display a '>' in the last column to avoid wrapping.
Bram Moolenaare4ebd292010-01-19 17:40:46 +01001616 c = '>';
1617 mbyte_cells = 1;
1618 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 }
1620 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001622 force_redraw_this = force_redraw_next;
1623 force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001624
1625 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 || (mbyte_cells == 2
1627 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
1628 || (enc_dbcs == DBCS_JPNU
1629 && c == 0x8e
1630 && ScreenLines2[off] != ptr[1])
1631 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01001632 && (ScreenLinesUC[off] !=
1633 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
1634 || (ScreenLinesUC[off] != 0
1635 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001637 || exmode_active;
1638
Bram Moolenaar24a5ac52019-06-08 19:01:18 +02001639 if ((need_redraw || force_redraw_this)
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001640#ifdef FEAT_PROP_POPUP
Bram Moolenaarc662ec92019-06-23 00:15:57 +02001641 && !blocked_by_popup(row, col)
Bram Moolenaar24a5ac52019-06-08 19:01:18 +02001642#endif
1643 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 {
1645#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001646 // The bold trick makes a single row of pixels appear in the next
1647 // character. When a bold character is removed, the next
1648 // character should be redrawn too. This happens for our own GUI
1649 // and for some xterms.
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001650 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651# ifdef FEAT_GUI
1652 gui.in_use
1653# endif
1654# if defined(FEAT_GUI) && defined(UNIX)
1655 ||
1656# endif
1657# ifdef UNIX
1658 term_is_xterm
1659# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001660 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001662 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001664 if (n > HL_ALL)
1665 n = syn_attr2attr(n);
1666 if (n & HL_BOLD)
1667 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 }
1669#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001670 // When at the end of the text and overwriting a two-cell
1671 // character with a one-cell character, need to clear the next
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001672 // cell. Also when overwriting the left half of a two-cell char
1673 // with the right half of a two-cell char. Do this only once
1674 // (mb_off2cells() may return 2 on the right half).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 if (clear_next_cell)
1676 clear_next_cell = FALSE;
1677 else if (has_mbyte
1678 && (len < 0 ? ptr[mbyte_blen] == NUL
1679 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00001680 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00001682 && (*mb_off2cells)(off, max_off) == 1
1683 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 clear_next_cell = TRUE;
1685
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001686 // Make sure we never leave a second byte of a double-byte behind,
1687 // it confuses mb_off2cells().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00001689 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00001691 && (*mb_off2cells)(off, max_off) == 1
1692 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 ScreenLines[off + mbyte_blen] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 ScreenLines[off] = c;
1695 ScreenAttrs[off] = attr;
Bram Moolenaarb9081882022-07-09 04:56:24 +01001696 ScreenCols[off] = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 if (enc_utf8)
1698 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001699 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 ScreenLinesUC[off] = 0;
1701 else
1702 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001703 int i;
1704
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001706 for (i = 0; i < Screen_mco; ++i)
1707 {
1708 ScreenLinesC[i][off] = u8cc[i];
1709 if (u8cc[i] == 0)
1710 break;
1711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 }
1713 if (mbyte_cells == 2)
1714 {
1715 ScreenLines[off + 1] = 0;
1716 ScreenAttrs[off + 1] = attr;
Bram Moolenaarb9081882022-07-09 04:56:24 +01001717 ScreenCols[off + 1] = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 }
1719 screen_char(off, row, col);
1720 }
1721 else if (mbyte_cells == 2)
1722 {
1723 ScreenLines[off + 1] = ptr[1];
1724 ScreenAttrs[off + 1] = attr;
Bram Moolenaarb9081882022-07-09 04:56:24 +01001725 ScreenCols[off + 1] = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 screen_char_2(off, row, col);
1727 }
1728 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
1729 {
1730 ScreenLines2[off] = ptr[1];
1731 screen_char(off, row, col);
1732 }
1733 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 screen_char(off, row, col);
1735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 if (has_mbyte)
1737 {
1738 off += mbyte_cells;
1739 col += mbyte_cells;
1740 ptr += mbyte_blen;
1741 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02001742 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001743 // This only happens at the end, display one space next.
Bram Moolenaar35d8c202022-03-03 11:46:00 +00001744 // Keep the attribute from before.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02001746 len = -1;
Bram Moolenaar35d8c202022-03-03 11:46:00 +00001747 attr = ScreenAttrs[off];
Bram Moolenaare4c21e62014-05-22 16:05:19 +02001748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 }
1750 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 {
1752 ++off;
1753 ++col;
1754 ++ptr;
1755 }
1756 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001757
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001758 // If we detected the next character needs to be redrawn, but the text
1759 // doesn't extend up to there, update the character here.
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001760 if (force_redraw_next && col < screen_Columns)
1761 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001762 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
1763 screen_char_2(off, row, col);
1764 else
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001765 screen_char(off, row, col);
1766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767}
1768
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001769#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001771 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001773 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001774start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775{
1776 if (p_hls && !no_hlsearch)
1777 {
Bram Moolenaar0b6849e2020-05-02 18:33:25 +02001778 end_search_hl(); // just in case it wasn't called before
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001779 last_pat_prog(&screen_search_hl.rm);
1780 screen_search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 }
1782}
1783
1784/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001785 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001787 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001788end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789{
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001790 if (screen_search_hl.rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 {
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001792 vim_regfree(screen_search_hl.rm.regprog);
1793 screen_search_hl.rm.regprog = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 }
1795}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02001796#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02001797
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001799screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800{
1801 attrentry_T *aep = NULL;
1802
1803 screen_attr = attr;
1804 if (full_screen
Bram Moolenaar4f974752019-02-17 17:44:42 +01001805#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 && termcap_active
1807#endif
1808 )
1809 {
1810#ifdef FEAT_GUI
1811 if (gui.in_use)
1812 {
1813 char buf[20];
1814
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001815 // The GUI handles this internally.
Bram Moolenaar424bcae2022-01-31 14:59:41 +00001816 sprintf(buf, "\033|%dh", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 OUT_STR(buf);
1818 }
1819 else
1820#endif
1821 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001822 if (attr > HL_ALL) // special HL attr.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001824 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 aep = syn_cterm_attr2entry(attr);
1826 else
1827 aep = syn_term_attr2entry(attr);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001828 if (aep == NULL) // did ":syntax clear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 attr = 0;
1830 else
1831 attr = aep->ae_attr;
1832 }
Bram Moolenaara050b942019-12-02 21:35:31 +01001833#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
1834 if (use_vtp())
1835 {
1836 guicolor_T defguifg, defguibg;
1837 int defctermfg, defctermbg;
1838
1839 // If FG and BG are unset, the color is undefined when
1840 // BOLD+INVERSE. Use Normal as the default value.
1841 get_default_console_color(&defctermfg, &defctermbg, &defguifg,
1842 &defguibg);
1843
1844 if (p_tgc)
1845 {
1846 if (aep == NULL || COLOR_INVALID(aep->ae_u.cterm.fg_rgb))
1847 term_fg_rgb_color(defguifg);
1848 if (aep == NULL || COLOR_INVALID(aep->ae_u.cterm.bg_rgb))
1849 term_bg_rgb_color(defguibg);
1850 }
1851 else if (t_colors >= 256)
1852 {
1853 if (aep == NULL || aep->ae_u.cterm.fg_color == 0)
1854 term_fg_color(defctermfg);
1855 if (aep == NULL || aep->ae_u.cterm.bg_color == 0)
1856 term_bg_color(defctermbg);
1857 }
1858 }
1859#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001860 if ((attr & HL_BOLD) && *T_MD != NUL) // bold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001862 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001863#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001864 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
1865 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
1866 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001867#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001868 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001869 // If the Normal FG color has BOLD attribute and the new HL
1870 // has a FG color defined, clear BOLD.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001871 out_str(T_ME);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001872 if ((attr & HL_STANDOUT) && *T_SO != NUL) // standout
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 out_str(T_SO);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001874 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) // undercurl
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01001875 out_str(T_UCS);
Bram Moolenaar84f54632022-06-29 18:39:11 +01001876 if ((attr & HL_UNDERDOUBLE) && *T_USS != NUL) // double underline
1877 out_str(T_USS);
1878 if ((attr & HL_UNDERDOTTED) && *T_DS != NUL) // dotted underline
1879 out_str(T_DS);
1880 if ((attr & HL_UNDERDASHED) && *T_CDS != NUL) // dashed underline
1881 out_str(T_CDS);
1882 if (((attr & HL_UNDERLINE) // underline or undercurl, etc.
1883 || ((attr & HL_UNDERCURL) && *T_UCS == NUL)
1884 || ((attr & HL_UNDERDOUBLE) && *T_USS == NUL)
1885 || ((attr & HL_UNDERDOTTED) && *T_DS == NUL)
1886 || ((attr & HL_UNDERDASHED) && *T_CDS == NUL))
Bram Moolenaar45a00002017-12-22 21:12:34 +01001887 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 out_str(T_US);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001889 if ((attr & HL_ITALIC) && *T_CZH != NUL) // italic
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 out_str(T_CZH);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001891 if ((attr & HL_INVERSE) && *T_MR != NUL) // inverse (reverse)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 out_str(T_MR);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001893 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) // strike
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02001894 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895
1896 /*
1897 * Output the color or start string after bold etc., in case the
1898 * bold etc. override the color setting.
1899 */
1900 if (aep != NULL)
1901 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001902#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001903 // When 'termguicolors' is set but fg or bg is unset,
1904 // fall back to the cterm colors. This helps for SpellBad,
1905 // where the GUI uses a red undercurl.
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001906 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001908 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001909 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001910 }
1911 else
1912#endif
1913 if (t_colors > 1)
1914 {
1915 if (aep->ae_u.cterm.fg_color)
1916 term_fg_color(aep->ae_u.cterm.fg_color - 1);
1917 }
1918#ifdef FEAT_TERMGUICOLORS
1919 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
1920 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001921 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001922 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 }
1924 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001925#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001926 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001928 if (aep->ae_u.cterm.bg_color)
1929 term_bg_color(aep->ae_u.cterm.bg_color - 1);
1930 }
Bram Moolenaare023e882020-05-31 16:42:30 +02001931#ifdef FEAT_TERMGUICOLORS
1932 if (p_tgc && aep->ae_u.cterm.ul_rgb != CTERMCOLOR)
1933 {
1934 if (aep->ae_u.cterm.ul_rgb != INVALCOLOR)
1935 term_ul_rgb_color(aep->ae_u.cterm.ul_rgb);
1936 }
1937 else
1938#endif
1939 if (t_colors > 1)
1940 {
1941 if (aep->ae_u.cterm.ul_color)
1942 term_ul_color(aep->ae_u.cterm.ul_color - 1);
1943 }
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001944
Bram Moolenaarf708ac52018-03-12 21:48:32 +01001945 if (!IS_CTERM)
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001946 {
1947 if (aep->ae_u.term.start != NULL)
1948 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 }
1950 }
1951 }
1952 }
1953}
1954
1955 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001956screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001958 int do_ME = FALSE; // output T_ME code
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02001959#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Bram Moolenaar09307e32020-05-29 21:42:55 +02001960 int do_ME_fg = FALSE, do_ME_bg = FALSE;
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02001961#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962
1963 if (screen_attr != 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01001964#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 && termcap_active
1966#endif
1967 )
1968 {
1969#ifdef FEAT_GUI
1970 if (gui.in_use)
1971 {
1972 char buf[20];
1973
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001974 // use internal GUI code
Bram Moolenaar424bcae2022-01-31 14:59:41 +00001975 sprintf(buf, "\033|%dH", screen_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 OUT_STR(buf);
1977 }
1978 else
1979#endif
1980 {
Bram Moolenaar84f54632022-06-29 18:39:11 +01001981 int is_under;
1982
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001983 if (screen_attr > HL_ALL) // special HL attr.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 {
1985 attrentry_T *aep;
1986
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001987 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 {
1989 /*
1990 * Assume that t_me restores the original colors!
1991 */
1992 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001993 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001994#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001995 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
1996 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02001997# ifdef FEAT_VTP
1998 ? !(do_ME_fg = TRUE) : (do_ME_fg = FALSE)
1999# endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01002000 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002001#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01002002 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02002003#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01002004 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
2005 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02002006# ifdef FEAT_VTP
2007 ? !(do_ME_bg = TRUE) : (do_ME_bg = FALSE)
2008# endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01002009 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002010#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01002011 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 do_ME = TRUE;
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02002013#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
2014 if (use_vtp())
2015 {
2016 if (do_ME_fg && do_ME_bg)
2017 do_ME = TRUE;
2018
2019 // FG and BG cannot be separated in T_ME, which is not
2020 // efficient.
2021 if (!do_ME && do_ME_fg)
2022 out_str((char_u *)"\033|39m"); // restore FG
2023 if (!do_ME && do_ME_bg)
2024 out_str((char_u *)"\033|49m"); // restore BG
2025 }
2026 else
2027 {
2028 // Process FG and BG at once.
2029 if (!do_ME)
2030 do_ME = do_ME_fg | do_ME_bg;
2031 }
2032#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 }
2034 else
2035 {
2036 aep = syn_term_attr2entry(screen_attr);
2037 if (aep != NULL && aep->ae_u.term.stop != NULL)
2038 {
2039 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
2040 do_ME = TRUE;
2041 else
2042 out_str(aep->ae_u.term.stop);
2043 }
2044 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002045 if (aep == NULL) // did ":syntax clear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 screen_attr = 0;
2047 else
2048 screen_attr = aep->ae_attr;
2049 }
2050
2051 /*
2052 * Often all ending-codes are equal to T_ME. Avoid outputting the
2053 * same sequence several times.
2054 */
2055 if (screen_attr & HL_STANDOUT)
2056 {
2057 if (STRCMP(T_SE, T_ME) == 0)
2058 do_ME = TRUE;
2059 else
2060 out_str(T_SE);
2061 }
Bram Moolenaar84f54632022-06-29 18:39:11 +01002062 is_under = (screen_attr & (HL_UNDERCURL
2063 | HL_UNDERDOUBLE | HL_UNDERDOTTED | HL_UNDERDASHED));
2064 if (is_under && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01002065 {
2066 if (STRCMP(T_UCE, T_ME) == 0)
2067 do_ME = TRUE;
2068 else
2069 out_str(T_UCE);
2070 }
Bram Moolenaar84f54632022-06-29 18:39:11 +01002071 if ((screen_attr & HL_UNDERLINE) || (is_under && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 {
2073 if (STRCMP(T_UE, T_ME) == 0)
2074 do_ME = TRUE;
2075 else
2076 out_str(T_UE);
2077 }
2078 if (screen_attr & HL_ITALIC)
2079 {
2080 if (STRCMP(T_CZR, T_ME) == 0)
2081 do_ME = TRUE;
2082 else
2083 out_str(T_CZR);
2084 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02002085 if (screen_attr & HL_STRIKETHROUGH)
2086 {
2087 if (STRCMP(T_STE, T_ME) == 0)
2088 do_ME = TRUE;
2089 else
2090 out_str(T_STE);
2091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
2093 out_str(T_ME);
2094
Bram Moolenaar61be73b2016-04-29 22:59:22 +02002095#ifdef FEAT_TERMGUICOLORS
2096 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002098 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002099 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002100 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002101 term_bg_rgb_color(cterm_normal_bg_gui_color);
Bram Moolenaare023e882020-05-31 16:42:30 +02002102 if (cterm_normal_ul_gui_color != INVALCOLOR)
2103 term_ul_rgb_color(cterm_normal_ul_gui_color);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002104 }
2105 else
2106#endif
2107 {
2108 if (t_colors > 1)
2109 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002110 // set Normal cterm colors
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002111 if (cterm_normal_fg_color != 0)
2112 term_fg_color(cterm_normal_fg_color - 1);
2113 if (cterm_normal_bg_color != 0)
2114 term_bg_color(cterm_normal_bg_color - 1);
Bram Moolenaare023e882020-05-31 16:42:30 +02002115 if (cterm_normal_ul_color != 0)
2116 term_ul_color(cterm_normal_ul_color - 1);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002117 if (cterm_normal_fg_bold)
2118 out_str(T_MD);
2119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 }
2121 }
2122 }
2123 screen_attr = 0;
2124}
2125
2126/*
2127 * Reset the colors for a cterm. Used when leaving Vim.
2128 * The machine specific code may override this again.
2129 */
2130 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002131reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002133 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002135 // set Normal cterm colors
Bram Moolenaar61be73b2016-04-29 22:59:22 +02002136#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002137 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
2138 || cterm_normal_bg_gui_color != INVALCOLOR)
2139 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002140#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002142#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 {
2144 out_str(T_OP);
2145 screen_attr = -1;
2146 }
2147 if (cterm_normal_fg_bold)
2148 {
2149 out_str(T_ME);
2150 screen_attr = -1;
2151 }
2152 }
2153}
2154
2155/*
2156 * Put character ScreenLines["off"] on the screen at position "row" and "col",
2157 * using the attributes from ScreenAttrs["off"].
2158 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002159 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002160screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161{
2162 int attr;
2163
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002164 // Check for illegal values, just in case (could happen just after
2165 // resizing).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 if (row >= screen_Rows || col >= screen_Columns)
2167 return;
2168
Bram Moolenaar33796b32019-06-08 16:01:13 +02002169 // Skip if under the popup menu.
2170 // Popup windows with zindex higher than POPUPMENU_ZINDEX go on top.
Bakudankun65555002021-11-17 20:40:16 +00002171 if (pum_under_menu(row, col, TRUE)
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002172#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002173 && screen_zindex <= POPUPMENU_ZINDEX
Bram Moolenaare2c453d2019-08-21 14:37:09 +02002174#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02002175 )
Bram Moolenaarae654382019-01-17 21:09:05 +01002176 return;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002177#ifdef FEAT_PROP_POPUP
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002178 if (blocked_by_popup(row, col))
Bram Moolenaar33796b32019-06-08 16:01:13 +02002179 return;
2180#endif
2181
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002182 // Outputting a character in the last cell on the screen may scroll the
2183 // screen up. Only do it when the "xn" termcap property is set, otherwise
2184 // mark the character invalid (update it when scrolled up).
Bram Moolenaar494838a2015-02-10 19:20:37 +01002185 if (*T_XN == NUL
2186 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187#ifdef FEAT_RIGHTLEFT
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002188 // account for first command-line character in rightleft mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 && !cmdmsg_rl
2190#endif
2191 )
2192 {
2193 ScreenAttrs[off] = (sattr_T)-1;
Bram Moolenaarb9081882022-07-09 04:56:24 +01002194 ScreenCols[off] = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 return;
2196 }
2197
2198 /*
2199 * Stop highlighting first, so it's easier to move the cursor.
2200 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 if (screen_char_attr != 0)
2202 attr = screen_char_attr;
2203 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 attr = ScreenAttrs[off];
2205 if (screen_attr != attr)
2206 screen_stop_highlight();
2207
2208 windgoto(row, col);
2209
2210 if (screen_attr != attr)
2211 screen_start_highlight(attr);
2212
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 if (enc_utf8 && ScreenLinesUC[off] != 0)
2214 {
2215 char_u buf[MB_MAXBYTES + 1];
2216
Bram Moolenaarcb070082016-04-02 22:14:51 +02002217 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002218 {
2219 if (*p_ambw == 'd'
Bram Moolenaara12a1612019-01-24 16:39:02 +01002220#ifdef FEAT_GUI
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002221 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01002222#endif
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002223 )
2224 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002225 // Clear the two screen cells. If the character is actually
2226 // single width it won't change the second cell.
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002227 out_str((char_u *)" ");
2228 term_windgoto(row, col);
2229 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002230 // not sure where the cursor is after drawing the ambiguous width
2231 // character
Bram Moolenaarcb070082016-04-02 22:14:51 +02002232 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002233 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02002234 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002236
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002237 // Convert the UTF-8 character to bytes and write it.
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002238 buf[utfc_char2bytes(off, buf)] = NUL;
2239 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 }
2241 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 out_char(ScreenLines[off]);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002245 // double-byte character in single-width cell
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2247 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 }
2249
2250 screen_cur_col++;
2251}
2252
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253/*
2254 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
2255 * on the screen at position 'row' and 'col'.
2256 * The attributes of the first byte is used for all. This is required to
2257 * output the two bytes of a double-byte character with nothing in between.
2258 */
2259 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002260screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002262 // Check for illegal values (could be wrong when screen was resized).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
2264 return;
2265
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002266 // Outputting the last character on the screen may scrollup the screen.
2267 // Don't to it! Mark the character invalid (update it when scrolled up)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
2269 {
2270 ScreenAttrs[off] = (sattr_T)-1;
Bram Moolenaarb9081882022-07-09 04:56:24 +01002271 ScreenCols[off] = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 return;
2273 }
2274
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002275 // Output the first byte normally (positions the cursor), then write the
2276 // second byte directly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 screen_char(off, row, col);
2278 out_char(ScreenLines[off + 1]);
2279 ++screen_cur_col;
2280}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282/*
2283 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
2284 * This uses the contents of ScreenLines[] and doesn't change it.
2285 */
2286 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002287screen_draw_rectangle(
2288 int row,
2289 int col,
2290 int height,
2291 int width,
2292 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293{
2294 int r, c;
2295 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00002296 int max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002298 // Can't use ScreenLines unless initialized
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002299 if (ScreenLines == NULL)
2300 return;
2301
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 if (invert)
2303 screen_char_attr = HL_INVERSE;
2304 for (r = row; r < row + height; ++r)
2305 {
2306 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00002307 max_off = off + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 for (c = col; c < col + width; ++c)
2309 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00002310 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 {
2312 screen_char_2(off + c, r, c);
2313 ++c;
2314 }
2315 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 {
2317 screen_char(off + c, r, c);
Bram Moolenaar367329b2007-08-30 11:53:22 +00002318 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 ++c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 }
2321 }
2322 }
2323 screen_char_attr = 0;
2324}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326/*
2327 * Redraw the characters for a vertically split window.
2328 */
2329 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002330redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331{
2332 int col;
2333 int width;
2334
2335# ifdef FEAT_CLIPBOARD
2336 clip_may_clear_selection(row, end - 1);
2337# endif
2338
2339 if (wp == NULL)
2340 {
2341 col = 0;
2342 width = Columns;
2343 }
2344 else
2345 {
2346 col = wp->w_wincol;
2347 width = wp->w_width;
2348 }
2349 screen_draw_rectangle(row, col, end - row, width, FALSE);
2350}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002352 void
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002353space_to_screenline(int off, int attr)
2354{
2355 ScreenLines[off] = ' ';
2356 ScreenAttrs[off] = attr;
Bram Moolenaarb9081882022-07-09 04:56:24 +01002357 ScreenCols[off] = -1;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002358 if (enc_utf8)
2359 ScreenLinesUC[off] = 0;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002360}
2361
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362/*
Bram Moolenaarcee9c842022-04-09 12:40:13 +01002363 * Fill the screen from "start_row" to "end_row" (exclusive), from "start_col"
2364 * to "end_col" (exclusive) with character "c1" in first column followed by
2365 * "c2" in the other columns. Use attributes "attr".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 */
2367 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002368screen_fill(
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002369 int start_row,
2370 int end_row,
2371 int start_col,
2372 int end_col,
2373 int c1,
2374 int c2,
2375 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002377 int row;
2378 int col;
2379 int off;
2380 int end_off;
2381 int did_delete;
2382 int c;
2383 int norm_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002385 int force_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386#endif
2387
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002388 if (end_row > screen_Rows) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 end_row = screen_Rows;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002390 if (end_col > screen_Columns) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 end_col = screen_Columns;
2392 if (ScreenLines == NULL
2393 || start_row >= end_row
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002394 || start_col >= end_col) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 return;
2396
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002397 // it's a "normal" terminal when not in a GUI or cterm
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 norm_term = (
2399#ifdef FEAT_GUI
2400 !gui.in_use &&
2401#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002402 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 for (row = start_row; row < end_row; ++row)
2404 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00002405 if (has_mbyte
Bram Moolenaara12a1612019-01-24 16:39:02 +01002406#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00002407 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01002408#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00002409 )
2410 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00002411 // When drawing over the right half of a double-wide char clear
2412 // out the left half. When drawing over the left half of a
2413 // double wide-char clear out the right half. Only needed in a
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002414 // terminal.
Bram Moolenaar7693ec62008-07-24 18:29:37 +00002415 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00002416 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00002417 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00002418 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00002419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 /*
2421 * Try to use delete-line termcap code, when no attributes or in a
2422 * "normal" terminal, where a bold/italic space is just a
2423 * space.
2424 */
2425 did_delete = FALSE;
2426 if (c2 == ' '
2427 && end_col == Columns
2428 && can_clear(T_CE)
2429 && (attr == 0
2430 || (norm_term
2431 && attr <= HL_ALL
2432 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
2433 {
2434 /*
2435 * check if we really need to clear something
2436 */
2437 col = start_col;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002438 if (c1 != ' ') // don't clear first char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 ++col;
2440
2441 off = LineOffset[row] + col;
2442 end_off = LineOffset[row] + end_col;
2443
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002444 // skip blanks (used often, keep it fast!)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 if (enc_utf8)
2446 while (off < end_off && ScreenLines[off] == ' '
2447 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
2448 ++off;
2449 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 while (off < end_off && ScreenLines[off] == ' '
2451 && ScreenAttrs[off] == 0)
2452 ++off;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002453 if (off < end_off) // something to be cleared
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 {
2455 col = off - LineOffset[row];
2456 screen_stop_highlight();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002457 term_windgoto(row, col);// clear rest of this screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 out_str(T_CE);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002459 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 col = end_col - col;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002461 while (col--) // clear chars in ScreenLines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002463 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 ++off;
2465 }
2466 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002467 did_delete = TRUE; // the chars are cleared now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 }
2469
2470 off = LineOffset[row] + start_col;
2471 c = c1;
2472 for (col = start_col; col < end_col; ++col)
2473 {
Bram Moolenaar33796b32019-06-08 16:01:13 +02002474 if ((ScreenLines[off] != c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002475 || (enc_utf8 && (int)ScreenLinesUC[off]
2476 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 || ScreenAttrs[off] != attr
2478#if defined(FEAT_GUI) || defined(UNIX)
2479 || force_next
2480#endif
2481 )
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002482#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002483 // Skip if under a(nother) popup.
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002484 && !blocked_by_popup(row, col)
Bram Moolenaar33796b32019-06-08 16:01:13 +02002485#endif
2486 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 {
2488#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002489 // The bold trick may make a single row of pixels appear in
2490 // the next character. When a bold character is removed, the
2491 // next character should be redrawn too. This happens for our
2492 // own GUI and for some xterms.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 if (
2494# ifdef FEAT_GUI
2495 gui.in_use
2496# endif
2497# if defined(FEAT_GUI) && defined(UNIX)
2498 ||
2499# endif
2500# ifdef UNIX
2501 term_is_xterm
2502# endif
2503 )
2504 {
2505 if (ScreenLines[off] != ' '
2506 && (ScreenAttrs[off] > HL_ALL
2507 || ScreenAttrs[off] & HL_BOLD))
2508 force_next = TRUE;
2509 else
2510 force_next = FALSE;
2511 }
2512#endif
2513 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 if (enc_utf8)
2515 {
2516 if (c >= 0x80)
2517 {
2518 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002519 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 }
2521 else
2522 ScreenLinesUC[off] = 0;
2523 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 ScreenAttrs[off] = attr;
2525 if (!did_delete || c != ' ')
2526 screen_char(off, row, col);
2527 }
Bram Moolenaarb9081882022-07-09 04:56:24 +01002528 ScreenCols[off] = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 ++off;
2530 if (col == start_col)
2531 {
2532 if (did_delete)
2533 break;
2534 c = c2;
2535 }
2536 }
2537 if (end_col == Columns)
2538 LineWraps[row] = FALSE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002539 if (row == Rows - 1) // overwritten the command line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 {
2541 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02002542 if (start_col == 0 && end_col == Columns
2543 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002544 clear_cmdline = FALSE; // command line has been cleared
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002545 if (start_col == 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002546 mode_displayed = FALSE; // mode cleared or overwritten
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 }
2548 }
2549}
2550
2551/*
2552 * Check if there should be a delay. Used before clearing or redrawing the
2553 * screen or the command line.
2554 */
2555 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002556check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557{
2558 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
2559 && !did_wait_return
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002560 && emsg_silent == 0
2561 && !in_assert_fails)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 {
2563 out_flush();
Bram Moolenaareda1da02019-11-17 17:06:33 +01002564 ui_delay(1006L, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 emsg_on_display = FALSE;
2566 if (check_msg_scroll)
2567 msg_scroll = FALSE;
2568 }
2569}
2570
2571/*
Bram Moolenaarca57ab52019-04-13 14:53:16 +02002572 * Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect.
2573 */
2574 static void
2575clear_TabPageIdxs(void)
2576{
2577 int scol;
2578
2579 for (scol = 0; scol < Columns; ++scol)
2580 TabPageIdxs[scol] = 0;
2581}
2582
2583/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002585 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 * Returns TRUE if there is a valid screen to write to.
2587 * Returns FALSE when starting up and screen not initialized yet.
2588 */
2589 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002590screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002592 screenalloc(doclear); // allocate screen buffers if size changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 return (ScreenLines != NULL);
2594}
2595
2596/*
2597 * Resize the shell to Rows and Columns.
2598 * Allocate ScreenLines[] and associated items.
2599 *
2600 * There may be some time between setting Rows and Columns and (re)allocating
2601 * ScreenLines[]. This happens when starting up and when (manually) changing
2602 * the shell size. Always use screen_Rows and screen_Columns to access items
2603 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
2604 * final size of the shell is needed.
2605 */
2606 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002607screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608{
2609 int new_row, old_row;
2610#ifdef FEAT_GUI
2611 int old_Rows;
2612#endif
2613 win_T *wp;
2614 int outofmem = FALSE;
2615 int len;
2616 schar_T *new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002618 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002620 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 sattr_T *new_ScreenAttrs;
Bram Moolenaarb9081882022-07-09 04:56:24 +01002622 colnr_T *new_ScreenCols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 unsigned *new_LineOffset;
2624 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002625 short *new_TabPageIdxs;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002626#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002627 short *new_popup_mask;
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002628 short *new_popup_mask_next;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002629 char *new_popup_transparent;
Bram Moolenaar33796b32019-06-08 16:01:13 +02002630#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00002631 tabpage_T *tp;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002632 static int entered = FALSE; // avoid recursiveness
2633 static int done_outofmem_msg = FALSE; // did outofmem message
Bram Moolenaar87e817c2009-02-22 20:13:39 +00002634 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635
Bram Moolenaar87e817c2009-02-22 20:13:39 +00002636retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 /*
2638 * Allocation of the screen buffers is done only when the size changes and
2639 * when Rows and Columns have been set and we have started doing full
2640 * screen stuff.
2641 */
2642 if ((ScreenLines != NULL
2643 && Rows == screen_Rows
2644 && Columns == screen_Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 && enc_utf8 == (ScreenLinesUC != NULL)
2646 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaara12a1612019-01-24 16:39:02 +01002647 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 || Rows == 0
2649 || Columns == 0
2650 || (!full_screen && ScreenLines == NULL))
2651 return;
2652
2653 /*
2654 * It's possible that we produce an out-of-memory message below, which
2655 * will cause this function to be called again. To break the loop, just
2656 * return here.
2657 */
2658 if (entered)
2659 return;
2660 entered = TRUE;
2661
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00002662 /*
2663 * Note that the window sizes are updated before reallocating the arrays,
2664 * thus we must not redraw here!
2665 */
2666 ++RedrawingDisabled;
2667
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002668 win_new_shellsize(); // fit the windows in the new sized shell
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002670#ifdef FEAT_GUI_HAIKU
2671 vim_lock_screen(); // be safe, put it here
2672#endif
2673
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002674 comp_col(); // recompute columns for shown command and ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675
2676 /*
2677 * We're changing the size of the screen.
2678 * - Allocate new arrays for ScreenLines and ScreenAttrs.
2679 * - Move lines from the old arrays into the new arrays, clear extra
2680 * lines (unless the screen is going to be cleared).
2681 * - Free the old arrays.
2682 *
2683 * If anything fails, make ScreenLines NULL, so we don't do anything!
2684 * Continuing with the old ScreenLines may result in a crash, because the
2685 * size is wrong.
2686 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002687 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00002689 if (aucmd_win != NULL)
2690 win_free_lsize(aucmd_win);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002691#ifdef FEAT_PROP_POPUP
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002692 // global popup windows
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002693 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002694 win_free_lsize(wp);
2695 // tab-local popup windows
2696 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002697 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002698 win_free_lsize(wp);
2699#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002701 new_ScreenLines = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
Bram Moolenaar216b7102010-03-23 13:56:59 +01002702 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 if (enc_utf8)
2704 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002705 new_ScreenLinesUC = LALLOC_MULT(u8char_T, (Rows + 1) * Columns);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002706 for (i = 0; i < p_mco; ++i)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002707 new_ScreenLinesC[i] = LALLOC_CLEAR_MULT(u8char_T,
2708 (Rows + 1) * Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 }
2710 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002711 new_ScreenLines2 = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
2712 new_ScreenAttrs = LALLOC_MULT(sattr_T, (Rows + 1) * Columns);
Bram Moolenaarb9081882022-07-09 04:56:24 +01002713 new_ScreenCols = LALLOC_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 Moolenaar63d9e732019-12-05 21:10:38 +01002909 must_redraw = CLEAR; // need to clear the screen later
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002910 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 screenclear2();
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
2974 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002975screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976{
2977 check_for_delay(FALSE);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002978 screenalloc(FALSE); // allocate screen buffers if size changed
2979 screenclear2(); // clear the screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980}
2981
2982 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002983screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984{
2985 int i;
2986
2987 if (starting == NO_SCREEN || ScreenLines == NULL
2988#ifdef FEAT_GUI
2989 || (gui.in_use && gui.starting)
2990#endif
2991 )
2992 return;
2993
2994#ifdef FEAT_GUI
2995 if (!gui.in_use)
2996#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002997 screen_attr = -1; // force setting the Normal colors
2998 screen_stop_highlight(); // don't want highlighting here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999
3000#ifdef FEAT_CLIPBOARD
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003001 // disable selection without redrawing it
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 clip_scroll_selection(9999);
3003#endif
3004
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003005 // blank out ScreenLines
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 for (i = 0; i < Rows; ++i)
3007 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003008 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 LineWraps[i] = FALSE;
3010 }
3011
3012 if (can_clear(T_CL))
3013 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003014 out_str(T_CL); // clear the display
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003016 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 }
3018 else
3019 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003020 // can't clear the screen, mark all chars with invalid attributes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 for (i = 0; i < Rows; ++i)
3022 lineinvalid(LineOffset[i], (int)Columns);
3023 clear_cmdline = TRUE;
3024 }
3025
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003026 screen_cleared = TRUE; // can use contents of ScreenLines now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027
3028 win_rest_invalid(firstwin);
3029 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003030 redraw_tabline = TRUE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003031 if (must_redraw == CLEAR) // no need to clear again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 must_redraw = NOT_VALID;
3033 compute_cmdrow();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003034 msg_row = cmdline_row; // put cursor on last line for messages
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 msg_col = 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003036 screen_start(); // don't know where cursor is now
3037 msg_scrolled = 0; // can't scroll back
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 msg_didany = FALSE;
3039 msg_didout = FALSE;
3040}
3041
3042/*
3043 * Clear one line in ScreenLines.
3044 */
3045 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003046lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047{
3048 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 if (enc_utf8)
3050 (void)vim_memset(ScreenLinesUC + off, 0,
3051 (size_t)width * sizeof(u8char_T));
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003052 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaarb9081882022-07-09 04:56:24 +01003053 (void)vim_memset(ScreenCols + off, -1, (size_t)width * sizeof(colnr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054}
3055
3056/*
3057 * Mark one line in ScreenLines invalid by setting the attributes to an
3058 * invalid value.
3059 */
3060 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003061lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062{
3063 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
Bram Moolenaarb9081882022-07-09 04:56:24 +01003064 (void)vim_memset(ScreenCols + off, -1, (size_t)width * sizeof(colnr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065}
3066
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067/*
Bram Moolenaar96916ac2020-07-08 23:09:28 +02003068 * To be called when characters were sent to the terminal directly, outputting
3069 * test on "screen_lnum".
3070 */
3071 void
3072line_was_clobbered(int screen_lnum)
3073{
3074 lineinvalid(LineOffset[screen_lnum], (int)Columns);
3075}
3076
3077/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 * Copy part of a Screenline for vertically split window "wp".
3079 */
3080 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003081linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082{
3083 unsigned off_to = LineOffset[to] + wp->w_wincol;
3084 unsigned off_from = LineOffset[from] + wp->w_wincol;
3085
3086 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
3087 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 if (enc_utf8)
3089 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003090 int i;
3091
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
3093 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003094 for (i = 0; i < p_mco; ++i)
3095 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
3096 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 }
3098 if (enc_dbcs == DBCS_JPNU)
3099 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
3100 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
3102 wp->w_width * sizeof(sattr_T));
Bram Moolenaarb9081882022-07-09 04:56:24 +01003103 mch_memmove(ScreenCols + off_to, ScreenCols + off_from,
3104 wp->w_width * sizeof(colnr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106
3107/*
3108 * Return TRUE if clearing with term string "p" would work.
3109 * It can't work when the string is empty or it won't set the right background.
Bram Moolenaar33796b32019-06-08 16:01:13 +02003110 * Don't clear to end-of-line when there are popups, it may cause flicker.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 */
3112 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003113can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114{
3115 return (*p != NUL && (t_colors <= 1
3116#ifdef FEAT_GUI
3117 || gui.in_use
3118#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003119#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003120 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02003121 || (!p_tgc && cterm_normal_bg_color == 0)
3122#else
3123 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003124#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02003125 || *T_UT != NUL)
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003126#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02003127 && !(p == T_CE && popup_visible)
3128#endif
3129 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130}
3131
3132/*
3133 * Reset cursor position. Use whenever cursor was moved because of outputting
3134 * something directly to the screen (shell commands) or a terminal control
3135 * code.
3136 */
3137 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003138screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139{
3140 screen_cur_row = screen_cur_col = 9999;
3141}
3142
3143/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 * Move the cursor to position "row","col" in the screen.
3145 * This tries to find the most efficient way to move, minimizing the number of
3146 * characters sent to the terminal.
3147 */
3148 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003149windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003151 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 int i;
3153 int plan;
3154 int cost;
3155 int wouldbe_col;
3156 int noinvcurs;
3157 char_u *bs;
3158 int goto_cost;
3159 int attr;
3160
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003161#define GOTO_COST 7 // assume a term_windgoto() takes about 7 chars
3162#define HIGHL_COST 5 // assume unhighlight takes 5 chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163
3164#define PLAN_LE 1
3165#define PLAN_CR 2
3166#define PLAN_NL 3
3167#define PLAN_WRITE 4
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003168 // Can't use ScreenLines unless initialized
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 if (ScreenLines == NULL)
3170 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 if (col != screen_cur_col || row != screen_cur_row)
3172 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003173 // Check for valid position.
3174 if (row < 0) // window without text lines?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 row = 0;
3176 if (row >= screen_Rows)
3177 row = screen_Rows - 1;
3178 if (col >= screen_Columns)
3179 col = screen_Columns - 1;
3180
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003181 // check if no cursor movement is allowed in highlight mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 if (screen_attr && *T_MS == NUL)
3183 noinvcurs = HIGHL_COST;
3184 else
3185 noinvcurs = 0;
3186 goto_cost = GOTO_COST + noinvcurs;
3187
3188 /*
3189 * Plan how to do the positioning:
3190 * 1. Use CR to move it to column 0, same row.
3191 * 2. Use T_LE to move it a few columns to the left.
3192 * 3. Use NL to move a few lines down, column 0.
3193 * 4. Move a few columns to the right with T_ND or by writing chars.
3194 *
3195 * Don't do this if the cursor went beyond the last column, the cursor
3196 * position is unknown then (some terminals wrap, some don't )
3197 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00003198 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 * characters to move the cursor to the right.
3200 */
3201 if (row >= screen_cur_row && screen_cur_col < Columns)
3202 {
3203 /*
3204 * If the cursor is in the same row, bigger col, we can use CR
3205 * or T_LE.
3206 */
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003207 bs = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 attr = screen_attr;
3209 if (row == screen_cur_row && col < screen_cur_col)
3210 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003211 // "le" is preferred over "bc", because "bc" is obsolete
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 if (*T_LE)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003213 bs = T_LE; // "cursor left"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003215 bs = T_BC; // "backspace character (old)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 if (*bs)
3217 cost = (screen_cur_col - col) * (int)STRLEN(bs);
3218 else
3219 cost = 999;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003220 if (col + 1 < cost) // using CR is less characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 {
3222 plan = PLAN_CR;
3223 wouldbe_col = 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003224 cost = 1; // CR is just one character
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 }
3226 else
3227 {
3228 plan = PLAN_LE;
3229 wouldbe_col = col;
3230 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003231 if (noinvcurs) // will stop highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 {
3233 cost += noinvcurs;
3234 attr = 0;
3235 }
3236 }
3237
3238 /*
3239 * If the cursor is above where we want to be, we can use CR LF.
3240 */
3241 else if (row > screen_cur_row)
3242 {
3243 plan = PLAN_NL;
3244 wouldbe_col = 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003245 cost = (row - screen_cur_row) * 2; // CR LF
3246 if (noinvcurs) // will stop highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 {
3248 cost += noinvcurs;
3249 attr = 0;
3250 }
3251 }
3252
3253 /*
3254 * If the cursor is in the same row, smaller col, just use write.
3255 */
3256 else
3257 {
3258 plan = PLAN_WRITE;
3259 wouldbe_col = screen_cur_col;
3260 cost = 0;
3261 }
3262
3263 /*
3264 * Check if any characters that need to be written have the
3265 * correct attributes. Also avoid UTF-8 characters.
3266 */
3267 i = col - wouldbe_col;
3268 if (i > 0)
3269 cost += i;
3270 if (cost < goto_cost && i > 0)
3271 {
3272 /*
3273 * Check if the attributes are correct without additionally
3274 * stopping highlighting.
3275 */
3276 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
3277 while (i && *p++ == attr)
3278 --i;
3279 if (i != 0)
3280 {
3281 /*
3282 * Try if it works when highlighting is stopped here.
3283 */
3284 if (*--p == 0)
3285 {
3286 cost += noinvcurs;
3287 while (i && *p++ == 0)
3288 --i;
3289 }
3290 if (i != 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003291 cost = 999; // different attributes, don't do it
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 if (enc_utf8)
3294 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003295 // Don't use an UTF-8 char for positioning, it's slow.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 for (i = wouldbe_col; i < col; ++i)
3297 if (ScreenLinesUC[LineOffset[row] + i] != 0)
3298 {
3299 cost = 999;
3300 break;
3301 }
3302 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 }
3304
3305 /*
3306 * We can do it without term_windgoto()!
3307 */
3308 if (cost < goto_cost)
3309 {
3310 if (plan == PLAN_LE)
3311 {
3312 if (noinvcurs)
3313 screen_stop_highlight();
3314 while (screen_cur_col > col)
3315 {
3316 out_str(bs);
3317 --screen_cur_col;
3318 }
3319 }
3320 else if (plan == PLAN_CR)
3321 {
3322 if (noinvcurs)
3323 screen_stop_highlight();
3324 out_char('\r');
3325 screen_cur_col = 0;
3326 }
3327 else if (plan == PLAN_NL)
3328 {
3329 if (noinvcurs)
3330 screen_stop_highlight();
3331 while (screen_cur_row < row)
3332 {
3333 out_char('\n');
3334 ++screen_cur_row;
3335 }
3336 screen_cur_col = 0;
3337 }
3338
3339 i = col - screen_cur_col;
3340 if (i > 0)
3341 {
3342 /*
3343 * Use cursor-right if it's one character only. Avoids
3344 * removing a line of pixels from the last bold char, when
3345 * using the bold trick in the GUI.
3346 */
3347 if (T_ND[0] != NUL && T_ND[1] == NUL)
3348 {
3349 while (i-- > 0)
3350 out_char(*T_ND);
3351 }
3352 else
3353 {
3354 int off;
3355
3356 off = LineOffset[row] + screen_cur_col;
3357 while (i-- > 0)
3358 {
3359 if (ScreenAttrs[off] != screen_attr)
3360 screen_stop_highlight();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 if (enc_dbcs == DBCS_JPNU
3364 && ScreenLines[off] == 0x8e)
3365 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 ++off;
3367 }
3368 }
3369 }
3370 }
3371 }
3372 else
3373 cost = 999;
3374
3375 if (cost >= goto_cost)
3376 {
3377 if (noinvcurs)
3378 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02003379 if (row == screen_cur_row && (col > screen_cur_col)
3380 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 term_cursor_right(col - screen_cur_col);
3382 else
3383 term_windgoto(row, col);
3384 }
3385 screen_cur_row = row;
3386 screen_cur_col = col;
3387 }
3388}
3389
3390/*
3391 * Set cursor to its position in the current window.
3392 */
3393 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003394setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395{
Bram Moolenaar987723e2018-03-06 11:43:04 +01003396 setcursor_mayforce(FALSE);
3397}
3398
3399/*
3400 * Set cursor to its position in the current window.
3401 * When "force" is TRUE also when not redrawing.
3402 */
3403 void
3404setcursor_mayforce(int force)
3405{
3406 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 {
3408 validate_cursor();
3409 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02003410 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411#ifdef FEAT_RIGHTLEFT
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003412 // With 'rightleft' set and the cursor on a double-wide
3413 // character, position it on the leftmost column.
Bram Moolenaara12a1612019-01-24 16:39:02 +01003414 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol
3415 - ((has_mbyte
Bram Moolenaar561f9db2008-02-20 13:16:29 +00003416 && (*mb_ptr2cells)(ml_get_cursor()) == 2
Bram Moolenaara12a1612019-01-24 16:39:02 +01003417 && vim_isprintc(gchar_cursor())) ? 2 : 1)) :
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418#endif
3419 curwin->w_wcol));
3420 }
3421}
3422
3423
3424/*
Bram Moolenaar86033562017-07-12 20:24:41 +02003425 * Insert 'line_count' lines at 'row' in window 'wp'.
3426 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
3427 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 * scrolling.
3429 * Returns FAIL if the lines are not inserted, OK for success.
3430 */
3431 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003432win_ins_lines(
3433 win_T *wp,
3434 int row,
3435 int line_count,
3436 int invalid,
3437 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438{
3439 int did_delete;
3440 int nextrow;
3441 int lastrow;
3442 int retval;
3443
3444 if (invalid)
3445 wp->w_lines_valid = 0;
3446
Bram Moolenaarc856ceb2022-06-21 18:10:39 +01003447 // with only a few lines it's not worth the effort
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 if (wp->w_height < 5)
3449 return FAIL;
3450
Bram Moolenaarc856ceb2022-06-21 18:10:39 +01003451 // with the popup menu visible this might not work correctly
3452 if (pum_visible())
3453 return FAIL;
3454
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 if (line_count > wp->w_height - row)
3456 line_count = wp->w_height - row;
3457
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003458 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 if (retval != MAYBE)
3460 return retval;
3461
3462 /*
3463 * If there is a next window or a status line, we first try to delete the
3464 * lines at the bottom to avoid messing what is after the window.
Bram Moolenaarc363fe12019-08-04 18:13:46 +02003465 * If this fails and there are following windows, don't do anything to
3466 * avoid messing up those windows, better just redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 */
3468 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 if (wp->w_next != NULL || wp->w_status_height)
3470 {
3471 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003472 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 did_delete = TRUE;
3474 else if (wp->w_next)
3475 return FAIL;
3476 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 /*
3478 * if no lines deleted, blank the lines that will end up below the window
3479 */
3480 if (!did_delete)
3481 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003484 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 lastrow = nextrow + line_count;
3486 if (lastrow > Rows)
3487 lastrow = Rows;
3488 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02003489 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 ' ', ' ', 0);
3491 }
3492
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003493 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 == FAIL)
3495 {
Bram Moolenaarc363fe12019-08-04 18:13:46 +02003496 // deletion will have messed up other windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 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 win_rest_invalid(W_NEXT(wp));
3501 }
3502 return FAIL;
3503 }
3504
3505 return OK;
3506}
3507
3508/*
Bram Moolenaar86033562017-07-12 20:24:41 +02003509 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
3511 * If "mayclear" is TRUE the screen will be cleared if it is faster than
3512 * scrolling
3513 * Return OK for success, FAIL if the lines are not deleted.
3514 */
3515 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003516win_del_lines(
3517 win_T *wp,
3518 int row,
3519 int line_count,
3520 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003521 int mayclear,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003522 int clear_attr) // for clearing lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523{
3524 int retval;
3525
3526 if (invalid)
3527 wp->w_lines_valid = 0;
3528
3529 if (line_count > wp->w_height - row)
3530 line_count = wp->w_height - row;
3531
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003532 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 if (retval != MAYBE)
3534 return retval;
3535
3536 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003537 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 return FAIL;
3539
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 /*
3541 * If there are windows or status lines below, try to put them at the
3542 * correct place. If we can't do that, they have to be redrawn.
3543 */
3544 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
3545 {
3546 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003547 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 {
3549 wp->w_redr_status = TRUE;
3550 win_rest_invalid(wp->w_next);
3551 }
3552 }
3553 /*
3554 * If this is the last window and there is no status line, redraw the
3555 * command line later.
3556 */
3557 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 redraw_cmdline = TRUE;
3559 return OK;
3560}
3561
3562/*
3563 * Common code for win_ins_lines() and win_del_lines().
3564 * Returns OK or FAIL when the work has been done.
3565 * Returns MAYBE when not finished yet.
3566 */
3567 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003568win_do_lines(
3569 win_T *wp,
3570 int row,
3571 int line_count,
3572 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003573 int del,
3574 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575{
3576 int retval;
3577
3578 if (!redrawing() || line_count <= 0)
3579 return FAIL;
3580
Bram Moolenaar33796b32019-06-08 16:01:13 +02003581 // When inserting lines would result in loss of command output, just redraw
3582 // the lines.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003583 if (no_win_do_lines_ins && !del)
3584 return FAIL;
3585
Bram Moolenaar33796b32019-06-08 16:01:13 +02003586 // only a few lines left: redraw is faster
Bram Moolenaar4033c552017-09-16 20:54:51 +02003587 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003589 if (!no_win_do_lines_ins)
Bram Moolenaar33796b32019-06-08 16:01:13 +02003590 screenclear(); // will set wp->w_lines_valid to 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 return FAIL;
3592 }
3593
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003594#ifdef FEAT_PROP_POPUP
Bram Moolenaar4c063a02019-06-10 21:24:12 +02003595 // this doesn't work when there are popups visible
Bram Moolenaar33796b32019-06-08 16:01:13 +02003596 if (popup_visible)
3597 return FAIL;
3598#endif
3599
3600 // Delete all remaining lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 if (row + line_count >= wp->w_height)
3602 {
3603 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02003604 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 ' ', ' ', 0);
3606 return OK;
3607 }
3608
3609 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003610 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003612 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003614 if (!no_win_do_lines_ins)
3615 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616
3617 /*
3618 * If the terminal can set a scroll region, use that.
3619 * Always do this in a vertically split window. This will redraw from
3620 * ScreenLines[] when t_CV isn't defined. That's faster than using
3621 * win_line().
3622 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003623 * a character in the lower right corner of the scroll region may cause a
3624 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 */
Bram Moolenaar02631462017-09-22 15:20:32 +02003626 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 scroll_region_set(wp, row);
3630 if (del)
3631 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003632 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 else
3634 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003635 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 scroll_region_reset();
3638 return retval;
3639 }
3640
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003641 if (wp->w_next != NULL && p_tf) // don't delete/insert on fast terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643
3644 return MAYBE;
3645}
3646
3647/*
3648 * window 'wp' and everything after it is messed up, mark it for redraw
3649 */
3650 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003651win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 {
3655 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 wp->w_redr_status = TRUE;
3657 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 }
3659 redraw_cmdline = TRUE;
3660}
3661
3662/*
3663 * The rest of the routines in this file perform screen manipulations. The
3664 * given operation is performed physically on the screen. The corresponding
3665 * change is also made to the internal screen image. In this way, the editor
3666 * anticipates the effect of editing changes on the appearance of the screen.
3667 * That way, when we call screenupdate a complete redraw isn't usually
3668 * necessary. Another advantage is that we can keep adding code to anticipate
3669 * screen changes, and in the meantime, everything still works.
3670 */
3671
3672/*
3673 * types for inserting or deleting lines
3674 */
3675#define USE_T_CAL 1
3676#define USE_T_CDL 2
3677#define USE_T_AL 3
3678#define USE_T_CE 4
3679#define USE_T_DL 5
3680#define USE_T_SR 6
3681#define USE_NL 7
3682#define USE_T_CD 8
3683#define USE_REDRAW 9
3684
3685/*
3686 * insert lines on the screen and update ScreenLines[]
Bram Moolenaar17fa2332022-04-01 19:44:47 +01003687 * "end" is the line after the scrolled part. Normally it is Rows.
3688 * When scrolling region used "off" is the offset from the top for the region.
3689 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 *
3691 * return FAIL for failure, OK for success.
3692 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003693 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003694screen_ins_lines(
3695 int off,
3696 int row,
3697 int line_count,
3698 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003699 int clear_attr,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003700 win_T *wp) // NULL or window to use width from
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701{
3702 int i;
3703 int j;
3704 unsigned temp;
3705 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003706 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 int type;
3708 int result_empty;
3709 int can_ce = can_clear(T_CE);
3710
3711 /*
3712 * FAIL if
3713 * - there is no valid screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 * - the line count is less than one
3715 * - the line count is more than 'ttyscroll'
Bram Moolenaar17fa2332022-04-01 19:44:47 +01003716 * - "end" is more than "Rows" (safety check, should not happen)
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003717 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar33796b32019-06-08 16:01:13 +02003718 * - there is a popup window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 */
Bram Moolenaar33796b32019-06-08 16:01:13 +02003720 if (!screen_valid(TRUE)
3721 || line_count <= 0 || line_count > p_ttyscroll
Bram Moolenaar17fa2332022-04-01 19:44:47 +01003722 || end > Rows
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003723#ifdef FEAT_CLIPBOARD
3724 || (clip_star.state != SELECT_CLEARED
3725 && redrawing_for_callback > 0)
3726#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003727#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02003728 || popup_visible
3729#endif
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003730 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 return FAIL;
3732
3733 /*
3734 * There are seven ways to insert lines:
3735 * 0. When in a vertically split window and t_CV isn't set, redraw the
3736 * characters from ScreenLines[].
3737 * 1. Use T_CD (clear to end of display) if it exists and the result of
3738 * the insert is just empty lines
3739 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
3740 * present or line_count > 1. It looks better if we do all the inserts
3741 * at once.
3742 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
3743 * insert is just empty lines and T_CE is not present or line_count >
3744 * 1.
3745 * 4. Use T_AL (insert line) if it exists.
3746 * 5. Use T_CE (erase line) if it exists and the result of the insert is
3747 * just empty lines.
3748 * 6. Use T_DL (delete line) if it exists and the result of the insert is
3749 * just empty lines.
3750 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
3751 * the 'da' flag is not set or we have clear line capability.
3752 * 8. redraw the characters from ScreenLines[].
3753 *
3754 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
3755 * the scrollbar for the window. It does have insert line, use that if it
3756 * exists.
3757 */
3758 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
Bram Moolenaar8ef69972022-04-03 13:23:22 +01003760 {
3761 // Avoid that lines are first cleared here and then redrawn, which
3762 // results in many characters updated twice. This happens with CTRL-F
3763 // in a vertically split window. With line-by-line scrolling
3764 // USE_REDRAW should be faster.
3765 if (line_count > 3)
3766 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 type = USE_REDRAW;
Bram Moolenaar8ef69972022-04-03 13:23:22 +01003768 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02003769 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 type = USE_T_CD;
3771 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
3772 type = USE_T_CAL;
3773 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
3774 type = USE_T_CDL;
3775 else if (*T_AL != NUL)
3776 type = USE_T_AL;
3777 else if (can_ce && result_empty)
3778 type = USE_T_CE;
3779 else if (*T_DL != NUL && result_empty)
3780 type = USE_T_DL;
3781 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
3782 type = USE_T_SR;
3783 else
3784 return FAIL;
3785
3786 /*
3787 * For clearing the lines screen_del_lines() is used. This will also take
3788 * care of t_db if necessary.
3789 */
3790 if (type == USE_T_CD || type == USE_T_CDL ||
3791 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003792 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793
3794 /*
3795 * If text is retained below the screen, first clear or delete as many
3796 * lines at the bottom of the window as are about to be inserted so that
3797 * the deleted lines won't later surface during a screen_del_lines.
3798 */
3799 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003800 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801
3802#ifdef FEAT_CLIPBOARD
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003803 // Remove a modeless selection when inserting lines halfway the screen
3804 // or not the full width of the screen.
Bram Moolenaar4033c552017-09-16 20:54:51 +02003805 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003806 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 else
3808 clip_scroll_selection(-line_count);
3809#endif
3810
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003811#ifdef FEAT_GUI_HAIKU
3812 vim_lock_screen();
3813#endif
3814
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815#ifdef FEAT_GUI
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003816 // Don't update the GUI cursor here, ScreenLines[] is invalid until the
3817 // scrolling is actually carried out.
Bram Moolenaar107abd22016-08-12 14:08:25 +02003818 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819#endif
3820
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003821 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
3822 cursor_col = wp->w_wincol;
3823
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003824 if (*T_CCS != NUL) // cursor relative to region
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 cursor_row = row;
3826 else
3827 cursor_row = row + off;
3828
3829 /*
3830 * Shift LineOffset[] line_count down to reflect the inserted lines.
3831 * Clear the inserted lines in ScreenLines[].
3832 */
3833 row += off;
3834 end += off;
3835 for (i = 0; i < line_count; ++i)
3836 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 if (wp != NULL && wp->w_width != Columns)
3838 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003839 // need to copy part of a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 j = end - 1 - i;
3841 while ((j -= line_count) >= row)
3842 linecopy(j + line_count, j, wp);
3843 j += line_count;
3844 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003845 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
3846 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 else
3848 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
3849 LineWraps[j] = FALSE;
3850 }
3851 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 {
3853 j = end - 1 - i;
3854 temp = LineOffset[j];
3855 while ((j -= line_count) >= row)
3856 {
3857 LineOffset[j + line_count] = LineOffset[j];
3858 LineWraps[j + line_count] = LineWraps[j];
3859 }
3860 LineOffset[j + line_count] = temp;
3861 LineWraps[j + line_count] = FALSE;
3862 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003863 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 else
3865 lineinvalid(temp, (int)Columns);
3866 }
3867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003869#ifdef FEAT_GUI_HAIKU
3870 vim_unlock_screen();
3871#endif
3872
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003874 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003875 if (clear_attr != 0)
3876 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003878 // redraw the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 if (type == USE_REDRAW)
3880 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02003881 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 {
3883 term_append_lines(line_count);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003884 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 }
3886 else
3887 {
3888 for (i = 0; i < line_count; i++)
3889 {
3890 if (type == USE_T_AL)
3891 {
3892 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003893 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 out_str(T_AL);
3895 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003896 else // type == USE_T_SR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 out_str(T_SR);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003898 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 }
3900 }
3901
3902 /*
3903 * With scroll-reverse and 'da' flag set we need to clear the lines that
3904 * have been scrolled down into the region.
3905 */
3906 if (type == USE_T_SR && *T_DA)
3907 {
3908 for (i = 0; i < line_count; ++i)
3909 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003910 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 out_str(T_CE);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003912 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 }
3914 }
3915
3916#ifdef FEAT_GUI
3917 gui_can_update_cursor();
3918 if (gui.in_use)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003919 out_flush(); // always flush after a scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920#endif
3921 return OK;
3922}
3923
3924/*
Bram Moolenaar107abd22016-08-12 14:08:25 +02003925 * Delete lines on the screen and update ScreenLines[].
3926 * "end" is the line after the scrolled part. Normally it is Rows.
3927 * When scrolling region used "off" is the offset from the top for the region.
3928 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 *
3930 * Return OK for success, FAIL if the lines are not deleted.
3931 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003933screen_del_lines(
3934 int off,
3935 int row,
3936 int line_count,
3937 int end,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003938 int force, // even when line_count > p_ttyscroll
3939 int clear_attr, // used for clearing lines
Bram Moolenaar8ef69972022-04-03 13:23:22 +01003940 win_T *wp) // NULL or window to use width from
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941{
3942 int j;
3943 int i;
3944 unsigned temp;
3945 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003946 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 int cursor_end;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003948 int result_empty; // result is empty until end of region
3949 int can_delete; // deleting line codes can be used
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 int type;
3951
3952 /*
3953 * FAIL if
3954 * - there is no valid screen
3955 * - the screen has to be redrawn completely
3956 * - the line count is less than one
3957 * - the line count is more than 'ttyscroll'
Bram Moolenaar17fa2332022-04-01 19:44:47 +01003958 * - "end" is more than "Rows" (safety check, should not happen)
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003959 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 */
Bram Moolenaar17fa2332022-04-01 19:44:47 +01003961 if (!screen_valid(TRUE)
3962 || line_count <= 0
3963 || (!force && line_count > p_ttyscroll)
3964 || end > Rows
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003965#ifdef FEAT_CLIPBOARD
Bram Moolenaar17fa2332022-04-01 19:44:47 +01003966 || (clip_star.state != SELECT_CLEARED && redrawing_for_callback > 0)
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003967#endif
3968 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 return FAIL;
3970
3971 /*
3972 * Check if the rest of the current region will become empty.
3973 */
3974 result_empty = row + line_count >= end;
3975
3976 /*
3977 * We can delete lines only when 'db' flag not set or when 'ce' option
3978 * available.
3979 */
3980 can_delete = (*T_DB == NUL || can_clear(T_CE));
3981
3982 /*
3983 * There are six ways to delete lines:
3984 * 0. When in a vertically split window and t_CV isn't set, redraw the
3985 * characters from ScreenLines[].
3986 * 1. Use T_CD if it exists and the result is empty.
3987 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
3988 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
3989 * none of the other ways work.
3990 * 4. Use T_CE (erase line) if the result is empty.
3991 * 5. Use T_DL (delete line) if it exists.
3992 * 6. redraw the characters from ScreenLines[].
3993 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
Bram Moolenaar8ef69972022-04-03 13:23:22 +01003995 {
3996 // Avoid that lines are first cleared here and then redrawn, which
3997 // results in many characters updated twice. This happens with CTRL-F
3998 // in a vertically split window. With line-by-line scrolling
3999 // USE_REDRAW should be faster.
4000 if (line_count > 3)
4001 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 type = USE_REDRAW;
Bram Moolenaar8ef69972022-04-03 13:23:22 +01004003 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02004004 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 type = USE_T_CD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 else if (row == 0 && (
4007#ifndef AMIGA
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004008 // On the Amiga, somehow '\n' on the last line doesn't always scroll
4009 // up, so use delete-line command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 line_count == 1 ||
4011#endif
4012 *T_CDL == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 type = USE_NL;
4014 else if (*T_CDL != NUL && line_count > 1 && can_delete)
4015 type = USE_T_CDL;
4016 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +02004017 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 type = USE_T_CE;
4019 else if (*T_DL != NUL && can_delete)
4020 type = USE_T_DL;
4021 else if (*T_CDL != NUL && can_delete)
4022 type = USE_T_CDL;
4023 else
4024 return FAIL;
4025
4026#ifdef FEAT_CLIPBOARD
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004027 // Remove a modeless selection when deleting lines halfway the screen or
4028 // not the full width of the screen.
Bram Moolenaar4033c552017-09-16 20:54:51 +02004029 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02004030 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 else
4032 clip_scroll_selection(line_count);
4033#endif
4034
Bram Moolenaar92c461e2020-04-24 22:19:00 +02004035#ifdef FEAT_GUI_HAIKU
4036 vim_lock_screen();
4037#endif
4038
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039#ifdef FEAT_GUI
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004040 // Don't update the GUI cursor here, ScreenLines[] is invalid until the
4041 // scrolling is actually carried out.
Bram Moolenaar107abd22016-08-12 14:08:25 +02004042 gui_dont_update_cursor(gui.cursor_row >= row + off
4043 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044#endif
4045
Bram Moolenaarbfa42462018-06-16 16:20:52 +02004046 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
4047 cursor_col = wp->w_wincol;
4048
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004049 if (*T_CCS != NUL) // cursor relative to region
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 {
4051 cursor_row = row;
4052 cursor_end = end;
4053 }
4054 else
4055 {
4056 cursor_row = row + off;
4057 cursor_end = end + off;
4058 }
4059
4060 /*
4061 * Now shift LineOffset[] line_count up to reflect the deleted lines.
4062 * Clear the inserted lines in ScreenLines[].
4063 */
4064 row += off;
4065 end += off;
4066 for (i = 0; i < line_count; ++i)
4067 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 if (wp != NULL && wp->w_width != Columns)
4069 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004070 // need to copy part of a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 j = row + i;
4072 while ((j += line_count) <= end - 1)
4073 linecopy(j - line_count, j, wp);
4074 j -= line_count;
4075 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02004076 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
4077 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 else
4079 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
4080 LineWraps[j] = FALSE;
4081 }
4082 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004084 // whole width, moving the line pointers is faster
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 j = row + i;
4086 temp = LineOffset[j];
4087 while ((j += line_count) <= end - 1)
4088 {
4089 LineOffset[j - line_count] = LineOffset[j];
4090 LineWraps[j - line_count] = LineWraps[j];
4091 }
4092 LineOffset[j - line_count] = temp;
4093 LineWraps[j - line_count] = FALSE;
4094 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02004095 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 else
4097 lineinvalid(temp, (int)Columns);
4098 }
4099 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004101#ifdef FEAT_GUI_HAIKU
4102 vim_unlock_screen();
4103#endif
4104
Bram Moolenaarcfce7172017-08-17 20:31:48 +02004105 if (screen_attr != clear_attr)
4106 screen_stop_highlight();
4107 if (clear_attr != 0)
4108 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004110 // redraw the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 if (type == USE_REDRAW)
4112 redraw_block(row, end, wp);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004113 else if (type == USE_T_CD) // delete the lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02004115 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 out_str(T_CD);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004117 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 }
4119 else if (type == USE_T_CDL)
4120 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02004121 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 term_delete_lines(line_count);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004123 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 }
4125 /*
4126 * Deleting lines at top of the screen or scroll region: Just scroll
4127 * the whole screen (scroll region) up by outputting newlines on the
4128 * last line.
4129 */
4130 else if (type == USE_NL)
4131 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02004132 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 for (i = line_count; --i >= 0; )
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004134 out_char('\n'); // cursor will remain on same line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 }
4136 else
4137 {
4138 for (i = line_count; --i >= 0; )
4139 {
4140 if (type == USE_T_DL)
4141 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02004142 windgoto(cursor_row, cursor_col);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004143 out_str(T_DL); // delete a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004145 else // type == USE_T_CE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02004147 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004148 out_str(T_CE); // erase a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004150 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 }
4152 }
4153
4154 /*
4155 * If the 'db' flag is set, we need to clear the lines that have been
4156 * scrolled up at the bottom of the region.
4157 */
4158 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
4159 {
4160 for (i = line_count; i > 0; --i)
4161 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02004162 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004163 out_str(T_CE); // erase a line
4164 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 }
4166 }
4167
4168#ifdef FEAT_GUI
4169 gui_can_update_cursor();
4170 if (gui.in_use)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004171 out_flush(); // always flush after a scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172#endif
4173
4174 return OK;
4175}
4176
4177/*
Bram Moolenaarcb574f42019-01-25 22:29:57 +01004178 * Return TRUE when postponing displaying the mode message: when not redrawing
4179 * or inside a mapping.
4180 */
4181 int
4182skip_showmode()
4183{
4184 // Call char_avail() only when we are going to show something, because it
Bram Moolenaar944cc9c2022-06-27 22:17:37 +01004185 // takes a bit of time. redrawing() may also call char_avail().
Bram Moolenaarcb574f42019-01-25 22:29:57 +01004186 if (global_busy
4187 || msg_silent != 0
4188 || !redrawing()
4189 || (char_avail() && !KeyTyped))
4190 {
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004191 redraw_mode = TRUE; // show mode later
Bram Moolenaarcb574f42019-01-25 22:29:57 +01004192 return TRUE;
4193 }
4194 return FALSE;
4195}
4196
4197/*
Bram Moolenaar81226e02018-02-20 21:44:45 +01004198 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 *
4200 * If clear_cmdline is TRUE, clear the rest of the cmdline.
4201 * If clear_cmdline is FALSE there may be a message there that needs to be
4202 * cleared only if a mode is shown.
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004203 * If redraw_mode is TRUE show or clear the mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 * Return the length of the message (0 if no message).
4205 */
4206 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004207showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208{
4209 int need_clear;
4210 int length = 0;
4211 int do_mode;
4212 int attr;
4213 int nwr_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 int sub_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215
Bram Moolenaar7df351e2006-01-23 22:30:28 +00004216 do_mode = ((p_smd && msg_silent == 0)
Bram Moolenaar24959102022-05-07 20:01:16 +01004217 && ((State & MODE_INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +02004218 || restart_edit != NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004219 || VIsual_active));
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02004220 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 {
Bram Moolenaarcb574f42019-01-25 22:29:57 +01004222 if (skip_showmode())
4223 return 0; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224
4225 nwr_save = need_wait_return;
4226
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004227 // wait a bit before overwriting an important message
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 check_for_delay(FALSE);
4229
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004230 // if the cmdline is more than one line high, erase top lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 need_clear = clear_cmdline;
4232 if (clear_cmdline && cmdline_row < Rows - 1)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004233 msg_clr_cmdline(); // will reset clear_cmdline
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004235 // Position on the last line in the window, column 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 msg_pos_mode();
4237 cursor_off();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004238 attr = HL_ATTR(HLF_CM); // Highlight mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 if (do_mode)
4240 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004241 msg_puts_attr("--", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00004243 if (
Bram Moolenaar09092152010-08-08 16:38:42 +02004244# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +00004245 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +02004246# else
Bram Moolenaarc236c162008-07-13 17:41:49 +00004247 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +00004248# endif
Bram Moolenaar09092152010-08-08 16:38:42 +02004249 )
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004250# ifdef FEAT_GUI_GTK // most of the time, it's not XIM being used
Bram Moolenaar32526b32019-01-19 17:43:09 +01004251 msg_puts_attr(" IM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252# else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004253 msg_puts_attr(" XIM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254# endif
4255#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004256 // CTRL-X in Insert mode
Bram Moolenaarea389e92014-05-28 21:40:52 +02004257 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004259 // These messages can get long, avoid a wrap in a narrow
4260 // window. Prefer showing edit_submode_extra.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 length = (Rows - msg_row) * Columns - 3;
4262 if (edit_submode_extra != NULL)
4263 length -= vim_strsize(edit_submode_extra);
4264 if (length > 0)
4265 {
4266 if (edit_submode_pre != NULL)
4267 length -= vim_strsize(edit_submode_pre);
4268 if (length - vim_strsize(edit_submode) > 0)
4269 {
4270 if (edit_submode_pre != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004271 msg_puts_attr((char *)edit_submode_pre, attr);
4272 msg_puts_attr((char *)edit_submode, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 }
4274 if (edit_submode_extra != NULL)
4275 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004276 msg_puts_attr(" ", attr); // add a space in between
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004278 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 else
4280 sub_attr = attr;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004281 msg_puts_attr((char *)edit_submode_extra, sub_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 }
4283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 }
4285 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 if (State & VREPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004288 msg_puts_attr(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02004289 else if (State & REPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004290 msg_puts_attr(_(" REPLACE"), attr);
Bram Moolenaar24959102022-05-07 20:01:16 +01004291 else if (State & MODE_INSERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 {
4293#ifdef FEAT_RIGHTLEFT
4294 if (p_ri)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004295 msg_puts_attr(_(" REVERSE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01004297 msg_puts_attr(_(" INSERT"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01004299 else if (restart_edit == 'I' || restart_edit == 'i' ||
4300 restart_edit == 'a' || restart_edit == 'A')
Bram Moolenaar32526b32019-01-19 17:43:09 +01004301 msg_puts_attr(_(" (insert)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 else if (restart_edit == 'R')
Bram Moolenaar32526b32019-01-19 17:43:09 +01004303 msg_puts_attr(_(" (replace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 else if (restart_edit == 'V')
Bram Moolenaar32526b32019-01-19 17:43:09 +01004305 msg_puts_attr(_(" (vreplace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306#ifdef FEAT_RIGHTLEFT
4307 if (p_hkmap)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004308 msg_puts_attr(_(" Hebrew"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309#endif
4310#ifdef FEAT_KEYMAP
Bram Moolenaar24959102022-05-07 20:01:16 +01004311 if (State & MODE_LANGMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 {
4313# ifdef FEAT_ARABIC
4314 if (curwin->w_p_arab)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004315 msg_puts_attr(_(" Arabic"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 else
4317# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02004318 if (get_keymap_str(curwin, (char_u *)" (%s)",
4319 NameBuff, MAXPATHL))
Bram Moolenaar32526b32019-01-19 17:43:09 +01004320 msg_puts_attr((char *)NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 }
4322#endif
Bram Moolenaar24959102022-05-07 20:01:16 +01004323 if ((State & MODE_INSERT) && p_paste)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004324 msg_puts_attr(_(" (paste)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 if (VIsual_active)
4327 {
4328 char *p;
4329
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004330 // Don't concatenate separate words to avoid translation
4331 // problems.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 switch ((VIsual_select ? 4 : 0)
4333 + (VIsual_mode == Ctrl_V) * 2
4334 + (VIsual_mode == 'V'))
4335 {
4336 case 0: p = N_(" VISUAL"); break;
4337 case 1: p = N_(" VISUAL LINE"); break;
4338 case 2: p = N_(" VISUAL BLOCK"); break;
4339 case 4: p = N_(" SELECT"); break;
4340 case 5: p = N_(" SELECT LINE"); break;
4341 default: p = N_(" SELECT BLOCK"); break;
4342 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01004343 msg_puts_attr(_(p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01004345 msg_puts_attr(" --", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004347
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 need_clear = TRUE;
4349 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02004350 if (reg_recording != 0
Bram Moolenaare2c453d2019-08-21 14:37:09 +02004351 && edit_submode == NULL) // otherwise it gets too long
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004353 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 need_clear = TRUE;
4355 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004356
4357 mode_displayed = TRUE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004358 if (need_clear || clear_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 msg_clr_eos();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004360 msg_didout = FALSE; // overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 length = msg_col;
4362 msg_col = 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004363 need_wait_return = nwr_save; // never ask for hit-return for this
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 }
4365 else if (clear_cmdline && msg_silent == 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004366 // Clear the whole command line. Will reset "clear_cmdline".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 msg_clr_cmdline();
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004368 else if (redraw_mode)
4369 {
4370 msg_pos_mode();
4371 msg_clr_eos();
4372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373
4374#ifdef FEAT_CMDL_INFO
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004375 // In Visual mode the size of the selected area must be redrawn.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 if (VIsual_active)
4377 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004379 // If the last window has no status line, the ruler is after the mode
4380 // message and must be redrawn
Bram Moolenaar4033c552017-09-16 20:54:51 +02004381 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +02004382 win_redr_ruler(lastwin, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383#endif
4384 redraw_cmdline = FALSE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004385 redraw_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 clear_cmdline = FALSE;
4387
4388 return length;
4389}
4390
4391/*
4392 * Position for a mode message.
4393 */
4394 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004395msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396{
4397 msg_col = 0;
4398 msg_row = Rows - 1;
4399}
4400
4401/*
4402 * Delete mode message. Used when ESC is typed which is expected to end
4403 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004404 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 */
4406 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004407unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408{
4409 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +01004410 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 */
4412 if (!redrawing() || (!force && char_avail() && !KeyTyped))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004413 redraw_cmdline = TRUE; // delete mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +02004415 clearmode();
4416}
4417
4418/*
4419 * Clear the mode message.
4420 */
4421 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02004422clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +02004423{
Bram Moolenaar2abad542018-05-19 14:43:45 +02004424 int save_msg_row = msg_row;
4425 int save_msg_col = msg_col;
4426
Bram Moolenaarfd773e92016-04-02 19:39:16 +02004427 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02004428 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004429 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +02004430 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +02004431
4432 msg_col = save_msg_col;
4433 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434}
4435
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004436 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004437recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004438{
Bram Moolenaar32526b32019-01-19 17:43:09 +01004439 msg_puts_attr(_("recording"), attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004440 if (!shortmess(SHM_RECORDING))
4441 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004442 char s[4];
4443
4444 sprintf(s, " @%c", reg_recording);
4445 msg_puts_attr(s, attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004446 }
4447}
4448
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004449/*
4450 * Draw the tab pages line at the top of the Vim window.
4451 */
Bram Moolenaare12bab32019-01-08 22:02:56 +01004452 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004453draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004454{
4455 int tabcount = 0;
4456 tabpage_T *tp;
4457 int tabwidth;
4458 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00004459 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004460 int attr;
4461 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004462 win_T *cwp;
4463 int wincount;
4464 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004465 int c;
4466 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004467 int attr_sel = HL_ATTR(HLF_TPS);
4468 int attr_nosel = HL_ATTR(HLF_TP);
4469 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00004470 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004471 int room;
4472 int use_sep_chars = (t_colors < 8
4473#ifdef FEAT_GUI
4474 && !gui.in_use
4475#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02004476#ifdef FEAT_TERMGUICOLORS
4477 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +02004478#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004479 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004480
Bram Moolenaarc695cec2017-01-08 20:00:04 +01004481 if (ScreenLines == NULL)
4482 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00004483 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004484
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004485#ifdef FEAT_GUI_TABLINE
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004486 // Take care of a GUI tabline.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004487 if (gui_use_tabline())
4488 {
4489 gui_update_tabline();
4490 return;
4491 }
4492#endif
4493
4494 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004495 return;
4496
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004497#if defined(FEAT_STL_OPT)
Bram Moolenaarca57ab52019-04-13 14:53:16 +02004498 clear_TabPageIdxs();
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004499
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004500 // Use the 'tabline' option if it's set.
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004501 if (*p_tal != NUL)
4502 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +02004503 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +00004504
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004505 // Check for an error. If there is one we would loop in redrawing the
4506 // screen. Avoid that by making 'tabline' empty.
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +02004507 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004508 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +02004509 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +00004510 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004511 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +02004512 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004513 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00004514 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004515#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004516 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004517 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00004518 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004519
Bram Moolenaar238a5642006-02-21 22:12:05 +00004520 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
4521 if (tabwidth < 6)
4522 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004523
Bram Moolenaar238a5642006-02-21 22:12:05 +00004524 attr = attr_nosel;
4525 tabcount = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004526 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
4527 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00004528 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00004529 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004530
Bram Moolenaar238a5642006-02-21 22:12:05 +00004531 if (tp->tp_topframe == topframe)
4532 attr = attr_sel;
4533 if (use_sep_chars && col > 0)
4534 screen_putchar('|', 0, col++, attr);
4535
4536 if (tp->tp_topframe != topframe)
4537 attr = attr_nosel;
4538
4539 screen_putchar(' ', 0, col++, attr);
4540
4541 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00004542 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00004543 cwp = curwin;
4544 wp = firstwin;
4545 }
4546 else
4547 {
4548 cwp = tp->tp_curwin;
4549 wp = tp->tp_firstwin;
4550 }
4551
4552 modified = FALSE;
4553 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
4554 if (bufIsChanged(wp->w_buffer))
4555 modified = TRUE;
4556 if (modified || wincount > 1)
4557 {
4558 if (wincount > 1)
4559 {
4560 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004561 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004562 if (col + len >= Columns - 3)
4563 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +00004564 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004565#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004566 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004567#else
Bram Moolenaare0f14822014-08-06 13:20:56 +02004568 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004569#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00004570 );
4571 col += len;
4572 }
4573 if (modified)
4574 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
4575 screen_putchar(' ', 0, col++, attr);
4576 }
4577
4578 room = scol - col + tabwidth - 1;
4579 if (room > 0)
4580 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004581 // Get buffer name in NameBuff[]
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004582 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004583 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +00004584 len = vim_strsize(NameBuff);
4585 p = NameBuff;
Bram Moolenaar238a5642006-02-21 22:12:05 +00004586 if (has_mbyte)
4587 while (len > room)
4588 {
4589 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004590 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +00004591 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01004592 else if (len > room)
Bram Moolenaar238a5642006-02-21 22:12:05 +00004593 {
4594 p += len - room;
4595 len = room;
4596 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004597 if (len > Columns - col - 1)
4598 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +00004599
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004600 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +00004601 col += len;
4602 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00004603 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +00004604
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004605 // Store the tab page number in TabPageIdxs[], so that
4606 // jump_to_mouse() knows where each one is.
Bram Moolenaar238a5642006-02-21 22:12:05 +00004607 ++tabcount;
4608 while (scol < col)
4609 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004610 }
4611
Bram Moolenaar238a5642006-02-21 22:12:05 +00004612 if (use_sep_chars)
4613 c = '_';
4614 else
4615 c = ' ';
4616 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004617
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004618 // Put an "X" for closing the current tab if there are several.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004619 if (first_tabpage->tp_next != NULL)
4620 {
4621 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
4622 TabPageIdxs[Columns - 1] = -999;
4623 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004624 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +00004625
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004626 // Reset the flag here again, in case evaluating 'tabline' causes it to be
4627 // set.
Bram Moolenaarb21e5842006-04-16 18:30:08 +00004628 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004629}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004630
4631/*
4632 * Get buffer name for "buf" into NameBuff[].
4633 * Takes care of special buffer names and translates special characters.
4634 */
4635 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004636get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004637{
4638 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02004639 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004640 else
4641 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
4642 trans_characters(NameBuff, MAXPATHL);
4643}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004644
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645/*
4646 * Get the character to use in a status line. Get its attributes in "*attr".
4647 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02004648 int
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004649fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650{
4651 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004652
4653#ifdef FEAT_TERMINAL
4654 if (bt_terminal(wp->w_buffer))
4655 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004656 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02004657 {
4658 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004659 fill = wp->w_fill_chars.stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02004660 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004661 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02004662 {
4663 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004664 fill = wp->w_fill_chars.stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02004665 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004666 }
4667 else
4668#endif
4669 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004671 *attr = HL_ATTR(HLF_S);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004672 fill = wp->w_fill_chars.stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 }
4674 else
4675 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004676 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004677 fill = wp->w_fill_chars.stlnc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004679 // Use fill when there is highlighting, and highlighting of current
4680 // window differs, or the fillchars differ, or this is not the
4681 // current window
Bram Moolenaar8820b482017-03-16 17:23:31 +01004682 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004683 || wp != curwin || ONE_WINDOW)
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004684 || (wp->w_fill_chars.stl != wp->w_fill_chars.stlnc)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004686 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 return '^';
4688 return '=';
4689}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691/*
4692 * Get the character to use in a separator between vertically split windows.
4693 * Get its attributes in "*attr".
4694 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02004695 int
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004696fillchar_vsep(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697{
Bram Moolenaar8820b482017-03-16 17:23:31 +01004698 *attr = HL_ATTR(HLF_C);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004699 if (*attr == 0 && wp->w_fill_chars.vert == ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 return '|';
4701 else
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004702 return wp->w_fill_chars.vert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704
4705/*
4706 * Return TRUE if redrawing should currently be done.
4707 */
4708 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004709redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710{
Bram Moolenaareb992cb2017-03-09 18:20:16 +01004711#ifdef FEAT_EVAL
4712 if (disable_redraw_for_testing)
4713 return 0;
4714 else
4715#endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +02004716 return ((!RedrawingDisabled
4717#ifdef FEAT_EVAL
4718 || ignore_redraw_flag_for_testing
4719#endif
4720 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721}
4722
4723/*
4724 * Return TRUE if printing messages should currently be done.
4725 */
4726 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004727messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728{
Shougo Matsushitaf39cfb72022-07-30 16:54:05 +01004729 return (!(p_lz && char_avail() && !KeyTyped)) && p_ch > 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730}
4731
Bram Moolenaare677df82019-09-02 22:31:11 +02004732/*
4733 * Compute columns for ruler and shown command. 'sc_col' is also used to
4734 * decide what the maximum length of a message on the status line can be.
4735 * If there is a status line for the last window, 'sc_col' is independent
4736 * of 'ru_col'.
4737 */
4738
4739#define COL_RULER 17 // columns needed by standard ruler
4740
4741 void
4742comp_col(void)
4743{
4744#if defined(FEAT_CMDL_INFO)
4745 int last_has_status = (p_ls == 2 || (p_ls == 1 && !ONE_WINDOW));
4746
4747 sc_col = 0;
4748 ru_col = 0;
4749 if (p_ru)
4750 {
4751# ifdef FEAT_STL_OPT
4752 ru_col = (ru_wid ? ru_wid : COL_RULER) + 1;
4753# else
4754 ru_col = COL_RULER + 1;
4755# endif
4756 // no last status line, adjust sc_col
4757 if (!last_has_status)
4758 sc_col = ru_col;
4759 }
4760 if (p_sc)
4761 {
4762 sc_col += SHOWCMD_COLS;
4763 if (!p_ru || last_has_status) // no need for separating space
4764 ++sc_col;
4765 }
4766 sc_col = Columns - sc_col;
4767 ru_col = Columns - ru_col;
4768 if (sc_col <= 0) // screen too narrow, will become a mess
4769 sc_col = 1;
4770 if (ru_col <= 0)
4771 ru_col = 1;
4772#else
4773 sc_col = Columns;
4774 ru_col = Columns;
4775#endif
4776#ifdef FEAT_EVAL
4777 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
4778#endif
4779}
4780
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004781#if defined(FEAT_LINEBREAK) || defined(PROTO)
4782/*
Bram Moolenaar64486672010-05-16 15:46:46 +02004783 * Return the width of the 'number' and 'relativenumber' column.
4784 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004785 * Otherwise it depends on 'numberwidth' and the line count.
4786 */
4787 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004788number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004789{
4790 int n;
4791 linenr_T lnum;
4792
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004793 if (wp->w_p_rnu && !wp->w_p_nu)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004794 // cursor line shows "0"
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004795 lnum = wp->w_height;
4796 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004797 // cursor line shows absolute line number
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004798 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +02004799
Bram Moolenaar6b314672015-03-20 15:42:10 +01004800 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004801 return wp->w_nrwidth_width;
4802 wp->w_nrwidth_line_count = lnum;
4803
4804 n = 0;
4805 do
4806 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004807 lnum /= 10;
4808 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004809 } while (lnum > 0);
4810
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004811 // 'numberwidth' gives the minimal width plus one
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004812 if (n < wp->w_p_nuw - 1)
4813 n = wp->w_p_nuw - 1;
4814
Bram Moolenaare4b407f2019-07-04 11:59:28 +02004815# ifdef FEAT_SIGNS
4816 // If 'signcolumn' is set to 'number' and there is a sign to display, then
4817 // the minimal width for the number column is 2.
Bram Moolenaar4eb7dae2019-11-12 22:33:45 +01004818 if (n < 2 && get_first_valid_sign(wp) != NULL
Bram Moolenaare4b407f2019-07-04 11:59:28 +02004819 && (*wp->w_p_scl == 'n' && *(wp->w_p_scl + 1) == 'u'))
4820 n = 2;
4821# endif
4822
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004823 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +01004824 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004825 return n;
4826}
4827#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +01004828
Bram Moolenaar113e1072019-01-20 15:30:40 +01004829#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9750bb12012-12-05 16:10:42 +01004830/*
4831 * Return the current cursor column. This is the actual position on the
4832 * screen. First column is 0.
4833 */
4834 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004835screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +01004836{
4837 return screen_cur_col;
4838}
4839
4840/*
4841 * Return the current cursor row. This is the actual position on the screen.
4842 * First row is 0.
4843 */
4844 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004845screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +01004846{
4847 return screen_cur_row;
4848}
Bram Moolenaar113e1072019-01-20 15:30:40 +01004849#endif
Bram Moolenaare677df82019-09-02 22:31:11 +02004850
4851/*
Bram Moolenaar93ff6722021-10-16 17:51:40 +01004852 * Calls mb_ptr2char_adv(p) and returns the character.
4853 * If "p" starts with "\x", "\u" or "\U" the hex or unicode value is used.
4854 */
4855 static int
4856get_encoded_char_adv(char_u **p)
4857{
4858 char_u *s = *p;
4859
4860 if (s[0] == '\\' && (s[1] == 'x' || s[1] == 'u' || s[1] == 'U'))
4861 {
4862 varnumber_T num = 0;
4863 int bytes;
4864 int n;
4865
4866 for (bytes = s[1] == 'x' ? 1 : s[1] == 'u' ? 2 : 4; bytes > 0; --bytes)
4867 {
4868 *p += 2;
4869 n = hexhex2nr(*p);
4870 if (n < 0)
4871 return 0;
4872 num = num * 256 + n;
4873 }
4874 *p += 2;
4875 return num;
4876 }
4877 return mb_ptr2char_adv(p);
4878}
4879
4880/*
Bram Moolenaare677df82019-09-02 22:31:11 +02004881 * Handle setting 'listchars' or 'fillchars'.
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004882 * "varp" points to either the global or the window-local value.
4883 * When "apply" is FALSE do not store the flags, only check for errors.
Bram Moolenaareed9d462021-02-15 20:38:25 +01004884 * Assume monocell characters.
Bram Moolenaare677df82019-09-02 22:31:11 +02004885 * Returns error message, NULL if it's OK.
4886 */
4887 char *
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004888set_chars_option(win_T *wp, char_u **varp, int apply)
Bram Moolenaare677df82019-09-02 22:31:11 +02004889{
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004890 int round, i, len, len2, entries;
4891 char_u *p, *s;
4892 int c1 = 0, c2 = 0, c3 = 0;
4893 char_u *last_multispace = NULL; // Last occurrence of "multispace:"
4894 char_u *last_lmultispace = NULL; // Last occurrence of "leadmultispace:"
4895 int multispace_len = 0; // Length of lcs-multispace string
4896 int lead_multispace_len = 0; // Length of lcs-leadmultispace string
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004897 int is_listchars = (varp == &p_lcs || varp == &wp->w_p_lcs);
4898 char_u *value = *varp;
4899
Bram Moolenaare677df82019-09-02 22:31:11 +02004900 struct charstab
4901 {
4902 int *cp;
4903 char *name;
4904 };
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004905 struct charstab *tab;
4906
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004907 static fill_chars_T fill_chars;
Bram Moolenaare677df82019-09-02 22:31:11 +02004908 static struct charstab filltab[] =
4909 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004910 {&fill_chars.stl, "stl"},
4911 {&fill_chars.stlnc, "stlnc"},
4912 {&fill_chars.vert, "vert"},
4913 {&fill_chars.fold, "fold"},
4914 {&fill_chars.foldopen, "foldopen"},
4915 {&fill_chars.foldclosed, "foldclose"},
4916 {&fill_chars.foldsep, "foldsep"},
4917 {&fill_chars.diff, "diff"},
4918 {&fill_chars.eob, "eob"},
Bram Moolenaare677df82019-09-02 22:31:11 +02004919 };
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004920
Bram Moolenaar333bd562021-02-16 22:22:13 +01004921 static lcs_chars_T lcs_chars;
Bram Moolenaareed9d462021-02-15 20:38:25 +01004922 struct charstab lcstab[] =
Bram Moolenaare677df82019-09-02 22:31:11 +02004923 {
Bram Moolenaar333bd562021-02-16 22:22:13 +01004924 {&lcs_chars.eol, "eol"},
4925 {&lcs_chars.ext, "extends"},
4926 {&lcs_chars.nbsp, "nbsp"},
4927 {&lcs_chars.prec, "precedes"},
4928 {&lcs_chars.space, "space"},
4929 {&lcs_chars.tab2, "tab"},
4930 {&lcs_chars.trail, "trail"},
4931 {&lcs_chars.lead, "lead"},
Bram Moolenaare677df82019-09-02 22:31:11 +02004932#ifdef FEAT_CONCEAL
Bram Moolenaar333bd562021-02-16 22:22:13 +01004933 {&lcs_chars.conceal, "conceal"},
Bram Moolenaare677df82019-09-02 22:31:11 +02004934#else
Bram Moolenaar333bd562021-02-16 22:22:13 +01004935 {NULL, "conceal"},
Bram Moolenaare677df82019-09-02 22:31:11 +02004936#endif
4937 };
Bram Moolenaare677df82019-09-02 22:31:11 +02004938
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004939 if (is_listchars)
Bram Moolenaare677df82019-09-02 22:31:11 +02004940 {
4941 tab = lcstab;
Bram Moolenaar333bd562021-02-16 22:22:13 +01004942 CLEAR_FIELD(lcs_chars);
K.Takataeeec2542021-06-02 13:28:16 +02004943 entries = ARRAY_LENGTH(lcstab);
Bram Moolenaareed9d462021-02-15 20:38:25 +01004944 if (varp == &wp->w_p_lcs && wp->w_p_lcs[0] == NUL)
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004945 value = p_lcs; // local value is empty, us the global value
Bram Moolenaare677df82019-09-02 22:31:11 +02004946 }
4947 else
4948 {
4949 tab = filltab;
K.Takataeeec2542021-06-02 13:28:16 +02004950 entries = ARRAY_LENGTH(filltab);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004951 if (varp == &wp->w_p_fcs && wp->w_p_fcs[0] == NUL)
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004952 value = p_fcs; // local value is empty, us the global value
Bram Moolenaare677df82019-09-02 22:31:11 +02004953 }
4954
4955 // first round: check for valid value, second round: assign values
4956 for (round = 0; round <= 1; ++round)
4957 {
4958 if (round > 0)
4959 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004960 // After checking that the value is valid: set defaults.
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004961 if (is_listchars)
Bram Moolenaare677df82019-09-02 22:31:11 +02004962 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004963 for (i = 0; i < entries; ++i)
4964 if (tab[i].cp != NULL)
4965 *(tab[i].cp) = NUL;
Bram Moolenaar333bd562021-02-16 22:22:13 +01004966 lcs_chars.tab1 = NUL;
4967 lcs_chars.tab3 = NUL;
zeertzjqb5f08012022-06-09 13:55:28 +01004968
Mike Williamsf5785cf2021-09-13 22:17:38 +02004969 if (multispace_len > 0)
zeertzjqf14b8ba2021-09-10 16:58:30 +02004970 {
4971 lcs_chars.multispace = ALLOC_MULT(int, multispace_len + 1);
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004972 if (lcs_chars.multispace != NULL)
4973 lcs_chars.multispace[multispace_len] = NUL;
zeertzjqf14b8ba2021-09-10 16:58:30 +02004974 }
4975 else
4976 lcs_chars.multispace = NULL;
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01004977
4978 if (lead_multispace_len > 0)
4979 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004980 lcs_chars.leadmultispace =
4981 ALLOC_MULT(int, lead_multispace_len + 1);
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01004982 lcs_chars.leadmultispace[lead_multispace_len] = NUL;
4983 }
4984 else
4985 lcs_chars.leadmultispace = NULL;
Bram Moolenaare677df82019-09-02 22:31:11 +02004986 }
4987 else
Bram Moolenaara98f8a22021-02-13 18:24:23 +01004988 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004989 fill_chars.stl = ' ';
4990 fill_chars.stlnc = ' ';
4991 fill_chars.vert = ' ';
4992 fill_chars.fold = '-';
4993 fill_chars.foldopen = '-';
4994 fill_chars.foldclosed = '+';
4995 fill_chars.foldsep = '|';
4996 fill_chars.diff = '-';
4997 fill_chars.eob = '~';
Bram Moolenaara98f8a22021-02-13 18:24:23 +01004998 }
Bram Moolenaare677df82019-09-02 22:31:11 +02004999 }
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01005000 p = value;
Bram Moolenaare677df82019-09-02 22:31:11 +02005001 while (*p)
5002 {
5003 for (i = 0; i < entries; ++i)
5004 {
5005 len = (int)STRLEN(tab[i].name);
5006 if (STRNCMP(p, tab[i].name, len) == 0
5007 && p[len] == ':'
5008 && p[len + 1] != NUL)
5009 {
5010 c2 = c3 = 0;
5011 s = p + len + 1;
Bram Moolenaar93ff6722021-10-16 17:51:40 +01005012 c1 = get_encoded_char_adv(&s);
zeertzjq60618c82021-12-18 15:32:46 +00005013 if (char2cells(c1) > 1)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005014 return e_invalid_argument;
Bram Moolenaar333bd562021-02-16 22:22:13 +01005015 if (tab[i].cp == &lcs_chars.tab2)
Bram Moolenaare677df82019-09-02 22:31:11 +02005016 {
5017 if (*s == NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005018 return e_invalid_argument;
Bram Moolenaar93ff6722021-10-16 17:51:40 +01005019 c2 = get_encoded_char_adv(&s);
zeertzjq60618c82021-12-18 15:32:46 +00005020 if (char2cells(c2) > 1)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005021 return e_invalid_argument;
Bram Moolenaare677df82019-09-02 22:31:11 +02005022 if (!(*s == ',' || *s == NUL))
5023 {
Bram Moolenaar93ff6722021-10-16 17:51:40 +01005024 c3 = get_encoded_char_adv(&s);
zeertzjq60618c82021-12-18 15:32:46 +00005025 if (char2cells(c3) > 1)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005026 return e_invalid_argument;
Bram Moolenaare677df82019-09-02 22:31:11 +02005027 }
5028 }
5029
5030 if (*s == ',' || *s == NUL)
5031 {
Mike Williamsf5785cf2021-09-13 22:17:38 +02005032 if (round > 0)
Bram Moolenaare677df82019-09-02 22:31:11 +02005033 {
Bram Moolenaar333bd562021-02-16 22:22:13 +01005034 if (tab[i].cp == &lcs_chars.tab2)
Bram Moolenaare677df82019-09-02 22:31:11 +02005035 {
Bram Moolenaar333bd562021-02-16 22:22:13 +01005036 lcs_chars.tab1 = c1;
5037 lcs_chars.tab2 = c2;
5038 lcs_chars.tab3 = c3;
Bram Moolenaare677df82019-09-02 22:31:11 +02005039 }
5040 else if (tab[i].cp != NULL)
5041 *(tab[i].cp) = c1;
5042
5043 }
5044 p = s;
5045 break;
5046 }
5047 }
5048 }
5049
5050 if (i == entries)
zeertzjqf14b8ba2021-09-10 16:58:30 +02005051 {
Mike Williamsf5785cf2021-09-13 22:17:38 +02005052 len = (int)STRLEN("multispace");
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01005053 len2 = (int)STRLEN("leadmultispace");
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01005054 if (is_listchars
zeertzjqf14b8ba2021-09-10 16:58:30 +02005055 && STRNCMP(p, "multispace", len) == 0
5056 && p[len] == ':'
5057 && p[len + 1] != NUL)
5058 {
5059 s = p + len + 1;
5060 if (round == 0)
5061 {
5062 // Get length of lcs-multispace string in first round
5063 last_multispace = p;
5064 multispace_len = 0;
5065 while (*s != NUL && *s != ',')
5066 {
Bram Moolenaar93ff6722021-10-16 17:51:40 +01005067 c1 = get_encoded_char_adv(&s);
zeertzjq60618c82021-12-18 15:32:46 +00005068 if (char2cells(c1) > 1)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005069 return e_invalid_argument;
zeertzjqf14b8ba2021-09-10 16:58:30 +02005070 ++multispace_len;
5071 }
5072 if (multispace_len == 0)
5073 // lcs-multispace cannot be an empty string
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005074 return e_invalid_argument;
zeertzjqf14b8ba2021-09-10 16:58:30 +02005075 p = s;
5076 }
5077 else
5078 {
5079 int multispace_pos = 0;
Mike Williamsf5785cf2021-09-13 22:17:38 +02005080
zeertzjqf14b8ba2021-09-10 16:58:30 +02005081 while (*s != NUL && *s != ',')
5082 {
Bram Moolenaar93ff6722021-10-16 17:51:40 +01005083 c1 = get_encoded_char_adv(&s);
zeertzjqf14b8ba2021-09-10 16:58:30 +02005084 if (p == last_multispace)
5085 lcs_chars.multispace[multispace_pos++] = c1;
5086 }
5087 p = s;
5088 }
5089 }
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01005090
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01005091 else if (is_listchars
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01005092 && STRNCMP(p, "leadmultispace", len2) == 0
5093 && p[len2] == ':'
5094 && p[len2 + 1] != NUL)
5095 {
5096 s = p + len2 + 1;
5097 if (round == 0)
5098 {
zeertzjqb5f08012022-06-09 13:55:28 +01005099 // get length of lcs-leadmultispace string in first
5100 // round
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01005101 last_lmultispace = p;
5102 lead_multispace_len = 0;
5103 while (*s != NUL && *s != ',')
5104 {
5105 c1 = get_encoded_char_adv(&s);
5106 if (char2cells(c1) > 1)
5107 return e_invalid_argument;
5108 ++lead_multispace_len;
5109 }
5110 if (lead_multispace_len == 0)
zeertzjqb5f08012022-06-09 13:55:28 +01005111 // lcs-leadmultispace cannot be an empty string
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01005112 return e_invalid_argument;
5113 p = s;
5114 }
5115 else
5116 {
5117 int multispace_pos = 0;
5118
5119 while (*s != NUL && *s != ',')
5120 {
5121 c1 = get_encoded_char_adv(&s);
5122 if (p == last_lmultispace)
5123 lcs_chars.leadmultispace[multispace_pos++] = c1;
5124 }
5125 p = s;
5126 }
5127 }
zeertzjqf14b8ba2021-09-10 16:58:30 +02005128 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005129 return e_invalid_argument;
zeertzjqf14b8ba2021-09-10 16:58:30 +02005130 }
5131
Bram Moolenaare677df82019-09-02 22:31:11 +02005132 if (*p == ',')
5133 ++p;
5134 }
5135 }
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005136
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01005137 if (apply)
zeertzjqf14b8ba2021-09-10 16:58:30 +02005138 {
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01005139 if (is_listchars)
5140 {
5141 vim_free(wp->w_lcs_chars.multispace);
5142 vim_free(wp->w_lcs_chars.leadmultispace);
5143 wp->w_lcs_chars = lcs_chars;
5144 }
5145 else
5146 {
5147 wp->w_fill_chars = fill_chars;
5148 }
zeertzjqf14b8ba2021-09-10 16:58:30 +02005149 }
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01005150 else if (is_listchars)
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005151 {
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01005152 vim_free(lcs_chars.multispace);
5153 vim_free(lcs_chars.leadmultispace);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005154 }
Bram Moolenaare677df82019-09-02 22:31:11 +02005155
5156 return NULL; // no error
5157}
zeertzjq8ca29b62022-08-09 12:53:14 +01005158
5159/*
5160 * Check all global and local values of 'listchars' and 'fillchars'.
5161 * Return an untranslated error messages if any of them is invalid, NULL
5162 * otherwise.
5163 */
5164 char *
5165check_chars_options(void)
5166{
5167 tabpage_T *tp;
5168 win_T *wp;
5169
5170 if (set_chars_option(curwin, &p_lcs, FALSE) != NULL)
5171 return e_conflicts_with_value_of_listchars;
5172 if (set_chars_option(curwin, &p_fcs, FALSE) != NULL)
5173 return e_conflicts_with_value_of_fillchars;
5174 FOR_ALL_TAB_WINDOWS(tp, wp)
5175 {
5176 if (set_chars_option(wp, &wp->w_p_lcs, FALSE) != NULL)
5177 return e_conflicts_with_value_of_listchars;
5178 if (set_chars_option(wp, &wp->w_p_fcs, FALSE) != NULL)
5179 return e_conflicts_with_value_of_fillchars;
5180 }
5181 return NULL;
5182}