blob: 15fbe598b32d4f595b5edfe0c6bedfe773812816 [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 Moolenaar838b7462022-09-26 15:19:56 +010052static int screenclear2(int doclear);
Bram Moolenaarcfce7172017-08-17 20:31:48 +020053static void lineclear(unsigned off, int width, int attr);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010054static void lineinvalid(unsigned off, int width);
Bram Moolenaarcfce7172017-08-17 20:31:48 +020055static int win_do_lines(win_T *wp, int row, int line_count, int mayclear, int del, int clear_attr);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010056static void win_rest_invalid(win_T *wp);
57static void msg_pos_mode(void);
58static void recording_mode(int attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000059
Bram Moolenaar63d9e732019-12-05 21:10:38 +010060// Ugly global: overrule attribute used by screen_char()
Bram Moolenaar071d4272004-06-13 20:20:40 +000061static int screen_char_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000062
Bram Moolenaar860cae12010-06-05 23:22:07 +020063#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +020064/*
65 * Return TRUE if the cursor line in window "wp" may be concealed, according
66 * to the 'concealcursor' option.
67 */
68 int
Bram Moolenaar05540972016-01-30 20:31:25 +010069conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +020070{
71 int c;
72
73 if (*wp->w_p_cocu == NUL)
74 return FALSE;
Bram Moolenaar24959102022-05-07 20:01:16 +010075 if (get_real_state() & MODE_VISUAL)
Bram Moolenaarf5963f72010-07-23 22:10:27 +020076 c = 'v';
Bram Moolenaar24959102022-05-07 20:01:16 +010077 else if (State & MODE_INSERT)
Bram Moolenaarf5963f72010-07-23 22:10:27 +020078 c = 'i';
Bram Moolenaar24959102022-05-07 20:01:16 +010079 else if (State & MODE_NORMAL)
Bram Moolenaarf5963f72010-07-23 22:10:27 +020080 c = 'n';
Bram Moolenaar24959102022-05-07 20:01:16 +010081 else if (State & MODE_CMDLINE)
Bram Moolenaarca8c9862010-07-24 15:00:38 +020082 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +020083 else
84 return FALSE;
85 return vim_strchr(wp->w_p_cocu, c) != NULL;
86}
87
88/*
89 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
Bram Moolenaarea042672021-06-29 20:22:32 +020090 * To be called after changing the state, "was_concealed" is the value of
91 * "conceal_cursor_line()" before the change.
92 * "
Bram Moolenaarf5963f72010-07-23 22:10:27 +020093 */
94 void
Bram Moolenaarea042672021-06-29 20:22:32 +020095conceal_check_cursor_line(int was_concealed)
Bram Moolenaarf5963f72010-07-23 22:10:27 +020096{
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 Moolenaar393f8d62022-10-02 14:28:30 +0100423 * Return TRUE if the character at "row" / "col" is under the popup menu and it
424 * will be redrawn soon or it is under another popup.
425 */
426 static int
427skip_for_popup(int row, int col)
428{
429 // Popup windows with zindex higher than POPUPMENU_ZINDEX go on top.
430 if (pum_under_menu(row, col, TRUE)
431#ifdef FEAT_PROP_POPUP
432 && screen_zindex <= POPUPMENU_ZINDEX
433#endif
434 )
435 return TRUE;
436#ifdef FEAT_PROP_POPUP
437 if (blocked_by_popup(row, col))
438 return TRUE;
439#endif
440 return FALSE;
441}
442
443/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444 * Move one "cooked" screen line to the screen, but only the characters that
445 * have actually changed. Handle insert/delete character.
446 * "coloff" gives the first column on the screen for this line.
447 * "endcol" gives the columns where valid characters are.
448 * "clear_width" is the width of the window. It's > 0 if the rest of the line
449 * needs to be cleared, negative otherwise.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200450 * "flags" can have bits:
451 * SLF_POPUP popup window
452 * SLF_RIGHTLEFT rightleft window:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
454 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
455 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200456 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100457screen_line(
Bram Moolenaar96ba25a2022-07-04 17:34:33 +0100458 win_T *wp,
459 int row,
460 int coloff,
461 int endcol,
462 int clear_width,
463 int flags UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464{
465 unsigned off_from;
466 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +0000467 unsigned max_off_from;
468 unsigned max_off_to;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470 int hl;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100471 int force = FALSE; // force update rest of the line
472 int redraw_this // bool: does character need redraw?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473#ifdef FEAT_GUI
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100474 = TRUE // For GUI when while-loop empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475#endif
476 ;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100477 int redraw_next; // redraw_this for next character
Bram Moolenaar0c502d22022-10-11 12:48:44 +0100478#ifdef FEAT_GUI_MSWIN
479 int changed_this; // TRUE if character changed
480 int changed_next; // TRUE if next character changed
481#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482 int clear_next = FALSE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100483 int char_cells; // 1: normal char
484 // 2: occupies two display cells
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100486 // Check for illegal row and col, just in case.
Bram Moolenaar5ad15df2012-03-16 19:07:58 +0100487 if (row >= Rows)
488 row = Rows - 1;
489 if (endcol > Columns)
490 endcol = Columns;
491
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492# ifdef FEAT_CLIPBOARD
493 clip_may_clear_selection(row, row);
494# endif
495
496 off_from = (unsigned)(current_ScreenLine - ScreenLines);
497 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +0000498 max_off_from = off_from + screen_Columns;
499 max_off_to = LineOffset[row] + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500
501#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200502 if (flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100504 // Clear rest first, because it's left of the text.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505 if (clear_width > 0)
506 {
507 while (col <= endcol && ScreenLines[off_to] == ' '
508 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +0100509 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510 {
511 ++off_to;
512 ++col;
513 }
514 if (col <= endcol)
515 screen_fill(row, row + 1, col + coloff,
516 endcol + coloff + 1, ' ', ' ', 0);
517 }
518 col = endcol + 1;
519 off_to = LineOffset[row] + col + coloff;
520 off_from += col;
521 endcol = (clear_width > 0 ? clear_width : -clear_width);
522 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100523#endif // FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100525#ifdef FEAT_PROP_POPUP
Bram Moolenaar92e25ab2019-11-26 22:39:10 +0100526 // First char of a popup window may go on top of the right half of a
527 // double-wide character. Clear the left half to avoid it getting the popup
528 // window background color.
Bram Moolenaar927495b2020-11-06 17:58:35 +0100529 if (coloff > 0 && enc_utf8
530 && ScreenLines[off_to] == 0
Bram Moolenaardc0cf1d2020-08-23 15:09:36 +0200531 && ScreenLinesUC[off_to - 1] != 0
532 && (*mb_char2cells)(ScreenLinesUC[off_to - 1]) > 1)
Bram Moolenaar92e25ab2019-11-26 22:39:10 +0100533 {
534 ScreenLines[off_to - 1] = ' ';
535 ScreenLinesUC[off_to - 1] = 0;
536 screen_char(off_to - 1, row, col + coloff - 1);
537 }
538#endif
539
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
Bram Moolenaar0c502d22022-10-11 12:48:44 +0100541#ifdef FEAT_GUI_MSWIN
542 changed_next = redraw_next;
543#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544
545 while (col < endcol)
546 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +0000548 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549 else
550 char_cells = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551
552 redraw_this = redraw_next;
Bram Moolenaarb9081882022-07-09 04:56:24 +0100553 redraw_next = force || char_needs_redraw(off_from + char_cells,
554 off_to + char_cells, endcol - col - char_cells);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555
556#ifdef FEAT_GUI
Bram Moolenaar0c502d22022-10-11 12:48:44 +0100557# ifdef FEAT_GUI_MSWIN
558 changed_this = changed_next;
559 changed_next = redraw_next;
560# endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100561 // If the next character was bold, then redraw the current character to
562 // remove any pixels that might have spilt over into us. This only
563 // happens in the GUI.
Bram Moolenaar0c502d22022-10-11 12:48:44 +0100564 // With MS-Windows antialiasing may also cause pixels to spill over
565 // from a previous character, no matter attributes, always redraw if a
566 // character changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 if (redraw_next && gui.in_use)
568 {
Bram Moolenaar0c502d22022-10-11 12:48:44 +0100569# ifndef FEAT_GUI_MSWIN
Bram Moolenaarb9081882022-07-09 04:56:24 +0100570 hl = ScreenAttrs[off_to + char_cells];
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000571 if (hl > HL_ALL)
572 hl = syn_attr2attr(hl);
573 if (hl & HL_BOLD)
Bram Moolenaar0c502d22022-10-11 12:48:44 +0100574# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 redraw_this = TRUE;
576 }
577#endif
Bram Moolenaar393f8d62022-10-02 14:28:30 +0100578 // Do not redraw if under the popup menu.
579 if (redraw_this && skip_for_popup(row, col + coloff))
Bram Moolenaar33796b32019-06-08 16:01:13 +0200580 redraw_this = FALSE;
Bram Moolenaar393f8d62022-10-02 14:28:30 +0100581
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 if (redraw_this)
583 {
584 /*
585 * Special handling when 'xs' termcap flag set (hpterm):
586 * Attributes for characters are stored at the position where the
587 * cursor is when writing the highlighting code. The
588 * start-highlighting code must be written with the cursor on the
589 * first highlighted character. The stop-highlighting code must
590 * be written with the cursor just after the last highlighted
591 * character.
Bram Moolenaarc4568ab2018-11-16 16:21:05 +0100592 * Overwriting a character doesn't remove its highlighting. Need
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 * to clear the rest of the line, and force redrawing it
594 * completely.
595 */
596 if ( p_wiv
597 && !force
598#ifdef FEAT_GUI
599 && !gui.in_use
600#endif
601 && ScreenAttrs[off_to] != 0
602 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
603 {
604 /*
605 * Need to remove highlighting attributes here.
606 */
607 windgoto(row, col + coloff);
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100608 out_str(T_CE); // clear rest of this screen line
609 screen_start(); // don't know where cursor is now
610 force = TRUE; // force redraw of rest of the line
611 redraw_next = TRUE; // or else next char would miss out
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612
613 /*
614 * If the previous character was highlighted, need to stop
615 * highlighting at this character.
616 */
617 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
618 {
619 screen_attr = ScreenAttrs[off_to - 1];
620 term_windgoto(row, col + coloff);
621 screen_stop_highlight();
622 }
623 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100624 screen_attr = 0; // highlighting has stopped
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 if (enc_dbcs != 0)
627 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100628 // Check if overwriting a double-byte with a single-byte or
629 // the other way around requires another character to be
630 // redrawn. For UTF-8 this isn't needed, because comparing
631 // ScreenLinesUC[] is sufficient.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 if (char_cells == 1
633 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +0000634 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100636 // Writing a single-cell character over a double-cell
637 // character: need to redraw the next cell.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 ScreenLines[off_to + 1] = 0;
639 redraw_next = TRUE;
640 }
641 else if (char_cells == 2
642 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +0000643 && (*mb_off2cells)(off_to, max_off_to) == 1
644 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100646 // Writing the second half of a double-cell character over
647 // a double-cell character: need to redraw the second
648 // cell.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 ScreenLines[off_to + 2] = 0;
650 redraw_next = TRUE;
651 }
652
653 if (enc_dbcs == DBCS_JPNU)
654 ScreenLines2[off_to] = ScreenLines2[off_from];
655 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100656 // When writing a single-width character over a double-width
657 // character and at the end of the redrawn text, need to clear out
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000658 // the right half of the old character.
659 // Also required when writing the right half of a double-width
660 // char over the left half of an existing one.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 if (has_mbyte && col + char_cells == endcol
662 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +0000663 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +0000665 && (*mb_off2cells)(off_to, max_off_to) == 1
666 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 clear_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668
669 ScreenLines[off_to] = ScreenLines[off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 if (enc_utf8)
671 {
672 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
673 if (ScreenLinesUC[off_from] != 0)
674 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000675 int i;
676
677 for (i = 0; i < Screen_mco; ++i)
678 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 }
680 }
681 if (char_cells == 2)
682 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683
684#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100685 // The bold trick makes a single column of pixels appear in the
686 // next character. When a bold character is removed, the next
687 // character should be redrawn too. This happens for our own GUI
688 // and for some xterms.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 if (
690# ifdef FEAT_GUI
691 gui.in_use
692# endif
693# if defined(FEAT_GUI) && defined(UNIX)
694 ||
695# endif
696# ifdef UNIX
697 term_is_xterm
698# endif
699 )
700 {
701 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000702 if (hl > HL_ALL)
703 hl = syn_attr2attr(hl);
704 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 redraw_next = TRUE;
706 }
707#endif
Bram Moolenaar0c502d22022-10-11 12:48:44 +0100708#ifdef FEAT_GUI_MSWIN
709 // MS-Windows antialiasing may spill over to the next character,
710 // redraw that one if this one changed, no matter attributes.
711 if (gui.in_use && changed_this)
712 redraw_next = TRUE;
713#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 ScreenAttrs[off_to] = ScreenAttrs[off_from];
Bram Moolenaara12a1612019-01-24 16:39:02 +0100715
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100716 // For simplicity set the attributes of second half of a
717 // double-wide character equal to the first half.
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000718 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000720
721 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 screen_char(off_to, row, col + coloff);
725 }
726 else if ( p_wiv
727#ifdef FEAT_GUI
728 && !gui.in_use
729#endif
730 && col + coloff > 0)
731 {
732 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
733 {
734 /*
735 * Don't output stop-highlight when moving the cursor, it will
736 * stop the highlighting when it should continue.
737 */
738 screen_attr = 0;
739 }
740 else if (screen_attr != 0)
741 screen_stop_highlight();
742 }
743
Bram Moolenaarb9081882022-07-09 04:56:24 +0100744 ScreenCols[off_to] = ScreenCols[off_from];
745 if (char_cells == 2)
746 ScreenCols[off_to + 1] = ScreenCols[off_from];
747
748 off_to += char_cells;
749 off_from += char_cells;
750 col += char_cells;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 }
752
Bram Moolenaarff85d4a2022-10-02 15:21:04 +0100753 if (clear_next && !skip_for_popup(row, col + coloff))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100755 // Clear the second half of a double-wide character of which the left
756 // half was overwritten with a single-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 ScreenLines[off_to] = ' ';
758 if (enc_utf8)
759 ScreenLinesUC[off_to] = 0;
760 screen_char(off_to, row, col + coloff);
761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762
763 if (clear_width > 0
764#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200765 && !(flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766#endif
767 )
768 {
769#ifdef FEAT_GUI
770 int startCol = col;
771#endif
772
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100773 // blank out the rest of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 while (col < clear_width && ScreenLines[off_to] == ' '
775 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +0100776 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 {
Bram Moolenaarb9081882022-07-09 04:56:24 +0100778 ScreenCols[off_to] = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 ++off_to;
780 ++col;
781 }
782 if (col < clear_width)
783 {
784#ifdef FEAT_GUI
785 /*
786 * In the GUI, clearing the rest of the line may leave pixels
787 * behind if the first character cleared was bold. Some bold
788 * fonts spill over the left. In this case we redraw the previous
789 * character too. If we didn't skip any blanks above, then we
790 * only redraw if the character wasn't already redrawn anyway.
791 */
Bram Moolenaar9c697322006-10-09 20:11:17 +0000792 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 {
794 hl = ScreenAttrs[off_to];
795 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +0000796 {
797 int prev_cells = 1;
Bram Moolenaara12a1612019-01-24 16:39:02 +0100798
Bram Moolenaar9c697322006-10-09 20:11:17 +0000799 if (enc_utf8)
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100800 // for utf-8, ScreenLines[char_offset + 1] == 0 means
801 // that its width is 2.
Bram Moolenaar9c697322006-10-09 20:11:17 +0000802 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
803 else if (enc_dbcs != 0)
804 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100805 // find previous character by counting from first
806 // column and get its width.
Bram Moolenaar9c697322006-10-09 20:11:17 +0000807 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +0000808 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +0000809
810 while (off < off_to)
811 {
Bram Moolenaar367329b2007-08-30 11:53:22 +0000812 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +0000813 off += prev_cells;
814 }
815 }
816
Bram Moolenaarff85d4a2022-10-02 15:21:04 +0100817 if (!skip_for_popup(row, col + coloff - prev_cells))
818 {
819 if (enc_dbcs != 0 && prev_cells > 1)
820 screen_char_2(off_to - prev_cells, row,
Bram Moolenaar9c697322006-10-09 20:11:17 +0000821 col + coloff - prev_cells);
Bram Moolenaarff85d4a2022-10-02 15:21:04 +0100822 else
823 screen_char(off_to - prev_cells, row,
Bram Moolenaar9c697322006-10-09 20:11:17 +0000824 col + coloff - prev_cells);
Bram Moolenaarff85d4a2022-10-02 15:21:04 +0100825 }
Bram Moolenaar9c697322006-10-09 20:11:17 +0000826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 }
828#endif
829 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
830 ' ', ' ', 0);
Bram Moolenaarb9081882022-07-09 04:56:24 +0100831 while (col < clear_width)
832 {
833 ScreenCols[off_to++] = MAXCOL;
834 ++col;
835 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 }
837 }
838
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200839 if (clear_width > 0
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100840#ifdef FEAT_PROP_POPUP
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200841 && !(flags & SLF_POPUP) // no separator for popup window
842#endif
843 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200845 // For a window that has a right neighbor, draw the separator char
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200846 // right of the window contents. But not on top of a popup window.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200847 if (coloff + col < Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 {
Bram Moolenaarff85d4a2022-10-02 15:21:04 +0100849 if (!skip_for_popup(row, col + coloff))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200851 int c;
852
Bram Moolenaar96ba25a2022-07-04 17:34:33 +0100853 c = fillchar_vsep(&hl, wp);
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200854 if (ScreenLines[off_to] != (schar_T)c
855 || (enc_utf8 && (int)ScreenLinesUC[off_to]
856 != (c >= 0x80 ? c : 0))
857 || ScreenAttrs[off_to] != hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200859 ScreenLines[off_to] = c;
860 ScreenAttrs[off_to] = hl;
861 if (enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200863 if (c >= 0x80)
864 {
865 ScreenLinesUC[off_to] = c;
866 ScreenLinesC[0][off_to] = 0;
867 }
868 else
869 ScreenLinesUC[off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 }
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200871 screen_char(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 }
874 }
875 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 LineWraps[row] = FALSE;
877 }
878}
879
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000880#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000882 * Mirror text "str" for right-left displaying.
883 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000885 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100886rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887{
888 char_u *p1, *p2;
889 int t;
890
891 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
892 {
893 t = *p1;
894 *p1 = *p2;
895 *p2 = t;
896 }
897}
898#endif
899
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 * Draw the verticap separator right of window "wp" starting with line "row".
902 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200903 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100904draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905{
906 int hl;
907 int c;
908
909 if (wp->w_vsep_width)
910 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100911 // draw the vertical separator right of this window
Bram Moolenaar96ba25a2022-07-04 17:34:33 +0100912 c = fillchar_vsep(&hl, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
914 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
915 c, ' ', hl);
916 }
917}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100919static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920
921/*
Bram Moolenaar367329b2007-08-30 11:53:22 +0000922 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 */
924 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100925status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926{
927 int len = 0;
928
929#ifdef FEAT_MENU
930 int emenu = (xp->xp_context == EXPAND_MENUS
931 || xp->xp_context == EXPAND_MENUNAMES);
932
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100933 // Check for menu separators - replace with '|'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 if (emenu && menu_is_separator(s))
935 return 1;
936#endif
937
938 while (*s != NUL)
939 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +0000940 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +0000941 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100942 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 }
944
945 return len;
946}
947
948/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +0000949 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000950 * These are backslashes used for escaping. Do show backslashes in help tags.
951 */
952 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100953skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000954{
Bram Moolenaar7693ec62008-07-24 18:29:37 +0000955 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000956#ifdef FEAT_MENU
957 || ((xp->xp_context == EXPAND_MENUS
958 || xp->xp_context == EXPAND_MENUNAMES)
959 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
960#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +0000961 )
962 {
963#ifndef BACKSLASH_IN_FILENAME
964 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
965 return 2;
966#endif
967 return 1;
968 }
969 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000970}
971
972/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 * Show wildchar matches in the status line.
974 * Show at least the "match" item.
975 * We start at item 'first_match' in the list and show all matches that fit.
976 *
977 * If inversion is possible we use it. Else '=' characters are used.
978 */
979 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100980win_redr_status_matches(
981 expand_T *xp,
982 int num_matches,
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100983 char_u **matches, // list of matches
Bram Moolenaar05540972016-01-30 20:31:25 +0100984 int match,
985 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986{
987#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
988 int row;
989 char_u *buf;
990 int len;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100991 int clen; // length in screen cells
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 int fillchar;
993 int attr;
994 int i;
995 int highlight = TRUE;
996 char_u *selstart = NULL;
997 int selstart_col = 0;
998 char_u *selend = NULL;
999 static int first_match = 0;
1000 int add_left = FALSE;
1001 char_u *s;
1002#ifdef FEAT_MENU
1003 int emenu;
1004#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001007 if (matches == NULL) // interrupted completion?
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 return;
1009
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001010 if (has_mbyte)
Bram Moolenaar964b3742019-05-24 18:54:09 +02001011 buf = alloc(Columns * MB_MAXBYTES + 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001012 else
Bram Moolenaar964b3742019-05-24 18:54:09 +02001013 buf = alloc(Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 if (buf == NULL)
1015 return;
1016
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001017 if (match == -1) // don't show match but original text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 {
1019 match = 0;
1020 highlight = FALSE;
1021 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001022 // count 1 for the ending ">"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 clen = status_match_len(xp, L_MATCH(match)) + 3;
1024 if (match == 0)
1025 first_match = 0;
1026 else if (match < first_match)
1027 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001028 // jumping left, as far as we can go
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 first_match = match;
1030 add_left = TRUE;
1031 }
1032 else
1033 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001034 // check if match fits on the screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 for (i = first_match; i < match; ++i)
1036 clen += status_match_len(xp, L_MATCH(i)) + 2;
1037 if (first_match > 0)
1038 clen += 2;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001039 // jumping right, put match at the left
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 if ((long)clen > Columns)
1041 {
1042 first_match = match;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001043 // if showing the last match, we can add some on the left
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 clen = 2;
1045 for (i = match; i < num_matches; ++i)
1046 {
1047 clen += status_match_len(xp, L_MATCH(i)) + 2;
1048 if ((long)clen >= Columns)
1049 break;
1050 }
1051 if (i == num_matches)
1052 add_left = TRUE;
1053 }
1054 }
1055 if (add_left)
1056 while (first_match > 0)
1057 {
1058 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
1059 if ((long)clen >= Columns)
1060 break;
1061 --first_match;
1062 }
1063
Bram Moolenaar3633cf52017-07-31 22:29:35 +02001064 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065
1066 if (first_match == 0)
1067 {
1068 *buf = NUL;
1069 len = 0;
1070 }
1071 else
1072 {
1073 STRCPY(buf, "< ");
1074 len = 2;
1075 }
1076 clen = len;
1077
1078 i = first_match;
1079 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
1080 {
1081 if (i == match)
1082 {
1083 selstart = buf + len;
1084 selstart_col = clen;
1085 }
1086
1087 s = L_MATCH(i);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001088 // Check for menu separators - replace with '|'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089#ifdef FEAT_MENU
1090 emenu = (xp->xp_context == EXPAND_MENUS
1091 || xp->xp_context == EXPAND_MENUNAMES);
1092 if (emenu && menu_is_separator(s))
1093 {
1094 STRCPY(buf + len, transchar('|'));
1095 l = (int)STRLEN(buf + len);
1096 len += l;
1097 clen += l;
1098 }
1099 else
1100#endif
1101 for ( ; *s != NUL; ++s)
1102 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00001103 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 clen += ptr2cells(s);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001105 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 {
1107 STRNCPY(buf + len, s, l);
1108 s += l - 1;
1109 len += l;
1110 }
1111 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 {
1113 STRCPY(buf + len, transchar_byte(*s));
1114 len += (int)STRLEN(buf + len);
1115 }
1116 }
1117 if (i == match)
1118 selend = buf + len;
1119
1120 *(buf + len++) = ' ';
1121 *(buf + len++) = ' ';
1122 clen += 2;
1123 if (++i == num_matches)
1124 break;
1125 }
1126
1127 if (i != num_matches)
1128 {
1129 *(buf + len++) = '>';
1130 ++clen;
1131 }
1132
1133 buf[len] = NUL;
1134
1135 row = cmdline_row - 1;
1136 if (row >= 0)
1137 {
1138 if (wild_menu_showing == 0)
1139 {
1140 if (msg_scrolled > 0)
1141 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001142 // Put the wildmenu just above the command line. If there is
1143 // no room, scroll the screen one line up.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 if (cmdline_row == Rows - 1)
1145 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001146 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 ++msg_scrolled;
1148 }
1149 else
1150 {
1151 ++cmdline_row;
1152 ++row;
1153 }
1154 wild_menu_showing = WM_SCROLLED;
1155 }
1156 else
1157 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001158 // Create status line if needed by setting 'laststatus' to 2.
1159 // Set 'winminheight' to zero to avoid that the window is
1160 // resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 if (lastwin->w_status_height == 0)
1162 {
1163 save_p_ls = p_ls;
1164 save_p_wmh = p_wmh;
1165 p_ls = 2;
1166 p_wmh = 0;
1167 last_status(FALSE);
1168 }
1169 wild_menu_showing = WM_SHOWN;
1170 }
1171 }
1172
1173 screen_puts(buf, row, 0, attr);
1174 if (selstart != NULL && highlight)
1175 {
1176 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001177 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 }
1179
1180 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
1181 }
1182
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 vim_free(buf);
1185}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 * Return TRUE if the status line of window "wp" is connected to the status
1189 * line of the window right of it. If not, then it's a vertical separator.
1190 * Only call if (wp->w_vsep_width != 0).
1191 */
1192 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001193stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194{
1195 frame_T *fr;
1196
1197 fr = wp->w_frame;
1198 while (fr->fr_parent != NULL)
1199 {
1200 if (fr->fr_parent->fr_layout == FR_COL)
1201 {
1202 if (fr->fr_next != NULL)
1203 break;
1204 }
1205 else
1206 {
1207 if (fr->fr_next != NULL)
1208 return TRUE;
1209 }
1210 fr = fr->fr_parent;
1211 }
1212 return FALSE;
1213}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216/*
1217 * Get the value to show for the language mappings, active 'keymap'.
1218 */
1219 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001220get_keymap_str(
1221 win_T *wp,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001222 char_u *fmt, // format string containing one %s item
1223 char_u *buf, // buffer for the result
1224 int len) // length of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225{
1226 char_u *p;
1227
1228 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
1229 return FALSE;
1230
1231 {
1232#ifdef FEAT_EVAL
1233 buf_T *old_curbuf = curbuf;
1234 win_T *old_curwin = curwin;
1235 char_u *s;
1236
1237 curbuf = wp->w_buffer;
1238 curwin = wp;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001239 STRCPY(buf, "b:keymap_name"); // must be writable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 ++emsg_skip;
Bram Moolenaara4e0b972022-10-01 19:43:52 +01001241 s = p = eval_to_string(buf, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 --emsg_skip;
1243 curbuf = old_curbuf;
1244 curwin = old_curwin;
1245 if (p == NULL || *p == NUL)
1246#endif
1247 {
1248#ifdef FEAT_KEYMAP
1249 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
1250 p = wp->w_buffer->b_p_keymap;
1251 else
1252#endif
1253 p = (char_u *)"lang";
1254 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02001255 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 buf[0] = NUL;
1257#ifdef FEAT_EVAL
1258 vim_free(s);
1259#endif
1260 }
1261 return buf[0] != NUL;
1262}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263
1264#if defined(FEAT_STL_OPT) || defined(PROTO)
1265/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001266 * Redraw the status line or ruler of window "wp".
1267 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001269 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001270win_redr_custom(
1271 win_T *wp,
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001272 int draw_ruler) // TRUE or FALSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273{
Bram Moolenaar1d633412013-12-11 15:52:01 +01001274 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 int attr;
1276 int curattr;
1277 int row;
1278 int col = 0;
1279 int maxwidth;
1280 int width;
1281 int n;
1282 int len;
1283 int fillchar;
1284 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00001285 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 char_u *p;
Luuk van Baal7b224fd2022-11-07 12:16:51 +00001287 char_u *opt_name;
1288 int opt_scope = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01001289 stl_hlrec_T *hltab;
1290 stl_hlrec_T *tabtab;
Bram Moolenaar61452852011-02-01 18:01:11 +01001291 win_T *ewp;
1292 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001294 // There is a tiny chance that this gets called recursively: When
1295 // redrawing a status line triggers redrawing the ruler or tabline.
1296 // Avoid trouble by not allowing recursion.
Bram Moolenaar1d633412013-12-11 15:52:01 +01001297 if (entered)
1298 return;
1299 entered = TRUE;
1300
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001301 // setup environment for the task at hand
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001302 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 {
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001304 // Use 'tabline'. Always at the first line of the screen.
Bram Moolenaar362f3562009-11-03 16:20:34 +00001305 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001306 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00001307 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01001308 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001309 maxwidth = Columns;
Luuk van Baal7b224fd2022-11-07 12:16:51 +00001310 opt_name = (char_u *)"tabline";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001312 else
1313 {
Bram Moolenaar49c51b82021-04-01 16:16:18 +02001314 row = statusline_row(wp);
Bram Moolenaar3633cf52017-07-31 22:29:35 +02001315 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02001316 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001317
1318 if (draw_ruler)
1319 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00001320 stl = p_ruf;
Luuk van Baal7b224fd2022-11-07 12:16:51 +00001321 opt_name = (char_u *)"rulerformat";
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001322 // advance past any leading group spec - implicit in ru_col
Bram Moolenaar362f3562009-11-03 16:20:34 +00001323 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001324 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00001325 if (*++stl == '-')
1326 stl++;
1327 if (atoi((char *)stl))
1328 while (VIM_ISDIGIT(*stl))
1329 stl++;
1330 if (*stl++ != '(')
1331 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001332 }
Bram Moolenaar02631462017-09-22 15:20:32 +02001333 col = ru_col - (Columns - wp->w_width);
1334 if (col < (wp->w_width + 1) / 2)
1335 col = (wp->w_width + 1) / 2;
1336 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001337 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001338 {
1339 row = Rows - 1;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001340 --maxwidth; // writing in last column may cause scrolling
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001341 fillchar = ' ';
1342 attr = 0;
1343 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001344 }
1345 else
1346 {
Luuk van Baal7b224fd2022-11-07 12:16:51 +00001347 opt_name = (char_u *)"statusline";
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001348 if (*wp->w_p_stl != NUL)
Luuk van Baal7b224fd2022-11-07 12:16:51 +00001349 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00001350 stl = wp->w_p_stl;
Luuk van Baal7b224fd2022-11-07 12:16:51 +00001351 opt_scope = OPT_LOCAL;
1352 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001353 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00001354 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001355 }
1356
Bram Moolenaar53f81742017-09-22 14:35:51 +02001357 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001358 }
1359
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01001361 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001363 // Temporarily reset 'cursorbind', we don't want a side effect from moving
1364 // the cursor away and back.
Bram Moolenaar61452852011-02-01 18:01:11 +01001365 ewp = wp == NULL ? curwin : wp;
1366 p_crb_save = ewp->w_p_crb;
1367 ewp->w_p_crb = FALSE;
1368
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001369 // Make a copy, because the statusline may include a function call that
1370 // might change the option value and free the memory.
Bram Moolenaar362f3562009-11-03 16:20:34 +00001371 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01001372 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Luuk van Baal7b224fd2022-11-07 12:16:51 +00001373 stl, opt_name, opt_scope,
Bram Moolenaar8133cc62020-10-26 21:05:27 +01001374 fillchar, maxwidth, &hltab, &tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00001375 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01001376 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001378 // Make all characters printable.
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01001379 p = transstr(buf);
1380 if (p != NULL)
1381 {
1382 vim_strncpy(buf, p, sizeof(buf) - 1);
1383 vim_free(p);
1384 }
1385
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001386 // fill up with "fillchar"
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01001387 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00001388 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 len += (*mb_char2bytes)(fillchar, buf + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 ++width;
1392 }
1393 buf[len] = NUL;
1394
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001395 /*
1396 * Draw each snippet with the specified highlighting.
1397 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 curattr = attr;
1399 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001400 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001402 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 screen_puts_len(p, len, row, col, curattr);
1404 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001405 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001407 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001409 else if (hltab[n].userhl < 0)
1410 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02001411#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02001412 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
1413 && wp->w_status_height != 0)
1414 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02001415 else if (wp != NULL && bt_terminal(wp->w_buffer)
1416 && wp->w_status_height != 0)
1417 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02001418#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00001419 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001420 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001422 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 }
1424 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001425
1426 if (wp == NULL)
1427 {
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001428 // Fill the TabPageIdxs[] array for clicking in the tab pagesline.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001429 col = 0;
1430 len = 0;
1431 p = buf;
1432 fillchar = 0;
1433 for (n = 0; tabtab[n].start != NULL; n++)
1434 {
1435 len += vim_strnsize(p, (int)(tabtab[n].start - p));
1436 while (col < len)
1437 TabPageIdxs[col++] = fillchar;
1438 p = tabtab[n].start;
1439 fillchar = tabtab[n].userhl;
1440 }
1441 while (col < Columns)
1442 TabPageIdxs[col++] = fillchar;
1443 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01001444
1445theend:
1446 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447}
1448
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001449#endif // FEAT_STL_OPT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450
1451/*
1452 * Output a single character directly to the screen and update ScreenLines.
1453 */
1454 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001455screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 char_u buf[MB_MAXBYTES + 1];
1458
Bram Moolenaar9a920d82012-06-01 15:21:02 +02001459 if (has_mbyte)
1460 buf[(*mb_char2bytes)(c, buf)] = NUL;
1461 else
Bram Moolenaar9a920d82012-06-01 15:21:02 +02001462 {
1463 buf[0] = c;
1464 buf[1] = NUL;
1465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 screen_puts(buf, row, col, attr);
1467}
1468
1469/*
1470 * Get a single character directly from ScreenLines into "bytes[]".
1471 * Also return its attribute in *attrp;
1472 */
1473 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001474screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475{
1476 unsigned off;
1477
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001478 // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
1480 {
1481 off = LineOffset[row] + col;
1482 *attrp = ScreenAttrs[off];
1483 bytes[0] = ScreenLines[off];
1484 bytes[1] = NUL;
1485
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 if (enc_utf8 && ScreenLinesUC[off] != 0)
1487 bytes[utfc_char2bytes(off, bytes)] = NUL;
1488 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
1489 {
1490 bytes[0] = ScreenLines[off];
1491 bytes[1] = ScreenLines2[off];
1492 bytes[2] = NUL;
1493 }
1494 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
1495 {
1496 bytes[1] = ScreenLines[off + 1];
1497 bytes[2] = NUL;
1498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 }
1500}
1501
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001502/*
1503 * Return TRUE if composing characters for screen posn "off" differs from
1504 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01001505 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001506 */
1507 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001508screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001509{
1510 int i;
1511
1512 for (i = 0; i < Screen_mco; ++i)
1513 {
1514 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
1515 return TRUE;
1516 if (u8cc[i] == 0)
1517 break;
1518 }
1519 return FALSE;
1520}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001521
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522/*
1523 * Put string '*text' on the screen at position 'row' and 'col', with
1524 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
1525 * Note: only outputs within one row, message is truncated at screen boundary!
1526 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
1527 */
1528 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001529screen_puts(
1530 char_u *text,
1531 int row,
1532 int col,
1533 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534{
1535 screen_puts_len(text, -1, row, col, attr);
1536}
1537
1538/*
1539 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
1540 * a NUL.
1541 */
1542 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001543screen_puts_len(
1544 char_u *text,
1545 int textlen,
1546 int row,
1547 int col,
Bram Moolenaar35d8c202022-03-03 11:46:00 +00001548 int attr_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549{
Bram Moolenaar35d8c202022-03-03 11:46:00 +00001550 int attr = attr_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 unsigned off;
1552 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02001553 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 int c;
Bram Moolenaar367329b2007-08-30 11:53:22 +00001555 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 int mbyte_blen = 1;
1557 int mbyte_cells = 1;
1558 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001559 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 int clear_next_cell = FALSE;
Bram Moolenaara12a1612019-01-24 16:39:02 +01001561#ifdef FEAT_ARABIC
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001562 int prev_c = 0; // previous Arabic character
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001563 int pc, nc, nc1;
1564 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001566 int force_redraw_this;
1567 int force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001568 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569
Bram Moolenaar0b4c9ed2019-06-03 22:04:23 +02001570 // Safety check. The check for negative row and column is to fix issue
1571 // #4102. TODO: find out why row/col could be negative.
1572 if (ScreenLines == NULL
1573 || row >= screen_Rows || row < 0
1574 || col >= screen_Columns || col < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001576 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001578 // When drawing over the right half of a double-wide char clear out the
1579 // left half. Only needed in a terminal.
Bram Moolenaar7693ec62008-07-24 18:29:37 +00001580 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaara12a1612019-01-24 16:39:02 +01001581#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00001582 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01001583#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00001584 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001585 {
Bram Moolenaarff85d4a2022-10-02 15:21:04 +01001586 if (!skip_for_popup(row, col - 1))
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001587 {
Bram Moolenaarff85d4a2022-10-02 15:21:04 +01001588 ScreenLines[off - 1] = ' ';
1589 ScreenAttrs[off - 1] = 0;
1590 if (enc_utf8)
1591 {
1592 ScreenLinesUC[off - 1] = 0;
1593 ScreenLinesC[0][off - 1] = 0;
1594 }
1595 // redraw the previous cell, make it empty
1596 screen_char(off - 1, row, col - 1);
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001597 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001598 // force the cell at "col" to be redrawn
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001599 force_redraw_next = TRUE;
1600 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00001601
Bram Moolenaar367329b2007-08-30 11:53:22 +00001602 max_off = LineOffset[row] + screen_Columns;
Bram Moolenaara064ac82007-08-05 18:10:54 +00001603 while (col < screen_Columns
1604 && (len < 0 || (int)(ptr - text) < len)
1605 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 {
1607 c = *ptr;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001608 // check if this is the first byte of a multibyte
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 if (has_mbyte)
1610 {
zeertzjq4dc513a2022-07-25 19:42:02 +01001611 mbyte_blen = enc_utf8 && len > 0
1612 ? utfc_ptr2len_len(ptr, (int)((text + len) - ptr))
1613 : (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
1615 mbyte_cells = 1;
1616 else if (enc_dbcs != 0)
1617 mbyte_cells = mbyte_blen;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001618 else // enc_utf8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 {
zeertzjq4dc513a2022-07-25 19:42:02 +01001620 u8c = len >= 0
1621 ? utfc_ptr2char_len(ptr, u8cc, (int)((text + len) - ptr))
1622 : utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaara12a1612019-01-24 16:39:02 +01001624#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
1626 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001627 // Do Arabic shaping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
1629 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001630 // Past end of string to be displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 nc = NUL;
1632 nc1 = NUL;
1633 }
1634 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001635 {
zeertzjq4dc513a2022-07-25 19:42:02 +01001636 nc = len >= 0
1637 ? utfc_ptr2char_len(ptr + mbyte_blen, pcc,
1638 (int)((text + len) - ptr - mbyte_blen))
1639 : utfc_ptr2char(ptr + mbyte_blen, pcc);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001640 nc1 = pcc[0];
1641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 pc = prev_c;
1643 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001644 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 }
1646 else
1647 prev_c = u8c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01001648#endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01001649 if (col + mbyte_cells > screen_Columns)
1650 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001651 // Only 1 cell left, but character requires 2 cells:
1652 // display a '>' in the last column to avoid wrapping.
Bram Moolenaare4ebd292010-01-19 17:40:46 +01001653 c = '>';
1654 mbyte_cells = 1;
1655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 }
1657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001659 force_redraw_this = force_redraw_next;
1660 force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001661
1662 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 || (mbyte_cells == 2
1664 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
1665 || (enc_dbcs == DBCS_JPNU
1666 && c == 0x8e
1667 && ScreenLines2[off] != ptr[1])
1668 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01001669 && (ScreenLinesUC[off] !=
1670 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
1671 || (ScreenLinesUC[off] != 0
1672 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001674 || exmode_active;
1675
Bram Moolenaarff85d4a2022-10-02 15:21:04 +01001676 if ((need_redraw || force_redraw_this) && !skip_for_popup(row, col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 {
1678#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001679 // The bold trick makes a single row of pixels appear in the next
1680 // character. When a bold character is removed, the next
1681 // character should be redrawn too. This happens for our own GUI
1682 // and for some xterms.
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001683 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684# ifdef FEAT_GUI
1685 gui.in_use
1686# endif
1687# if defined(FEAT_GUI) && defined(UNIX)
1688 ||
1689# endif
1690# ifdef UNIX
1691 term_is_xterm
1692# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001693 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001695 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001697 if (n > HL_ALL)
1698 n = syn_attr2attr(n);
1699 if (n & HL_BOLD)
1700 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 }
1702#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001703 // When at the end of the text and overwriting a two-cell
1704 // character with a one-cell character, need to clear the next
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001705 // cell. Also when overwriting the left half of a two-cell char
1706 // with the right half of a two-cell char. Do this only once
1707 // (mb_off2cells() may return 2 on the right half).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 if (clear_next_cell)
1709 clear_next_cell = FALSE;
1710 else if (has_mbyte
1711 && (len < 0 ? ptr[mbyte_blen] == NUL
1712 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00001713 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00001715 && (*mb_off2cells)(off, max_off) == 1
1716 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 clear_next_cell = TRUE;
1718
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001719 // Make sure we never leave a second byte of a double-byte behind,
1720 // it confuses mb_off2cells().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00001722 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00001724 && (*mb_off2cells)(off, max_off) == 1
1725 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 ScreenLines[off + mbyte_blen] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 ScreenLines[off] = c;
1728 ScreenAttrs[off] = attr;
Bram Moolenaarb9081882022-07-09 04:56:24 +01001729 ScreenCols[off] = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 if (enc_utf8)
1731 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001732 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 ScreenLinesUC[off] = 0;
1734 else
1735 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001736 int i;
1737
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001739 for (i = 0; i < Screen_mco; ++i)
1740 {
1741 ScreenLinesC[i][off] = u8cc[i];
1742 if (u8cc[i] == 0)
1743 break;
1744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 }
1746 if (mbyte_cells == 2)
1747 {
1748 ScreenLines[off + 1] = 0;
1749 ScreenAttrs[off + 1] = attr;
Bram Moolenaarb9081882022-07-09 04:56:24 +01001750 ScreenCols[off + 1] = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 }
1752 screen_char(off, row, col);
1753 }
1754 else if (mbyte_cells == 2)
1755 {
1756 ScreenLines[off + 1] = ptr[1];
1757 ScreenAttrs[off + 1] = attr;
Bram Moolenaarb9081882022-07-09 04:56:24 +01001758 ScreenCols[off + 1] = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 screen_char_2(off, row, col);
1760 }
1761 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
1762 {
1763 ScreenLines2[off] = ptr[1];
1764 screen_char(off, row, col);
1765 }
1766 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 screen_char(off, row, col);
1768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 if (has_mbyte)
1770 {
1771 off += mbyte_cells;
1772 col += mbyte_cells;
1773 ptr += mbyte_blen;
1774 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02001775 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001776 // This only happens at the end, display one space next.
Bram Moolenaar35d8c202022-03-03 11:46:00 +00001777 // Keep the attribute from before.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02001779 len = -1;
Bram Moolenaar35d8c202022-03-03 11:46:00 +00001780 attr = ScreenAttrs[off];
Bram Moolenaare4c21e62014-05-22 16:05:19 +02001781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 }
1783 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 {
1785 ++off;
1786 ++col;
1787 ++ptr;
1788 }
1789 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001790
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001791 // If we detected the next character needs to be redrawn, but the text
1792 // doesn't extend up to there, update the character here.
Bram Moolenaarff85d4a2022-10-02 15:21:04 +01001793 if (force_redraw_next && col < screen_Columns && !skip_for_popup(row, col))
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001794 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001795 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
1796 screen_char_2(off, row, col);
1797 else
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001798 screen_char(off, row, col);
1799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800}
1801
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001802#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001804 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001806 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001807start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808{
1809 if (p_hls && !no_hlsearch)
1810 {
Bram Moolenaar0b6849e2020-05-02 18:33:25 +02001811 end_search_hl(); // just in case it wasn't called before
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001812 last_pat_prog(&screen_search_hl.rm);
1813 screen_search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 }
1815}
1816
1817/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001818 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001820 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001821end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822{
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001823 if (screen_search_hl.rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 {
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001825 vim_regfree(screen_search_hl.rm.regprog);
1826 screen_search_hl.rm.regprog = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 }
1828}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02001829#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02001830
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001832screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833{
1834 attrentry_T *aep = NULL;
1835
1836 screen_attr = attr;
1837 if (full_screen
Bram Moolenaar4f974752019-02-17 17:44:42 +01001838#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 && termcap_active
1840#endif
1841 )
1842 {
1843#ifdef FEAT_GUI
1844 if (gui.in_use)
1845 {
1846 char buf[20];
1847
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001848 // The GUI handles this internally.
Bram Moolenaar424bcae2022-01-31 14:59:41 +00001849 sprintf(buf, "\033|%dh", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 OUT_STR(buf);
1851 }
1852 else
1853#endif
1854 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001855 if (attr > HL_ALL) // special HL attr.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001857 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 aep = syn_cterm_attr2entry(attr);
1859 else
1860 aep = syn_term_attr2entry(attr);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001861 if (aep == NULL) // did ":syntax clear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 attr = 0;
1863 else
1864 attr = aep->ae_attr;
1865 }
Bram Moolenaara050b942019-12-02 21:35:31 +01001866#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
1867 if (use_vtp())
1868 {
1869 guicolor_T defguifg, defguibg;
1870 int defctermfg, defctermbg;
1871
1872 // If FG and BG are unset, the color is undefined when
1873 // BOLD+INVERSE. Use Normal as the default value.
1874 get_default_console_color(&defctermfg, &defctermbg, &defguifg,
1875 &defguibg);
1876
1877 if (p_tgc)
1878 {
1879 if (aep == NULL || COLOR_INVALID(aep->ae_u.cterm.fg_rgb))
1880 term_fg_rgb_color(defguifg);
1881 if (aep == NULL || COLOR_INVALID(aep->ae_u.cterm.bg_rgb))
1882 term_bg_rgb_color(defguibg);
1883 }
1884 else if (t_colors >= 256)
1885 {
1886 if (aep == NULL || aep->ae_u.cterm.fg_color == 0)
1887 term_fg_color(defctermfg);
1888 if (aep == NULL || aep->ae_u.cterm.bg_color == 0)
1889 term_bg_color(defctermbg);
1890 }
1891 }
1892#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001893 if ((attr & HL_BOLD) && *T_MD != NUL) // bold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001895 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001896#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001897 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
1898 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
1899 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001900#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001901 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001902 // If the Normal FG color has BOLD attribute and the new HL
1903 // has a FG color defined, clear BOLD.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001904 out_str(T_ME);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001905 if ((attr & HL_STANDOUT) && *T_SO != NUL) // standout
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 out_str(T_SO);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001907 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) // undercurl
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01001908 out_str(T_UCS);
Bram Moolenaar84f54632022-06-29 18:39:11 +01001909 if ((attr & HL_UNDERDOUBLE) && *T_USS != NUL) // double underline
1910 out_str(T_USS);
1911 if ((attr & HL_UNDERDOTTED) && *T_DS != NUL) // dotted underline
1912 out_str(T_DS);
1913 if ((attr & HL_UNDERDASHED) && *T_CDS != NUL) // dashed underline
1914 out_str(T_CDS);
1915 if (((attr & HL_UNDERLINE) // underline or undercurl, etc.
1916 || ((attr & HL_UNDERCURL) && *T_UCS == NUL)
1917 || ((attr & HL_UNDERDOUBLE) && *T_USS == NUL)
1918 || ((attr & HL_UNDERDOTTED) && *T_DS == NUL)
1919 || ((attr & HL_UNDERDASHED) && *T_CDS == NUL))
Bram Moolenaar45a00002017-12-22 21:12:34 +01001920 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 out_str(T_US);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001922 if ((attr & HL_ITALIC) && *T_CZH != NUL) // italic
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 out_str(T_CZH);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001924 if ((attr & HL_INVERSE) && *T_MR != NUL) // inverse (reverse)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 out_str(T_MR);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001926 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) // strike
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02001927 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928
1929 /*
1930 * Output the color or start string after bold etc., in case the
1931 * bold etc. override the color setting.
1932 */
1933 if (aep != NULL)
1934 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001935#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001936 // When 'termguicolors' is set but fg or bg is unset,
1937 // fall back to the cterm colors. This helps for SpellBad,
1938 // where the GUI uses a red undercurl.
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001939 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001941 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001942 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001943 }
1944 else
1945#endif
1946 if (t_colors > 1)
1947 {
1948 if (aep->ae_u.cterm.fg_color)
1949 term_fg_color(aep->ae_u.cterm.fg_color - 1);
1950 }
1951#ifdef FEAT_TERMGUICOLORS
1952 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
1953 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001954 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001955 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 }
1957 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001958#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001959 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001961 if (aep->ae_u.cterm.bg_color)
1962 term_bg_color(aep->ae_u.cterm.bg_color - 1);
1963 }
Bram Moolenaare023e882020-05-31 16:42:30 +02001964#ifdef FEAT_TERMGUICOLORS
1965 if (p_tgc && aep->ae_u.cterm.ul_rgb != CTERMCOLOR)
1966 {
1967 if (aep->ae_u.cterm.ul_rgb != INVALCOLOR)
1968 term_ul_rgb_color(aep->ae_u.cterm.ul_rgb);
1969 }
1970 else
1971#endif
1972 if (t_colors > 1)
1973 {
1974 if (aep->ae_u.cterm.ul_color)
1975 term_ul_color(aep->ae_u.cterm.ul_color - 1);
1976 }
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001977
Bram Moolenaarf708ac52018-03-12 21:48:32 +01001978 if (!IS_CTERM)
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001979 {
1980 if (aep->ae_u.term.start != NULL)
1981 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 }
1983 }
1984 }
1985 }
1986}
1987
1988 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001989screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001991 int do_ME = FALSE; // output T_ME code
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02001992#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Bram Moolenaar09307e32020-05-29 21:42:55 +02001993 int do_ME_fg = FALSE, do_ME_bg = FALSE;
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02001994#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995
1996 if (screen_attr != 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01001997#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 && termcap_active
1999#endif
2000 )
2001 {
2002#ifdef FEAT_GUI
2003 if (gui.in_use)
2004 {
2005 char buf[20];
2006
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002007 // use internal GUI code
Bram Moolenaar424bcae2022-01-31 14:59:41 +00002008 sprintf(buf, "\033|%dH", screen_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 OUT_STR(buf);
2010 }
2011 else
2012#endif
2013 {
Bram Moolenaar84f54632022-06-29 18:39:11 +01002014 int is_under;
2015
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002016 if (screen_attr > HL_ALL) // special HL attr.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 {
2018 attrentry_T *aep;
2019
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002020 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 {
2022 /*
2023 * Assume that t_me restores the original colors!
2024 */
2025 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01002026 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02002027#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01002028 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
2029 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02002030# ifdef FEAT_VTP
2031 ? !(do_ME_fg = TRUE) : (do_ME_fg = FALSE)
2032# endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01002033 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002034#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01002035 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02002036#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01002037 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
2038 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02002039# ifdef FEAT_VTP
2040 ? !(do_ME_bg = TRUE) : (do_ME_bg = FALSE)
2041# endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01002042 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002043#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01002044 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 do_ME = TRUE;
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02002046#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
2047 if (use_vtp())
2048 {
2049 if (do_ME_fg && do_ME_bg)
2050 do_ME = TRUE;
2051
2052 // FG and BG cannot be separated in T_ME, which is not
2053 // efficient.
2054 if (!do_ME && do_ME_fg)
2055 out_str((char_u *)"\033|39m"); // restore FG
2056 if (!do_ME && do_ME_bg)
2057 out_str((char_u *)"\033|49m"); // restore BG
2058 }
2059 else
2060 {
2061 // Process FG and BG at once.
2062 if (!do_ME)
2063 do_ME = do_ME_fg | do_ME_bg;
2064 }
2065#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 }
2067 else
2068 {
2069 aep = syn_term_attr2entry(screen_attr);
2070 if (aep != NULL && aep->ae_u.term.stop != NULL)
2071 {
2072 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
2073 do_ME = TRUE;
2074 else
2075 out_str(aep->ae_u.term.stop);
2076 }
2077 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002078 if (aep == NULL) // did ":syntax clear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 screen_attr = 0;
2080 else
2081 screen_attr = aep->ae_attr;
2082 }
2083
2084 /*
2085 * Often all ending-codes are equal to T_ME. Avoid outputting the
2086 * same sequence several times.
2087 */
2088 if (screen_attr & HL_STANDOUT)
2089 {
2090 if (STRCMP(T_SE, T_ME) == 0)
2091 do_ME = TRUE;
2092 else
2093 out_str(T_SE);
2094 }
Bram Moolenaar84f54632022-06-29 18:39:11 +01002095 is_under = (screen_attr & (HL_UNDERCURL
2096 | HL_UNDERDOUBLE | HL_UNDERDOTTED | HL_UNDERDASHED));
2097 if (is_under && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01002098 {
2099 if (STRCMP(T_UCE, T_ME) == 0)
2100 do_ME = TRUE;
2101 else
2102 out_str(T_UCE);
2103 }
Bram Moolenaar84f54632022-06-29 18:39:11 +01002104 if ((screen_attr & HL_UNDERLINE) || (is_under && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 {
2106 if (STRCMP(T_UE, T_ME) == 0)
2107 do_ME = TRUE;
2108 else
2109 out_str(T_UE);
2110 }
2111 if (screen_attr & HL_ITALIC)
2112 {
2113 if (STRCMP(T_CZR, T_ME) == 0)
2114 do_ME = TRUE;
2115 else
2116 out_str(T_CZR);
2117 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02002118 if (screen_attr & HL_STRIKETHROUGH)
2119 {
2120 if (STRCMP(T_STE, T_ME) == 0)
2121 do_ME = TRUE;
2122 else
2123 out_str(T_STE);
2124 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
2126 out_str(T_ME);
2127
Bram Moolenaar61be73b2016-04-29 22:59:22 +02002128#ifdef FEAT_TERMGUICOLORS
2129 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002131 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002132 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002133 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002134 term_bg_rgb_color(cterm_normal_bg_gui_color);
Bram Moolenaare023e882020-05-31 16:42:30 +02002135 if (cterm_normal_ul_gui_color != INVALCOLOR)
2136 term_ul_rgb_color(cterm_normal_ul_gui_color);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002137 }
2138 else
2139#endif
2140 {
2141 if (t_colors > 1)
2142 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002143 // set Normal cterm colors
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002144 if (cterm_normal_fg_color != 0)
2145 term_fg_color(cterm_normal_fg_color - 1);
2146 if (cterm_normal_bg_color != 0)
2147 term_bg_color(cterm_normal_bg_color - 1);
Bram Moolenaare023e882020-05-31 16:42:30 +02002148 if (cterm_normal_ul_color != 0)
2149 term_ul_color(cterm_normal_ul_color - 1);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002150 if (cterm_normal_fg_bold)
2151 out_str(T_MD);
2152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 }
2154 }
2155 }
2156 screen_attr = 0;
2157}
2158
2159/*
2160 * Reset the colors for a cterm. Used when leaving Vim.
2161 * The machine specific code may override this again.
2162 */
2163 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002164reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002166 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002168 // set Normal cterm colors
Bram Moolenaar61be73b2016-04-29 22:59:22 +02002169#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002170 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
2171 || cterm_normal_bg_gui_color != INVALCOLOR)
2172 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002173#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002175#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 {
2177 out_str(T_OP);
2178 screen_attr = -1;
2179 }
2180 if (cterm_normal_fg_bold)
2181 {
2182 out_str(T_ME);
2183 screen_attr = -1;
2184 }
2185 }
2186}
2187
2188/*
2189 * Put character ScreenLines["off"] on the screen at position "row" and "col",
2190 * using the attributes from ScreenAttrs["off"].
2191 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002192 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002193screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194{
2195 int attr;
2196
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002197 // Check for illegal values, just in case (could happen just after
2198 // resizing).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 if (row >= screen_Rows || col >= screen_Columns)
2200 return;
2201
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002202 // Outputting a character in the last cell on the screen may scroll the
2203 // screen up. Only do it when the "xn" termcap property is set, otherwise
2204 // mark the character invalid (update it when scrolled up).
Bram Moolenaar494838a2015-02-10 19:20:37 +01002205 if (*T_XN == NUL
2206 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207#ifdef FEAT_RIGHTLEFT
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002208 // account for first command-line character in rightleft mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 && !cmdmsg_rl
2210#endif
2211 )
2212 {
2213 ScreenAttrs[off] = (sattr_T)-1;
Bram Moolenaarb9081882022-07-09 04:56:24 +01002214 ScreenCols[off] = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 return;
2216 }
2217
2218 /*
2219 * Stop highlighting first, so it's easier to move the cursor.
2220 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 if (screen_char_attr != 0)
2222 attr = screen_char_attr;
2223 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 attr = ScreenAttrs[off];
2225 if (screen_attr != attr)
2226 screen_stop_highlight();
2227
2228 windgoto(row, col);
2229
2230 if (screen_attr != attr)
2231 screen_start_highlight(attr);
2232
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 if (enc_utf8 && ScreenLinesUC[off] != 0)
2234 {
2235 char_u buf[MB_MAXBYTES + 1];
2236
Bram Moolenaarcb070082016-04-02 22:14:51 +02002237 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002238 {
2239 if (*p_ambw == 'd'
Bram Moolenaara12a1612019-01-24 16:39:02 +01002240#ifdef FEAT_GUI
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002241 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01002242#endif
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002243 )
2244 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002245 // Clear the two screen cells. If the character is actually
2246 // single width it won't change the second cell.
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002247 out_str((char_u *)" ");
2248 term_windgoto(row, col);
2249 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002250 // not sure where the cursor is after drawing the ambiguous width
2251 // character
Bram Moolenaarcb070082016-04-02 22:14:51 +02002252 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002253 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02002254 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002256
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002257 // Convert the UTF-8 character to bytes and write it.
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002258 buf[utfc_char2bytes(off, buf)] = NUL;
2259 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 }
2261 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 out_char(ScreenLines[off]);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002265 // double-byte character in single-width cell
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2267 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 }
2269
2270 screen_cur_col++;
2271}
2272
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273/*
2274 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
2275 * on the screen at position 'row' and 'col'.
2276 * The attributes of the first byte is used for all. This is required to
2277 * output the two bytes of a double-byte character with nothing in between.
2278 */
2279 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002280screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002282 // Check for illegal values (could be wrong when screen was resized).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
2284 return;
2285
dundargocc57b5bc2022-11-02 13:30:51 +00002286 // Outputting the last character on the screen may scroll the screen up.
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002287 // Don't to it! Mark the character invalid (update it when scrolled up)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
2289 {
2290 ScreenAttrs[off] = (sattr_T)-1;
Bram Moolenaarb9081882022-07-09 04:56:24 +01002291 ScreenCols[off] = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 return;
2293 }
2294
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002295 // Output the first byte normally (positions the cursor), then write the
2296 // second byte directly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 screen_char(off, row, col);
2298 out_char(ScreenLines[off + 1]);
2299 ++screen_cur_col;
2300}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302/*
2303 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
2304 * This uses the contents of ScreenLines[] and doesn't change it.
2305 */
2306 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002307screen_draw_rectangle(
2308 int row,
2309 int col,
2310 int height,
2311 int width,
2312 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313{
2314 int r, c;
2315 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00002316 int max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002318 // Can't use ScreenLines unless initialized
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002319 if (ScreenLines == NULL)
2320 return;
2321
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 if (invert)
2323 screen_char_attr = HL_INVERSE;
2324 for (r = row; r < row + height; ++r)
2325 {
2326 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00002327 max_off = off + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 for (c = col; c < col + width; ++c)
2329 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00002330 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 {
Bram Moolenaarff85d4a2022-10-02 15:21:04 +01002332 if (!skip_for_popup(r, c))
2333 screen_char_2(off + c, r, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 ++c;
2335 }
2336 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 {
Bram Moolenaarff85d4a2022-10-02 15:21:04 +01002338 if (!skip_for_popup(r, c))
2339 screen_char(off + c, r, c);
Bram Moolenaar367329b2007-08-30 11:53:22 +00002340 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 ++c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 }
2343 }
2344 }
2345 screen_char_attr = 0;
2346}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348/*
2349 * Redraw the characters for a vertically split window.
2350 */
2351 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002352redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353{
2354 int col;
2355 int width;
2356
2357# ifdef FEAT_CLIPBOARD
2358 clip_may_clear_selection(row, end - 1);
2359# endif
2360
2361 if (wp == NULL)
2362 {
2363 col = 0;
2364 width = Columns;
2365 }
2366 else
2367 {
2368 col = wp->w_wincol;
2369 width = wp->w_width;
2370 }
2371 screen_draw_rectangle(row, col, end - row, width, FALSE);
2372}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002374 void
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002375space_to_screenline(int off, int attr)
2376{
2377 ScreenLines[off] = ' ';
2378 ScreenAttrs[off] = attr;
Bram Moolenaarb9081882022-07-09 04:56:24 +01002379 ScreenCols[off] = -1;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002380 if (enc_utf8)
2381 ScreenLinesUC[off] = 0;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002382}
2383
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384/*
Bram Moolenaarcee9c842022-04-09 12:40:13 +01002385 * Fill the screen from "start_row" to "end_row" (exclusive), from "start_col"
2386 * to "end_col" (exclusive) with character "c1" in first column followed by
2387 * "c2" in the other columns. Use attributes "attr".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 */
2389 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002390screen_fill(
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002391 int start_row,
2392 int end_row,
2393 int start_col,
2394 int end_col,
2395 int c1,
2396 int c2,
2397 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002399 int row;
2400 int col;
2401 int off;
2402 int end_off;
2403 int did_delete;
2404 int c;
2405 int norm_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002407 int force_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408#endif
2409
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002410 if (end_row > screen_Rows) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 end_row = screen_Rows;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002412 if (end_col > screen_Columns) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 end_col = screen_Columns;
2414 if (ScreenLines == NULL
2415 || start_row >= end_row
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002416 || start_col >= end_col) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 return;
2418
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002419 // it's a "normal" terminal when not in a GUI or cterm
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 norm_term = (
2421#ifdef FEAT_GUI
2422 !gui.in_use &&
2423#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002424 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 for (row = start_row; row < end_row; ++row)
2426 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00002427 if (has_mbyte
Bram Moolenaara12a1612019-01-24 16:39:02 +01002428#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00002429 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01002430#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00002431 )
2432 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00002433 // When drawing over the right half of a double-wide char clear
2434 // out the left half. When drawing over the left half of a
2435 // double wide-char clear out the right half. Only needed in a
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002436 // terminal.
Bram Moolenaar7693ec62008-07-24 18:29:37 +00002437 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00002438 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00002439 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00002440 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00002441 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 /*
2443 * Try to use delete-line termcap code, when no attributes or in a
2444 * "normal" terminal, where a bold/italic space is just a
2445 * space.
2446 */
2447 did_delete = FALSE;
2448 if (c2 == ' '
2449 && end_col == Columns
2450 && can_clear(T_CE)
2451 && (attr == 0
2452 || (norm_term
2453 && attr <= HL_ALL
2454 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
2455 {
2456 /*
2457 * check if we really need to clear something
2458 */
2459 col = start_col;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002460 if (c1 != ' ') // don't clear first char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 ++col;
2462
2463 off = LineOffset[row] + col;
2464 end_off = LineOffset[row] + end_col;
2465
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002466 // skip blanks (used often, keep it fast!)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 if (enc_utf8)
2468 while (off < end_off && ScreenLines[off] == ' '
2469 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
2470 ++off;
2471 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 while (off < end_off && ScreenLines[off] == ' '
2473 && ScreenAttrs[off] == 0)
2474 ++off;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002475 if (off < end_off) // something to be cleared
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 {
2477 col = off - LineOffset[row];
2478 screen_stop_highlight();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002479 term_windgoto(row, col);// clear rest of this screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 out_str(T_CE);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002481 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 col = end_col - col;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002483 while (col--) // clear chars in ScreenLines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002485 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 ++off;
2487 }
2488 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002489 did_delete = TRUE; // the chars are cleared now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 }
2491
2492 off = LineOffset[row] + start_col;
2493 c = c1;
2494 for (col = start_col; col < end_col; ++col)
2495 {
Bram Moolenaar33796b32019-06-08 16:01:13 +02002496 if ((ScreenLines[off] != c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002497 || (enc_utf8 && (int)ScreenLinesUC[off]
2498 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 || ScreenAttrs[off] != attr
Bram Moolenaar838b7462022-09-26 15:19:56 +01002500 || must_redraw == UPD_CLEAR // screen clear pending
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501#if defined(FEAT_GUI) || defined(UNIX)
2502 || force_next
2503#endif
2504 )
Bram Moolenaar33796b32019-06-08 16:01:13 +02002505 // Skip if under a(nother) popup.
Bram Moolenaarff85d4a2022-10-02 15:21:04 +01002506 && !skip_for_popup(row, col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 {
2508#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002509 // The bold trick may make a single row of pixels appear in
2510 // the next character. When a bold character is removed, the
2511 // next character should be redrawn too. This happens for our
2512 // own GUI and for some xterms.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 if (
2514# ifdef FEAT_GUI
2515 gui.in_use
2516# endif
2517# if defined(FEAT_GUI) && defined(UNIX)
2518 ||
2519# endif
2520# ifdef UNIX
2521 term_is_xterm
2522# endif
2523 )
2524 {
2525 if (ScreenLines[off] != ' '
2526 && (ScreenAttrs[off] > HL_ALL
2527 || ScreenAttrs[off] & HL_BOLD))
2528 force_next = TRUE;
2529 else
2530 force_next = FALSE;
2531 }
Bram Moolenaar4ba5f1d2022-10-04 14:36:29 +01002532#endif // FEAT_GUI || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 if (enc_utf8)
2535 {
2536 if (c >= 0x80)
2537 {
2538 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002539 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 }
2541 else
2542 ScreenLinesUC[off] = 0;
2543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 ScreenAttrs[off] = attr;
2545 if (!did_delete || c != ' ')
2546 screen_char(off, row, col);
2547 }
Bram Moolenaarb9081882022-07-09 04:56:24 +01002548 ScreenCols[off] = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 ++off;
2550 if (col == start_col)
2551 {
2552 if (did_delete)
2553 break;
2554 c = c2;
2555 }
2556 }
2557 if (end_col == Columns)
2558 LineWraps[row] = FALSE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002559 if (row == Rows - 1) // overwritten the command line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 {
2561 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02002562 if (start_col == 0 && end_col == Columns
2563 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002564 clear_cmdline = FALSE; // command line has been cleared
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002565 if (start_col == 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002566 mode_displayed = FALSE; // mode cleared or overwritten
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 }
2568 }
2569}
2570
2571/*
2572 * Check if there should be a delay. Used before clearing or redrawing the
2573 * screen or the command line.
2574 */
2575 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002576check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577{
2578 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
2579 && !did_wait_return
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002580 && emsg_silent == 0
2581 && !in_assert_fails)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 {
2583 out_flush();
Bram Moolenaareda1da02019-11-17 17:06:33 +01002584 ui_delay(1006L, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 emsg_on_display = FALSE;
2586 if (check_msg_scroll)
2587 msg_scroll = FALSE;
2588 }
2589}
2590
2591/*
Bram Moolenaarca57ab52019-04-13 14:53:16 +02002592 * Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect.
2593 */
2594 static void
2595clear_TabPageIdxs(void)
2596{
2597 int scol;
2598
2599 for (scol = 0; scol < Columns; ++scol)
2600 TabPageIdxs[scol] = 0;
2601}
2602
2603/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002605 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 * Returns TRUE if there is a valid screen to write to.
2607 * Returns FALSE when starting up and screen not initialized yet.
2608 */
2609 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002610screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002612 screenalloc(doclear); // allocate screen buffers if size changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 return (ScreenLines != NULL);
2614}
2615
2616/*
2617 * Resize the shell to Rows and Columns.
2618 * Allocate ScreenLines[] and associated items.
2619 *
2620 * There may be some time between setting Rows and Columns and (re)allocating
2621 * ScreenLines[]. This happens when starting up and when (manually) changing
2622 * the shell size. Always use screen_Rows and screen_Columns to access items
2623 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
2624 * final size of the shell is needed.
2625 */
2626 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002627screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628{
2629 int new_row, old_row;
2630#ifdef FEAT_GUI
2631 int old_Rows;
2632#endif
2633 win_T *wp;
2634 int outofmem = FALSE;
2635 int len;
2636 schar_T *new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002638 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002640 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 sattr_T *new_ScreenAttrs;
Bram Moolenaarb9081882022-07-09 04:56:24 +01002642 colnr_T *new_ScreenCols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 unsigned *new_LineOffset;
2644 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002645 short *new_TabPageIdxs;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002646#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002647 short *new_popup_mask;
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002648 short *new_popup_mask_next;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002649 char *new_popup_transparent;
Bram Moolenaar33796b32019-06-08 16:01:13 +02002650#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00002651 tabpage_T *tp;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002652 static int entered = FALSE; // avoid recursiveness
2653 static int done_outofmem_msg = FALSE; // did outofmem message
Bram Moolenaar87e817c2009-02-22 20:13:39 +00002654 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655
Bram Moolenaar87e817c2009-02-22 20:13:39 +00002656retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 /*
2658 * Allocation of the screen buffers is done only when the size changes and
2659 * when Rows and Columns have been set and we have started doing full
2660 * screen stuff.
2661 */
2662 if ((ScreenLines != NULL
2663 && Rows == screen_Rows
2664 && Columns == screen_Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 && enc_utf8 == (ScreenLinesUC != NULL)
2666 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaara12a1612019-01-24 16:39:02 +01002667 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 || Rows == 0
2669 || Columns == 0
2670 || (!full_screen && ScreenLines == NULL))
2671 return;
2672
2673 /*
2674 * It's possible that we produce an out-of-memory message below, which
2675 * will cause this function to be called again. To break the loop, just
2676 * return here.
2677 */
2678 if (entered)
2679 return;
2680 entered = TRUE;
2681
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00002682 /*
2683 * Note that the window sizes are updated before reallocating the arrays,
2684 * thus we must not redraw here!
2685 */
2686 ++RedrawingDisabled;
2687
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002688 win_new_shellsize(); // fit the windows in the new sized shell
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002690#ifdef FEAT_GUI_HAIKU
2691 vim_lock_screen(); // be safe, put it here
2692#endif
2693
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002694 comp_col(); // recompute columns for shown command and ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695
2696 /*
2697 * We're changing the size of the screen.
2698 * - Allocate new arrays for ScreenLines and ScreenAttrs.
2699 * - Move lines from the old arrays into the new arrays, clear extra
2700 * lines (unless the screen is going to be cleared).
2701 * - Free the old arrays.
2702 *
2703 * If anything fails, make ScreenLines NULL, so we don't do anything!
2704 * Continuing with the old ScreenLines may result in a crash, because the
2705 * size is wrong.
2706 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002707 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00002709 if (aucmd_win != NULL)
2710 win_free_lsize(aucmd_win);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002711#ifdef FEAT_PROP_POPUP
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002712 // global popup windows
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002713 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002714 win_free_lsize(wp);
2715 // tab-local popup windows
2716 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002717 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002718 win_free_lsize(wp);
2719#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002721 new_ScreenLines = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
Bram Moolenaar216b7102010-03-23 13:56:59 +01002722 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 if (enc_utf8)
2724 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002725 new_ScreenLinesUC = LALLOC_MULT(u8char_T, (Rows + 1) * Columns);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002726 for (i = 0; i < p_mco; ++i)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002727 new_ScreenLinesC[i] = LALLOC_CLEAR_MULT(u8char_T,
2728 (Rows + 1) * Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 }
2730 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002731 new_ScreenLines2 = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
2732 new_ScreenAttrs = LALLOC_MULT(sattr_T, (Rows + 1) * Columns);
dundargocc57b5bc2022-11-02 13:30:51 +00002733 // Clear ScreenCols to avoid a warning for uninitialized memory in
Bram Moolenaar18ee0fe2022-09-19 11:44:11 +01002734 // jump_to_mouse().
2735 new_ScreenCols = LALLOC_CLEAR_MULT(colnr_T, (Rows + 1) * Columns);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002736 new_LineOffset = LALLOC_MULT(unsigned, Rows);
2737 new_LineWraps = LALLOC_MULT(char_u, Rows);
2738 new_TabPageIdxs = LALLOC_MULT(short, Columns);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002739#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002740 new_popup_mask = LALLOC_MULT(short, Rows * Columns);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002741 new_popup_mask_next = LALLOC_MULT(short, Rows * Columns);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002742 new_popup_transparent = LALLOC_MULT(char, Rows * Columns);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002743#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00002745 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 {
2747 if (win_alloc_lines(wp) == FAIL)
2748 {
2749 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00002750 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 }
2752 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00002753 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
2754 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002755 outofmem = TRUE;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002756#ifdef FEAT_PROP_POPUP
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002757 // global popup windows
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002758 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002759 if (win_alloc_lines(wp) == FAIL)
2760 {
2761 outofmem = TRUE;
2762 goto give_up;
2763 }
2764 // tab-local popup windows
2765 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002766 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002767 if (win_alloc_lines(wp) == FAIL)
2768 {
2769 outofmem = TRUE;
2770 goto give_up;
2771 }
2772#endif
2773
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00002774give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002776 for (i = 0; i < p_mco; ++i)
2777 if (new_ScreenLinesC[i] == NULL)
2778 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 if (new_ScreenLines == NULL
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002780 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 || new_ScreenAttrs == NULL
Bram Moolenaarb9081882022-07-09 04:56:24 +01002783 || new_ScreenCols == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 || new_LineOffset == NULL
2785 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00002786 || new_TabPageIdxs == NULL
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002787#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002788 || new_popup_mask == NULL
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002789 || new_popup_mask_next == NULL
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002790 || new_popup_transparent == NULL
Bram Moolenaar33796b32019-06-08 16:01:13 +02002791#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 || outofmem)
2793 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002794 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002795 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002796 // guess the size
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002797 do_outofmem_msg((long_u)((Rows + 1) * Columns));
2798
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002799 // Remember we did this to avoid getting outofmem messages over
2800 // and over again.
Bram Moolenaar89d40322006-08-29 15:30:07 +00002801 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002802 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01002803 VIM_CLEAR(new_ScreenLines);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002804 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002805 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01002806 VIM_CLEAR(new_ScreenLinesC[i]);
2807 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002808 VIM_CLEAR(new_ScreenAttrs);
Bram Moolenaarb9081882022-07-09 04:56:24 +01002809 VIM_CLEAR(new_ScreenCols);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002810 VIM_CLEAR(new_LineOffset);
2811 VIM_CLEAR(new_LineWraps);
2812 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002813#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002814 VIM_CLEAR(new_popup_mask);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002815 VIM_CLEAR(new_popup_mask_next);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002816 VIM_CLEAR(new_popup_transparent);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002817#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 }
2819 else
2820 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002821 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002822
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 for (new_row = 0; new_row < Rows; ++new_row)
2824 {
2825 new_LineOffset[new_row] = new_row * Columns;
2826 new_LineWraps[new_row] = FALSE;
2827
2828 /*
2829 * If the screen is not going to be cleared, copy as much as
2830 * possible from the old screen to the new one and clear the rest
2831 * (used when resizing the window at the "--more--" prompt or when
2832 * executing an external command, for the GUI).
2833 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002834 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 {
2836 (void)vim_memset(new_ScreenLines + new_row * Columns,
2837 ' ', (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 if (enc_utf8)
2839 {
2840 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
2841 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002842 for (i = 0; i < p_mco; ++i)
2843 (void)vim_memset(new_ScreenLinesC[i]
2844 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 0, (size_t)Columns * sizeof(u8char_T));
2846 }
2847 if (enc_dbcs == DBCS_JPNU)
2848 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
2849 0, (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
2851 0, (size_t)Columns * sizeof(sattr_T));
Bram Moolenaarb9081882022-07-09 04:56:24 +01002852 (void)vim_memset(new_ScreenCols + new_row * Columns,
2853 0, (size_t)Columns * sizeof(colnr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002855 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 {
2857 if (screen_Columns < Columns)
2858 len = screen_Columns;
2859 else
2860 len = Columns;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002861 // When switching to utf-8 don't copy characters, they
2862 // may be invalid now. Also when p_mco changes.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002863 if (!(enc_utf8 && ScreenLinesUC == NULL)
2864 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002865 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
2866 ScreenLines + LineOffset[old_row],
2867 (size_t)len * sizeof(schar_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002868 if (enc_utf8 && ScreenLinesUC != NULL
2869 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 {
2871 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
2872 ScreenLinesUC + LineOffset[old_row],
2873 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002874 for (i = 0; i < p_mco; ++i)
2875 mch_memmove(new_ScreenLinesC[i]
2876 + new_LineOffset[new_row],
2877 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 (size_t)len * sizeof(u8char_T));
2879 }
2880 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
2881 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
2882 ScreenLines2 + LineOffset[old_row],
2883 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
2885 ScreenAttrs + LineOffset[old_row],
2886 (size_t)len * sizeof(sattr_T));
Bram Moolenaarb9081882022-07-09 04:56:24 +01002887 mch_memmove(new_ScreenCols + new_LineOffset[new_row],
2888 ScreenAttrs + LineOffset[old_row],
2889 (size_t)len * sizeof(colnr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 }
2891 }
2892 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002893 // Use the last line of the screen for the current line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 current_ScreenLine = new_ScreenLines + Rows * Columns;
Bram Moolenaar6ace95e2019-08-13 23:09:49 +02002895
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002896#ifdef FEAT_PROP_POPUP
Bram Moolenaar6ace95e2019-08-13 23:09:49 +02002897 vim_memset(new_popup_mask, 0, Rows * Columns * sizeof(short));
2898 vim_memset(new_popup_transparent, 0, Rows * Columns * sizeof(char));
2899#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 }
2901
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002902 free_screenlines();
2903
Bram Moolenaar6ace95e2019-08-13 23:09:49 +02002904 // NOTE: this may result in all pointers to become NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 ScreenLines = new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002907 for (i = 0; i < p_mco; ++i)
2908 ScreenLinesC[i] = new_ScreenLinesC[i];
2909 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 ScreenLines2 = new_ScreenLines2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 ScreenAttrs = new_ScreenAttrs;
Bram Moolenaarb9081882022-07-09 04:56:24 +01002912 ScreenCols = new_ScreenCols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 LineOffset = new_LineOffset;
2914 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002915 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002916#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002917 popup_mask = new_popup_mask;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002918 popup_mask_next = new_popup_mask_next;
2919 popup_transparent = new_popup_transparent;
Bram Moolenaar33796b32019-06-08 16:01:13 +02002920 popup_mask_refresh = TRUE;
2921#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002923 // It's important that screen_Rows and screen_Columns reflect the actual
2924 // size of ScreenLines[]. Set them before calling anything.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925#ifdef FEAT_GUI
2926 old_Rows = screen_Rows;
2927#endif
2928 screen_Rows = Rows;
2929 screen_Columns = Columns;
2930
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01002931 set_must_redraw(UPD_CLEAR); // need to clear the screen later
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002932 if (doclear)
Bram Moolenaarb13d3402022-08-29 13:44:28 +01002933 screenclear2(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934#ifdef FEAT_GUI
2935 else if (gui.in_use
2936 && !gui.starting
2937 && ScreenLines != NULL
2938 && old_Rows != Rows)
2939 {
Bram Moolenaar7c003aa2020-03-28 20:44:41 +01002940 gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
2941
2942 // Adjust the position of the cursor, for when executing an external
2943 // command.
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002944 if (msg_row >= Rows) // Rows got smaller
2945 msg_row = Rows - 1; // put cursor at last row
2946 else if (Rows > old_Rows) // Rows got bigger
2947 msg_row += Rows - old_Rows; // put cursor in same place
2948 if (msg_col >= Columns) // Columns got smaller
2949 msg_col = Columns - 1; // put cursor at last column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 }
2951#endif
Bram Moolenaarca57ab52019-04-13 14:53:16 +02002952 clear_TabPageIdxs();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002954#ifdef FEAT_GUI_HAIKU
2955 vim_unlock_screen();
2956#endif
2957
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00002959 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00002960
Bram Moolenaar87e817c2009-02-22 20:13:39 +00002961 /*
2962 * Do not apply autocommands more than 3 times to avoid an endless loop
2963 * in case applying autocommands always changes Rows or Columns.
2964 */
2965 if (starting == 0 && ++retry_count <= 3)
2966 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00002967 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002968 // In rare cases, autocommands may have altered Rows or Columns,
2969 // jump back to check if we need to allocate the screen again.
Bram Moolenaar87e817c2009-02-22 20:13:39 +00002970 goto retry;
2971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972}
2973
2974 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002975free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002976{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002977 int i;
2978
Bram Moolenaar33796b32019-06-08 16:01:13 +02002979 VIM_CLEAR(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002980 for (i = 0; i < Screen_mco; ++i)
Bram Moolenaar33796b32019-06-08 16:01:13 +02002981 VIM_CLEAR(ScreenLinesC[i]);
2982 VIM_CLEAR(ScreenLines2);
2983 VIM_CLEAR(ScreenLines);
2984 VIM_CLEAR(ScreenAttrs);
Bram Moolenaarb9081882022-07-09 04:56:24 +01002985 VIM_CLEAR(ScreenCols);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002986 VIM_CLEAR(LineOffset);
2987 VIM_CLEAR(LineWraps);
2988 VIM_CLEAR(TabPageIdxs);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002989#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002990 VIM_CLEAR(popup_mask);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002991 VIM_CLEAR(popup_mask_next);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002992 VIM_CLEAR(popup_transparent);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002993#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002994}
2995
Bram Moolenaarb13d3402022-08-29 13:44:28 +01002996/*
2997 * Clear the screen.
2998 * May delay if there is something the user should read.
2999 * Allocated the screen for resizing if needed.
Bram Moolenaara4e0b972022-10-01 19:43:52 +01003000 * Returns TRUE when the screen was actually cleared, FALSE if all display
Bram Moolenaar838b7462022-09-26 15:19:56 +01003001 * cells were marked for updating.
Bram Moolenaarb13d3402022-08-29 13:44:28 +01003002 */
Bram Moolenaar838b7462022-09-26 15:19:56 +01003003 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003004screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005{
3006 check_for_delay(FALSE);
Bram Moolenaar838b7462022-09-26 15:19:56 +01003007 screenalloc(FALSE); // allocate screen buffers if size changed
3008 return screenclear2(TRUE); // clear the screen
Bram Moolenaarb13d3402022-08-29 13:44:28 +01003009}
3010
3011/*
3012 * Do not clear the screen but mark everything for redraw.
3013 */
3014 void
3015redraw_as_cleared(void)
3016{
3017 screenclear2(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018}
3019
Bram Moolenaar838b7462022-09-26 15:19:56 +01003020 static int
Bram Moolenaarb13d3402022-08-29 13:44:28 +01003021screenclear2(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022{
3023 int i;
Bram Moolenaar838b7462022-09-26 15:19:56 +01003024 int did_clear = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025
3026 if (starting == NO_SCREEN || ScreenLines == NULL
3027#ifdef FEAT_GUI
3028 || (gui.in_use && gui.starting)
3029#endif
3030 )
Bram Moolenaar838b7462022-09-26 15:19:56 +01003031 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032
3033#ifdef FEAT_GUI
3034 if (!gui.in_use)
3035#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003036 screen_attr = -1; // force setting the Normal colors
3037 screen_stop_highlight(); // don't want highlighting here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038
3039#ifdef FEAT_CLIPBOARD
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003040 // disable selection without redrawing it
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 clip_scroll_selection(9999);
3042#endif
3043
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003044 // blank out ScreenLines
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 for (i = 0; i < Rows; ++i)
3046 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003047 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 LineWraps[i] = FALSE;
3049 }
3050
Bram Moolenaarb13d3402022-08-29 13:44:28 +01003051 if (doclear && can_clear(T_CL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003053 out_str(T_CL); // clear the display
Bram Moolenaar838b7462022-09-26 15:19:56 +01003054 did_clear = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003056 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 }
3058 else
3059 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003060 // can't clear the screen, mark all chars with invalid attributes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 for (i = 0; i < Rows; ++i)
3062 lineinvalid(LineOffset[i], (int)Columns);
3063 clear_cmdline = TRUE;
3064 }
3065
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003066 screen_cleared = TRUE; // can use contents of ScreenLines now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067
Bram Moolenaarb13d3402022-08-29 13:44:28 +01003068 win_rest_invalid(firstwin); // redraw all regular windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003070 redraw_tabline = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003071 if (must_redraw == UPD_CLEAR) // no need to clear again
3072 must_redraw = UPD_NOT_VALID;
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01003073 msg_scrolled = 0; // compute_cmdrow() uses this
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 compute_cmdrow();
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01003075#ifdef FEAT_PROP_POPUP
3076 popup_redraw_all(); // redraw all popup windows
3077#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003078 msg_row = cmdline_row; // put cursor on last line for messages
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 msg_col = 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003080 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 msg_didany = FALSE;
3082 msg_didout = FALSE;
Bram Moolenaar838b7462022-09-26 15:19:56 +01003083
3084 return did_clear;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085}
3086
3087/*
3088 * Clear one line in ScreenLines.
3089 */
3090 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003091lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092{
3093 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 if (enc_utf8)
3095 (void)vim_memset(ScreenLinesUC + off, 0,
3096 (size_t)width * sizeof(u8char_T));
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003097 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaarb9081882022-07-09 04:56:24 +01003098 (void)vim_memset(ScreenCols + off, -1, (size_t)width * sizeof(colnr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099}
3100
3101/*
3102 * Mark one line in ScreenLines invalid by setting the attributes to an
3103 * invalid value.
3104 */
3105 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003106lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107{
3108 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
Bram Moolenaarb9081882022-07-09 04:56:24 +01003109 (void)vim_memset(ScreenCols + off, -1, (size_t)width * sizeof(colnr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110}
3111
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112/*
Bram Moolenaar96916ac2020-07-08 23:09:28 +02003113 * To be called when characters were sent to the terminal directly, outputting
3114 * test on "screen_lnum".
3115 */
3116 void
3117line_was_clobbered(int screen_lnum)
3118{
3119 lineinvalid(LineOffset[screen_lnum], (int)Columns);
3120}
3121
3122/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 * Copy part of a Screenline for vertically split window "wp".
3124 */
3125 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003126linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127{
3128 unsigned off_to = LineOffset[to] + wp->w_wincol;
3129 unsigned off_from = LineOffset[from] + wp->w_wincol;
3130
3131 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
3132 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 if (enc_utf8)
3134 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003135 int i;
3136
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
3138 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003139 for (i = 0; i < p_mco; ++i)
3140 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
3141 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 }
3143 if (enc_dbcs == DBCS_JPNU)
3144 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
3145 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
3147 wp->w_width * sizeof(sattr_T));
Bram Moolenaarb9081882022-07-09 04:56:24 +01003148 mch_memmove(ScreenCols + off_to, ScreenCols + off_from,
3149 wp->w_width * sizeof(colnr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151
3152/*
3153 * Return TRUE if clearing with term string "p" would work.
3154 * It can't work when the string is empty or it won't set the right background.
Bram Moolenaar33796b32019-06-08 16:01:13 +02003155 * Don't clear to end-of-line when there are popups, it may cause flicker.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 */
3157 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003158can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159{
3160 return (*p != NUL && (t_colors <= 1
3161#ifdef FEAT_GUI
3162 || gui.in_use
3163#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003164#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003165 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02003166 || (!p_tgc && cterm_normal_bg_color == 0)
3167#else
3168 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003169#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02003170 || *T_UT != NUL)
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003171#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02003172 && !(p == T_CE && popup_visible)
3173#endif
3174 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175}
3176
3177/*
3178 * Reset cursor position. Use whenever cursor was moved because of outputting
3179 * something directly to the screen (shell commands) or a terminal control
3180 * code.
3181 */
3182 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003183screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184{
3185 screen_cur_row = screen_cur_col = 9999;
3186}
3187
3188/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 * Move the cursor to position "row","col" in the screen.
3190 * This tries to find the most efficient way to move, minimizing the number of
3191 * characters sent to the terminal.
3192 */
3193 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003194windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003196 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 int i;
3198 int plan;
3199 int cost;
3200 int wouldbe_col;
3201 int noinvcurs;
3202 char_u *bs;
3203 int goto_cost;
3204 int attr;
3205
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003206#define GOTO_COST 7 // assume a term_windgoto() takes about 7 chars
3207#define HIGHL_COST 5 // assume unhighlight takes 5 chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208
3209#define PLAN_LE 1
3210#define PLAN_CR 2
3211#define PLAN_NL 3
3212#define PLAN_WRITE 4
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003213 // Can't use ScreenLines unless initialized
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 if (ScreenLines == NULL)
3215 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 if (col != screen_cur_col || row != screen_cur_row)
3217 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003218 // Check for valid position.
3219 if (row < 0) // window without text lines?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 row = 0;
3221 if (row >= screen_Rows)
3222 row = screen_Rows - 1;
3223 if (col >= screen_Columns)
3224 col = screen_Columns - 1;
3225
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003226 // check if no cursor movement is allowed in highlight mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 if (screen_attr && *T_MS == NUL)
3228 noinvcurs = HIGHL_COST;
3229 else
3230 noinvcurs = 0;
3231 goto_cost = GOTO_COST + noinvcurs;
3232
3233 /*
3234 * Plan how to do the positioning:
3235 * 1. Use CR to move it to column 0, same row.
3236 * 2. Use T_LE to move it a few columns to the left.
3237 * 3. Use NL to move a few lines down, column 0.
3238 * 4. Move a few columns to the right with T_ND or by writing chars.
3239 *
3240 * Don't do this if the cursor went beyond the last column, the cursor
3241 * position is unknown then (some terminals wrap, some don't )
3242 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00003243 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 * characters to move the cursor to the right.
3245 */
3246 if (row >= screen_cur_row && screen_cur_col < Columns)
3247 {
3248 /*
3249 * If the cursor is in the same row, bigger col, we can use CR
3250 * or T_LE.
3251 */
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003252 bs = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 attr = screen_attr;
3254 if (row == screen_cur_row && col < screen_cur_col)
3255 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003256 // "le" is preferred over "bc", because "bc" is obsolete
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 if (*T_LE)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003258 bs = T_LE; // "cursor left"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003260 bs = T_BC; // "backspace character (old)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 if (*bs)
3262 cost = (screen_cur_col - col) * (int)STRLEN(bs);
3263 else
3264 cost = 999;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003265 if (col + 1 < cost) // using CR is less characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 {
3267 plan = PLAN_CR;
3268 wouldbe_col = 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003269 cost = 1; // CR is just one character
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 }
3271 else
3272 {
3273 plan = PLAN_LE;
3274 wouldbe_col = col;
3275 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003276 if (noinvcurs) // will stop highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 {
3278 cost += noinvcurs;
3279 attr = 0;
3280 }
3281 }
3282
3283 /*
3284 * If the cursor is above where we want to be, we can use CR LF.
3285 */
3286 else if (row > screen_cur_row)
3287 {
3288 plan = PLAN_NL;
3289 wouldbe_col = 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003290 cost = (row - screen_cur_row) * 2; // CR LF
3291 if (noinvcurs) // will stop highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 {
3293 cost += noinvcurs;
3294 attr = 0;
3295 }
3296 }
3297
3298 /*
3299 * If the cursor is in the same row, smaller col, just use write.
3300 */
3301 else
3302 {
3303 plan = PLAN_WRITE;
3304 wouldbe_col = screen_cur_col;
3305 cost = 0;
3306 }
3307
3308 /*
3309 * Check if any characters that need to be written have the
3310 * correct attributes. Also avoid UTF-8 characters.
3311 */
3312 i = col - wouldbe_col;
3313 if (i > 0)
3314 cost += i;
3315 if (cost < goto_cost && i > 0)
3316 {
3317 /*
3318 * Check if the attributes are correct without additionally
3319 * stopping highlighting.
3320 */
3321 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
3322 while (i && *p++ == attr)
3323 --i;
3324 if (i != 0)
3325 {
3326 /*
3327 * Try if it works when highlighting is stopped here.
3328 */
3329 if (*--p == 0)
3330 {
3331 cost += noinvcurs;
3332 while (i && *p++ == 0)
3333 --i;
3334 }
3335 if (i != 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003336 cost = 999; // different attributes, don't do it
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 if (enc_utf8)
3339 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003340 // Don't use an UTF-8 char for positioning, it's slow.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 for (i = wouldbe_col; i < col; ++i)
3342 if (ScreenLinesUC[LineOffset[row] + i] != 0)
3343 {
3344 cost = 999;
3345 break;
3346 }
3347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 }
3349
3350 /*
3351 * We can do it without term_windgoto()!
3352 */
3353 if (cost < goto_cost)
3354 {
3355 if (plan == PLAN_LE)
3356 {
3357 if (noinvcurs)
3358 screen_stop_highlight();
3359 while (screen_cur_col > col)
3360 {
3361 out_str(bs);
3362 --screen_cur_col;
3363 }
3364 }
3365 else if (plan == PLAN_CR)
3366 {
3367 if (noinvcurs)
3368 screen_stop_highlight();
3369 out_char('\r');
3370 screen_cur_col = 0;
3371 }
3372 else if (plan == PLAN_NL)
3373 {
3374 if (noinvcurs)
3375 screen_stop_highlight();
3376 while (screen_cur_row < row)
3377 {
3378 out_char('\n');
3379 ++screen_cur_row;
3380 }
3381 screen_cur_col = 0;
3382 }
3383
3384 i = col - screen_cur_col;
3385 if (i > 0)
3386 {
3387 /*
3388 * Use cursor-right if it's one character only. Avoids
3389 * removing a line of pixels from the last bold char, when
3390 * using the bold trick in the GUI.
3391 */
3392 if (T_ND[0] != NUL && T_ND[1] == NUL)
3393 {
3394 while (i-- > 0)
3395 out_char(*T_ND);
3396 }
3397 else
3398 {
3399 int off;
3400
3401 off = LineOffset[row] + screen_cur_col;
3402 while (i-- > 0)
3403 {
3404 if (ScreenAttrs[off] != screen_attr)
3405 screen_stop_highlight();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 if (enc_dbcs == DBCS_JPNU
3409 && ScreenLines[off] == 0x8e)
3410 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 ++off;
3412 }
3413 }
3414 }
3415 }
3416 }
3417 else
3418 cost = 999;
3419
3420 if (cost >= goto_cost)
3421 {
3422 if (noinvcurs)
3423 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02003424 if (row == screen_cur_row && (col > screen_cur_col)
3425 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 term_cursor_right(col - screen_cur_col);
3427 else
3428 term_windgoto(row, col);
3429 }
3430 screen_cur_row = row;
3431 screen_cur_col = col;
3432 }
3433}
3434
3435/*
3436 * Set cursor to its position in the current window.
3437 */
3438 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003439setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440{
Bram Moolenaar987723e2018-03-06 11:43:04 +01003441 setcursor_mayforce(FALSE);
3442}
3443
3444/*
3445 * Set cursor to its position in the current window.
3446 * When "force" is TRUE also when not redrawing.
3447 */
3448 void
3449setcursor_mayforce(int force)
3450{
3451 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 {
3453 validate_cursor();
3454 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02003455 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456#ifdef FEAT_RIGHTLEFT
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003457 // With 'rightleft' set and the cursor on a double-wide
3458 // character, position it on the leftmost column.
Bram Moolenaara12a1612019-01-24 16:39:02 +01003459 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol
3460 - ((has_mbyte
Bram Moolenaar561f9db2008-02-20 13:16:29 +00003461 && (*mb_ptr2cells)(ml_get_cursor()) == 2
Bram Moolenaara12a1612019-01-24 16:39:02 +01003462 && vim_isprintc(gchar_cursor())) ? 2 : 1)) :
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463#endif
3464 curwin->w_wcol));
3465 }
3466}
3467
3468
3469/*
Bram Moolenaar86033562017-07-12 20:24:41 +02003470 * Insert 'line_count' lines at 'row' in window 'wp'.
3471 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
3472 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 * scrolling.
3474 * Returns FAIL if the lines are not inserted, OK for success.
3475 */
3476 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003477win_ins_lines(
3478 win_T *wp,
3479 int row,
3480 int line_count,
3481 int invalid,
3482 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483{
3484 int did_delete;
3485 int nextrow;
3486 int lastrow;
3487 int retval;
3488
3489 if (invalid)
3490 wp->w_lines_valid = 0;
3491
Bram Moolenaarc856ceb2022-06-21 18:10:39 +01003492 // with only a few lines it's not worth the effort
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 if (wp->w_height < 5)
3494 return FAIL;
3495
Bram Moolenaarc856ceb2022-06-21 18:10:39 +01003496 // with the popup menu visible this might not work correctly
3497 if (pum_visible())
3498 return FAIL;
3499
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 if (line_count > wp->w_height - row)
3501 line_count = wp->w_height - row;
3502
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003503 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 if (retval != MAYBE)
3505 return retval;
3506
3507 /*
3508 * If there is a next window or a status line, we first try to delete the
3509 * lines at the bottom to avoid messing what is after the window.
Bram Moolenaarc363fe12019-08-04 18:13:46 +02003510 * If this fails and there are following windows, don't do anything to
3511 * avoid messing up those windows, better just redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 */
3513 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 if (wp->w_next != NULL || wp->w_status_height)
3515 {
3516 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003517 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 did_delete = TRUE;
3519 else if (wp->w_next)
3520 return FAIL;
3521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 /*
3523 * if no lines deleted, blank the lines that will end up below the window
3524 */
3525 if (!did_delete)
3526 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003529 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 lastrow = nextrow + line_count;
3531 if (lastrow > Rows)
3532 lastrow = Rows;
3533 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02003534 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 ' ', ' ', 0);
3536 }
3537
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003538 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 == FAIL)
3540 {
Bram Moolenaarc363fe12019-08-04 18:13:46 +02003541 // deletion will have messed up other windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 if (did_delete)
3543 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 win_rest_invalid(W_NEXT(wp));
3546 }
3547 return FAIL;
3548 }
3549
3550 return OK;
3551}
3552
3553/*
Bram Moolenaar86033562017-07-12 20:24:41 +02003554 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
3556 * If "mayclear" is TRUE the screen will be cleared if it is faster than
3557 * scrolling
3558 * Return OK for success, FAIL if the lines are not deleted.
3559 */
3560 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003561win_del_lines(
3562 win_T *wp,
3563 int row,
3564 int line_count,
3565 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003566 int mayclear,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003567 int clear_attr) // for clearing lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568{
3569 int retval;
3570
3571 if (invalid)
3572 wp->w_lines_valid = 0;
3573
3574 if (line_count > wp->w_height - row)
3575 line_count = wp->w_height - row;
3576
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003577 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 if (retval != MAYBE)
3579 return retval;
3580
3581 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003582 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 return FAIL;
3584
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 /*
3586 * If there are windows or status lines below, try to put them at the
3587 * correct place. If we can't do that, they have to be redrawn.
3588 */
3589 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
3590 {
3591 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003592 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 {
3594 wp->w_redr_status = TRUE;
3595 win_rest_invalid(wp->w_next);
3596 }
3597 }
3598 /*
3599 * If this is the last window and there is no status line, redraw the
3600 * command line later.
3601 */
3602 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 redraw_cmdline = TRUE;
3604 return OK;
3605}
3606
3607/*
3608 * Common code for win_ins_lines() and win_del_lines().
3609 * Returns OK or FAIL when the work has been done.
3610 * Returns MAYBE when not finished yet.
3611 */
3612 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003613win_do_lines(
3614 win_T *wp,
3615 int row,
3616 int line_count,
3617 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003618 int del,
3619 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620{
3621 int retval;
3622
3623 if (!redrawing() || line_count <= 0)
3624 return FAIL;
3625
Bram Moolenaar33796b32019-06-08 16:01:13 +02003626 // When inserting lines would result in loss of command output, just redraw
3627 // the lines.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003628 if (no_win_do_lines_ins && !del)
3629 return FAIL;
3630
Bram Moolenaar33796b32019-06-08 16:01:13 +02003631 // only a few lines left: redraw is faster
Bram Moolenaar4033c552017-09-16 20:54:51 +02003632 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003634 if (!no_win_do_lines_ins)
Bram Moolenaar33796b32019-06-08 16:01:13 +02003635 screenclear(); // will set wp->w_lines_valid to 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 return FAIL;
3637 }
3638
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003639#ifdef FEAT_PROP_POPUP
Bram Moolenaar4c063a02019-06-10 21:24:12 +02003640 // this doesn't work when there are popups visible
Bram Moolenaar33796b32019-06-08 16:01:13 +02003641 if (popup_visible)
3642 return FAIL;
3643#endif
3644
3645 // Delete all remaining lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 if (row + line_count >= wp->w_height)
3647 {
3648 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02003649 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 ' ', ' ', 0);
3651 return OK;
3652 }
3653
3654 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003655 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003657 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003659 if (!no_win_do_lines_ins)
3660 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661
3662 /*
3663 * If the terminal can set a scroll region, use that.
3664 * Always do this in a vertically split window. This will redraw from
3665 * ScreenLines[] when t_CV isn't defined. That's faster than using
3666 * win_line().
3667 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003668 * a character in the lower right corner of the scroll region may cause a
3669 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 */
Bram Moolenaar02631462017-09-22 15:20:32 +02003671 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 scroll_region_set(wp, row);
3675 if (del)
3676 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003677 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 else
3679 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003680 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 scroll_region_reset();
3683 return retval;
3684 }
3685
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003686 if (wp->w_next != NULL && p_tf) // don't delete/insert on fast terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688
3689 return MAYBE;
3690}
3691
3692/*
3693 * window 'wp' and everything after it is messed up, mark it for redraw
3694 */
3695 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003696win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003700 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 wp->w_redr_status = TRUE;
3702 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 }
3704 redraw_cmdline = TRUE;
3705}
3706
3707/*
3708 * The rest of the routines in this file perform screen manipulations. The
3709 * given operation is performed physically on the screen. The corresponding
3710 * change is also made to the internal screen image. In this way, the editor
3711 * anticipates the effect of editing changes on the appearance of the screen.
3712 * That way, when we call screenupdate a complete redraw isn't usually
3713 * necessary. Another advantage is that we can keep adding code to anticipate
3714 * screen changes, and in the meantime, everything still works.
3715 */
3716
3717/*
3718 * types for inserting or deleting lines
3719 */
3720#define USE_T_CAL 1
3721#define USE_T_CDL 2
3722#define USE_T_AL 3
3723#define USE_T_CE 4
3724#define USE_T_DL 5
3725#define USE_T_SR 6
3726#define USE_NL 7
3727#define USE_T_CD 8
3728#define USE_REDRAW 9
3729
3730/*
3731 * insert lines on the screen and update ScreenLines[]
Bram Moolenaar17fa2332022-04-01 19:44:47 +01003732 * "end" is the line after the scrolled part. Normally it is Rows.
3733 * When scrolling region used "off" is the offset from the top for the region.
3734 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 *
3736 * return FAIL for failure, OK for success.
3737 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003738 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003739screen_ins_lines(
3740 int off,
3741 int row,
3742 int line_count,
3743 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003744 int clear_attr,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003745 win_T *wp) // NULL or window to use width from
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746{
3747 int i;
3748 int j;
3749 unsigned temp;
3750 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003751 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 int type;
3753 int result_empty;
3754 int can_ce = can_clear(T_CE);
3755
3756 /*
3757 * FAIL if
3758 * - there is no valid screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 * - the line count is less than one
3760 * - the line count is more than 'ttyscroll'
Bram Moolenaar17fa2332022-04-01 19:44:47 +01003761 * - "end" is more than "Rows" (safety check, should not happen)
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003762 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar33796b32019-06-08 16:01:13 +02003763 * - there is a popup window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 */
Bram Moolenaar33796b32019-06-08 16:01:13 +02003765 if (!screen_valid(TRUE)
3766 || line_count <= 0 || line_count > p_ttyscroll
Bram Moolenaar17fa2332022-04-01 19:44:47 +01003767 || end > Rows
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003768#ifdef FEAT_CLIPBOARD
3769 || (clip_star.state != SELECT_CLEARED
3770 && redrawing_for_callback > 0)
3771#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003772#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02003773 || popup_visible
3774#endif
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003775 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 return FAIL;
3777
3778 /*
3779 * There are seven ways to insert lines:
3780 * 0. When in a vertically split window and t_CV isn't set, redraw the
3781 * characters from ScreenLines[].
3782 * 1. Use T_CD (clear to end of display) if it exists and the result of
3783 * the insert is just empty lines
3784 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
3785 * present or line_count > 1. It looks better if we do all the inserts
3786 * at once.
3787 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
3788 * insert is just empty lines and T_CE is not present or line_count >
3789 * 1.
3790 * 4. Use T_AL (insert line) if it exists.
3791 * 5. Use T_CE (erase line) if it exists and the result of the insert is
3792 * just empty lines.
3793 * 6. Use T_DL (delete line) if it exists and the result of the insert is
3794 * just empty lines.
3795 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
3796 * the 'da' flag is not set or we have clear line capability.
3797 * 8. redraw the characters from ScreenLines[].
3798 *
3799 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
3800 * the scrollbar for the window. It does have insert line, use that if it
3801 * exists.
3802 */
3803 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
Bram Moolenaar8ef69972022-04-03 13:23:22 +01003805 {
3806 // Avoid that lines are first cleared here and then redrawn, which
3807 // results in many characters updated twice. This happens with CTRL-F
3808 // in a vertically split window. With line-by-line scrolling
3809 // USE_REDRAW should be faster.
3810 if (line_count > 3)
3811 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 type = USE_REDRAW;
Bram Moolenaar8ef69972022-04-03 13:23:22 +01003813 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02003814 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 type = USE_T_CD;
3816 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
3817 type = USE_T_CAL;
3818 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
3819 type = USE_T_CDL;
3820 else if (*T_AL != NUL)
3821 type = USE_T_AL;
3822 else if (can_ce && result_empty)
3823 type = USE_T_CE;
3824 else if (*T_DL != NUL && result_empty)
3825 type = USE_T_DL;
3826 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
3827 type = USE_T_SR;
3828 else
3829 return FAIL;
3830
3831 /*
3832 * For clearing the lines screen_del_lines() is used. This will also take
3833 * care of t_db if necessary.
3834 */
3835 if (type == USE_T_CD || type == USE_T_CDL ||
3836 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003837 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838
3839 /*
3840 * If text is retained below the screen, first clear or delete as many
3841 * lines at the bottom of the window as are about to be inserted so that
3842 * the deleted lines won't later surface during a screen_del_lines.
3843 */
3844 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003845 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846
3847#ifdef FEAT_CLIPBOARD
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003848 // Remove a modeless selection when inserting lines halfway the screen
3849 // or not the full width of the screen.
Bram Moolenaar4033c552017-09-16 20:54:51 +02003850 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003851 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 else
3853 clip_scroll_selection(-line_count);
3854#endif
3855
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003856#ifdef FEAT_GUI_HAIKU
3857 vim_lock_screen();
3858#endif
3859
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860#ifdef FEAT_GUI
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003861 // Don't update the GUI cursor here, ScreenLines[] is invalid until the
3862 // scrolling is actually carried out.
Bram Moolenaar107abd22016-08-12 14:08:25 +02003863 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864#endif
3865
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003866 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
3867 cursor_col = wp->w_wincol;
3868
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003869 if (*T_CCS != NUL) // cursor relative to region
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 cursor_row = row;
3871 else
3872 cursor_row = row + off;
3873
3874 /*
3875 * Shift LineOffset[] line_count down to reflect the inserted lines.
3876 * Clear the inserted lines in ScreenLines[].
3877 */
3878 row += off;
3879 end += off;
3880 for (i = 0; i < line_count; ++i)
3881 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 if (wp != NULL && wp->w_width != Columns)
3883 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003884 // need to copy part of a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 j = end - 1 - i;
3886 while ((j -= line_count) >= row)
3887 linecopy(j + line_count, j, wp);
3888 j += line_count;
3889 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003890 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
3891 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 else
3893 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
3894 LineWraps[j] = FALSE;
3895 }
3896 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 {
3898 j = end - 1 - i;
3899 temp = LineOffset[j];
3900 while ((j -= line_count) >= row)
3901 {
3902 LineOffset[j + line_count] = LineOffset[j];
3903 LineWraps[j + line_count] = LineWraps[j];
3904 }
3905 LineOffset[j + line_count] = temp;
3906 LineWraps[j + line_count] = FALSE;
3907 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003908 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 else
3910 lineinvalid(temp, (int)Columns);
3911 }
3912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003914#ifdef FEAT_GUI_HAIKU
3915 vim_unlock_screen();
3916#endif
3917
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003919 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003920 if (clear_attr != 0)
3921 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003923 // redraw the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 if (type == USE_REDRAW)
3925 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02003926 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 {
3928 term_append_lines(line_count);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003929 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 }
3931 else
3932 {
3933 for (i = 0; i < line_count; i++)
3934 {
3935 if (type == USE_T_AL)
3936 {
3937 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003938 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 out_str(T_AL);
3940 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003941 else // type == USE_T_SR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 out_str(T_SR);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003943 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 }
3945 }
3946
3947 /*
3948 * With scroll-reverse and 'da' flag set we need to clear the lines that
3949 * have been scrolled down into the region.
3950 */
3951 if (type == USE_T_SR && *T_DA)
3952 {
3953 for (i = 0; i < line_count; ++i)
3954 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003955 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 out_str(T_CE);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003957 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 }
3959 }
3960
3961#ifdef FEAT_GUI
3962 gui_can_update_cursor();
3963 if (gui.in_use)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003964 out_flush(); // always flush after a scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965#endif
3966 return OK;
3967}
3968
3969/*
Bram Moolenaar107abd22016-08-12 14:08:25 +02003970 * Delete lines on the screen and update ScreenLines[].
3971 * "end" is the line after the scrolled part. Normally it is Rows.
3972 * When scrolling region used "off" is the offset from the top for the region.
3973 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 *
3975 * Return OK for success, FAIL if the lines are not deleted.
3976 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003978screen_del_lines(
3979 int off,
3980 int row,
3981 int line_count,
3982 int end,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003983 int force, // even when line_count > p_ttyscroll
3984 int clear_attr, // used for clearing lines
Bram Moolenaar8ef69972022-04-03 13:23:22 +01003985 win_T *wp) // NULL or window to use width from
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986{
3987 int j;
3988 int i;
3989 unsigned temp;
3990 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003991 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 int cursor_end;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003993 int result_empty; // result is empty until end of region
3994 int can_delete; // deleting line codes can be used
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 int type;
3996
3997 /*
3998 * FAIL if
3999 * - there is no valid screen
4000 * - the screen has to be redrawn completely
4001 * - the line count is less than one
4002 * - the line count is more than 'ttyscroll'
Bram Moolenaar17fa2332022-04-01 19:44:47 +01004003 * - "end" is more than "Rows" (safety check, should not happen)
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02004004 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 */
Bram Moolenaar17fa2332022-04-01 19:44:47 +01004006 if (!screen_valid(TRUE)
4007 || line_count <= 0
4008 || (!force && line_count > p_ttyscroll)
4009 || end > Rows
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02004010#ifdef FEAT_CLIPBOARD
Bram Moolenaar17fa2332022-04-01 19:44:47 +01004011 || (clip_star.state != SELECT_CLEARED && redrawing_for_callback > 0)
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02004012#endif
4013 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 return FAIL;
4015
4016 /*
4017 * Check if the rest of the current region will become empty.
4018 */
4019 result_empty = row + line_count >= end;
4020
4021 /*
4022 * We can delete lines only when 'db' flag not set or when 'ce' option
4023 * available.
4024 */
4025 can_delete = (*T_DB == NUL || can_clear(T_CE));
4026
4027 /*
4028 * There are six ways to delete lines:
4029 * 0. When in a vertically split window and t_CV isn't set, redraw the
4030 * characters from ScreenLines[].
4031 * 1. Use T_CD if it exists and the result is empty.
4032 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
4033 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
4034 * none of the other ways work.
4035 * 4. Use T_CE (erase line) if the result is empty.
4036 * 5. Use T_DL (delete line) if it exists.
4037 * 6. redraw the characters from ScreenLines[].
4038 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
Bram Moolenaar8ef69972022-04-03 13:23:22 +01004040 {
4041 // Avoid that lines are first cleared here and then redrawn, which
4042 // results in many characters updated twice. This happens with CTRL-F
4043 // in a vertically split window. With line-by-line scrolling
4044 // USE_REDRAW should be faster.
4045 if (line_count > 3)
4046 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 type = USE_REDRAW;
Bram Moolenaar8ef69972022-04-03 13:23:22 +01004048 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02004049 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 type = USE_T_CD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 else if (row == 0 && (
4052#ifndef AMIGA
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004053 // On the Amiga, somehow '\n' on the last line doesn't always scroll
4054 // up, so use delete-line command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 line_count == 1 ||
4056#endif
4057 *T_CDL == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 type = USE_NL;
4059 else if (*T_CDL != NUL && line_count > 1 && can_delete)
4060 type = USE_T_CDL;
4061 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +02004062 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 type = USE_T_CE;
4064 else if (*T_DL != NUL && can_delete)
4065 type = USE_T_DL;
4066 else if (*T_CDL != NUL && can_delete)
4067 type = USE_T_CDL;
4068 else
4069 return FAIL;
4070
4071#ifdef FEAT_CLIPBOARD
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004072 // Remove a modeless selection when deleting lines halfway the screen or
4073 // not the full width of the screen.
Bram Moolenaar4033c552017-09-16 20:54:51 +02004074 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02004075 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 else
4077 clip_scroll_selection(line_count);
4078#endif
4079
Bram Moolenaar92c461e2020-04-24 22:19:00 +02004080#ifdef FEAT_GUI_HAIKU
4081 vim_lock_screen();
4082#endif
4083
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084#ifdef FEAT_GUI
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004085 // Don't update the GUI cursor here, ScreenLines[] is invalid until the
4086 // scrolling is actually carried out.
Bram Moolenaar107abd22016-08-12 14:08:25 +02004087 gui_dont_update_cursor(gui.cursor_row >= row + off
4088 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089#endif
4090
Bram Moolenaarbfa42462018-06-16 16:20:52 +02004091 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
4092 cursor_col = wp->w_wincol;
4093
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004094 if (*T_CCS != NUL) // cursor relative to region
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 {
4096 cursor_row = row;
4097 cursor_end = end;
4098 }
4099 else
4100 {
4101 cursor_row = row + off;
4102 cursor_end = end + off;
4103 }
4104
4105 /*
4106 * Now shift LineOffset[] line_count up to reflect the deleted lines.
4107 * Clear the inserted lines in ScreenLines[].
4108 */
4109 row += off;
4110 end += off;
4111 for (i = 0; i < line_count; ++i)
4112 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 if (wp != NULL && wp->w_width != Columns)
4114 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004115 // need to copy part of a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 j = row + i;
4117 while ((j += line_count) <= end - 1)
4118 linecopy(j - line_count, j, wp);
4119 j -= line_count;
4120 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02004121 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
4122 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 else
4124 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
4125 LineWraps[j] = FALSE;
4126 }
4127 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004129 // whole width, moving the line pointers is faster
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 j = row + i;
4131 temp = LineOffset[j];
4132 while ((j += line_count) <= end - 1)
4133 {
4134 LineOffset[j - line_count] = LineOffset[j];
4135 LineWraps[j - line_count] = LineWraps[j];
4136 }
4137 LineOffset[j - line_count] = temp;
4138 LineWraps[j - line_count] = FALSE;
4139 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02004140 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 else
4142 lineinvalid(temp, (int)Columns);
4143 }
4144 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004146#ifdef FEAT_GUI_HAIKU
4147 vim_unlock_screen();
4148#endif
4149
Bram Moolenaarcfce7172017-08-17 20:31:48 +02004150 if (screen_attr != clear_attr)
4151 screen_stop_highlight();
4152 if (clear_attr != 0)
4153 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004155 // redraw the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 if (type == USE_REDRAW)
4157 redraw_block(row, end, wp);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004158 else if (type == USE_T_CD) // delete the lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02004160 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 out_str(T_CD);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004162 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 }
4164 else if (type == USE_T_CDL)
4165 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02004166 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 term_delete_lines(line_count);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004168 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 }
4170 /*
4171 * Deleting lines at top of the screen or scroll region: Just scroll
4172 * the whole screen (scroll region) up by outputting newlines on the
4173 * last line.
4174 */
4175 else if (type == USE_NL)
4176 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02004177 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 for (i = line_count; --i >= 0; )
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004179 out_char('\n'); // cursor will remain on same line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 }
4181 else
4182 {
4183 for (i = line_count; --i >= 0; )
4184 {
4185 if (type == USE_T_DL)
4186 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02004187 windgoto(cursor_row, cursor_col);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004188 out_str(T_DL); // delete a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004190 else // type == USE_T_CE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02004192 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004193 out_str(T_CE); // erase a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004195 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 }
4197 }
4198
4199 /*
4200 * If the 'db' flag is set, we need to clear the lines that have been
4201 * scrolled up at the bottom of the region.
4202 */
4203 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
4204 {
4205 for (i = line_count; i > 0; --i)
4206 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02004207 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004208 out_str(T_CE); // erase a line
4209 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 }
4211 }
4212
4213#ifdef FEAT_GUI
4214 gui_can_update_cursor();
4215 if (gui.in_use)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004216 out_flush(); // always flush after a scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217#endif
4218
4219 return OK;
4220}
4221
4222/*
Bram Moolenaarcb574f42019-01-25 22:29:57 +01004223 * Return TRUE when postponing displaying the mode message: when not redrawing
4224 * or inside a mapping.
4225 */
4226 int
4227skip_showmode()
4228{
4229 // Call char_avail() only when we are going to show something, because it
Bram Moolenaar944cc9c2022-06-27 22:17:37 +01004230 // takes a bit of time. redrawing() may also call char_avail().
Bram Moolenaarcb574f42019-01-25 22:29:57 +01004231 if (global_busy
4232 || msg_silent != 0
4233 || !redrawing()
4234 || (char_avail() && !KeyTyped))
4235 {
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004236 redraw_mode = TRUE; // show mode later
Bram Moolenaarcb574f42019-01-25 22:29:57 +01004237 return TRUE;
4238 }
4239 return FALSE;
4240}
4241
4242/*
Bram Moolenaar81226e02018-02-20 21:44:45 +01004243 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 *
4245 * If clear_cmdline is TRUE, clear the rest of the cmdline.
4246 * If clear_cmdline is FALSE there may be a message there that needs to be
4247 * cleared only if a mode is shown.
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004248 * If redraw_mode is TRUE show or clear the mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 * Return the length of the message (0 if no message).
4250 */
4251 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004252showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253{
4254 int need_clear;
4255 int length = 0;
4256 int do_mode;
4257 int attr;
4258 int nwr_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 int sub_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260
Bram Moolenaara2a89732022-08-31 14:46:18 +01004261 do_mode = p_smd && msg_silent == 0
Bram Moolenaar24959102022-05-07 20:01:16 +01004262 && ((State & MODE_INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +02004263 || restart_edit != NUL
Bram Moolenaar9198de32022-08-27 21:30:03 +01004264 || VIsual_active);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02004265 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 {
Bram Moolenaarcb574f42019-01-25 22:29:57 +01004267 if (skip_showmode())
4268 return 0; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269
4270 nwr_save = need_wait_return;
4271
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004272 // wait a bit before overwriting an important message
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 check_for_delay(FALSE);
4274
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004275 // if the cmdline is more than one line high, erase top lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 need_clear = clear_cmdline;
4277 if (clear_cmdline && cmdline_row < Rows - 1)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004278 msg_clr_cmdline(); // will reset clear_cmdline
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004280 // Position on the last line in the window, column 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 msg_pos_mode();
4282 cursor_off();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004283 attr = HL_ATTR(HLF_CM); // Highlight mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 if (do_mode)
4285 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004286 msg_puts_attr("--", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00004288 if (
Bram Moolenaar09092152010-08-08 16:38:42 +02004289# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +00004290 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +02004291# else
Bram Moolenaarc236c162008-07-13 17:41:49 +00004292 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +00004293# endif
Bram Moolenaar09092152010-08-08 16:38:42 +02004294 )
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004295# ifdef FEAT_GUI_GTK // most of the time, it's not XIM being used
Bram Moolenaar32526b32019-01-19 17:43:09 +01004296 msg_puts_attr(" IM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297# else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004298 msg_puts_attr(" XIM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299# endif
4300#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004301 // CTRL-X in Insert mode
Bram Moolenaarea389e92014-05-28 21:40:52 +02004302 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004304 // These messages can get long, avoid a wrap in a narrow
4305 // window. Prefer showing edit_submode_extra.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 length = (Rows - msg_row) * Columns - 3;
4307 if (edit_submode_extra != NULL)
4308 length -= vim_strsize(edit_submode_extra);
4309 if (length > 0)
4310 {
4311 if (edit_submode_pre != NULL)
4312 length -= vim_strsize(edit_submode_pre);
4313 if (length - vim_strsize(edit_submode) > 0)
4314 {
4315 if (edit_submode_pre != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004316 msg_puts_attr((char *)edit_submode_pre, attr);
4317 msg_puts_attr((char *)edit_submode, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 }
4319 if (edit_submode_extra != NULL)
4320 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004321 msg_puts_attr(" ", attr); // add a space in between
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004323 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 else
4325 sub_attr = attr;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004326 msg_puts_attr((char *)edit_submode_extra, sub_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 }
4328 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 }
4330 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 if (State & VREPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004333 msg_puts_attr(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02004334 else if (State & REPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004335 msg_puts_attr(_(" REPLACE"), attr);
Bram Moolenaar24959102022-05-07 20:01:16 +01004336 else if (State & MODE_INSERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 {
4338#ifdef FEAT_RIGHTLEFT
4339 if (p_ri)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004340 msg_puts_attr(_(" REVERSE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01004342 msg_puts_attr(_(" INSERT"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01004344 else if (restart_edit == 'I' || restart_edit == 'i' ||
4345 restart_edit == 'a' || restart_edit == 'A')
Bram Moolenaar32526b32019-01-19 17:43:09 +01004346 msg_puts_attr(_(" (insert)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 else if (restart_edit == 'R')
Bram Moolenaar32526b32019-01-19 17:43:09 +01004348 msg_puts_attr(_(" (replace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 else if (restart_edit == 'V')
Bram Moolenaar32526b32019-01-19 17:43:09 +01004350 msg_puts_attr(_(" (vreplace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351#ifdef FEAT_RIGHTLEFT
4352 if (p_hkmap)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004353 msg_puts_attr(_(" Hebrew"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354#endif
4355#ifdef FEAT_KEYMAP
Bram Moolenaar24959102022-05-07 20:01:16 +01004356 if (State & MODE_LANGMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 {
4358# ifdef FEAT_ARABIC
4359 if (curwin->w_p_arab)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004360 msg_puts_attr(_(" Arabic"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 else
4362# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02004363 if (get_keymap_str(curwin, (char_u *)" (%s)",
4364 NameBuff, MAXPATHL))
Bram Moolenaar32526b32019-01-19 17:43:09 +01004365 msg_puts_attr((char *)NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 }
4367#endif
Bram Moolenaar24959102022-05-07 20:01:16 +01004368 if ((State & MODE_INSERT) && p_paste)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004369 msg_puts_attr(_(" (paste)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 if (VIsual_active)
4372 {
4373 char *p;
4374
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004375 // Don't concatenate separate words to avoid translation
4376 // problems.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 switch ((VIsual_select ? 4 : 0)
4378 + (VIsual_mode == Ctrl_V) * 2
4379 + (VIsual_mode == 'V'))
4380 {
4381 case 0: p = N_(" VISUAL"); break;
4382 case 1: p = N_(" VISUAL LINE"); break;
4383 case 2: p = N_(" VISUAL BLOCK"); break;
4384 case 4: p = N_(" SELECT"); break;
4385 case 5: p = N_(" SELECT LINE"); break;
4386 default: p = N_(" SELECT BLOCK"); break;
4387 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01004388 msg_puts_attr(_(p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01004390 msg_puts_attr(" --", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004392
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 need_clear = TRUE;
4394 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02004395 if (reg_recording != 0
Bram Moolenaare2c453d2019-08-21 14:37:09 +02004396 && edit_submode == NULL) // otherwise it gets too long
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004398 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 need_clear = TRUE;
4400 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004401
4402 mode_displayed = TRUE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004403 if (need_clear || clear_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 msg_clr_eos();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004405 msg_didout = FALSE; // overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 length = msg_col;
4407 msg_col = 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004408 need_wait_return = nwr_save; // never ask for hit-return for this
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 }
4410 else if (clear_cmdline && msg_silent == 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004411 // Clear the whole command line. Will reset "clear_cmdline".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 msg_clr_cmdline();
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004413 else if (redraw_mode)
4414 {
4415 msg_pos_mode();
4416 msg_clr_eos();
4417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004419 // In Visual mode the size of the selected area must be redrawn.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 if (VIsual_active)
4421 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004423 // If the last window has no status line, the ruler is after the mode
4424 // message and must be redrawn
Bram Moolenaar4033c552017-09-16 20:54:51 +02004425 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +02004426 win_redr_ruler(lastwin, TRUE, FALSE);
Martin Tournoijba43e762022-10-13 22:12:15 +01004427
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 redraw_cmdline = FALSE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004429 redraw_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 clear_cmdline = FALSE;
4431
4432 return length;
4433}
4434
4435/*
4436 * Position for a mode message.
4437 */
4438 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004439msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440{
4441 msg_col = 0;
4442 msg_row = Rows - 1;
4443}
4444
4445/*
4446 * Delete mode message. Used when ESC is typed which is expected to end
4447 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004448 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 */
4450 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004451unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452{
4453 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +01004454 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 */
4456 if (!redrawing() || (!force && char_avail() && !KeyTyped))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004457 redraw_cmdline = TRUE; // delete mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +02004459 clearmode();
4460}
4461
4462/*
4463 * Clear the mode message.
4464 */
4465 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02004466clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +02004467{
Bram Moolenaar2abad542018-05-19 14:43:45 +02004468 int save_msg_row = msg_row;
4469 int save_msg_col = msg_col;
4470
Bram Moolenaarfd773e92016-04-02 19:39:16 +02004471 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02004472 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004473 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +02004474 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +02004475
4476 msg_col = save_msg_col;
4477 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478}
4479
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004480 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004481recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004482{
Bram Moolenaar32526b32019-01-19 17:43:09 +01004483 msg_puts_attr(_("recording"), attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004484 if (!shortmess(SHM_RECORDING))
4485 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004486 char s[4];
4487
4488 sprintf(s, " @%c", reg_recording);
4489 msg_puts_attr(s, attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004490 }
4491}
4492
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004493/*
4494 * Draw the tab pages line at the top of the Vim window.
4495 */
Bram Moolenaare12bab32019-01-08 22:02:56 +01004496 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004497draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004498{
4499 int tabcount = 0;
4500 tabpage_T *tp;
4501 int tabwidth;
4502 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00004503 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004504 int attr;
4505 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004506 win_T *cwp;
4507 int wincount;
4508 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004509 int c;
4510 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004511 int attr_sel = HL_ATTR(HLF_TPS);
4512 int attr_nosel = HL_ATTR(HLF_TP);
4513 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00004514 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004515 int room;
4516 int use_sep_chars = (t_colors < 8
4517#ifdef FEAT_GUI
4518 && !gui.in_use
4519#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02004520#ifdef FEAT_TERMGUICOLORS
4521 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +02004522#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004523 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004524
Bram Moolenaarc695cec2017-01-08 20:00:04 +01004525 if (ScreenLines == NULL)
4526 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00004527 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004528
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004529#ifdef FEAT_GUI_TABLINE
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004530 // Take care of a GUI tabline.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004531 if (gui_use_tabline())
4532 {
4533 gui_update_tabline();
4534 return;
4535 }
4536#endif
4537
4538 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004539 return;
4540
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004541#if defined(FEAT_STL_OPT)
Bram Moolenaarca57ab52019-04-13 14:53:16 +02004542 clear_TabPageIdxs();
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004543
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004544 // Use the 'tabline' option if it's set.
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004545 if (*p_tal != NUL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004546 win_redr_custom(NULL, FALSE);
Bram Moolenaar238a5642006-02-21 22:12:05 +00004547 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004548#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004549 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004550 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00004551 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004552
Bram Moolenaar238a5642006-02-21 22:12:05 +00004553 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
4554 if (tabwidth < 6)
4555 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004556
Bram Moolenaar238a5642006-02-21 22:12:05 +00004557 attr = attr_nosel;
4558 tabcount = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004559 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
4560 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00004561 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00004562 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004563
Bram Moolenaar238a5642006-02-21 22:12:05 +00004564 if (tp->tp_topframe == topframe)
4565 attr = attr_sel;
4566 if (use_sep_chars && col > 0)
4567 screen_putchar('|', 0, col++, attr);
4568
4569 if (tp->tp_topframe != topframe)
4570 attr = attr_nosel;
4571
4572 screen_putchar(' ', 0, col++, attr);
4573
4574 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00004575 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00004576 cwp = curwin;
4577 wp = firstwin;
4578 }
4579 else
4580 {
4581 cwp = tp->tp_curwin;
4582 wp = tp->tp_firstwin;
4583 }
4584
4585 modified = FALSE;
4586 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
4587 if (bufIsChanged(wp->w_buffer))
4588 modified = TRUE;
4589 if (modified || wincount > 1)
4590 {
4591 if (wincount > 1)
4592 {
4593 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004594 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004595 if (col + len >= Columns - 3)
4596 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +00004597 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004598#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004599 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004600#else
Bram Moolenaare0f14822014-08-06 13:20:56 +02004601 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004602#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00004603 );
4604 col += len;
4605 }
4606 if (modified)
4607 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
4608 screen_putchar(' ', 0, col++, attr);
4609 }
4610
4611 room = scol - col + tabwidth - 1;
4612 if (room > 0)
4613 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004614 // Get buffer name in NameBuff[]
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004615 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004616 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +00004617 len = vim_strsize(NameBuff);
4618 p = NameBuff;
Bram Moolenaar238a5642006-02-21 22:12:05 +00004619 if (has_mbyte)
4620 while (len > room)
4621 {
4622 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004623 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +00004624 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01004625 else if (len > room)
Bram Moolenaar238a5642006-02-21 22:12:05 +00004626 {
4627 p += len - room;
4628 len = room;
4629 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004630 if (len > Columns - col - 1)
4631 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +00004632
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004633 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +00004634 col += len;
4635 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00004636 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +00004637
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004638 // Store the tab page number in TabPageIdxs[], so that
4639 // jump_to_mouse() knows where each one is.
Bram Moolenaar238a5642006-02-21 22:12:05 +00004640 ++tabcount;
4641 while (scol < col)
4642 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004643 }
4644
Bram Moolenaar238a5642006-02-21 22:12:05 +00004645 if (use_sep_chars)
4646 c = '_';
4647 else
4648 c = ' ';
4649 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004650
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004651 // Put an "X" for closing the current tab if there are several.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004652 if (first_tabpage->tp_next != NULL)
4653 {
4654 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
4655 TabPageIdxs[Columns - 1] = -999;
4656 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004657 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +00004658
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004659 // Reset the flag here again, in case evaluating 'tabline' causes it to be
4660 // set.
Bram Moolenaarb21e5842006-04-16 18:30:08 +00004661 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004662}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004663
4664/*
4665 * Get buffer name for "buf" into NameBuff[].
4666 * Takes care of special buffer names and translates special characters.
4667 */
4668 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004669get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004670{
4671 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02004672 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004673 else
4674 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
4675 trans_characters(NameBuff, MAXPATHL);
4676}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004677
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678/*
4679 * Get the character to use in a status line. Get its attributes in "*attr".
4680 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02004681 int
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004682fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683{
4684 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004685
4686#ifdef FEAT_TERMINAL
4687 if (bt_terminal(wp->w_buffer))
4688 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004689 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02004690 {
4691 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004692 fill = wp->w_fill_chars.stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02004693 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004694 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02004695 {
4696 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004697 fill = wp->w_fill_chars.stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02004698 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004699 }
4700 else
4701#endif
4702 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004704 *attr = HL_ATTR(HLF_S);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004705 fill = wp->w_fill_chars.stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 }
4707 else
4708 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004709 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004710 fill = wp->w_fill_chars.stlnc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004712 // Use fill when there is highlighting, and highlighting of current
4713 // window differs, or the fillchars differ, or this is not the
4714 // current window
Bram Moolenaar8820b482017-03-16 17:23:31 +01004715 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004716 || wp != curwin || ONE_WINDOW)
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004717 || (wp->w_fill_chars.stl != wp->w_fill_chars.stlnc)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004719 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 return '^';
4721 return '=';
4722}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724/*
4725 * Get the character to use in a separator between vertically split windows.
4726 * Get its attributes in "*attr".
4727 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02004728 int
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004729fillchar_vsep(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730{
Bram Moolenaar8820b482017-03-16 17:23:31 +01004731 *attr = HL_ATTR(HLF_C);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004732 if (*attr == 0 && wp->w_fill_chars.vert == ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 return '|';
4734 else
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004735 return wp->w_fill_chars.vert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737
4738/*
4739 * Return TRUE if redrawing should currently be done.
4740 */
4741 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004742redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743{
Bram Moolenaareb992cb2017-03-09 18:20:16 +01004744#ifdef FEAT_EVAL
4745 if (disable_redraw_for_testing)
4746 return 0;
4747 else
4748#endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +02004749 return ((!RedrawingDisabled
4750#ifdef FEAT_EVAL
4751 || ignore_redraw_flag_for_testing
4752#endif
4753 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754}
4755
4756/*
4757 * Return TRUE if printing messages should currently be done.
4758 */
4759 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004760messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761{
Bram Moolenaara2a89732022-08-31 14:46:18 +01004762 return (!(p_lz && char_avail() && !KeyTyped));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763}
4764
Bram Moolenaare677df82019-09-02 22:31:11 +02004765/*
4766 * Compute columns for ruler and shown command. 'sc_col' is also used to
4767 * decide what the maximum length of a message on the status line can be.
4768 * If there is a status line for the last window, 'sc_col' is independent
4769 * of 'ru_col'.
4770 */
4771
4772#define COL_RULER 17 // columns needed by standard ruler
4773
4774 void
4775comp_col(void)
4776{
Bram Moolenaare677df82019-09-02 22:31:11 +02004777 int last_has_status = (p_ls == 2 || (p_ls == 1 && !ONE_WINDOW));
4778
4779 sc_col = 0;
4780 ru_col = 0;
4781 if (p_ru)
4782 {
Martin Tournoijba43e762022-10-13 22:12:15 +01004783#ifdef FEAT_STL_OPT
Bram Moolenaare677df82019-09-02 22:31:11 +02004784 ru_col = (ru_wid ? ru_wid : COL_RULER) + 1;
Martin Tournoijba43e762022-10-13 22:12:15 +01004785#else
Bram Moolenaare677df82019-09-02 22:31:11 +02004786 ru_col = COL_RULER + 1;
Martin Tournoijba43e762022-10-13 22:12:15 +01004787#endif
Bram Moolenaare677df82019-09-02 22:31:11 +02004788 // no last status line, adjust sc_col
4789 if (!last_has_status)
4790 sc_col = ru_col;
4791 }
4792 if (p_sc)
4793 {
4794 sc_col += SHOWCMD_COLS;
4795 if (!p_ru || last_has_status) // no need for separating space
4796 ++sc_col;
4797 }
4798 sc_col = Columns - sc_col;
4799 ru_col = Columns - ru_col;
4800 if (sc_col <= 0) // screen too narrow, will become a mess
4801 sc_col = 1;
4802 if (ru_col <= 0)
4803 ru_col = 1;
Bram Moolenaare677df82019-09-02 22:31:11 +02004804#ifdef FEAT_EVAL
4805 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
4806#endif
4807}
4808
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004809#if defined(FEAT_LINEBREAK) || defined(PROTO)
4810/*
Bram Moolenaar64486672010-05-16 15:46:46 +02004811 * Return the width of the 'number' and 'relativenumber' column.
4812 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004813 * Otherwise it depends on 'numberwidth' and the line count.
4814 */
4815 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004816number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004817{
4818 int n;
4819 linenr_T lnum;
4820
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004821 if (wp->w_p_rnu && !wp->w_p_nu)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004822 // cursor line shows "0"
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004823 lnum = wp->w_height;
4824 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004825 // cursor line shows absolute line number
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004826 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +02004827
Bram Moolenaar6b314672015-03-20 15:42:10 +01004828 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004829 return wp->w_nrwidth_width;
4830 wp->w_nrwidth_line_count = lnum;
4831
4832 n = 0;
4833 do
4834 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004835 lnum /= 10;
4836 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004837 } while (lnum > 0);
4838
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004839 // 'numberwidth' gives the minimal width plus one
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004840 if (n < wp->w_p_nuw - 1)
4841 n = wp->w_p_nuw - 1;
4842
Bram Moolenaare4b407f2019-07-04 11:59:28 +02004843# ifdef FEAT_SIGNS
4844 // If 'signcolumn' is set to 'number' and there is a sign to display, then
4845 // the minimal width for the number column is 2.
Bram Moolenaar4eb7dae2019-11-12 22:33:45 +01004846 if (n < 2 && get_first_valid_sign(wp) != NULL
Bram Moolenaare4b407f2019-07-04 11:59:28 +02004847 && (*wp->w_p_scl == 'n' && *(wp->w_p_scl + 1) == 'u'))
4848 n = 2;
4849# endif
4850
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004851 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +01004852 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004853 return n;
4854}
4855#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +01004856
Bram Moolenaar113e1072019-01-20 15:30:40 +01004857#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9750bb12012-12-05 16:10:42 +01004858/*
4859 * Return the current cursor column. This is the actual position on the
4860 * screen. First column is 0.
4861 */
4862 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004863screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +01004864{
4865 return screen_cur_col;
4866}
4867
4868/*
4869 * Return the current cursor row. This is the actual position on the screen.
4870 * First row is 0.
4871 */
4872 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004873screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +01004874{
4875 return screen_cur_row;
4876}
Bram Moolenaar113e1072019-01-20 15:30:40 +01004877#endif
Bram Moolenaare677df82019-09-02 22:31:11 +02004878
4879/*
Bram Moolenaar93ff6722021-10-16 17:51:40 +01004880 * Calls mb_ptr2char_adv(p) and returns the character.
4881 * If "p" starts with "\x", "\u" or "\U" the hex or unicode value is used.
4882 */
4883 static int
4884get_encoded_char_adv(char_u **p)
4885{
4886 char_u *s = *p;
4887
4888 if (s[0] == '\\' && (s[1] == 'x' || s[1] == 'u' || s[1] == 'U'))
4889 {
4890 varnumber_T num = 0;
4891 int bytes;
4892 int n;
4893
4894 for (bytes = s[1] == 'x' ? 1 : s[1] == 'u' ? 2 : 4; bytes > 0; --bytes)
4895 {
4896 *p += 2;
4897 n = hexhex2nr(*p);
4898 if (n < 0)
4899 return 0;
4900 num = num * 256 + n;
4901 }
4902 *p += 2;
4903 return num;
4904 }
4905 return mb_ptr2char_adv(p);
4906}
4907
4908/*
Bram Moolenaare677df82019-09-02 22:31:11 +02004909 * Handle setting 'listchars' or 'fillchars'.
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004910 * "varp" points to either the global or the window-local value.
4911 * When "apply" is FALSE do not store the flags, only check for errors.
Bram Moolenaareed9d462021-02-15 20:38:25 +01004912 * Assume monocell characters.
Bram Moolenaare677df82019-09-02 22:31:11 +02004913 * Returns error message, NULL if it's OK.
4914 */
4915 char *
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004916set_chars_option(win_T *wp, char_u **varp, int apply)
Bram Moolenaare677df82019-09-02 22:31:11 +02004917{
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004918 int round, i, len, len2, entries;
4919 char_u *p, *s;
4920 int c1 = 0, c2 = 0, c3 = 0;
4921 char_u *last_multispace = NULL; // Last occurrence of "multispace:"
4922 char_u *last_lmultispace = NULL; // Last occurrence of "leadmultispace:"
4923 int multispace_len = 0; // Length of lcs-multispace string
4924 int lead_multispace_len = 0; // Length of lcs-leadmultispace string
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004925 int is_listchars = (varp == &p_lcs || varp == &wp->w_p_lcs);
4926 char_u *value = *varp;
4927
Bram Moolenaare677df82019-09-02 22:31:11 +02004928 struct charstab
4929 {
4930 int *cp;
4931 char *name;
4932 };
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004933 struct charstab *tab;
4934
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004935 static fill_chars_T fill_chars;
Bram Moolenaare677df82019-09-02 22:31:11 +02004936 static struct charstab filltab[] =
4937 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004938 {&fill_chars.stl, "stl"},
4939 {&fill_chars.stlnc, "stlnc"},
4940 {&fill_chars.vert, "vert"},
4941 {&fill_chars.fold, "fold"},
4942 {&fill_chars.foldopen, "foldopen"},
4943 {&fill_chars.foldclosed, "foldclose"},
4944 {&fill_chars.foldsep, "foldsep"},
4945 {&fill_chars.diff, "diff"},
4946 {&fill_chars.eob, "eob"},
Bram Moolenaar4ba5f1d2022-10-04 14:36:29 +01004947 {&fill_chars.lastline, "lastline"},
Bram Moolenaare677df82019-09-02 22:31:11 +02004948 };
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004949
Bram Moolenaar333bd562021-02-16 22:22:13 +01004950 static lcs_chars_T lcs_chars;
Bram Moolenaareed9d462021-02-15 20:38:25 +01004951 struct charstab lcstab[] =
Bram Moolenaare677df82019-09-02 22:31:11 +02004952 {
Bram Moolenaar333bd562021-02-16 22:22:13 +01004953 {&lcs_chars.eol, "eol"},
4954 {&lcs_chars.ext, "extends"},
4955 {&lcs_chars.nbsp, "nbsp"},
4956 {&lcs_chars.prec, "precedes"},
4957 {&lcs_chars.space, "space"},
4958 {&lcs_chars.tab2, "tab"},
4959 {&lcs_chars.trail, "trail"},
4960 {&lcs_chars.lead, "lead"},
Bram Moolenaare677df82019-09-02 22:31:11 +02004961#ifdef FEAT_CONCEAL
Bram Moolenaar333bd562021-02-16 22:22:13 +01004962 {&lcs_chars.conceal, "conceal"},
Bram Moolenaare677df82019-09-02 22:31:11 +02004963#else
Bram Moolenaar333bd562021-02-16 22:22:13 +01004964 {NULL, "conceal"},
Bram Moolenaare677df82019-09-02 22:31:11 +02004965#endif
4966 };
Bram Moolenaare677df82019-09-02 22:31:11 +02004967
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004968 if (is_listchars)
Bram Moolenaare677df82019-09-02 22:31:11 +02004969 {
4970 tab = lcstab;
Bram Moolenaar333bd562021-02-16 22:22:13 +01004971 CLEAR_FIELD(lcs_chars);
K.Takataeeec2542021-06-02 13:28:16 +02004972 entries = ARRAY_LENGTH(lcstab);
Bram Moolenaareed9d462021-02-15 20:38:25 +01004973 if (varp == &wp->w_p_lcs && wp->w_p_lcs[0] == NUL)
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004974 value = p_lcs; // local value is empty, us the global value
Bram Moolenaare677df82019-09-02 22:31:11 +02004975 }
4976 else
4977 {
4978 tab = filltab;
K.Takataeeec2542021-06-02 13:28:16 +02004979 entries = ARRAY_LENGTH(filltab);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004980 if (varp == &wp->w_p_fcs && wp->w_p_fcs[0] == NUL)
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004981 value = p_fcs; // local value is empty, us the global value
Bram Moolenaare677df82019-09-02 22:31:11 +02004982 }
4983
4984 // first round: check for valid value, second round: assign values
4985 for (round = 0; round <= 1; ++round)
4986 {
4987 if (round > 0)
4988 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004989 // After checking that the value is valid: set defaults.
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004990 if (is_listchars)
Bram Moolenaare677df82019-09-02 22:31:11 +02004991 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004992 for (i = 0; i < entries; ++i)
4993 if (tab[i].cp != NULL)
4994 *(tab[i].cp) = NUL;
Bram Moolenaar333bd562021-02-16 22:22:13 +01004995 lcs_chars.tab1 = NUL;
4996 lcs_chars.tab3 = NUL;
zeertzjqb5f08012022-06-09 13:55:28 +01004997
Mike Williamsf5785cf2021-09-13 22:17:38 +02004998 if (multispace_len > 0)
zeertzjqf14b8ba2021-09-10 16:58:30 +02004999 {
5000 lcs_chars.multispace = ALLOC_MULT(int, multispace_len + 1);
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01005001 if (lcs_chars.multispace != NULL)
5002 lcs_chars.multispace[multispace_len] = NUL;
zeertzjqf14b8ba2021-09-10 16:58:30 +02005003 }
5004 else
5005 lcs_chars.multispace = NULL;
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01005006
5007 if (lead_multispace_len > 0)
5008 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005009 lcs_chars.leadmultispace =
5010 ALLOC_MULT(int, lead_multispace_len + 1);
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01005011 lcs_chars.leadmultispace[lead_multispace_len] = NUL;
5012 }
5013 else
5014 lcs_chars.leadmultispace = NULL;
Bram Moolenaare677df82019-09-02 22:31:11 +02005015 }
5016 else
Bram Moolenaara98f8a22021-02-13 18:24:23 +01005017 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005018 fill_chars.stl = ' ';
5019 fill_chars.stlnc = ' ';
5020 fill_chars.vert = ' ';
5021 fill_chars.fold = '-';
5022 fill_chars.foldopen = '-';
5023 fill_chars.foldclosed = '+';
5024 fill_chars.foldsep = '|';
5025 fill_chars.diff = '-';
5026 fill_chars.eob = '~';
Bram Moolenaar4ba5f1d2022-10-04 14:36:29 +01005027 fill_chars.lastline = '@';
Bram Moolenaara98f8a22021-02-13 18:24:23 +01005028 }
Bram Moolenaare677df82019-09-02 22:31:11 +02005029 }
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01005030 p = value;
Bram Moolenaare677df82019-09-02 22:31:11 +02005031 while (*p)
5032 {
5033 for (i = 0; i < entries; ++i)
5034 {
5035 len = (int)STRLEN(tab[i].name);
5036 if (STRNCMP(p, tab[i].name, len) == 0
5037 && p[len] == ':'
5038 && p[len + 1] != NUL)
5039 {
5040 c2 = c3 = 0;
5041 s = p + len + 1;
Bram Moolenaar93ff6722021-10-16 17:51:40 +01005042 c1 = get_encoded_char_adv(&s);
zeertzjq60618c82021-12-18 15:32:46 +00005043 if (char2cells(c1) > 1)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005044 return e_invalid_argument;
Bram Moolenaar333bd562021-02-16 22:22:13 +01005045 if (tab[i].cp == &lcs_chars.tab2)
Bram Moolenaare677df82019-09-02 22:31:11 +02005046 {
5047 if (*s == NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005048 return e_invalid_argument;
Bram Moolenaar93ff6722021-10-16 17:51:40 +01005049 c2 = get_encoded_char_adv(&s);
zeertzjq60618c82021-12-18 15:32:46 +00005050 if (char2cells(c2) > 1)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005051 return e_invalid_argument;
Bram Moolenaare677df82019-09-02 22:31:11 +02005052 if (!(*s == ',' || *s == NUL))
5053 {
Bram Moolenaar93ff6722021-10-16 17:51:40 +01005054 c3 = get_encoded_char_adv(&s);
zeertzjq60618c82021-12-18 15:32:46 +00005055 if (char2cells(c3) > 1)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005056 return e_invalid_argument;
Bram Moolenaare677df82019-09-02 22:31:11 +02005057 }
5058 }
5059
5060 if (*s == ',' || *s == NUL)
5061 {
Mike Williamsf5785cf2021-09-13 22:17:38 +02005062 if (round > 0)
Bram Moolenaare677df82019-09-02 22:31:11 +02005063 {
Bram Moolenaar333bd562021-02-16 22:22:13 +01005064 if (tab[i].cp == &lcs_chars.tab2)
Bram Moolenaare677df82019-09-02 22:31:11 +02005065 {
Bram Moolenaar333bd562021-02-16 22:22:13 +01005066 lcs_chars.tab1 = c1;
5067 lcs_chars.tab2 = c2;
5068 lcs_chars.tab3 = c3;
Bram Moolenaare677df82019-09-02 22:31:11 +02005069 }
5070 else if (tab[i].cp != NULL)
5071 *(tab[i].cp) = c1;
5072
5073 }
5074 p = s;
5075 break;
5076 }
5077 }
5078 }
5079
5080 if (i == entries)
zeertzjqf14b8ba2021-09-10 16:58:30 +02005081 {
Mike Williamsf5785cf2021-09-13 22:17:38 +02005082 len = (int)STRLEN("multispace");
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01005083 len2 = (int)STRLEN("leadmultispace");
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01005084 if (is_listchars
zeertzjqf14b8ba2021-09-10 16:58:30 +02005085 && STRNCMP(p, "multispace", len) == 0
5086 && p[len] == ':'
5087 && p[len + 1] != NUL)
5088 {
5089 s = p + len + 1;
5090 if (round == 0)
5091 {
5092 // Get length of lcs-multispace string in first round
5093 last_multispace = p;
5094 multispace_len = 0;
5095 while (*s != NUL && *s != ',')
5096 {
Bram Moolenaar93ff6722021-10-16 17:51:40 +01005097 c1 = get_encoded_char_adv(&s);
zeertzjq60618c82021-12-18 15:32:46 +00005098 if (char2cells(c1) > 1)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005099 return e_invalid_argument;
zeertzjqf14b8ba2021-09-10 16:58:30 +02005100 ++multispace_len;
5101 }
5102 if (multispace_len == 0)
5103 // lcs-multispace cannot be an empty string
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005104 return e_invalid_argument;
zeertzjqf14b8ba2021-09-10 16:58:30 +02005105 p = s;
5106 }
5107 else
5108 {
5109 int multispace_pos = 0;
Mike Williamsf5785cf2021-09-13 22:17:38 +02005110
zeertzjqf14b8ba2021-09-10 16:58:30 +02005111 while (*s != NUL && *s != ',')
5112 {
Bram Moolenaar93ff6722021-10-16 17:51:40 +01005113 c1 = get_encoded_char_adv(&s);
zeertzjqf14b8ba2021-09-10 16:58:30 +02005114 if (p == last_multispace)
5115 lcs_chars.multispace[multispace_pos++] = c1;
5116 }
5117 p = s;
5118 }
5119 }
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01005120
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01005121 else if (is_listchars
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01005122 && STRNCMP(p, "leadmultispace", len2) == 0
5123 && p[len2] == ':'
5124 && p[len2 + 1] != NUL)
5125 {
5126 s = p + len2 + 1;
5127 if (round == 0)
5128 {
zeertzjqb5f08012022-06-09 13:55:28 +01005129 // get length of lcs-leadmultispace string in first
5130 // round
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01005131 last_lmultispace = p;
5132 lead_multispace_len = 0;
5133 while (*s != NUL && *s != ',')
5134 {
5135 c1 = get_encoded_char_adv(&s);
5136 if (char2cells(c1) > 1)
5137 return e_invalid_argument;
5138 ++lead_multispace_len;
5139 }
5140 if (lead_multispace_len == 0)
zeertzjqb5f08012022-06-09 13:55:28 +01005141 // lcs-leadmultispace cannot be an empty string
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01005142 return e_invalid_argument;
5143 p = s;
5144 }
5145 else
5146 {
5147 int multispace_pos = 0;
5148
5149 while (*s != NUL && *s != ',')
5150 {
5151 c1 = get_encoded_char_adv(&s);
5152 if (p == last_lmultispace)
5153 lcs_chars.leadmultispace[multispace_pos++] = c1;
5154 }
5155 p = s;
5156 }
5157 }
zeertzjqf14b8ba2021-09-10 16:58:30 +02005158 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005159 return e_invalid_argument;
zeertzjqf14b8ba2021-09-10 16:58:30 +02005160 }
5161
Bram Moolenaare677df82019-09-02 22:31:11 +02005162 if (*p == ',')
5163 ++p;
5164 }
5165 }
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005166
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01005167 if (apply)
zeertzjqf14b8ba2021-09-10 16:58:30 +02005168 {
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01005169 if (is_listchars)
5170 {
5171 vim_free(wp->w_lcs_chars.multispace);
5172 vim_free(wp->w_lcs_chars.leadmultispace);
5173 wp->w_lcs_chars = lcs_chars;
5174 }
5175 else
5176 {
5177 wp->w_fill_chars = fill_chars;
5178 }
zeertzjqf14b8ba2021-09-10 16:58:30 +02005179 }
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01005180 else if (is_listchars)
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005181 {
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01005182 vim_free(lcs_chars.multispace);
5183 vim_free(lcs_chars.leadmultispace);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005184 }
Bram Moolenaare677df82019-09-02 22:31:11 +02005185
5186 return NULL; // no error
5187}
zeertzjq8ca29b62022-08-09 12:53:14 +01005188
5189/*
5190 * Check all global and local values of 'listchars' and 'fillchars'.
5191 * Return an untranslated error messages if any of them is invalid, NULL
5192 * otherwise.
5193 */
5194 char *
5195check_chars_options(void)
5196{
5197 tabpage_T *tp;
5198 win_T *wp;
5199
5200 if (set_chars_option(curwin, &p_lcs, FALSE) != NULL)
5201 return e_conflicts_with_value_of_listchars;
5202 if (set_chars_option(curwin, &p_fcs, FALSE) != NULL)
5203 return e_conflicts_with_value_of_fillchars;
5204 FOR_ALL_TAB_WINDOWS(tp, wp)
5205 {
5206 if (set_chars_option(wp, &wp->w_p_lcs, FALSE) != NULL)
5207 return e_conflicts_with_value_of_listchars;
5208 if (set_chars_option(wp, &wp->w_p_fcs, FALSE) != NULL)
5209 return e_conflicts_with_value_of_fillchars;
5210 }
5211 return NULL;
5212}