blob: 68142f48586e40f60171fd39043d65449847d4df [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;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01001287 stl_hlrec_T *hltab;
1288 stl_hlrec_T *tabtab;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001289 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01001290 win_T *ewp;
1291 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001293 // There is a tiny chance that this gets called recursively: When
1294 // redrawing a status line triggers redrawing the ruler or tabline.
1295 // Avoid trouble by not allowing recursion.
Bram Moolenaar1d633412013-12-11 15:52:01 +01001296 if (entered)
1297 return;
1298 entered = TRUE;
1299
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001300 // setup environment for the task at hand
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001301 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 {
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001303 // Use 'tabline'. Always at the first line of the screen.
Bram Moolenaar362f3562009-11-03 16:20:34 +00001304 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001305 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00001306 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01001307 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001308 maxwidth = Columns;
1309# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001310 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001311# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001313 else
1314 {
Bram Moolenaar49c51b82021-04-01 16:16:18 +02001315 row = statusline_row(wp);
Bram Moolenaar3633cf52017-07-31 22:29:35 +02001316 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02001317 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001318
1319 if (draw_ruler)
1320 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00001321 stl = p_ruf;
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 }
1344
1345# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001346 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001347# endif
1348 }
1349 else
1350 {
1351 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00001352 stl = wp->w_p_stl;
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# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001356 use_sandbox = was_set_insecurely((char_u *)"statusline",
1357 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001358# endif
1359 }
1360
Bram Moolenaar53f81742017-09-22 14:35:51 +02001361 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001362 }
1363
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01001365 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001367 // Temporarily reset 'cursorbind', we don't want a side effect from moving
1368 // the cursor away and back.
Bram Moolenaar61452852011-02-01 18:01:11 +01001369 ewp = wp == NULL ? curwin : wp;
1370 p_crb_save = ewp->w_p_crb;
1371 ewp->w_p_crb = FALSE;
1372
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001373 // Make a copy, because the statusline may include a function call that
1374 // might change the option value and free the memory.
Bram Moolenaar362f3562009-11-03 16:20:34 +00001375 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01001376 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00001377 stl, use_sandbox,
Bram Moolenaar8133cc62020-10-26 21:05:27 +01001378 fillchar, maxwidth, &hltab, &tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00001379 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01001380 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001382 // Make all characters printable.
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01001383 p = transstr(buf);
1384 if (p != NULL)
1385 {
1386 vim_strncpy(buf, p, sizeof(buf) - 1);
1387 vim_free(p);
1388 }
1389
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001390 // fill up with "fillchar"
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01001391 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00001392 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 len += (*mb_char2bytes)(fillchar, buf + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 ++width;
1396 }
1397 buf[len] = NUL;
1398
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001399 /*
1400 * Draw each snippet with the specified highlighting.
1401 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 curattr = attr;
1403 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001404 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001406 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 screen_puts_len(p, len, row, col, curattr);
1408 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001409 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001411 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001413 else if (hltab[n].userhl < 0)
1414 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02001415#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02001416 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
1417 && wp->w_status_height != 0)
1418 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02001419 else if (wp != NULL && bt_terminal(wp->w_buffer)
1420 && wp->w_status_height != 0)
1421 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02001422#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00001423 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001424 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001426 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 }
1428 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001429
1430 if (wp == NULL)
1431 {
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001432 // Fill the TabPageIdxs[] array for clicking in the tab pagesline.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001433 col = 0;
1434 len = 0;
1435 p = buf;
1436 fillchar = 0;
1437 for (n = 0; tabtab[n].start != NULL; n++)
1438 {
1439 len += vim_strnsize(p, (int)(tabtab[n].start - p));
1440 while (col < len)
1441 TabPageIdxs[col++] = fillchar;
1442 p = tabtab[n].start;
1443 fillchar = tabtab[n].userhl;
1444 }
1445 while (col < Columns)
1446 TabPageIdxs[col++] = fillchar;
1447 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01001448
1449theend:
1450 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451}
1452
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001453#endif // FEAT_STL_OPT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454
1455/*
1456 * Output a single character directly to the screen and update ScreenLines.
1457 */
1458 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001459screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 char_u buf[MB_MAXBYTES + 1];
1462
Bram Moolenaar9a920d82012-06-01 15:21:02 +02001463 if (has_mbyte)
1464 buf[(*mb_char2bytes)(c, buf)] = NUL;
1465 else
Bram Moolenaar9a920d82012-06-01 15:21:02 +02001466 {
1467 buf[0] = c;
1468 buf[1] = NUL;
1469 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 screen_puts(buf, row, col, attr);
1471}
1472
1473/*
1474 * Get a single character directly from ScreenLines into "bytes[]".
1475 * Also return its attribute in *attrp;
1476 */
1477 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001478screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479{
1480 unsigned off;
1481
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001482 // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
1484 {
1485 off = LineOffset[row] + col;
1486 *attrp = ScreenAttrs[off];
1487 bytes[0] = ScreenLines[off];
1488 bytes[1] = NUL;
1489
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 if (enc_utf8 && ScreenLinesUC[off] != 0)
1491 bytes[utfc_char2bytes(off, bytes)] = NUL;
1492 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
1493 {
1494 bytes[0] = ScreenLines[off];
1495 bytes[1] = ScreenLines2[off];
1496 bytes[2] = NUL;
1497 }
1498 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
1499 {
1500 bytes[1] = ScreenLines[off + 1];
1501 bytes[2] = NUL;
1502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 }
1504}
1505
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001506/*
1507 * Return TRUE if composing characters for screen posn "off" differs from
1508 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01001509 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001510 */
1511 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001512screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001513{
1514 int i;
1515
1516 for (i = 0; i < Screen_mco; ++i)
1517 {
1518 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
1519 return TRUE;
1520 if (u8cc[i] == 0)
1521 break;
1522 }
1523 return FALSE;
1524}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001525
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526/*
1527 * Put string '*text' on the screen at position 'row' and 'col', with
1528 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
1529 * Note: only outputs within one row, message is truncated at screen boundary!
1530 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
1531 */
1532 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001533screen_puts(
1534 char_u *text,
1535 int row,
1536 int col,
1537 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538{
1539 screen_puts_len(text, -1, row, col, attr);
1540}
1541
1542/*
1543 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
1544 * a NUL.
1545 */
1546 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001547screen_puts_len(
1548 char_u *text,
1549 int textlen,
1550 int row,
1551 int col,
Bram Moolenaar35d8c202022-03-03 11:46:00 +00001552 int attr_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553{
Bram Moolenaar35d8c202022-03-03 11:46:00 +00001554 int attr = attr_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 unsigned off;
1556 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02001557 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 int c;
Bram Moolenaar367329b2007-08-30 11:53:22 +00001559 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 int mbyte_blen = 1;
1561 int mbyte_cells = 1;
1562 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001563 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 int clear_next_cell = FALSE;
Bram Moolenaara12a1612019-01-24 16:39:02 +01001565#ifdef FEAT_ARABIC
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001566 int prev_c = 0; // previous Arabic character
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001567 int pc, nc, nc1;
1568 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001570 int force_redraw_this;
1571 int force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001572 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573
Bram Moolenaar0b4c9ed2019-06-03 22:04:23 +02001574 // Safety check. The check for negative row and column is to fix issue
1575 // #4102. TODO: find out why row/col could be negative.
1576 if (ScreenLines == NULL
1577 || row >= screen_Rows || row < 0
1578 || col >= screen_Columns || col < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001580 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001582 // When drawing over the right half of a double-wide char clear out the
1583 // left half. Only needed in a terminal.
Bram Moolenaar7693ec62008-07-24 18:29:37 +00001584 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaara12a1612019-01-24 16:39:02 +01001585#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00001586 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01001587#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00001588 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001589 {
Bram Moolenaarff85d4a2022-10-02 15:21:04 +01001590 if (!skip_for_popup(row, col - 1))
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001591 {
Bram Moolenaarff85d4a2022-10-02 15:21:04 +01001592 ScreenLines[off - 1] = ' ';
1593 ScreenAttrs[off - 1] = 0;
1594 if (enc_utf8)
1595 {
1596 ScreenLinesUC[off - 1] = 0;
1597 ScreenLinesC[0][off - 1] = 0;
1598 }
1599 // redraw the previous cell, make it empty
1600 screen_char(off - 1, row, col - 1);
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001601 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001602 // force the cell at "col" to be redrawn
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001603 force_redraw_next = TRUE;
1604 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00001605
Bram Moolenaar367329b2007-08-30 11:53:22 +00001606 max_off = LineOffset[row] + screen_Columns;
Bram Moolenaara064ac82007-08-05 18:10:54 +00001607 while (col < screen_Columns
1608 && (len < 0 || (int)(ptr - text) < len)
1609 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 {
1611 c = *ptr;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001612 // check if this is the first byte of a multibyte
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 if (has_mbyte)
1614 {
zeertzjq4dc513a2022-07-25 19:42:02 +01001615 mbyte_blen = enc_utf8 && len > 0
1616 ? utfc_ptr2len_len(ptr, (int)((text + len) - ptr))
1617 : (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
1619 mbyte_cells = 1;
1620 else if (enc_dbcs != 0)
1621 mbyte_cells = mbyte_blen;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001622 else // enc_utf8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 {
zeertzjq4dc513a2022-07-25 19:42:02 +01001624 u8c = len >= 0
1625 ? utfc_ptr2char_len(ptr, u8cc, (int)((text + len) - ptr))
1626 : utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaara12a1612019-01-24 16:39:02 +01001628#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
1630 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001631 // Do Arabic shaping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
1633 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001634 // Past end of string to be displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 nc = NUL;
1636 nc1 = NUL;
1637 }
1638 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001639 {
zeertzjq4dc513a2022-07-25 19:42:02 +01001640 nc = len >= 0
1641 ? utfc_ptr2char_len(ptr + mbyte_blen, pcc,
1642 (int)((text + len) - ptr - mbyte_blen))
1643 : utfc_ptr2char(ptr + mbyte_blen, pcc);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001644 nc1 = pcc[0];
1645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 pc = prev_c;
1647 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001648 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 }
1650 else
1651 prev_c = u8c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01001652#endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01001653 if (col + mbyte_cells > screen_Columns)
1654 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001655 // Only 1 cell left, but character requires 2 cells:
1656 // display a '>' in the last column to avoid wrapping.
Bram Moolenaare4ebd292010-01-19 17:40:46 +01001657 c = '>';
1658 mbyte_cells = 1;
1659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 }
1661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001663 force_redraw_this = force_redraw_next;
1664 force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001665
1666 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 || (mbyte_cells == 2
1668 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
1669 || (enc_dbcs == DBCS_JPNU
1670 && c == 0x8e
1671 && ScreenLines2[off] != ptr[1])
1672 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01001673 && (ScreenLinesUC[off] !=
1674 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
1675 || (ScreenLinesUC[off] != 0
1676 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001678 || exmode_active;
1679
Bram Moolenaarff85d4a2022-10-02 15:21:04 +01001680 if ((need_redraw || force_redraw_this) && !skip_for_popup(row, col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 {
1682#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001683 // The bold trick makes a single row of pixels appear in the next
1684 // character. When a bold character is removed, the next
1685 // character should be redrawn too. This happens for our own GUI
1686 // and for some xterms.
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001687 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688# ifdef FEAT_GUI
1689 gui.in_use
1690# endif
1691# if defined(FEAT_GUI) && defined(UNIX)
1692 ||
1693# endif
1694# ifdef UNIX
1695 term_is_xterm
1696# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001697 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001699 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001701 if (n > HL_ALL)
1702 n = syn_attr2attr(n);
1703 if (n & HL_BOLD)
1704 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 }
1706#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001707 // When at the end of the text and overwriting a two-cell
1708 // character with a one-cell character, need to clear the next
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001709 // cell. Also when overwriting the left half of a two-cell char
1710 // with the right half of a two-cell char. Do this only once
1711 // (mb_off2cells() may return 2 on the right half).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 if (clear_next_cell)
1713 clear_next_cell = FALSE;
1714 else if (has_mbyte
1715 && (len < 0 ? ptr[mbyte_blen] == NUL
1716 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00001717 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00001719 && (*mb_off2cells)(off, max_off) == 1
1720 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 clear_next_cell = TRUE;
1722
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001723 // Make sure we never leave a second byte of a double-byte behind,
1724 // it confuses mb_off2cells().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00001726 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00001728 && (*mb_off2cells)(off, max_off) == 1
1729 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 ScreenLines[off + mbyte_blen] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 ScreenLines[off] = c;
1732 ScreenAttrs[off] = attr;
Bram Moolenaarb9081882022-07-09 04:56:24 +01001733 ScreenCols[off] = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 if (enc_utf8)
1735 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001736 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 ScreenLinesUC[off] = 0;
1738 else
1739 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001740 int i;
1741
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001743 for (i = 0; i < Screen_mco; ++i)
1744 {
1745 ScreenLinesC[i][off] = u8cc[i];
1746 if (u8cc[i] == 0)
1747 break;
1748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 }
1750 if (mbyte_cells == 2)
1751 {
1752 ScreenLines[off + 1] = 0;
1753 ScreenAttrs[off + 1] = attr;
Bram Moolenaarb9081882022-07-09 04:56:24 +01001754 ScreenCols[off + 1] = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 }
1756 screen_char(off, row, col);
1757 }
1758 else if (mbyte_cells == 2)
1759 {
1760 ScreenLines[off + 1] = ptr[1];
1761 ScreenAttrs[off + 1] = attr;
Bram Moolenaarb9081882022-07-09 04:56:24 +01001762 ScreenCols[off + 1] = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 screen_char_2(off, row, col);
1764 }
1765 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
1766 {
1767 ScreenLines2[off] = ptr[1];
1768 screen_char(off, row, col);
1769 }
1770 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 screen_char(off, row, col);
1772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 if (has_mbyte)
1774 {
1775 off += mbyte_cells;
1776 col += mbyte_cells;
1777 ptr += mbyte_blen;
1778 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02001779 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001780 // This only happens at the end, display one space next.
Bram Moolenaar35d8c202022-03-03 11:46:00 +00001781 // Keep the attribute from before.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02001783 len = -1;
Bram Moolenaar35d8c202022-03-03 11:46:00 +00001784 attr = ScreenAttrs[off];
Bram Moolenaare4c21e62014-05-22 16:05:19 +02001785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 }
1787 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 {
1789 ++off;
1790 ++col;
1791 ++ptr;
1792 }
1793 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001794
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001795 // If we detected the next character needs to be redrawn, but the text
1796 // doesn't extend up to there, update the character here.
Bram Moolenaarff85d4a2022-10-02 15:21:04 +01001797 if (force_redraw_next && col < screen_Columns && !skip_for_popup(row, col))
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001798 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001799 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
1800 screen_char_2(off, row, col);
1801 else
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001802 screen_char(off, row, col);
1803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804}
1805
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001806#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001808 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001810 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001811start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812{
1813 if (p_hls && !no_hlsearch)
1814 {
Bram Moolenaar0b6849e2020-05-02 18:33:25 +02001815 end_search_hl(); // just in case it wasn't called before
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001816 last_pat_prog(&screen_search_hl.rm);
1817 screen_search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 }
1819}
1820
1821/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001822 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001824 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001825end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826{
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001827 if (screen_search_hl.rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 {
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001829 vim_regfree(screen_search_hl.rm.regprog);
1830 screen_search_hl.rm.regprog = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 }
1832}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02001833#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02001834
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001836screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837{
1838 attrentry_T *aep = NULL;
1839
1840 screen_attr = attr;
1841 if (full_screen
Bram Moolenaar4f974752019-02-17 17:44:42 +01001842#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 && termcap_active
1844#endif
1845 )
1846 {
1847#ifdef FEAT_GUI
1848 if (gui.in_use)
1849 {
1850 char buf[20];
1851
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001852 // The GUI handles this internally.
Bram Moolenaar424bcae2022-01-31 14:59:41 +00001853 sprintf(buf, "\033|%dh", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 OUT_STR(buf);
1855 }
1856 else
1857#endif
1858 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001859 if (attr > HL_ALL) // special HL attr.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001861 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 aep = syn_cterm_attr2entry(attr);
1863 else
1864 aep = syn_term_attr2entry(attr);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001865 if (aep == NULL) // did ":syntax clear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 attr = 0;
1867 else
1868 attr = aep->ae_attr;
1869 }
Bram Moolenaara050b942019-12-02 21:35:31 +01001870#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
1871 if (use_vtp())
1872 {
1873 guicolor_T defguifg, defguibg;
1874 int defctermfg, defctermbg;
1875
1876 // If FG and BG are unset, the color is undefined when
1877 // BOLD+INVERSE. Use Normal as the default value.
1878 get_default_console_color(&defctermfg, &defctermbg, &defguifg,
1879 &defguibg);
1880
1881 if (p_tgc)
1882 {
1883 if (aep == NULL || COLOR_INVALID(aep->ae_u.cterm.fg_rgb))
1884 term_fg_rgb_color(defguifg);
1885 if (aep == NULL || COLOR_INVALID(aep->ae_u.cterm.bg_rgb))
1886 term_bg_rgb_color(defguibg);
1887 }
1888 else if (t_colors >= 256)
1889 {
1890 if (aep == NULL || aep->ae_u.cterm.fg_color == 0)
1891 term_fg_color(defctermfg);
1892 if (aep == NULL || aep->ae_u.cterm.bg_color == 0)
1893 term_bg_color(defctermbg);
1894 }
1895 }
1896#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001897 if ((attr & HL_BOLD) && *T_MD != NUL) // bold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001899 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001900#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001901 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
1902 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
1903 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001904#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001905 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001906 // If the Normal FG color has BOLD attribute and the new HL
1907 // has a FG color defined, clear BOLD.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001908 out_str(T_ME);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001909 if ((attr & HL_STANDOUT) && *T_SO != NUL) // standout
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 out_str(T_SO);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001911 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) // undercurl
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01001912 out_str(T_UCS);
Bram Moolenaar84f54632022-06-29 18:39:11 +01001913 if ((attr & HL_UNDERDOUBLE) && *T_USS != NUL) // double underline
1914 out_str(T_USS);
1915 if ((attr & HL_UNDERDOTTED) && *T_DS != NUL) // dotted underline
1916 out_str(T_DS);
1917 if ((attr & HL_UNDERDASHED) && *T_CDS != NUL) // dashed underline
1918 out_str(T_CDS);
1919 if (((attr & HL_UNDERLINE) // underline or undercurl, etc.
1920 || ((attr & HL_UNDERCURL) && *T_UCS == NUL)
1921 || ((attr & HL_UNDERDOUBLE) && *T_USS == NUL)
1922 || ((attr & HL_UNDERDOTTED) && *T_DS == NUL)
1923 || ((attr & HL_UNDERDASHED) && *T_CDS == NUL))
Bram Moolenaar45a00002017-12-22 21:12:34 +01001924 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 out_str(T_US);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001926 if ((attr & HL_ITALIC) && *T_CZH != NUL) // italic
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 out_str(T_CZH);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001928 if ((attr & HL_INVERSE) && *T_MR != NUL) // inverse (reverse)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 out_str(T_MR);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001930 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) // strike
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02001931 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932
1933 /*
1934 * Output the color or start string after bold etc., in case the
1935 * bold etc. override the color setting.
1936 */
1937 if (aep != NULL)
1938 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001939#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001940 // When 'termguicolors' is set but fg or bg is unset,
1941 // fall back to the cterm colors. This helps for SpellBad,
1942 // where the GUI uses a red undercurl.
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001943 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001945 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001946 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001947 }
1948 else
1949#endif
1950 if (t_colors > 1)
1951 {
1952 if (aep->ae_u.cterm.fg_color)
1953 term_fg_color(aep->ae_u.cterm.fg_color - 1);
1954 }
1955#ifdef FEAT_TERMGUICOLORS
1956 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
1957 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001958 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001959 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 }
1961 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001962#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001963 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001965 if (aep->ae_u.cterm.bg_color)
1966 term_bg_color(aep->ae_u.cterm.bg_color - 1);
1967 }
Bram Moolenaare023e882020-05-31 16:42:30 +02001968#ifdef FEAT_TERMGUICOLORS
1969 if (p_tgc && aep->ae_u.cterm.ul_rgb != CTERMCOLOR)
1970 {
1971 if (aep->ae_u.cterm.ul_rgb != INVALCOLOR)
1972 term_ul_rgb_color(aep->ae_u.cterm.ul_rgb);
1973 }
1974 else
1975#endif
1976 if (t_colors > 1)
1977 {
1978 if (aep->ae_u.cterm.ul_color)
1979 term_ul_color(aep->ae_u.cterm.ul_color - 1);
1980 }
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001981
Bram Moolenaarf708ac52018-03-12 21:48:32 +01001982 if (!IS_CTERM)
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001983 {
1984 if (aep->ae_u.term.start != NULL)
1985 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 }
1987 }
1988 }
1989 }
1990}
1991
1992 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001993screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001995 int do_ME = FALSE; // output T_ME code
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02001996#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Bram Moolenaar09307e32020-05-29 21:42:55 +02001997 int do_ME_fg = FALSE, do_ME_bg = FALSE;
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02001998#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999
2000 if (screen_attr != 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01002001#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 && termcap_active
2003#endif
2004 )
2005 {
2006#ifdef FEAT_GUI
2007 if (gui.in_use)
2008 {
2009 char buf[20];
2010
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002011 // use internal GUI code
Bram Moolenaar424bcae2022-01-31 14:59:41 +00002012 sprintf(buf, "\033|%dH", screen_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 OUT_STR(buf);
2014 }
2015 else
2016#endif
2017 {
Bram Moolenaar84f54632022-06-29 18:39:11 +01002018 int is_under;
2019
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002020 if (screen_attr > HL_ALL) // special HL attr.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 {
2022 attrentry_T *aep;
2023
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002024 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 {
2026 /*
2027 * Assume that t_me restores the original colors!
2028 */
2029 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01002030 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02002031#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01002032 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
2033 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02002034# ifdef FEAT_VTP
2035 ? !(do_ME_fg = TRUE) : (do_ME_fg = FALSE)
2036# endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01002037 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002038#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01002039 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02002040#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01002041 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
2042 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02002043# ifdef FEAT_VTP
2044 ? !(do_ME_bg = TRUE) : (do_ME_bg = FALSE)
2045# endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01002046 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002047#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01002048 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 do_ME = TRUE;
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02002050#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
2051 if (use_vtp())
2052 {
2053 if (do_ME_fg && do_ME_bg)
2054 do_ME = TRUE;
2055
2056 // FG and BG cannot be separated in T_ME, which is not
2057 // efficient.
2058 if (!do_ME && do_ME_fg)
2059 out_str((char_u *)"\033|39m"); // restore FG
2060 if (!do_ME && do_ME_bg)
2061 out_str((char_u *)"\033|49m"); // restore BG
2062 }
2063 else
2064 {
2065 // Process FG and BG at once.
2066 if (!do_ME)
2067 do_ME = do_ME_fg | do_ME_bg;
2068 }
2069#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 }
2071 else
2072 {
2073 aep = syn_term_attr2entry(screen_attr);
2074 if (aep != NULL && aep->ae_u.term.stop != NULL)
2075 {
2076 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
2077 do_ME = TRUE;
2078 else
2079 out_str(aep->ae_u.term.stop);
2080 }
2081 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002082 if (aep == NULL) // did ":syntax clear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 screen_attr = 0;
2084 else
2085 screen_attr = aep->ae_attr;
2086 }
2087
2088 /*
2089 * Often all ending-codes are equal to T_ME. Avoid outputting the
2090 * same sequence several times.
2091 */
2092 if (screen_attr & HL_STANDOUT)
2093 {
2094 if (STRCMP(T_SE, T_ME) == 0)
2095 do_ME = TRUE;
2096 else
2097 out_str(T_SE);
2098 }
Bram Moolenaar84f54632022-06-29 18:39:11 +01002099 is_under = (screen_attr & (HL_UNDERCURL
2100 | HL_UNDERDOUBLE | HL_UNDERDOTTED | HL_UNDERDASHED));
2101 if (is_under && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01002102 {
2103 if (STRCMP(T_UCE, T_ME) == 0)
2104 do_ME = TRUE;
2105 else
2106 out_str(T_UCE);
2107 }
Bram Moolenaar84f54632022-06-29 18:39:11 +01002108 if ((screen_attr & HL_UNDERLINE) || (is_under && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 {
2110 if (STRCMP(T_UE, T_ME) == 0)
2111 do_ME = TRUE;
2112 else
2113 out_str(T_UE);
2114 }
2115 if (screen_attr & HL_ITALIC)
2116 {
2117 if (STRCMP(T_CZR, T_ME) == 0)
2118 do_ME = TRUE;
2119 else
2120 out_str(T_CZR);
2121 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02002122 if (screen_attr & HL_STRIKETHROUGH)
2123 {
2124 if (STRCMP(T_STE, T_ME) == 0)
2125 do_ME = TRUE;
2126 else
2127 out_str(T_STE);
2128 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
2130 out_str(T_ME);
2131
Bram Moolenaar61be73b2016-04-29 22:59:22 +02002132#ifdef FEAT_TERMGUICOLORS
2133 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002135 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002136 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002137 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002138 term_bg_rgb_color(cterm_normal_bg_gui_color);
Bram Moolenaare023e882020-05-31 16:42:30 +02002139 if (cterm_normal_ul_gui_color != INVALCOLOR)
2140 term_ul_rgb_color(cterm_normal_ul_gui_color);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002141 }
2142 else
2143#endif
2144 {
2145 if (t_colors > 1)
2146 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002147 // set Normal cterm colors
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002148 if (cterm_normal_fg_color != 0)
2149 term_fg_color(cterm_normal_fg_color - 1);
2150 if (cterm_normal_bg_color != 0)
2151 term_bg_color(cterm_normal_bg_color - 1);
Bram Moolenaare023e882020-05-31 16:42:30 +02002152 if (cterm_normal_ul_color != 0)
2153 term_ul_color(cterm_normal_ul_color - 1);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002154 if (cterm_normal_fg_bold)
2155 out_str(T_MD);
2156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 }
2158 }
2159 }
2160 screen_attr = 0;
2161}
2162
2163/*
2164 * Reset the colors for a cterm. Used when leaving Vim.
2165 * The machine specific code may override this again.
2166 */
2167 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002168reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002170 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002172 // set Normal cterm colors
Bram Moolenaar61be73b2016-04-29 22:59:22 +02002173#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002174 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
2175 || cterm_normal_bg_gui_color != INVALCOLOR)
2176 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002177#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002179#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 {
2181 out_str(T_OP);
2182 screen_attr = -1;
2183 }
2184 if (cterm_normal_fg_bold)
2185 {
2186 out_str(T_ME);
2187 screen_attr = -1;
2188 }
2189 }
2190}
2191
2192/*
2193 * Put character ScreenLines["off"] on the screen at position "row" and "col",
2194 * using the attributes from ScreenAttrs["off"].
2195 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002196 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002197screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198{
2199 int attr;
2200
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002201 // Check for illegal values, just in case (could happen just after
2202 // resizing).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 if (row >= screen_Rows || col >= screen_Columns)
2204 return;
2205
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002206 // Outputting a character in the last cell on the screen may scroll the
2207 // screen up. Only do it when the "xn" termcap property is set, otherwise
2208 // mark the character invalid (update it when scrolled up).
Bram Moolenaar494838a2015-02-10 19:20:37 +01002209 if (*T_XN == NUL
2210 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211#ifdef FEAT_RIGHTLEFT
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002212 // account for first command-line character in rightleft mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 && !cmdmsg_rl
2214#endif
2215 )
2216 {
2217 ScreenAttrs[off] = (sattr_T)-1;
Bram Moolenaarb9081882022-07-09 04:56:24 +01002218 ScreenCols[off] = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 return;
2220 }
2221
2222 /*
2223 * Stop highlighting first, so it's easier to move the cursor.
2224 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 if (screen_char_attr != 0)
2226 attr = screen_char_attr;
2227 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 attr = ScreenAttrs[off];
2229 if (screen_attr != attr)
2230 screen_stop_highlight();
2231
2232 windgoto(row, col);
2233
2234 if (screen_attr != attr)
2235 screen_start_highlight(attr);
2236
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 if (enc_utf8 && ScreenLinesUC[off] != 0)
2238 {
2239 char_u buf[MB_MAXBYTES + 1];
2240
Bram Moolenaarcb070082016-04-02 22:14:51 +02002241 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002242 {
2243 if (*p_ambw == 'd'
Bram Moolenaara12a1612019-01-24 16:39:02 +01002244#ifdef FEAT_GUI
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002245 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01002246#endif
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002247 )
2248 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002249 // Clear the two screen cells. If the character is actually
2250 // single width it won't change the second cell.
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002251 out_str((char_u *)" ");
2252 term_windgoto(row, col);
2253 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002254 // not sure where the cursor is after drawing the ambiguous width
2255 // character
Bram Moolenaarcb070082016-04-02 22:14:51 +02002256 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002257 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02002258 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002260
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002261 // Convert the UTF-8 character to bytes and write it.
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002262 buf[utfc_char2bytes(off, buf)] = NUL;
2263 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 }
2265 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 out_char(ScreenLines[off]);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002269 // double-byte character in single-width cell
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2271 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 }
2273
2274 screen_cur_col++;
2275}
2276
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277/*
2278 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
2279 * on the screen at position 'row' and 'col'.
2280 * The attributes of the first byte is used for all. This is required to
2281 * output the two bytes of a double-byte character with nothing in between.
2282 */
2283 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002284screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002286 // Check for illegal values (could be wrong when screen was resized).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
2288 return;
2289
dundargocc57b5bc2022-11-02 13:30:51 +00002290 // Outputting the last character on the screen may scroll the screen up.
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002291 // Don't to it! Mark the character invalid (update it when scrolled up)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
2293 {
2294 ScreenAttrs[off] = (sattr_T)-1;
Bram Moolenaarb9081882022-07-09 04:56:24 +01002295 ScreenCols[off] = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 return;
2297 }
2298
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002299 // Output the first byte normally (positions the cursor), then write the
2300 // second byte directly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 screen_char(off, row, col);
2302 out_char(ScreenLines[off + 1]);
2303 ++screen_cur_col;
2304}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306/*
2307 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
2308 * This uses the contents of ScreenLines[] and doesn't change it.
2309 */
2310 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002311screen_draw_rectangle(
2312 int row,
2313 int col,
2314 int height,
2315 int width,
2316 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317{
2318 int r, c;
2319 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00002320 int max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002322 // Can't use ScreenLines unless initialized
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002323 if (ScreenLines == NULL)
2324 return;
2325
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 if (invert)
2327 screen_char_attr = HL_INVERSE;
2328 for (r = row; r < row + height; ++r)
2329 {
2330 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00002331 max_off = off + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 for (c = col; c < col + width; ++c)
2333 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00002334 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 {
Bram Moolenaarff85d4a2022-10-02 15:21:04 +01002336 if (!skip_for_popup(r, c))
2337 screen_char_2(off + c, r, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 ++c;
2339 }
2340 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 {
Bram Moolenaarff85d4a2022-10-02 15:21:04 +01002342 if (!skip_for_popup(r, c))
2343 screen_char(off + c, r, c);
Bram Moolenaar367329b2007-08-30 11:53:22 +00002344 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 ++c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 }
2347 }
2348 }
2349 screen_char_attr = 0;
2350}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352/*
2353 * Redraw the characters for a vertically split window.
2354 */
2355 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002356redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357{
2358 int col;
2359 int width;
2360
2361# ifdef FEAT_CLIPBOARD
2362 clip_may_clear_selection(row, end - 1);
2363# endif
2364
2365 if (wp == NULL)
2366 {
2367 col = 0;
2368 width = Columns;
2369 }
2370 else
2371 {
2372 col = wp->w_wincol;
2373 width = wp->w_width;
2374 }
2375 screen_draw_rectangle(row, col, end - row, width, FALSE);
2376}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002378 void
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002379space_to_screenline(int off, int attr)
2380{
2381 ScreenLines[off] = ' ';
2382 ScreenAttrs[off] = attr;
Bram Moolenaarb9081882022-07-09 04:56:24 +01002383 ScreenCols[off] = -1;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002384 if (enc_utf8)
2385 ScreenLinesUC[off] = 0;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002386}
2387
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388/*
Bram Moolenaarcee9c842022-04-09 12:40:13 +01002389 * Fill the screen from "start_row" to "end_row" (exclusive), from "start_col"
2390 * to "end_col" (exclusive) with character "c1" in first column followed by
2391 * "c2" in the other columns. Use attributes "attr".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 */
2393 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002394screen_fill(
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002395 int start_row,
2396 int end_row,
2397 int start_col,
2398 int end_col,
2399 int c1,
2400 int c2,
2401 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002403 int row;
2404 int col;
2405 int off;
2406 int end_off;
2407 int did_delete;
2408 int c;
2409 int norm_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002411 int force_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412#endif
2413
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002414 if (end_row > screen_Rows) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 end_row = screen_Rows;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002416 if (end_col > screen_Columns) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 end_col = screen_Columns;
2418 if (ScreenLines == NULL
2419 || start_row >= end_row
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002420 || start_col >= end_col) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 return;
2422
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002423 // it's a "normal" terminal when not in a GUI or cterm
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 norm_term = (
2425#ifdef FEAT_GUI
2426 !gui.in_use &&
2427#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002428 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 for (row = start_row; row < end_row; ++row)
2430 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00002431 if (has_mbyte
Bram Moolenaara12a1612019-01-24 16:39:02 +01002432#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00002433 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01002434#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00002435 )
2436 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00002437 // When drawing over the right half of a double-wide char clear
2438 // out the left half. When drawing over the left half of a
2439 // double wide-char clear out the right half. Only needed in a
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002440 // terminal.
Bram Moolenaar7693ec62008-07-24 18:29:37 +00002441 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00002442 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00002443 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00002444 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00002445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 /*
2447 * Try to use delete-line termcap code, when no attributes or in a
2448 * "normal" terminal, where a bold/italic space is just a
2449 * space.
2450 */
2451 did_delete = FALSE;
2452 if (c2 == ' '
2453 && end_col == Columns
2454 && can_clear(T_CE)
2455 && (attr == 0
2456 || (norm_term
2457 && attr <= HL_ALL
2458 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
2459 {
2460 /*
2461 * check if we really need to clear something
2462 */
2463 col = start_col;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002464 if (c1 != ' ') // don't clear first char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 ++col;
2466
2467 off = LineOffset[row] + col;
2468 end_off = LineOffset[row] + end_col;
2469
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002470 // skip blanks (used often, keep it fast!)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 if (enc_utf8)
2472 while (off < end_off && ScreenLines[off] == ' '
2473 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
2474 ++off;
2475 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 while (off < end_off && ScreenLines[off] == ' '
2477 && ScreenAttrs[off] == 0)
2478 ++off;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002479 if (off < end_off) // something to be cleared
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 {
2481 col = off - LineOffset[row];
2482 screen_stop_highlight();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002483 term_windgoto(row, col);// clear rest of this screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 out_str(T_CE);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002485 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 col = end_col - col;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002487 while (col--) // clear chars in ScreenLines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002489 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 ++off;
2491 }
2492 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002493 did_delete = TRUE; // the chars are cleared now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 }
2495
2496 off = LineOffset[row] + start_col;
2497 c = c1;
2498 for (col = start_col; col < end_col; ++col)
2499 {
Bram Moolenaar33796b32019-06-08 16:01:13 +02002500 if ((ScreenLines[off] != c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002501 || (enc_utf8 && (int)ScreenLinesUC[off]
2502 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 || ScreenAttrs[off] != attr
Bram Moolenaar838b7462022-09-26 15:19:56 +01002504 || must_redraw == UPD_CLEAR // screen clear pending
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505#if defined(FEAT_GUI) || defined(UNIX)
2506 || force_next
2507#endif
2508 )
Bram Moolenaar33796b32019-06-08 16:01:13 +02002509 // Skip if under a(nother) popup.
Bram Moolenaarff85d4a2022-10-02 15:21:04 +01002510 && !skip_for_popup(row, col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 {
2512#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002513 // The bold trick may make a single row of pixels appear in
2514 // the next character. When a bold character is removed, the
2515 // next character should be redrawn too. This happens for our
2516 // own GUI and for some xterms.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 if (
2518# ifdef FEAT_GUI
2519 gui.in_use
2520# endif
2521# if defined(FEAT_GUI) && defined(UNIX)
2522 ||
2523# endif
2524# ifdef UNIX
2525 term_is_xterm
2526# endif
2527 )
2528 {
2529 if (ScreenLines[off] != ' '
2530 && (ScreenAttrs[off] > HL_ALL
2531 || ScreenAttrs[off] & HL_BOLD))
2532 force_next = TRUE;
2533 else
2534 force_next = FALSE;
2535 }
Bram Moolenaar4ba5f1d2022-10-04 14:36:29 +01002536#endif // FEAT_GUI || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 if (enc_utf8)
2539 {
2540 if (c >= 0x80)
2541 {
2542 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002543 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 }
2545 else
2546 ScreenLinesUC[off] = 0;
2547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 ScreenAttrs[off] = attr;
2549 if (!did_delete || c != ' ')
2550 screen_char(off, row, col);
2551 }
Bram Moolenaarb9081882022-07-09 04:56:24 +01002552 ScreenCols[off] = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 ++off;
2554 if (col == start_col)
2555 {
2556 if (did_delete)
2557 break;
2558 c = c2;
2559 }
2560 }
2561 if (end_col == Columns)
2562 LineWraps[row] = FALSE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002563 if (row == Rows - 1) // overwritten the command line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 {
2565 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02002566 if (start_col == 0 && end_col == Columns
2567 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002568 clear_cmdline = FALSE; // command line has been cleared
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002569 if (start_col == 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002570 mode_displayed = FALSE; // mode cleared or overwritten
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 }
2572 }
2573}
2574
2575/*
2576 * Check if there should be a delay. Used before clearing or redrawing the
2577 * screen or the command line.
2578 */
2579 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002580check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581{
2582 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
2583 && !did_wait_return
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002584 && emsg_silent == 0
2585 && !in_assert_fails)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 {
2587 out_flush();
Bram Moolenaareda1da02019-11-17 17:06:33 +01002588 ui_delay(1006L, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 emsg_on_display = FALSE;
2590 if (check_msg_scroll)
2591 msg_scroll = FALSE;
2592 }
2593}
2594
2595/*
Bram Moolenaarca57ab52019-04-13 14:53:16 +02002596 * Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect.
2597 */
2598 static void
2599clear_TabPageIdxs(void)
2600{
2601 int scol;
2602
2603 for (scol = 0; scol < Columns; ++scol)
2604 TabPageIdxs[scol] = 0;
2605}
2606
2607/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002609 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 * Returns TRUE if there is a valid screen to write to.
2611 * Returns FALSE when starting up and screen not initialized yet.
2612 */
2613 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002614screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002616 screenalloc(doclear); // allocate screen buffers if size changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 return (ScreenLines != NULL);
2618}
2619
2620/*
2621 * Resize the shell to Rows and Columns.
2622 * Allocate ScreenLines[] and associated items.
2623 *
2624 * There may be some time between setting Rows and Columns and (re)allocating
2625 * ScreenLines[]. This happens when starting up and when (manually) changing
2626 * the shell size. Always use screen_Rows and screen_Columns to access items
2627 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
2628 * final size of the shell is needed.
2629 */
2630 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002631screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632{
2633 int new_row, old_row;
2634#ifdef FEAT_GUI
2635 int old_Rows;
2636#endif
2637 win_T *wp;
2638 int outofmem = FALSE;
2639 int len;
2640 schar_T *new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002642 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002644 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 sattr_T *new_ScreenAttrs;
Bram Moolenaarb9081882022-07-09 04:56:24 +01002646 colnr_T *new_ScreenCols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 unsigned *new_LineOffset;
2648 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002649 short *new_TabPageIdxs;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002650#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002651 short *new_popup_mask;
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002652 short *new_popup_mask_next;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002653 char *new_popup_transparent;
Bram Moolenaar33796b32019-06-08 16:01:13 +02002654#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00002655 tabpage_T *tp;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002656 static int entered = FALSE; // avoid recursiveness
2657 static int done_outofmem_msg = FALSE; // did outofmem message
Bram Moolenaar87e817c2009-02-22 20:13:39 +00002658 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659
Bram Moolenaar87e817c2009-02-22 20:13:39 +00002660retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 /*
2662 * Allocation of the screen buffers is done only when the size changes and
2663 * when Rows and Columns have been set and we have started doing full
2664 * screen stuff.
2665 */
2666 if ((ScreenLines != NULL
2667 && Rows == screen_Rows
2668 && Columns == screen_Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 && enc_utf8 == (ScreenLinesUC != NULL)
2670 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaara12a1612019-01-24 16:39:02 +01002671 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 || Rows == 0
2673 || Columns == 0
2674 || (!full_screen && ScreenLines == NULL))
2675 return;
2676
2677 /*
2678 * It's possible that we produce an out-of-memory message below, which
2679 * will cause this function to be called again. To break the loop, just
2680 * return here.
2681 */
2682 if (entered)
2683 return;
2684 entered = TRUE;
2685
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00002686 /*
2687 * Note that the window sizes are updated before reallocating the arrays,
2688 * thus we must not redraw here!
2689 */
2690 ++RedrawingDisabled;
2691
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002692 win_new_shellsize(); // fit the windows in the new sized shell
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002694#ifdef FEAT_GUI_HAIKU
2695 vim_lock_screen(); // be safe, put it here
2696#endif
2697
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002698 comp_col(); // recompute columns for shown command and ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699
2700 /*
2701 * We're changing the size of the screen.
2702 * - Allocate new arrays for ScreenLines and ScreenAttrs.
2703 * - Move lines from the old arrays into the new arrays, clear extra
2704 * lines (unless the screen is going to be cleared).
2705 * - Free the old arrays.
2706 *
2707 * If anything fails, make ScreenLines NULL, so we don't do anything!
2708 * Continuing with the old ScreenLines may result in a crash, because the
2709 * size is wrong.
2710 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002711 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00002713 if (aucmd_win != NULL)
2714 win_free_lsize(aucmd_win);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002715#ifdef FEAT_PROP_POPUP
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002716 // global popup windows
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002717 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002718 win_free_lsize(wp);
2719 // tab-local popup windows
2720 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002721 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002722 win_free_lsize(wp);
2723#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002725 new_ScreenLines = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
Bram Moolenaar216b7102010-03-23 13:56:59 +01002726 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 if (enc_utf8)
2728 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002729 new_ScreenLinesUC = LALLOC_MULT(u8char_T, (Rows + 1) * Columns);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002730 for (i = 0; i < p_mco; ++i)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002731 new_ScreenLinesC[i] = LALLOC_CLEAR_MULT(u8char_T,
2732 (Rows + 1) * Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 }
2734 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002735 new_ScreenLines2 = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
2736 new_ScreenAttrs = LALLOC_MULT(sattr_T, (Rows + 1) * Columns);
dundargocc57b5bc2022-11-02 13:30:51 +00002737 // Clear ScreenCols to avoid a warning for uninitialized memory in
Bram Moolenaar18ee0fe2022-09-19 11:44:11 +01002738 // jump_to_mouse().
2739 new_ScreenCols = LALLOC_CLEAR_MULT(colnr_T, (Rows + 1) * Columns);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002740 new_LineOffset = LALLOC_MULT(unsigned, Rows);
2741 new_LineWraps = LALLOC_MULT(char_u, Rows);
2742 new_TabPageIdxs = LALLOC_MULT(short, Columns);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002743#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002744 new_popup_mask = LALLOC_MULT(short, Rows * Columns);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002745 new_popup_mask_next = LALLOC_MULT(short, Rows * Columns);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002746 new_popup_transparent = LALLOC_MULT(char, Rows * Columns);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002747#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00002749 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 {
2751 if (win_alloc_lines(wp) == FAIL)
2752 {
2753 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00002754 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 }
2756 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00002757 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
2758 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002759 outofmem = TRUE;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002760#ifdef FEAT_PROP_POPUP
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002761 // global popup windows
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002762 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002763 if (win_alloc_lines(wp) == FAIL)
2764 {
2765 outofmem = TRUE;
2766 goto give_up;
2767 }
2768 // tab-local popup windows
2769 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002770 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002771 if (win_alloc_lines(wp) == FAIL)
2772 {
2773 outofmem = TRUE;
2774 goto give_up;
2775 }
2776#endif
2777
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00002778give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002780 for (i = 0; i < p_mco; ++i)
2781 if (new_ScreenLinesC[i] == NULL)
2782 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 if (new_ScreenLines == NULL
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002784 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 || new_ScreenAttrs == NULL
Bram Moolenaarb9081882022-07-09 04:56:24 +01002787 || new_ScreenCols == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 || new_LineOffset == NULL
2789 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00002790 || new_TabPageIdxs == NULL
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002791#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002792 || new_popup_mask == NULL
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002793 || new_popup_mask_next == NULL
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002794 || new_popup_transparent == NULL
Bram Moolenaar33796b32019-06-08 16:01:13 +02002795#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 || outofmem)
2797 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002798 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002799 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002800 // guess the size
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002801 do_outofmem_msg((long_u)((Rows + 1) * Columns));
2802
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002803 // Remember we did this to avoid getting outofmem messages over
2804 // and over again.
Bram Moolenaar89d40322006-08-29 15:30:07 +00002805 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002806 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01002807 VIM_CLEAR(new_ScreenLines);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002808 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002809 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01002810 VIM_CLEAR(new_ScreenLinesC[i]);
2811 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002812 VIM_CLEAR(new_ScreenAttrs);
Bram Moolenaarb9081882022-07-09 04:56:24 +01002813 VIM_CLEAR(new_ScreenCols);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002814 VIM_CLEAR(new_LineOffset);
2815 VIM_CLEAR(new_LineWraps);
2816 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002817#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002818 VIM_CLEAR(new_popup_mask);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002819 VIM_CLEAR(new_popup_mask_next);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002820 VIM_CLEAR(new_popup_transparent);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002821#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 }
2823 else
2824 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002825 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002826
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 for (new_row = 0; new_row < Rows; ++new_row)
2828 {
2829 new_LineOffset[new_row] = new_row * Columns;
2830 new_LineWraps[new_row] = FALSE;
2831
2832 /*
2833 * If the screen is not going to be cleared, copy as much as
2834 * possible from the old screen to the new one and clear the rest
2835 * (used when resizing the window at the "--more--" prompt or when
2836 * executing an external command, for the GUI).
2837 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002838 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 {
2840 (void)vim_memset(new_ScreenLines + new_row * Columns,
2841 ' ', (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 if (enc_utf8)
2843 {
2844 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
2845 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002846 for (i = 0; i < p_mco; ++i)
2847 (void)vim_memset(new_ScreenLinesC[i]
2848 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 0, (size_t)Columns * sizeof(u8char_T));
2850 }
2851 if (enc_dbcs == DBCS_JPNU)
2852 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
2853 0, (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
2855 0, (size_t)Columns * sizeof(sattr_T));
Bram Moolenaarb9081882022-07-09 04:56:24 +01002856 (void)vim_memset(new_ScreenCols + new_row * Columns,
2857 0, (size_t)Columns * sizeof(colnr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002859 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 {
2861 if (screen_Columns < Columns)
2862 len = screen_Columns;
2863 else
2864 len = Columns;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002865 // When switching to utf-8 don't copy characters, they
2866 // may be invalid now. Also when p_mco changes.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002867 if (!(enc_utf8 && ScreenLinesUC == NULL)
2868 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002869 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
2870 ScreenLines + LineOffset[old_row],
2871 (size_t)len * sizeof(schar_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002872 if (enc_utf8 && ScreenLinesUC != NULL
2873 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 {
2875 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
2876 ScreenLinesUC + LineOffset[old_row],
2877 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002878 for (i = 0; i < p_mco; ++i)
2879 mch_memmove(new_ScreenLinesC[i]
2880 + new_LineOffset[new_row],
2881 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 (size_t)len * sizeof(u8char_T));
2883 }
2884 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
2885 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
2886 ScreenLines2 + LineOffset[old_row],
2887 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
2889 ScreenAttrs + LineOffset[old_row],
2890 (size_t)len * sizeof(sattr_T));
Bram Moolenaarb9081882022-07-09 04:56:24 +01002891 mch_memmove(new_ScreenCols + new_LineOffset[new_row],
2892 ScreenAttrs + LineOffset[old_row],
2893 (size_t)len * sizeof(colnr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 }
2895 }
2896 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002897 // Use the last line of the screen for the current line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 current_ScreenLine = new_ScreenLines + Rows * Columns;
Bram Moolenaar6ace95e2019-08-13 23:09:49 +02002899
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002900#ifdef FEAT_PROP_POPUP
Bram Moolenaar6ace95e2019-08-13 23:09:49 +02002901 vim_memset(new_popup_mask, 0, Rows * Columns * sizeof(short));
2902 vim_memset(new_popup_transparent, 0, Rows * Columns * sizeof(char));
2903#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 }
2905
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002906 free_screenlines();
2907
Bram Moolenaar6ace95e2019-08-13 23:09:49 +02002908 // NOTE: this may result in all pointers to become NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 ScreenLines = new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002911 for (i = 0; i < p_mco; ++i)
2912 ScreenLinesC[i] = new_ScreenLinesC[i];
2913 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 ScreenLines2 = new_ScreenLines2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 ScreenAttrs = new_ScreenAttrs;
Bram Moolenaarb9081882022-07-09 04:56:24 +01002916 ScreenCols = new_ScreenCols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 LineOffset = new_LineOffset;
2918 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002919 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002920#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002921 popup_mask = new_popup_mask;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002922 popup_mask_next = new_popup_mask_next;
2923 popup_transparent = new_popup_transparent;
Bram Moolenaar33796b32019-06-08 16:01:13 +02002924 popup_mask_refresh = TRUE;
2925#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002927 // It's important that screen_Rows and screen_Columns reflect the actual
2928 // size of ScreenLines[]. Set them before calling anything.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929#ifdef FEAT_GUI
2930 old_Rows = screen_Rows;
2931#endif
2932 screen_Rows = Rows;
2933 screen_Columns = Columns;
2934
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01002935 set_must_redraw(UPD_CLEAR); // need to clear the screen later
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002936 if (doclear)
Bram Moolenaarb13d3402022-08-29 13:44:28 +01002937 screenclear2(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938#ifdef FEAT_GUI
2939 else if (gui.in_use
2940 && !gui.starting
2941 && ScreenLines != NULL
2942 && old_Rows != Rows)
2943 {
Bram Moolenaar7c003aa2020-03-28 20:44:41 +01002944 gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
2945
2946 // Adjust the position of the cursor, for when executing an external
2947 // command.
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002948 if (msg_row >= Rows) // Rows got smaller
2949 msg_row = Rows - 1; // put cursor at last row
2950 else if (Rows > old_Rows) // Rows got bigger
2951 msg_row += Rows - old_Rows; // put cursor in same place
2952 if (msg_col >= Columns) // Columns got smaller
2953 msg_col = Columns - 1; // put cursor at last column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 }
2955#endif
Bram Moolenaarca57ab52019-04-13 14:53:16 +02002956 clear_TabPageIdxs();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002958#ifdef FEAT_GUI_HAIKU
2959 vim_unlock_screen();
2960#endif
2961
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00002963 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00002964
Bram Moolenaar87e817c2009-02-22 20:13:39 +00002965 /*
2966 * Do not apply autocommands more than 3 times to avoid an endless loop
2967 * in case applying autocommands always changes Rows or Columns.
2968 */
2969 if (starting == 0 && ++retry_count <= 3)
2970 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00002971 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002972 // In rare cases, autocommands may have altered Rows or Columns,
2973 // jump back to check if we need to allocate the screen again.
Bram Moolenaar87e817c2009-02-22 20:13:39 +00002974 goto retry;
2975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976}
2977
2978 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002979free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002980{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002981 int i;
2982
Bram Moolenaar33796b32019-06-08 16:01:13 +02002983 VIM_CLEAR(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002984 for (i = 0; i < Screen_mco; ++i)
Bram Moolenaar33796b32019-06-08 16:01:13 +02002985 VIM_CLEAR(ScreenLinesC[i]);
2986 VIM_CLEAR(ScreenLines2);
2987 VIM_CLEAR(ScreenLines);
2988 VIM_CLEAR(ScreenAttrs);
Bram Moolenaarb9081882022-07-09 04:56:24 +01002989 VIM_CLEAR(ScreenCols);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002990 VIM_CLEAR(LineOffset);
2991 VIM_CLEAR(LineWraps);
2992 VIM_CLEAR(TabPageIdxs);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002993#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002994 VIM_CLEAR(popup_mask);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002995 VIM_CLEAR(popup_mask_next);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002996 VIM_CLEAR(popup_transparent);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002997#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002998}
2999
Bram Moolenaarb13d3402022-08-29 13:44:28 +01003000/*
3001 * Clear the screen.
3002 * May delay if there is something the user should read.
3003 * Allocated the screen for resizing if needed.
Bram Moolenaara4e0b972022-10-01 19:43:52 +01003004 * Returns TRUE when the screen was actually cleared, FALSE if all display
Bram Moolenaar838b7462022-09-26 15:19:56 +01003005 * cells were marked for updating.
Bram Moolenaarb13d3402022-08-29 13:44:28 +01003006 */
Bram Moolenaar838b7462022-09-26 15:19:56 +01003007 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003008screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009{
3010 check_for_delay(FALSE);
Bram Moolenaar838b7462022-09-26 15:19:56 +01003011 screenalloc(FALSE); // allocate screen buffers if size changed
3012 return screenclear2(TRUE); // clear the screen
Bram Moolenaarb13d3402022-08-29 13:44:28 +01003013}
3014
3015/*
3016 * Do not clear the screen but mark everything for redraw.
3017 */
3018 void
3019redraw_as_cleared(void)
3020{
3021 screenclear2(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022}
3023
Bram Moolenaar838b7462022-09-26 15:19:56 +01003024 static int
Bram Moolenaarb13d3402022-08-29 13:44:28 +01003025screenclear2(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026{
3027 int i;
Bram Moolenaar838b7462022-09-26 15:19:56 +01003028 int did_clear = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029
3030 if (starting == NO_SCREEN || ScreenLines == NULL
3031#ifdef FEAT_GUI
3032 || (gui.in_use && gui.starting)
3033#endif
3034 )
Bram Moolenaar838b7462022-09-26 15:19:56 +01003035 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036
3037#ifdef FEAT_GUI
3038 if (!gui.in_use)
3039#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003040 screen_attr = -1; // force setting the Normal colors
3041 screen_stop_highlight(); // don't want highlighting here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042
3043#ifdef FEAT_CLIPBOARD
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003044 // disable selection without redrawing it
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 clip_scroll_selection(9999);
3046#endif
3047
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003048 // blank out ScreenLines
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 for (i = 0; i < Rows; ++i)
3050 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003051 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 LineWraps[i] = FALSE;
3053 }
3054
Bram Moolenaarb13d3402022-08-29 13:44:28 +01003055 if (doclear && can_clear(T_CL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003057 out_str(T_CL); // clear the display
Bram Moolenaar838b7462022-09-26 15:19:56 +01003058 did_clear = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003060 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 }
3062 else
3063 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003064 // can't clear the screen, mark all chars with invalid attributes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 for (i = 0; i < Rows; ++i)
3066 lineinvalid(LineOffset[i], (int)Columns);
3067 clear_cmdline = TRUE;
3068 }
3069
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003070 screen_cleared = TRUE; // can use contents of ScreenLines now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071
Bram Moolenaarb13d3402022-08-29 13:44:28 +01003072 win_rest_invalid(firstwin); // redraw all regular windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003074 redraw_tabline = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003075 if (must_redraw == UPD_CLEAR) // no need to clear again
3076 must_redraw = UPD_NOT_VALID;
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01003077 msg_scrolled = 0; // compute_cmdrow() uses this
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 compute_cmdrow();
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01003079#ifdef FEAT_PROP_POPUP
3080 popup_redraw_all(); // redraw all popup windows
3081#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003082 msg_row = cmdline_row; // put cursor on last line for messages
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 msg_col = 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003084 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 msg_didany = FALSE;
3086 msg_didout = FALSE;
Bram Moolenaar838b7462022-09-26 15:19:56 +01003087
3088 return did_clear;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089}
3090
3091/*
3092 * Clear one line in ScreenLines.
3093 */
3094 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003095lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096{
3097 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 if (enc_utf8)
3099 (void)vim_memset(ScreenLinesUC + off, 0,
3100 (size_t)width * sizeof(u8char_T));
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003101 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaarb9081882022-07-09 04:56:24 +01003102 (void)vim_memset(ScreenCols + off, -1, (size_t)width * sizeof(colnr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103}
3104
3105/*
3106 * Mark one line in ScreenLines invalid by setting the attributes to an
3107 * invalid value.
3108 */
3109 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003110lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111{
3112 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
Bram Moolenaarb9081882022-07-09 04:56:24 +01003113 (void)vim_memset(ScreenCols + off, -1, (size_t)width * sizeof(colnr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114}
3115
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116/*
Bram Moolenaar96916ac2020-07-08 23:09:28 +02003117 * To be called when characters were sent to the terminal directly, outputting
3118 * test on "screen_lnum".
3119 */
3120 void
3121line_was_clobbered(int screen_lnum)
3122{
3123 lineinvalid(LineOffset[screen_lnum], (int)Columns);
3124}
3125
3126/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 * Copy part of a Screenline for vertically split window "wp".
3128 */
3129 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003130linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131{
3132 unsigned off_to = LineOffset[to] + wp->w_wincol;
3133 unsigned off_from = LineOffset[from] + wp->w_wincol;
3134
3135 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
3136 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 if (enc_utf8)
3138 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003139 int i;
3140
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
3142 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003143 for (i = 0; i < p_mco; ++i)
3144 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
3145 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 }
3147 if (enc_dbcs == DBCS_JPNU)
3148 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
3149 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
3151 wp->w_width * sizeof(sattr_T));
Bram Moolenaarb9081882022-07-09 04:56:24 +01003152 mch_memmove(ScreenCols + off_to, ScreenCols + off_from,
3153 wp->w_width * sizeof(colnr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155
3156/*
3157 * Return TRUE if clearing with term string "p" would work.
3158 * It can't work when the string is empty or it won't set the right background.
Bram Moolenaar33796b32019-06-08 16:01:13 +02003159 * Don't clear to end-of-line when there are popups, it may cause flicker.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 */
3161 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003162can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163{
3164 return (*p != NUL && (t_colors <= 1
3165#ifdef FEAT_GUI
3166 || gui.in_use
3167#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003168#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003169 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02003170 || (!p_tgc && cterm_normal_bg_color == 0)
3171#else
3172 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003173#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02003174 || *T_UT != NUL)
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003175#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02003176 && !(p == T_CE && popup_visible)
3177#endif
3178 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179}
3180
3181/*
3182 * Reset cursor position. Use whenever cursor was moved because of outputting
3183 * something directly to the screen (shell commands) or a terminal control
3184 * code.
3185 */
3186 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003187screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188{
3189 screen_cur_row = screen_cur_col = 9999;
3190}
3191
3192/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 * Move the cursor to position "row","col" in the screen.
3194 * This tries to find the most efficient way to move, minimizing the number of
3195 * characters sent to the terminal.
3196 */
3197 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003198windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003200 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 int i;
3202 int plan;
3203 int cost;
3204 int wouldbe_col;
3205 int noinvcurs;
3206 char_u *bs;
3207 int goto_cost;
3208 int attr;
3209
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003210#define GOTO_COST 7 // assume a term_windgoto() takes about 7 chars
3211#define HIGHL_COST 5 // assume unhighlight takes 5 chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212
3213#define PLAN_LE 1
3214#define PLAN_CR 2
3215#define PLAN_NL 3
3216#define PLAN_WRITE 4
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003217 // Can't use ScreenLines unless initialized
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 if (ScreenLines == NULL)
3219 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 if (col != screen_cur_col || row != screen_cur_row)
3221 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003222 // Check for valid position.
3223 if (row < 0) // window without text lines?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 row = 0;
3225 if (row >= screen_Rows)
3226 row = screen_Rows - 1;
3227 if (col >= screen_Columns)
3228 col = screen_Columns - 1;
3229
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003230 // check if no cursor movement is allowed in highlight mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 if (screen_attr && *T_MS == NUL)
3232 noinvcurs = HIGHL_COST;
3233 else
3234 noinvcurs = 0;
3235 goto_cost = GOTO_COST + noinvcurs;
3236
3237 /*
3238 * Plan how to do the positioning:
3239 * 1. Use CR to move it to column 0, same row.
3240 * 2. Use T_LE to move it a few columns to the left.
3241 * 3. Use NL to move a few lines down, column 0.
3242 * 4. Move a few columns to the right with T_ND or by writing chars.
3243 *
3244 * Don't do this if the cursor went beyond the last column, the cursor
3245 * position is unknown then (some terminals wrap, some don't )
3246 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00003247 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 * characters to move the cursor to the right.
3249 */
3250 if (row >= screen_cur_row && screen_cur_col < Columns)
3251 {
3252 /*
3253 * If the cursor is in the same row, bigger col, we can use CR
3254 * or T_LE.
3255 */
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003256 bs = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 attr = screen_attr;
3258 if (row == screen_cur_row && col < screen_cur_col)
3259 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003260 // "le" is preferred over "bc", because "bc" is obsolete
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 if (*T_LE)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003262 bs = T_LE; // "cursor left"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003264 bs = T_BC; // "backspace character (old)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 if (*bs)
3266 cost = (screen_cur_col - col) * (int)STRLEN(bs);
3267 else
3268 cost = 999;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003269 if (col + 1 < cost) // using CR is less characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 {
3271 plan = PLAN_CR;
3272 wouldbe_col = 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003273 cost = 1; // CR is just one character
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 }
3275 else
3276 {
3277 plan = PLAN_LE;
3278 wouldbe_col = col;
3279 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003280 if (noinvcurs) // will stop highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 {
3282 cost += noinvcurs;
3283 attr = 0;
3284 }
3285 }
3286
3287 /*
3288 * If the cursor is above where we want to be, we can use CR LF.
3289 */
3290 else if (row > screen_cur_row)
3291 {
3292 plan = PLAN_NL;
3293 wouldbe_col = 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003294 cost = (row - screen_cur_row) * 2; // CR LF
3295 if (noinvcurs) // will stop highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 {
3297 cost += noinvcurs;
3298 attr = 0;
3299 }
3300 }
3301
3302 /*
3303 * If the cursor is in the same row, smaller col, just use write.
3304 */
3305 else
3306 {
3307 plan = PLAN_WRITE;
3308 wouldbe_col = screen_cur_col;
3309 cost = 0;
3310 }
3311
3312 /*
3313 * Check if any characters that need to be written have the
3314 * correct attributes. Also avoid UTF-8 characters.
3315 */
3316 i = col - wouldbe_col;
3317 if (i > 0)
3318 cost += i;
3319 if (cost < goto_cost && i > 0)
3320 {
3321 /*
3322 * Check if the attributes are correct without additionally
3323 * stopping highlighting.
3324 */
3325 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
3326 while (i && *p++ == attr)
3327 --i;
3328 if (i != 0)
3329 {
3330 /*
3331 * Try if it works when highlighting is stopped here.
3332 */
3333 if (*--p == 0)
3334 {
3335 cost += noinvcurs;
3336 while (i && *p++ == 0)
3337 --i;
3338 }
3339 if (i != 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003340 cost = 999; // different attributes, don't do it
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 if (enc_utf8)
3343 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003344 // Don't use an UTF-8 char for positioning, it's slow.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 for (i = wouldbe_col; i < col; ++i)
3346 if (ScreenLinesUC[LineOffset[row] + i] != 0)
3347 {
3348 cost = 999;
3349 break;
3350 }
3351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 }
3353
3354 /*
3355 * We can do it without term_windgoto()!
3356 */
3357 if (cost < goto_cost)
3358 {
3359 if (plan == PLAN_LE)
3360 {
3361 if (noinvcurs)
3362 screen_stop_highlight();
3363 while (screen_cur_col > col)
3364 {
3365 out_str(bs);
3366 --screen_cur_col;
3367 }
3368 }
3369 else if (plan == PLAN_CR)
3370 {
3371 if (noinvcurs)
3372 screen_stop_highlight();
3373 out_char('\r');
3374 screen_cur_col = 0;
3375 }
3376 else if (plan == PLAN_NL)
3377 {
3378 if (noinvcurs)
3379 screen_stop_highlight();
3380 while (screen_cur_row < row)
3381 {
3382 out_char('\n');
3383 ++screen_cur_row;
3384 }
3385 screen_cur_col = 0;
3386 }
3387
3388 i = col - screen_cur_col;
3389 if (i > 0)
3390 {
3391 /*
3392 * Use cursor-right if it's one character only. Avoids
3393 * removing a line of pixels from the last bold char, when
3394 * using the bold trick in the GUI.
3395 */
3396 if (T_ND[0] != NUL && T_ND[1] == NUL)
3397 {
3398 while (i-- > 0)
3399 out_char(*T_ND);
3400 }
3401 else
3402 {
3403 int off;
3404
3405 off = LineOffset[row] + screen_cur_col;
3406 while (i-- > 0)
3407 {
3408 if (ScreenAttrs[off] != screen_attr)
3409 screen_stop_highlight();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 if (enc_dbcs == DBCS_JPNU
3413 && ScreenLines[off] == 0x8e)
3414 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 ++off;
3416 }
3417 }
3418 }
3419 }
3420 }
3421 else
3422 cost = 999;
3423
3424 if (cost >= goto_cost)
3425 {
3426 if (noinvcurs)
3427 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02003428 if (row == screen_cur_row && (col > screen_cur_col)
3429 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 term_cursor_right(col - screen_cur_col);
3431 else
3432 term_windgoto(row, col);
3433 }
3434 screen_cur_row = row;
3435 screen_cur_col = col;
3436 }
3437}
3438
3439/*
3440 * Set cursor to its position in the current window.
3441 */
3442 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003443setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444{
Bram Moolenaar987723e2018-03-06 11:43:04 +01003445 setcursor_mayforce(FALSE);
3446}
3447
3448/*
3449 * Set cursor to its position in the current window.
3450 * When "force" is TRUE also when not redrawing.
3451 */
3452 void
3453setcursor_mayforce(int force)
3454{
3455 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 {
3457 validate_cursor();
3458 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02003459 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460#ifdef FEAT_RIGHTLEFT
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003461 // With 'rightleft' set and the cursor on a double-wide
3462 // character, position it on the leftmost column.
Bram Moolenaara12a1612019-01-24 16:39:02 +01003463 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol
3464 - ((has_mbyte
Bram Moolenaar561f9db2008-02-20 13:16:29 +00003465 && (*mb_ptr2cells)(ml_get_cursor()) == 2
Bram Moolenaara12a1612019-01-24 16:39:02 +01003466 && vim_isprintc(gchar_cursor())) ? 2 : 1)) :
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467#endif
3468 curwin->w_wcol));
3469 }
3470}
3471
3472
3473/*
Bram Moolenaar86033562017-07-12 20:24:41 +02003474 * Insert 'line_count' lines at 'row' in window 'wp'.
3475 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
3476 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 * scrolling.
3478 * Returns FAIL if the lines are not inserted, OK for success.
3479 */
3480 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003481win_ins_lines(
3482 win_T *wp,
3483 int row,
3484 int line_count,
3485 int invalid,
3486 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487{
3488 int did_delete;
3489 int nextrow;
3490 int lastrow;
3491 int retval;
3492
3493 if (invalid)
3494 wp->w_lines_valid = 0;
3495
Bram Moolenaarc856ceb2022-06-21 18:10:39 +01003496 // with only a few lines it's not worth the effort
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 if (wp->w_height < 5)
3498 return FAIL;
3499
Bram Moolenaarc856ceb2022-06-21 18:10:39 +01003500 // with the popup menu visible this might not work correctly
3501 if (pum_visible())
3502 return FAIL;
3503
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 if (line_count > wp->w_height - row)
3505 line_count = wp->w_height - row;
3506
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003507 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 if (retval != MAYBE)
3509 return retval;
3510
3511 /*
3512 * If there is a next window or a status line, we first try to delete the
3513 * lines at the bottom to avoid messing what is after the window.
Bram Moolenaarc363fe12019-08-04 18:13:46 +02003514 * If this fails and there are following windows, don't do anything to
3515 * avoid messing up those windows, better just redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 */
3517 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 if (wp->w_next != NULL || wp->w_status_height)
3519 {
3520 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003521 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 did_delete = TRUE;
3523 else if (wp->w_next)
3524 return FAIL;
3525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 /*
3527 * if no lines deleted, blank the lines that will end up below the window
3528 */
3529 if (!did_delete)
3530 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003533 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 lastrow = nextrow + line_count;
3535 if (lastrow > Rows)
3536 lastrow = Rows;
3537 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02003538 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 ' ', ' ', 0);
3540 }
3541
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003542 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 == FAIL)
3544 {
Bram Moolenaarc363fe12019-08-04 18:13:46 +02003545 // deletion will have messed up other windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 if (did_delete)
3547 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 win_rest_invalid(W_NEXT(wp));
3550 }
3551 return FAIL;
3552 }
3553
3554 return OK;
3555}
3556
3557/*
Bram Moolenaar86033562017-07-12 20:24:41 +02003558 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
3560 * If "mayclear" is TRUE the screen will be cleared if it is faster than
3561 * scrolling
3562 * Return OK for success, FAIL if the lines are not deleted.
3563 */
3564 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003565win_del_lines(
3566 win_T *wp,
3567 int row,
3568 int line_count,
3569 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003570 int mayclear,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003571 int clear_attr) // for clearing lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572{
3573 int retval;
3574
3575 if (invalid)
3576 wp->w_lines_valid = 0;
3577
3578 if (line_count > wp->w_height - row)
3579 line_count = wp->w_height - row;
3580
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003581 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 if (retval != MAYBE)
3583 return retval;
3584
3585 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003586 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 return FAIL;
3588
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 /*
3590 * If there are windows or status lines below, try to put them at the
3591 * correct place. If we can't do that, they have to be redrawn.
3592 */
3593 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
3594 {
3595 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003596 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 {
3598 wp->w_redr_status = TRUE;
3599 win_rest_invalid(wp->w_next);
3600 }
3601 }
3602 /*
3603 * If this is the last window and there is no status line, redraw the
3604 * command line later.
3605 */
3606 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 redraw_cmdline = TRUE;
3608 return OK;
3609}
3610
3611/*
3612 * Common code for win_ins_lines() and win_del_lines().
3613 * Returns OK or FAIL when the work has been done.
3614 * Returns MAYBE when not finished yet.
3615 */
3616 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003617win_do_lines(
3618 win_T *wp,
3619 int row,
3620 int line_count,
3621 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003622 int del,
3623 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624{
3625 int retval;
3626
3627 if (!redrawing() || line_count <= 0)
3628 return FAIL;
3629
Bram Moolenaar33796b32019-06-08 16:01:13 +02003630 // When inserting lines would result in loss of command output, just redraw
3631 // the lines.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003632 if (no_win_do_lines_ins && !del)
3633 return FAIL;
3634
Bram Moolenaar33796b32019-06-08 16:01:13 +02003635 // only a few lines left: redraw is faster
Bram Moolenaar4033c552017-09-16 20:54:51 +02003636 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003638 if (!no_win_do_lines_ins)
Bram Moolenaar33796b32019-06-08 16:01:13 +02003639 screenclear(); // will set wp->w_lines_valid to 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 return FAIL;
3641 }
3642
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003643#ifdef FEAT_PROP_POPUP
Bram Moolenaar4c063a02019-06-10 21:24:12 +02003644 // this doesn't work when there are popups visible
Bram Moolenaar33796b32019-06-08 16:01:13 +02003645 if (popup_visible)
3646 return FAIL;
3647#endif
3648
3649 // Delete all remaining lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 if (row + line_count >= wp->w_height)
3651 {
3652 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02003653 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 ' ', ' ', 0);
3655 return OK;
3656 }
3657
3658 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003659 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003661 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003663 if (!no_win_do_lines_ins)
3664 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665
3666 /*
3667 * If the terminal can set a scroll region, use that.
3668 * Always do this in a vertically split window. This will redraw from
3669 * ScreenLines[] when t_CV isn't defined. That's faster than using
3670 * win_line().
3671 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003672 * a character in the lower right corner of the scroll region may cause a
3673 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 */
Bram Moolenaar02631462017-09-22 15:20:32 +02003675 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 scroll_region_set(wp, row);
3679 if (del)
3680 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003681 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 else
3683 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003684 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 scroll_region_reset();
3687 return retval;
3688 }
3689
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003690 if (wp->w_next != NULL && p_tf) // don't delete/insert on fast terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692
3693 return MAYBE;
3694}
3695
3696/*
3697 * window 'wp' and everything after it is messed up, mark it for redraw
3698 */
3699 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003700win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003704 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 wp->w_redr_status = TRUE;
3706 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 }
3708 redraw_cmdline = TRUE;
3709}
3710
3711/*
3712 * The rest of the routines in this file perform screen manipulations. The
3713 * given operation is performed physically on the screen. The corresponding
3714 * change is also made to the internal screen image. In this way, the editor
3715 * anticipates the effect of editing changes on the appearance of the screen.
3716 * That way, when we call screenupdate a complete redraw isn't usually
3717 * necessary. Another advantage is that we can keep adding code to anticipate
3718 * screen changes, and in the meantime, everything still works.
3719 */
3720
3721/*
3722 * types for inserting or deleting lines
3723 */
3724#define USE_T_CAL 1
3725#define USE_T_CDL 2
3726#define USE_T_AL 3
3727#define USE_T_CE 4
3728#define USE_T_DL 5
3729#define USE_T_SR 6
3730#define USE_NL 7
3731#define USE_T_CD 8
3732#define USE_REDRAW 9
3733
3734/*
3735 * insert lines on the screen and update ScreenLines[]
Bram Moolenaar17fa2332022-04-01 19:44:47 +01003736 * "end" is the line after the scrolled part. Normally it is Rows.
3737 * When scrolling region used "off" is the offset from the top for the region.
3738 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 *
3740 * return FAIL for failure, OK for success.
3741 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003742 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003743screen_ins_lines(
3744 int off,
3745 int row,
3746 int line_count,
3747 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003748 int clear_attr,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003749 win_T *wp) // NULL or window to use width from
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750{
3751 int i;
3752 int j;
3753 unsigned temp;
3754 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003755 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 int type;
3757 int result_empty;
3758 int can_ce = can_clear(T_CE);
3759
3760 /*
3761 * FAIL if
3762 * - there is no valid screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 * - the line count is less than one
3764 * - the line count is more than 'ttyscroll'
Bram Moolenaar17fa2332022-04-01 19:44:47 +01003765 * - "end" is more than "Rows" (safety check, should not happen)
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003766 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar33796b32019-06-08 16:01:13 +02003767 * - there is a popup window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 */
Bram Moolenaar33796b32019-06-08 16:01:13 +02003769 if (!screen_valid(TRUE)
3770 || line_count <= 0 || line_count > p_ttyscroll
Bram Moolenaar17fa2332022-04-01 19:44:47 +01003771 || end > Rows
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003772#ifdef FEAT_CLIPBOARD
3773 || (clip_star.state != SELECT_CLEARED
3774 && redrawing_for_callback > 0)
3775#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003776#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02003777 || popup_visible
3778#endif
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003779 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 return FAIL;
3781
3782 /*
3783 * There are seven ways to insert lines:
3784 * 0. When in a vertically split window and t_CV isn't set, redraw the
3785 * characters from ScreenLines[].
3786 * 1. Use T_CD (clear to end of display) if it exists and the result of
3787 * the insert is just empty lines
3788 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
3789 * present or line_count > 1. It looks better if we do all the inserts
3790 * at once.
3791 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
3792 * insert is just empty lines and T_CE is not present or line_count >
3793 * 1.
3794 * 4. Use T_AL (insert line) if it exists.
3795 * 5. Use T_CE (erase line) if it exists and the result of the insert is
3796 * just empty lines.
3797 * 6. Use T_DL (delete line) if it exists and the result of the insert is
3798 * just empty lines.
3799 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
3800 * the 'da' flag is not set or we have clear line capability.
3801 * 8. redraw the characters from ScreenLines[].
3802 *
3803 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
3804 * the scrollbar for the window. It does have insert line, use that if it
3805 * exists.
3806 */
3807 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
Bram Moolenaar8ef69972022-04-03 13:23:22 +01003809 {
3810 // Avoid that lines are first cleared here and then redrawn, which
3811 // results in many characters updated twice. This happens with CTRL-F
3812 // in a vertically split window. With line-by-line scrolling
3813 // USE_REDRAW should be faster.
3814 if (line_count > 3)
3815 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 type = USE_REDRAW;
Bram Moolenaar8ef69972022-04-03 13:23:22 +01003817 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02003818 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 type = USE_T_CD;
3820 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
3821 type = USE_T_CAL;
3822 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
3823 type = USE_T_CDL;
3824 else if (*T_AL != NUL)
3825 type = USE_T_AL;
3826 else if (can_ce && result_empty)
3827 type = USE_T_CE;
3828 else if (*T_DL != NUL && result_empty)
3829 type = USE_T_DL;
3830 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
3831 type = USE_T_SR;
3832 else
3833 return FAIL;
3834
3835 /*
3836 * For clearing the lines screen_del_lines() is used. This will also take
3837 * care of t_db if necessary.
3838 */
3839 if (type == USE_T_CD || type == USE_T_CDL ||
3840 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003841 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842
3843 /*
3844 * If text is retained below the screen, first clear or delete as many
3845 * lines at the bottom of the window as are about to be inserted so that
3846 * the deleted lines won't later surface during a screen_del_lines.
3847 */
3848 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003849 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850
3851#ifdef FEAT_CLIPBOARD
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003852 // Remove a modeless selection when inserting lines halfway the screen
3853 // or not the full width of the screen.
Bram Moolenaar4033c552017-09-16 20:54:51 +02003854 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003855 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 else
3857 clip_scroll_selection(-line_count);
3858#endif
3859
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003860#ifdef FEAT_GUI_HAIKU
3861 vim_lock_screen();
3862#endif
3863
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864#ifdef FEAT_GUI
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003865 // Don't update the GUI cursor here, ScreenLines[] is invalid until the
3866 // scrolling is actually carried out.
Bram Moolenaar107abd22016-08-12 14:08:25 +02003867 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868#endif
3869
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003870 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
3871 cursor_col = wp->w_wincol;
3872
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003873 if (*T_CCS != NUL) // cursor relative to region
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 cursor_row = row;
3875 else
3876 cursor_row = row + off;
3877
3878 /*
3879 * Shift LineOffset[] line_count down to reflect the inserted lines.
3880 * Clear the inserted lines in ScreenLines[].
3881 */
3882 row += off;
3883 end += off;
3884 for (i = 0; i < line_count; ++i)
3885 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 if (wp != NULL && wp->w_width != Columns)
3887 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003888 // need to copy part of a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 j = end - 1 - i;
3890 while ((j -= line_count) >= row)
3891 linecopy(j + line_count, j, wp);
3892 j += line_count;
3893 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003894 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
3895 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 else
3897 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
3898 LineWraps[j] = FALSE;
3899 }
3900 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 {
3902 j = end - 1 - i;
3903 temp = LineOffset[j];
3904 while ((j -= line_count) >= row)
3905 {
3906 LineOffset[j + line_count] = LineOffset[j];
3907 LineWraps[j + line_count] = LineWraps[j];
3908 }
3909 LineOffset[j + line_count] = temp;
3910 LineWraps[j + line_count] = FALSE;
3911 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003912 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 else
3914 lineinvalid(temp, (int)Columns);
3915 }
3916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003918#ifdef FEAT_GUI_HAIKU
3919 vim_unlock_screen();
3920#endif
3921
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003923 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003924 if (clear_attr != 0)
3925 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003927 // redraw the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 if (type == USE_REDRAW)
3929 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02003930 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 {
3932 term_append_lines(line_count);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003933 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 }
3935 else
3936 {
3937 for (i = 0; i < line_count; i++)
3938 {
3939 if (type == USE_T_AL)
3940 {
3941 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003942 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 out_str(T_AL);
3944 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003945 else // type == USE_T_SR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 out_str(T_SR);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003947 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 }
3949 }
3950
3951 /*
3952 * With scroll-reverse and 'da' flag set we need to clear the lines that
3953 * have been scrolled down into the region.
3954 */
3955 if (type == USE_T_SR && *T_DA)
3956 {
3957 for (i = 0; i < line_count; ++i)
3958 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003959 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 out_str(T_CE);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003961 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 }
3963 }
3964
3965#ifdef FEAT_GUI
3966 gui_can_update_cursor();
3967 if (gui.in_use)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003968 out_flush(); // always flush after a scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969#endif
3970 return OK;
3971}
3972
3973/*
Bram Moolenaar107abd22016-08-12 14:08:25 +02003974 * Delete lines on the screen and update ScreenLines[].
3975 * "end" is the line after the scrolled part. Normally it is Rows.
3976 * When scrolling region used "off" is the offset from the top for the region.
3977 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 *
3979 * Return OK for success, FAIL if the lines are not deleted.
3980 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003982screen_del_lines(
3983 int off,
3984 int row,
3985 int line_count,
3986 int end,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003987 int force, // even when line_count > p_ttyscroll
3988 int clear_attr, // used for clearing lines
Bram Moolenaar8ef69972022-04-03 13:23:22 +01003989 win_T *wp) // NULL or window to use width from
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990{
3991 int j;
3992 int i;
3993 unsigned temp;
3994 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003995 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 int cursor_end;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003997 int result_empty; // result is empty until end of region
3998 int can_delete; // deleting line codes can be used
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 int type;
4000
4001 /*
4002 * FAIL if
4003 * - there is no valid screen
4004 * - the screen has to be redrawn completely
4005 * - the line count is less than one
4006 * - the line count is more than 'ttyscroll'
Bram Moolenaar17fa2332022-04-01 19:44:47 +01004007 * - "end" is more than "Rows" (safety check, should not happen)
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02004008 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 */
Bram Moolenaar17fa2332022-04-01 19:44:47 +01004010 if (!screen_valid(TRUE)
4011 || line_count <= 0
4012 || (!force && line_count > p_ttyscroll)
4013 || end > Rows
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02004014#ifdef FEAT_CLIPBOARD
Bram Moolenaar17fa2332022-04-01 19:44:47 +01004015 || (clip_star.state != SELECT_CLEARED && redrawing_for_callback > 0)
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02004016#endif
4017 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 return FAIL;
4019
4020 /*
4021 * Check if the rest of the current region will become empty.
4022 */
4023 result_empty = row + line_count >= end;
4024
4025 /*
4026 * We can delete lines only when 'db' flag not set or when 'ce' option
4027 * available.
4028 */
4029 can_delete = (*T_DB == NUL || can_clear(T_CE));
4030
4031 /*
4032 * There are six ways to delete lines:
4033 * 0. When in a vertically split window and t_CV isn't set, redraw the
4034 * characters from ScreenLines[].
4035 * 1. Use T_CD if it exists and the result is empty.
4036 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
4037 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
4038 * none of the other ways work.
4039 * 4. Use T_CE (erase line) if the result is empty.
4040 * 5. Use T_DL (delete line) if it exists.
4041 * 6. redraw the characters from ScreenLines[].
4042 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
Bram Moolenaar8ef69972022-04-03 13:23:22 +01004044 {
4045 // Avoid that lines are first cleared here and then redrawn, which
4046 // results in many characters updated twice. This happens with CTRL-F
4047 // in a vertically split window. With line-by-line scrolling
4048 // USE_REDRAW should be faster.
4049 if (line_count > 3)
4050 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 type = USE_REDRAW;
Bram Moolenaar8ef69972022-04-03 13:23:22 +01004052 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02004053 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 type = USE_T_CD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 else if (row == 0 && (
4056#ifndef AMIGA
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004057 // On the Amiga, somehow '\n' on the last line doesn't always scroll
4058 // up, so use delete-line command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 line_count == 1 ||
4060#endif
4061 *T_CDL == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 type = USE_NL;
4063 else if (*T_CDL != NUL && line_count > 1 && can_delete)
4064 type = USE_T_CDL;
4065 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +02004066 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 type = USE_T_CE;
4068 else if (*T_DL != NUL && can_delete)
4069 type = USE_T_DL;
4070 else if (*T_CDL != NUL && can_delete)
4071 type = USE_T_CDL;
4072 else
4073 return FAIL;
4074
4075#ifdef FEAT_CLIPBOARD
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004076 // Remove a modeless selection when deleting lines halfway the screen or
4077 // not the full width of the screen.
Bram Moolenaar4033c552017-09-16 20:54:51 +02004078 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02004079 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 else
4081 clip_scroll_selection(line_count);
4082#endif
4083
Bram Moolenaar92c461e2020-04-24 22:19:00 +02004084#ifdef FEAT_GUI_HAIKU
4085 vim_lock_screen();
4086#endif
4087
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088#ifdef FEAT_GUI
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004089 // Don't update the GUI cursor here, ScreenLines[] is invalid until the
4090 // scrolling is actually carried out.
Bram Moolenaar107abd22016-08-12 14:08:25 +02004091 gui_dont_update_cursor(gui.cursor_row >= row + off
4092 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093#endif
4094
Bram Moolenaarbfa42462018-06-16 16:20:52 +02004095 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
4096 cursor_col = wp->w_wincol;
4097
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004098 if (*T_CCS != NUL) // cursor relative to region
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 {
4100 cursor_row = row;
4101 cursor_end = end;
4102 }
4103 else
4104 {
4105 cursor_row = row + off;
4106 cursor_end = end + off;
4107 }
4108
4109 /*
4110 * Now shift LineOffset[] line_count up to reflect the deleted lines.
4111 * Clear the inserted lines in ScreenLines[].
4112 */
4113 row += off;
4114 end += off;
4115 for (i = 0; i < line_count; ++i)
4116 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 if (wp != NULL && wp->w_width != Columns)
4118 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004119 // need to copy part of a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 j = row + i;
4121 while ((j += line_count) <= end - 1)
4122 linecopy(j - line_count, j, wp);
4123 j -= line_count;
4124 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02004125 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
4126 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 else
4128 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
4129 LineWraps[j] = FALSE;
4130 }
4131 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004133 // whole width, moving the line pointers is faster
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 j = row + i;
4135 temp = LineOffset[j];
4136 while ((j += line_count) <= end - 1)
4137 {
4138 LineOffset[j - line_count] = LineOffset[j];
4139 LineWraps[j - line_count] = LineWraps[j];
4140 }
4141 LineOffset[j - line_count] = temp;
4142 LineWraps[j - line_count] = FALSE;
4143 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02004144 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 else
4146 lineinvalid(temp, (int)Columns);
4147 }
4148 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004150#ifdef FEAT_GUI_HAIKU
4151 vim_unlock_screen();
4152#endif
4153
Bram Moolenaarcfce7172017-08-17 20:31:48 +02004154 if (screen_attr != clear_attr)
4155 screen_stop_highlight();
4156 if (clear_attr != 0)
4157 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004159 // redraw the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 if (type == USE_REDRAW)
4161 redraw_block(row, end, wp);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004162 else if (type == USE_T_CD) // delete the lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02004164 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 out_str(T_CD);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004166 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 }
4168 else if (type == USE_T_CDL)
4169 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02004170 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 term_delete_lines(line_count);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004172 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 }
4174 /*
4175 * Deleting lines at top of the screen or scroll region: Just scroll
4176 * the whole screen (scroll region) up by outputting newlines on the
4177 * last line.
4178 */
4179 else if (type == USE_NL)
4180 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02004181 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 for (i = line_count; --i >= 0; )
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004183 out_char('\n'); // cursor will remain on same line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 }
4185 else
4186 {
4187 for (i = line_count; --i >= 0; )
4188 {
4189 if (type == USE_T_DL)
4190 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02004191 windgoto(cursor_row, cursor_col);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004192 out_str(T_DL); // delete a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004194 else // type == USE_T_CE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02004196 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004197 out_str(T_CE); // erase a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004199 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 }
4201 }
4202
4203 /*
4204 * If the 'db' flag is set, we need to clear the lines that have been
4205 * scrolled up at the bottom of the region.
4206 */
4207 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
4208 {
4209 for (i = line_count; i > 0; --i)
4210 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02004211 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004212 out_str(T_CE); // erase a line
4213 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 }
4215 }
4216
4217#ifdef FEAT_GUI
4218 gui_can_update_cursor();
4219 if (gui.in_use)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004220 out_flush(); // always flush after a scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221#endif
4222
4223 return OK;
4224}
4225
4226/*
Bram Moolenaarcb574f42019-01-25 22:29:57 +01004227 * Return TRUE when postponing displaying the mode message: when not redrawing
4228 * or inside a mapping.
4229 */
4230 int
4231skip_showmode()
4232{
4233 // Call char_avail() only when we are going to show something, because it
Bram Moolenaar944cc9c2022-06-27 22:17:37 +01004234 // takes a bit of time. redrawing() may also call char_avail().
Bram Moolenaarcb574f42019-01-25 22:29:57 +01004235 if (global_busy
4236 || msg_silent != 0
4237 || !redrawing()
4238 || (char_avail() && !KeyTyped))
4239 {
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004240 redraw_mode = TRUE; // show mode later
Bram Moolenaarcb574f42019-01-25 22:29:57 +01004241 return TRUE;
4242 }
4243 return FALSE;
4244}
4245
4246/*
Bram Moolenaar81226e02018-02-20 21:44:45 +01004247 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 *
4249 * If clear_cmdline is TRUE, clear the rest of the cmdline.
4250 * If clear_cmdline is FALSE there may be a message there that needs to be
4251 * cleared only if a mode is shown.
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004252 * If redraw_mode is TRUE show or clear the mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 * Return the length of the message (0 if no message).
4254 */
4255 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004256showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257{
4258 int need_clear;
4259 int length = 0;
4260 int do_mode;
4261 int attr;
4262 int nwr_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 int sub_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264
Bram Moolenaara2a89732022-08-31 14:46:18 +01004265 do_mode = p_smd && msg_silent == 0
Bram Moolenaar24959102022-05-07 20:01:16 +01004266 && ((State & MODE_INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +02004267 || restart_edit != NUL
Bram Moolenaar9198de32022-08-27 21:30:03 +01004268 || VIsual_active);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02004269 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 {
Bram Moolenaarcb574f42019-01-25 22:29:57 +01004271 if (skip_showmode())
4272 return 0; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273
4274 nwr_save = need_wait_return;
4275
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004276 // wait a bit before overwriting an important message
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 check_for_delay(FALSE);
4278
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004279 // if the cmdline is more than one line high, erase top lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 need_clear = clear_cmdline;
4281 if (clear_cmdline && cmdline_row < Rows - 1)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004282 msg_clr_cmdline(); // will reset clear_cmdline
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004284 // Position on the last line in the window, column 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 msg_pos_mode();
4286 cursor_off();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004287 attr = HL_ATTR(HLF_CM); // Highlight mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 if (do_mode)
4289 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004290 msg_puts_attr("--", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00004292 if (
Bram Moolenaar09092152010-08-08 16:38:42 +02004293# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +00004294 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +02004295# else
Bram Moolenaarc236c162008-07-13 17:41:49 +00004296 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +00004297# endif
Bram Moolenaar09092152010-08-08 16:38:42 +02004298 )
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004299# ifdef FEAT_GUI_GTK // most of the time, it's not XIM being used
Bram Moolenaar32526b32019-01-19 17:43:09 +01004300 msg_puts_attr(" IM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301# else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004302 msg_puts_attr(" XIM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303# endif
4304#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004305 // CTRL-X in Insert mode
Bram Moolenaarea389e92014-05-28 21:40:52 +02004306 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004308 // These messages can get long, avoid a wrap in a narrow
4309 // window. Prefer showing edit_submode_extra.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 length = (Rows - msg_row) * Columns - 3;
4311 if (edit_submode_extra != NULL)
4312 length -= vim_strsize(edit_submode_extra);
4313 if (length > 0)
4314 {
4315 if (edit_submode_pre != NULL)
4316 length -= vim_strsize(edit_submode_pre);
4317 if (length - vim_strsize(edit_submode) > 0)
4318 {
4319 if (edit_submode_pre != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004320 msg_puts_attr((char *)edit_submode_pre, attr);
4321 msg_puts_attr((char *)edit_submode, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 }
4323 if (edit_submode_extra != NULL)
4324 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004325 msg_puts_attr(" ", attr); // add a space in between
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004327 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 else
4329 sub_attr = attr;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004330 msg_puts_attr((char *)edit_submode_extra, sub_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 }
4332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 }
4334 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 if (State & VREPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004337 msg_puts_attr(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02004338 else if (State & REPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004339 msg_puts_attr(_(" REPLACE"), attr);
Bram Moolenaar24959102022-05-07 20:01:16 +01004340 else if (State & MODE_INSERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 {
4342#ifdef FEAT_RIGHTLEFT
4343 if (p_ri)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004344 msg_puts_attr(_(" REVERSE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01004346 msg_puts_attr(_(" INSERT"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01004348 else if (restart_edit == 'I' || restart_edit == 'i' ||
4349 restart_edit == 'a' || restart_edit == 'A')
Bram Moolenaar32526b32019-01-19 17:43:09 +01004350 msg_puts_attr(_(" (insert)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 else if (restart_edit == 'R')
Bram Moolenaar32526b32019-01-19 17:43:09 +01004352 msg_puts_attr(_(" (replace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 else if (restart_edit == 'V')
Bram Moolenaar32526b32019-01-19 17:43:09 +01004354 msg_puts_attr(_(" (vreplace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355#ifdef FEAT_RIGHTLEFT
4356 if (p_hkmap)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004357 msg_puts_attr(_(" Hebrew"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358#endif
4359#ifdef FEAT_KEYMAP
Bram Moolenaar24959102022-05-07 20:01:16 +01004360 if (State & MODE_LANGMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 {
4362# ifdef FEAT_ARABIC
4363 if (curwin->w_p_arab)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004364 msg_puts_attr(_(" Arabic"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 else
4366# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02004367 if (get_keymap_str(curwin, (char_u *)" (%s)",
4368 NameBuff, MAXPATHL))
Bram Moolenaar32526b32019-01-19 17:43:09 +01004369 msg_puts_attr((char *)NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 }
4371#endif
Bram Moolenaar24959102022-05-07 20:01:16 +01004372 if ((State & MODE_INSERT) && p_paste)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004373 msg_puts_attr(_(" (paste)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 if (VIsual_active)
4376 {
4377 char *p;
4378
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004379 // Don't concatenate separate words to avoid translation
4380 // problems.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 switch ((VIsual_select ? 4 : 0)
4382 + (VIsual_mode == Ctrl_V) * 2
4383 + (VIsual_mode == 'V'))
4384 {
4385 case 0: p = N_(" VISUAL"); break;
4386 case 1: p = N_(" VISUAL LINE"); break;
4387 case 2: p = N_(" VISUAL BLOCK"); break;
4388 case 4: p = N_(" SELECT"); break;
4389 case 5: p = N_(" SELECT LINE"); break;
4390 default: p = N_(" SELECT BLOCK"); break;
4391 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01004392 msg_puts_attr(_(p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01004394 msg_puts_attr(" --", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004396
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 need_clear = TRUE;
4398 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02004399 if (reg_recording != 0
Bram Moolenaare2c453d2019-08-21 14:37:09 +02004400 && edit_submode == NULL) // otherwise it gets too long
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004402 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 need_clear = TRUE;
4404 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004405
4406 mode_displayed = TRUE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004407 if (need_clear || clear_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 msg_clr_eos();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004409 msg_didout = FALSE; // overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 length = msg_col;
4411 msg_col = 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004412 need_wait_return = nwr_save; // never ask for hit-return for this
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 }
4414 else if (clear_cmdline && msg_silent == 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004415 // Clear the whole command line. Will reset "clear_cmdline".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 msg_clr_cmdline();
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004417 else if (redraw_mode)
4418 {
4419 msg_pos_mode();
4420 msg_clr_eos();
4421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004423 // In Visual mode the size of the selected area must be redrawn.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 if (VIsual_active)
4425 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004427 // If the last window has no status line, the ruler is after the mode
4428 // message and must be redrawn
Bram Moolenaar4033c552017-09-16 20:54:51 +02004429 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +02004430 win_redr_ruler(lastwin, TRUE, FALSE);
Martin Tournoijba43e762022-10-13 22:12:15 +01004431
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 redraw_cmdline = FALSE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004433 redraw_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 clear_cmdline = FALSE;
4435
4436 return length;
4437}
4438
4439/*
4440 * Position for a mode message.
4441 */
4442 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004443msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444{
4445 msg_col = 0;
4446 msg_row = Rows - 1;
4447}
4448
4449/*
4450 * Delete mode message. Used when ESC is typed which is expected to end
4451 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004452 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 */
4454 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004455unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456{
4457 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +01004458 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 */
4460 if (!redrawing() || (!force && char_avail() && !KeyTyped))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004461 redraw_cmdline = TRUE; // delete mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +02004463 clearmode();
4464}
4465
4466/*
4467 * Clear the mode message.
4468 */
4469 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02004470clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +02004471{
Bram Moolenaar2abad542018-05-19 14:43:45 +02004472 int save_msg_row = msg_row;
4473 int save_msg_col = msg_col;
4474
Bram Moolenaarfd773e92016-04-02 19:39:16 +02004475 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02004476 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004477 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +02004478 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +02004479
4480 msg_col = save_msg_col;
4481 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482}
4483
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004484 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004485recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004486{
Bram Moolenaar32526b32019-01-19 17:43:09 +01004487 msg_puts_attr(_("recording"), attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004488 if (!shortmess(SHM_RECORDING))
4489 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004490 char s[4];
4491
4492 sprintf(s, " @%c", reg_recording);
4493 msg_puts_attr(s, attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004494 }
4495}
4496
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004497/*
4498 * Draw the tab pages line at the top of the Vim window.
4499 */
Bram Moolenaare12bab32019-01-08 22:02:56 +01004500 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004501draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004502{
4503 int tabcount = 0;
4504 tabpage_T *tp;
4505 int tabwidth;
4506 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00004507 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004508 int attr;
4509 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004510 win_T *cwp;
4511 int wincount;
4512 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004513 int c;
4514 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004515 int attr_sel = HL_ATTR(HLF_TPS);
4516 int attr_nosel = HL_ATTR(HLF_TP);
4517 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00004518 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004519 int room;
4520 int use_sep_chars = (t_colors < 8
4521#ifdef FEAT_GUI
4522 && !gui.in_use
4523#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02004524#ifdef FEAT_TERMGUICOLORS
4525 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +02004526#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004527 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004528
Bram Moolenaarc695cec2017-01-08 20:00:04 +01004529 if (ScreenLines == NULL)
4530 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00004531 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004532
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004533#ifdef FEAT_GUI_TABLINE
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004534 // Take care of a GUI tabline.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004535 if (gui_use_tabline())
4536 {
4537 gui_update_tabline();
4538 return;
4539 }
4540#endif
4541
4542 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004543 return;
4544
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004545#if defined(FEAT_STL_OPT)
Bram Moolenaarca57ab52019-04-13 14:53:16 +02004546 clear_TabPageIdxs();
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004547
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004548 // Use the 'tabline' option if it's set.
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004549 if (*p_tal != NUL)
4550 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +02004551 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +00004552
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004553 // Check for an error. If there is one we would loop in redrawing the
4554 // screen. Avoid that by making 'tabline' empty.
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +02004555 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004556 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +02004557 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +00004558 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004559 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +02004560 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004561 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00004562 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004563#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004564 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004565 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00004566 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004567
Bram Moolenaar238a5642006-02-21 22:12:05 +00004568 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
4569 if (tabwidth < 6)
4570 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004571
Bram Moolenaar238a5642006-02-21 22:12:05 +00004572 attr = attr_nosel;
4573 tabcount = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004574 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
4575 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00004576 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00004577 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004578
Bram Moolenaar238a5642006-02-21 22:12:05 +00004579 if (tp->tp_topframe == topframe)
4580 attr = attr_sel;
4581 if (use_sep_chars && col > 0)
4582 screen_putchar('|', 0, col++, attr);
4583
4584 if (tp->tp_topframe != topframe)
4585 attr = attr_nosel;
4586
4587 screen_putchar(' ', 0, col++, attr);
4588
4589 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00004590 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00004591 cwp = curwin;
4592 wp = firstwin;
4593 }
4594 else
4595 {
4596 cwp = tp->tp_curwin;
4597 wp = tp->tp_firstwin;
4598 }
4599
4600 modified = FALSE;
4601 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
4602 if (bufIsChanged(wp->w_buffer))
4603 modified = TRUE;
4604 if (modified || wincount > 1)
4605 {
4606 if (wincount > 1)
4607 {
4608 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004609 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004610 if (col + len >= Columns - 3)
4611 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +00004612 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004613#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004614 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004615#else
Bram Moolenaare0f14822014-08-06 13:20:56 +02004616 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004617#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00004618 );
4619 col += len;
4620 }
4621 if (modified)
4622 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
4623 screen_putchar(' ', 0, col++, attr);
4624 }
4625
4626 room = scol - col + tabwidth - 1;
4627 if (room > 0)
4628 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004629 // Get buffer name in NameBuff[]
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004630 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004631 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +00004632 len = vim_strsize(NameBuff);
4633 p = NameBuff;
Bram Moolenaar238a5642006-02-21 22:12:05 +00004634 if (has_mbyte)
4635 while (len > room)
4636 {
4637 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004638 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +00004639 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01004640 else if (len > room)
Bram Moolenaar238a5642006-02-21 22:12:05 +00004641 {
4642 p += len - room;
4643 len = room;
4644 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004645 if (len > Columns - col - 1)
4646 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +00004647
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004648 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +00004649 col += len;
4650 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00004651 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +00004652
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004653 // Store the tab page number in TabPageIdxs[], so that
4654 // jump_to_mouse() knows where each one is.
Bram Moolenaar238a5642006-02-21 22:12:05 +00004655 ++tabcount;
4656 while (scol < col)
4657 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004658 }
4659
Bram Moolenaar238a5642006-02-21 22:12:05 +00004660 if (use_sep_chars)
4661 c = '_';
4662 else
4663 c = ' ';
4664 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004665
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004666 // Put an "X" for closing the current tab if there are several.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004667 if (first_tabpage->tp_next != NULL)
4668 {
4669 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
4670 TabPageIdxs[Columns - 1] = -999;
4671 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004672 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +00004673
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004674 // Reset the flag here again, in case evaluating 'tabline' causes it to be
4675 // set.
Bram Moolenaarb21e5842006-04-16 18:30:08 +00004676 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004677}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004678
4679/*
4680 * Get buffer name for "buf" into NameBuff[].
4681 * Takes care of special buffer names and translates special characters.
4682 */
4683 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004684get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004685{
4686 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02004687 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004688 else
4689 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
4690 trans_characters(NameBuff, MAXPATHL);
4691}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004692
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693/*
4694 * Get the character to use in a status line. Get its attributes in "*attr".
4695 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02004696 int
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004697fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698{
4699 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004700
4701#ifdef FEAT_TERMINAL
4702 if (bt_terminal(wp->w_buffer))
4703 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004704 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02004705 {
4706 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004707 fill = wp->w_fill_chars.stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02004708 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004709 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02004710 {
4711 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004712 fill = wp->w_fill_chars.stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02004713 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004714 }
4715 else
4716#endif
4717 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004719 *attr = HL_ATTR(HLF_S);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004720 fill = wp->w_fill_chars.stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 }
4722 else
4723 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004724 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004725 fill = wp->w_fill_chars.stlnc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004727 // Use fill when there is highlighting, and highlighting of current
4728 // window differs, or the fillchars differ, or this is not the
4729 // current window
Bram Moolenaar8820b482017-03-16 17:23:31 +01004730 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004731 || wp != curwin || ONE_WINDOW)
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004732 || (wp->w_fill_chars.stl != wp->w_fill_chars.stlnc)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004734 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 return '^';
4736 return '=';
4737}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739/*
4740 * Get the character to use in a separator between vertically split windows.
4741 * Get its attributes in "*attr".
4742 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02004743 int
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004744fillchar_vsep(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745{
Bram Moolenaar8820b482017-03-16 17:23:31 +01004746 *attr = HL_ATTR(HLF_C);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004747 if (*attr == 0 && wp->w_fill_chars.vert == ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 return '|';
4749 else
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004750 return wp->w_fill_chars.vert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752
4753/*
4754 * Return TRUE if redrawing should currently be done.
4755 */
4756 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004757redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758{
Bram Moolenaareb992cb2017-03-09 18:20:16 +01004759#ifdef FEAT_EVAL
4760 if (disable_redraw_for_testing)
4761 return 0;
4762 else
4763#endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +02004764 return ((!RedrawingDisabled
4765#ifdef FEAT_EVAL
4766 || ignore_redraw_flag_for_testing
4767#endif
4768 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769}
4770
4771/*
4772 * Return TRUE if printing messages should currently be done.
4773 */
4774 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004775messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776{
Bram Moolenaara2a89732022-08-31 14:46:18 +01004777 return (!(p_lz && char_avail() && !KeyTyped));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778}
4779
Bram Moolenaare677df82019-09-02 22:31:11 +02004780/*
4781 * Compute columns for ruler and shown command. 'sc_col' is also used to
4782 * decide what the maximum length of a message on the status line can be.
4783 * If there is a status line for the last window, 'sc_col' is independent
4784 * of 'ru_col'.
4785 */
4786
4787#define COL_RULER 17 // columns needed by standard ruler
4788
4789 void
4790comp_col(void)
4791{
Bram Moolenaare677df82019-09-02 22:31:11 +02004792 int last_has_status = (p_ls == 2 || (p_ls == 1 && !ONE_WINDOW));
4793
4794 sc_col = 0;
4795 ru_col = 0;
4796 if (p_ru)
4797 {
Martin Tournoijba43e762022-10-13 22:12:15 +01004798#ifdef FEAT_STL_OPT
Bram Moolenaare677df82019-09-02 22:31:11 +02004799 ru_col = (ru_wid ? ru_wid : COL_RULER) + 1;
Martin Tournoijba43e762022-10-13 22:12:15 +01004800#else
Bram Moolenaare677df82019-09-02 22:31:11 +02004801 ru_col = COL_RULER + 1;
Martin Tournoijba43e762022-10-13 22:12:15 +01004802#endif
Bram Moolenaare677df82019-09-02 22:31:11 +02004803 // no last status line, adjust sc_col
4804 if (!last_has_status)
4805 sc_col = ru_col;
4806 }
4807 if (p_sc)
4808 {
4809 sc_col += SHOWCMD_COLS;
4810 if (!p_ru || last_has_status) // no need for separating space
4811 ++sc_col;
4812 }
4813 sc_col = Columns - sc_col;
4814 ru_col = Columns - ru_col;
4815 if (sc_col <= 0) // screen too narrow, will become a mess
4816 sc_col = 1;
4817 if (ru_col <= 0)
4818 ru_col = 1;
Bram Moolenaare677df82019-09-02 22:31:11 +02004819#ifdef FEAT_EVAL
4820 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
4821#endif
4822}
4823
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004824#if defined(FEAT_LINEBREAK) || defined(PROTO)
4825/*
Bram Moolenaar64486672010-05-16 15:46:46 +02004826 * Return the width of the 'number' and 'relativenumber' column.
4827 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004828 * Otherwise it depends on 'numberwidth' and the line count.
4829 */
4830 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004831number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004832{
4833 int n;
4834 linenr_T lnum;
4835
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004836 if (wp->w_p_rnu && !wp->w_p_nu)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004837 // cursor line shows "0"
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004838 lnum = wp->w_height;
4839 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004840 // cursor line shows absolute line number
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004841 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +02004842
Bram Moolenaar6b314672015-03-20 15:42:10 +01004843 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004844 return wp->w_nrwidth_width;
4845 wp->w_nrwidth_line_count = lnum;
4846
4847 n = 0;
4848 do
4849 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004850 lnum /= 10;
4851 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004852 } while (lnum > 0);
4853
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004854 // 'numberwidth' gives the minimal width plus one
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004855 if (n < wp->w_p_nuw - 1)
4856 n = wp->w_p_nuw - 1;
4857
Bram Moolenaare4b407f2019-07-04 11:59:28 +02004858# ifdef FEAT_SIGNS
4859 // If 'signcolumn' is set to 'number' and there is a sign to display, then
4860 // the minimal width for the number column is 2.
Bram Moolenaar4eb7dae2019-11-12 22:33:45 +01004861 if (n < 2 && get_first_valid_sign(wp) != NULL
Bram Moolenaare4b407f2019-07-04 11:59:28 +02004862 && (*wp->w_p_scl == 'n' && *(wp->w_p_scl + 1) == 'u'))
4863 n = 2;
4864# endif
4865
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004866 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +01004867 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004868 return n;
4869}
4870#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +01004871
Bram Moolenaar113e1072019-01-20 15:30:40 +01004872#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9750bb12012-12-05 16:10:42 +01004873/*
4874 * Return the current cursor column. This is the actual position on the
4875 * screen. First column is 0.
4876 */
4877 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004878screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +01004879{
4880 return screen_cur_col;
4881}
4882
4883/*
4884 * Return the current cursor row. This is the actual position on the screen.
4885 * First row is 0.
4886 */
4887 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004888screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +01004889{
4890 return screen_cur_row;
4891}
Bram Moolenaar113e1072019-01-20 15:30:40 +01004892#endif
Bram Moolenaare677df82019-09-02 22:31:11 +02004893
4894/*
Bram Moolenaar93ff6722021-10-16 17:51:40 +01004895 * Calls mb_ptr2char_adv(p) and returns the character.
4896 * If "p" starts with "\x", "\u" or "\U" the hex or unicode value is used.
4897 */
4898 static int
4899get_encoded_char_adv(char_u **p)
4900{
4901 char_u *s = *p;
4902
4903 if (s[0] == '\\' && (s[1] == 'x' || s[1] == 'u' || s[1] == 'U'))
4904 {
4905 varnumber_T num = 0;
4906 int bytes;
4907 int n;
4908
4909 for (bytes = s[1] == 'x' ? 1 : s[1] == 'u' ? 2 : 4; bytes > 0; --bytes)
4910 {
4911 *p += 2;
4912 n = hexhex2nr(*p);
4913 if (n < 0)
4914 return 0;
4915 num = num * 256 + n;
4916 }
4917 *p += 2;
4918 return num;
4919 }
4920 return mb_ptr2char_adv(p);
4921}
4922
4923/*
Bram Moolenaare677df82019-09-02 22:31:11 +02004924 * Handle setting 'listchars' or 'fillchars'.
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004925 * "varp" points to either the global or the window-local value.
4926 * When "apply" is FALSE do not store the flags, only check for errors.
Bram Moolenaareed9d462021-02-15 20:38:25 +01004927 * Assume monocell characters.
Bram Moolenaare677df82019-09-02 22:31:11 +02004928 * Returns error message, NULL if it's OK.
4929 */
4930 char *
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004931set_chars_option(win_T *wp, char_u **varp, int apply)
Bram Moolenaare677df82019-09-02 22:31:11 +02004932{
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004933 int round, i, len, len2, entries;
4934 char_u *p, *s;
4935 int c1 = 0, c2 = 0, c3 = 0;
4936 char_u *last_multispace = NULL; // Last occurrence of "multispace:"
4937 char_u *last_lmultispace = NULL; // Last occurrence of "leadmultispace:"
4938 int multispace_len = 0; // Length of lcs-multispace string
4939 int lead_multispace_len = 0; // Length of lcs-leadmultispace string
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004940 int is_listchars = (varp == &p_lcs || varp == &wp->w_p_lcs);
4941 char_u *value = *varp;
4942
Bram Moolenaare677df82019-09-02 22:31:11 +02004943 struct charstab
4944 {
4945 int *cp;
4946 char *name;
4947 };
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004948 struct charstab *tab;
4949
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004950 static fill_chars_T fill_chars;
Bram Moolenaare677df82019-09-02 22:31:11 +02004951 static struct charstab filltab[] =
4952 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004953 {&fill_chars.stl, "stl"},
4954 {&fill_chars.stlnc, "stlnc"},
4955 {&fill_chars.vert, "vert"},
4956 {&fill_chars.fold, "fold"},
4957 {&fill_chars.foldopen, "foldopen"},
4958 {&fill_chars.foldclosed, "foldclose"},
4959 {&fill_chars.foldsep, "foldsep"},
4960 {&fill_chars.diff, "diff"},
4961 {&fill_chars.eob, "eob"},
Bram Moolenaar4ba5f1d2022-10-04 14:36:29 +01004962 {&fill_chars.lastline, "lastline"},
Bram Moolenaare677df82019-09-02 22:31:11 +02004963 };
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004964
Bram Moolenaar333bd562021-02-16 22:22:13 +01004965 static lcs_chars_T lcs_chars;
Bram Moolenaareed9d462021-02-15 20:38:25 +01004966 struct charstab lcstab[] =
Bram Moolenaare677df82019-09-02 22:31:11 +02004967 {
Bram Moolenaar333bd562021-02-16 22:22:13 +01004968 {&lcs_chars.eol, "eol"},
4969 {&lcs_chars.ext, "extends"},
4970 {&lcs_chars.nbsp, "nbsp"},
4971 {&lcs_chars.prec, "precedes"},
4972 {&lcs_chars.space, "space"},
4973 {&lcs_chars.tab2, "tab"},
4974 {&lcs_chars.trail, "trail"},
4975 {&lcs_chars.lead, "lead"},
Bram Moolenaare677df82019-09-02 22:31:11 +02004976#ifdef FEAT_CONCEAL
Bram Moolenaar333bd562021-02-16 22:22:13 +01004977 {&lcs_chars.conceal, "conceal"},
Bram Moolenaare677df82019-09-02 22:31:11 +02004978#else
Bram Moolenaar333bd562021-02-16 22:22:13 +01004979 {NULL, "conceal"},
Bram Moolenaare677df82019-09-02 22:31:11 +02004980#endif
4981 };
Bram Moolenaare677df82019-09-02 22:31:11 +02004982
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004983 if (is_listchars)
Bram Moolenaare677df82019-09-02 22:31:11 +02004984 {
4985 tab = lcstab;
Bram Moolenaar333bd562021-02-16 22:22:13 +01004986 CLEAR_FIELD(lcs_chars);
K.Takataeeec2542021-06-02 13:28:16 +02004987 entries = ARRAY_LENGTH(lcstab);
Bram Moolenaareed9d462021-02-15 20:38:25 +01004988 if (varp == &wp->w_p_lcs && wp->w_p_lcs[0] == NUL)
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004989 value = p_lcs; // local value is empty, us the global value
Bram Moolenaare677df82019-09-02 22:31:11 +02004990 }
4991 else
4992 {
4993 tab = filltab;
K.Takataeeec2542021-06-02 13:28:16 +02004994 entries = ARRAY_LENGTH(filltab);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004995 if (varp == &wp->w_p_fcs && wp->w_p_fcs[0] == NUL)
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004996 value = p_fcs; // local value is empty, us the global value
Bram Moolenaare677df82019-09-02 22:31:11 +02004997 }
4998
4999 // first round: check for valid value, second round: assign values
5000 for (round = 0; round <= 1; ++round)
5001 {
5002 if (round > 0)
5003 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005004 // After checking that the value is valid: set defaults.
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01005005 if (is_listchars)
Bram Moolenaare677df82019-09-02 22:31:11 +02005006 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005007 for (i = 0; i < entries; ++i)
5008 if (tab[i].cp != NULL)
5009 *(tab[i].cp) = NUL;
Bram Moolenaar333bd562021-02-16 22:22:13 +01005010 lcs_chars.tab1 = NUL;
5011 lcs_chars.tab3 = NUL;
zeertzjqb5f08012022-06-09 13:55:28 +01005012
Mike Williamsf5785cf2021-09-13 22:17:38 +02005013 if (multispace_len > 0)
zeertzjqf14b8ba2021-09-10 16:58:30 +02005014 {
5015 lcs_chars.multispace = ALLOC_MULT(int, multispace_len + 1);
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01005016 if (lcs_chars.multispace != NULL)
5017 lcs_chars.multispace[multispace_len] = NUL;
zeertzjqf14b8ba2021-09-10 16:58:30 +02005018 }
5019 else
5020 lcs_chars.multispace = NULL;
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01005021
5022 if (lead_multispace_len > 0)
5023 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005024 lcs_chars.leadmultispace =
5025 ALLOC_MULT(int, lead_multispace_len + 1);
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01005026 lcs_chars.leadmultispace[lead_multispace_len] = NUL;
5027 }
5028 else
5029 lcs_chars.leadmultispace = NULL;
Bram Moolenaare677df82019-09-02 22:31:11 +02005030 }
5031 else
Bram Moolenaara98f8a22021-02-13 18:24:23 +01005032 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005033 fill_chars.stl = ' ';
5034 fill_chars.stlnc = ' ';
5035 fill_chars.vert = ' ';
5036 fill_chars.fold = '-';
5037 fill_chars.foldopen = '-';
5038 fill_chars.foldclosed = '+';
5039 fill_chars.foldsep = '|';
5040 fill_chars.diff = '-';
5041 fill_chars.eob = '~';
Bram Moolenaar4ba5f1d2022-10-04 14:36:29 +01005042 fill_chars.lastline = '@';
Bram Moolenaara98f8a22021-02-13 18:24:23 +01005043 }
Bram Moolenaare677df82019-09-02 22:31:11 +02005044 }
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01005045 p = value;
Bram Moolenaare677df82019-09-02 22:31:11 +02005046 while (*p)
5047 {
5048 for (i = 0; i < entries; ++i)
5049 {
5050 len = (int)STRLEN(tab[i].name);
5051 if (STRNCMP(p, tab[i].name, len) == 0
5052 && p[len] == ':'
5053 && p[len + 1] != NUL)
5054 {
5055 c2 = c3 = 0;
5056 s = p + len + 1;
Bram Moolenaar93ff6722021-10-16 17:51:40 +01005057 c1 = get_encoded_char_adv(&s);
zeertzjq60618c82021-12-18 15:32:46 +00005058 if (char2cells(c1) > 1)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005059 return e_invalid_argument;
Bram Moolenaar333bd562021-02-16 22:22:13 +01005060 if (tab[i].cp == &lcs_chars.tab2)
Bram Moolenaare677df82019-09-02 22:31:11 +02005061 {
5062 if (*s == NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005063 return e_invalid_argument;
Bram Moolenaar93ff6722021-10-16 17:51:40 +01005064 c2 = get_encoded_char_adv(&s);
zeertzjq60618c82021-12-18 15:32:46 +00005065 if (char2cells(c2) > 1)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005066 return e_invalid_argument;
Bram Moolenaare677df82019-09-02 22:31:11 +02005067 if (!(*s == ',' || *s == NUL))
5068 {
Bram Moolenaar93ff6722021-10-16 17:51:40 +01005069 c3 = get_encoded_char_adv(&s);
zeertzjq60618c82021-12-18 15:32:46 +00005070 if (char2cells(c3) > 1)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005071 return e_invalid_argument;
Bram Moolenaare677df82019-09-02 22:31:11 +02005072 }
5073 }
5074
5075 if (*s == ',' || *s == NUL)
5076 {
Mike Williamsf5785cf2021-09-13 22:17:38 +02005077 if (round > 0)
Bram Moolenaare677df82019-09-02 22:31:11 +02005078 {
Bram Moolenaar333bd562021-02-16 22:22:13 +01005079 if (tab[i].cp == &lcs_chars.tab2)
Bram Moolenaare677df82019-09-02 22:31:11 +02005080 {
Bram Moolenaar333bd562021-02-16 22:22:13 +01005081 lcs_chars.tab1 = c1;
5082 lcs_chars.tab2 = c2;
5083 lcs_chars.tab3 = c3;
Bram Moolenaare677df82019-09-02 22:31:11 +02005084 }
5085 else if (tab[i].cp != NULL)
5086 *(tab[i].cp) = c1;
5087
5088 }
5089 p = s;
5090 break;
5091 }
5092 }
5093 }
5094
5095 if (i == entries)
zeertzjqf14b8ba2021-09-10 16:58:30 +02005096 {
Mike Williamsf5785cf2021-09-13 22:17:38 +02005097 len = (int)STRLEN("multispace");
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01005098 len2 = (int)STRLEN("leadmultispace");
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01005099 if (is_listchars
zeertzjqf14b8ba2021-09-10 16:58:30 +02005100 && STRNCMP(p, "multispace", len) == 0
5101 && p[len] == ':'
5102 && p[len + 1] != NUL)
5103 {
5104 s = p + len + 1;
5105 if (round == 0)
5106 {
5107 // Get length of lcs-multispace string in first round
5108 last_multispace = p;
5109 multispace_len = 0;
5110 while (*s != NUL && *s != ',')
5111 {
Bram Moolenaar93ff6722021-10-16 17:51:40 +01005112 c1 = get_encoded_char_adv(&s);
zeertzjq60618c82021-12-18 15:32:46 +00005113 if (char2cells(c1) > 1)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005114 return e_invalid_argument;
zeertzjqf14b8ba2021-09-10 16:58:30 +02005115 ++multispace_len;
5116 }
5117 if (multispace_len == 0)
5118 // lcs-multispace cannot be an empty string
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005119 return e_invalid_argument;
zeertzjqf14b8ba2021-09-10 16:58:30 +02005120 p = s;
5121 }
5122 else
5123 {
5124 int multispace_pos = 0;
Mike Williamsf5785cf2021-09-13 22:17:38 +02005125
zeertzjqf14b8ba2021-09-10 16:58:30 +02005126 while (*s != NUL && *s != ',')
5127 {
Bram Moolenaar93ff6722021-10-16 17:51:40 +01005128 c1 = get_encoded_char_adv(&s);
zeertzjqf14b8ba2021-09-10 16:58:30 +02005129 if (p == last_multispace)
5130 lcs_chars.multispace[multispace_pos++] = c1;
5131 }
5132 p = s;
5133 }
5134 }
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01005135
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01005136 else if (is_listchars
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01005137 && STRNCMP(p, "leadmultispace", len2) == 0
5138 && p[len2] == ':'
5139 && p[len2 + 1] != NUL)
5140 {
5141 s = p + len2 + 1;
5142 if (round == 0)
5143 {
zeertzjqb5f08012022-06-09 13:55:28 +01005144 // get length of lcs-leadmultispace string in first
5145 // round
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01005146 last_lmultispace = p;
5147 lead_multispace_len = 0;
5148 while (*s != NUL && *s != ',')
5149 {
5150 c1 = get_encoded_char_adv(&s);
5151 if (char2cells(c1) > 1)
5152 return e_invalid_argument;
5153 ++lead_multispace_len;
5154 }
5155 if (lead_multispace_len == 0)
zeertzjqb5f08012022-06-09 13:55:28 +01005156 // lcs-leadmultispace cannot be an empty string
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01005157 return e_invalid_argument;
5158 p = s;
5159 }
5160 else
5161 {
5162 int multispace_pos = 0;
5163
5164 while (*s != NUL && *s != ',')
5165 {
5166 c1 = get_encoded_char_adv(&s);
5167 if (p == last_lmultispace)
5168 lcs_chars.leadmultispace[multispace_pos++] = c1;
5169 }
5170 p = s;
5171 }
5172 }
zeertzjqf14b8ba2021-09-10 16:58:30 +02005173 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005174 return e_invalid_argument;
zeertzjqf14b8ba2021-09-10 16:58:30 +02005175 }
5176
Bram Moolenaare677df82019-09-02 22:31:11 +02005177 if (*p == ',')
5178 ++p;
5179 }
5180 }
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005181
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01005182 if (apply)
zeertzjqf14b8ba2021-09-10 16:58:30 +02005183 {
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01005184 if (is_listchars)
5185 {
5186 vim_free(wp->w_lcs_chars.multispace);
5187 vim_free(wp->w_lcs_chars.leadmultispace);
5188 wp->w_lcs_chars = lcs_chars;
5189 }
5190 else
5191 {
5192 wp->w_fill_chars = fill_chars;
5193 }
zeertzjqf14b8ba2021-09-10 16:58:30 +02005194 }
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01005195 else if (is_listchars)
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005196 {
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01005197 vim_free(lcs_chars.multispace);
5198 vim_free(lcs_chars.leadmultispace);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005199 }
Bram Moolenaare677df82019-09-02 22:31:11 +02005200
5201 return NULL; // no error
5202}
zeertzjq8ca29b62022-08-09 12:53:14 +01005203
5204/*
5205 * Check all global and local values of 'listchars' and 'fillchars'.
5206 * Return an untranslated error messages if any of them is invalid, NULL
5207 * otherwise.
5208 */
5209 char *
5210check_chars_options(void)
5211{
5212 tabpage_T *tp;
5213 win_T *wp;
5214
5215 if (set_chars_option(curwin, &p_lcs, FALSE) != NULL)
5216 return e_conflicts_with_value_of_listchars;
5217 if (set_chars_option(curwin, &p_fcs, FALSE) != NULL)
5218 return e_conflicts_with_value_of_fillchars;
5219 FOR_ALL_TAB_WINDOWS(tp, wp)
5220 {
5221 if (set_chars_option(wp, &wp->w_p_lcs, FALSE) != NULL)
5222 return e_conflicts_with_value_of_listchars;
5223 if (set_chars_option(wp, &wp->w_p_fcs, FALSE) != NULL)
5224 return e_conflicts_with_value_of_fillchars;
5225 }
5226 return NULL;
5227}