blob: ce4ab11461a26d805472d3ecc9b1a589e591d36d [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.
20 * LineOffset[row] Contains the offset into ScreenLines*[] and ScreenAttrs[]
21 * for each line.
22 * LineWraps[row] Flag for each line whether it wraps to the next line.
23 *
24 * For double-byte characters, two consecutive bytes in ScreenLines[] can form
25 * one character which occupies two display cells.
26 * For UTF-8 a multi-byte character is converted to Unicode and stored in
27 * ScreenLinesUC[]. ScreenLines[] contains the first byte only. For an ASCII
Bram Moolenaar70c49c12010-03-23 15:36:35 +010028 * character without composing chars ScreenLinesUC[] will be 0 and
29 * ScreenLinesC[][] is not used. When the character occupies two display
30 * cells the next byte in ScreenLines[] is 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +000031 * ScreenLinesC[][] contain up to 'maxcombine' composing characters
Bram Moolenaar70c49c12010-03-23 15:36:35 +010032 * (drawn on top of the first character). There is 0 after the last one used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000033 * ScreenLines2[] is only used for euc-jp to store the second byte if the
34 * first byte is 0x8e (single-width character).
35 *
36 * The screen_*() functions write to the screen and handle updating
37 * ScreenLines[].
Bram Moolenaar071d4272004-06-13 20:20:40 +000038 */
39
40#include "vim.h"
41
42/*
43 * The attributes that are actually active for writing to the screen.
44 */
45static int screen_attr = 0;
46
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010047static void screen_char_2(unsigned off, int row, int col);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010048static void screenclear2(void);
Bram Moolenaarcfce7172017-08-17 20:31:48 +020049static void lineclear(unsigned off, int width, int attr);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010050static void lineinvalid(unsigned off, int width);
Bram Moolenaarcfce7172017-08-17 20:31:48 +020051static 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 +010052static void win_rest_invalid(win_T *wp);
53static void msg_pos_mode(void);
54static void recording_mode(int attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000055
Bram Moolenaar63d9e732019-12-05 21:10:38 +010056// Ugly global: overrule attribute used by screen_char()
Bram Moolenaar071d4272004-06-13 20:20:40 +000057static int screen_char_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000058
Bram Moolenaar860cae12010-06-05 23:22:07 +020059#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +020060/*
61 * Return TRUE if the cursor line in window "wp" may be concealed, according
62 * to the 'concealcursor' option.
63 */
64 int
Bram Moolenaar05540972016-01-30 20:31:25 +010065conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +020066{
67 int c;
68
69 if (*wp->w_p_cocu == NUL)
70 return FALSE;
71 if (get_real_state() & VISUAL)
72 c = 'v';
73 else if (State & INSERT)
74 c = 'i';
75 else if (State & NORMAL)
76 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +020077 else if (State & CMDLINE)
78 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +020079 else
80 return FALSE;
81 return vim_strchr(wp->w_p_cocu, c) != NULL;
82}
83
84/*
85 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
86 */
87 void
Bram Moolenaarb9464822018-05-10 15:09:49 +020088conceal_check_cursor_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +020089{
90 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
91 {
92 need_cursor_line_redraw = TRUE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +010093 // Need to recompute cursor column, e.g., when starting Visual mode
94 // without concealing.
Bram Moolenaarf5963f72010-07-23 22:10:27 +020095 curs_columns(TRUE);
96 }
97}
Bram Moolenaar071d4272004-06-13 20:20:40 +000098#endif
99
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200100/*
101 * Get 'wincolor' attribute for window "wp". If not set and "wp" is a popup
102 * window then get the "Pmenu" highlight attribute.
103 */
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200104 int
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200105get_wcr_attr(win_T *wp)
106{
107 int wcr_attr = 0;
108
109 if (*wp->w_p_wcr != NUL)
110 wcr_attr = syn_name2attr(wp->w_p_wcr);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100111#ifdef FEAT_PROP_POPUP
Bram Moolenaarc363fe12019-08-04 18:13:46 +0200112 else if (WIN_IS_POPUP(wp))
Bram Moolenaar62a0cb42019-08-18 16:35:23 +0200113 {
114 if (wp->w_popup_flags & POPF_INFO)
115 wcr_attr = HL_ATTR(HLF_PSI); // PmenuSel
116 else
117 wcr_attr = HL_ATTR(HLF_PNI); // Pmenu
118 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200119#endif
120 return wcr_attr;
121}
122
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123/*
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100124 * Call screen_fill() with the columns adjusted for 'rightleft' if needed.
125 * Return the new offset.
126 */
127 static int
128screen_fill_end(
129 win_T *wp,
130 int c1,
131 int c2,
132 int off,
133 int width,
134 int row,
135 int endrow,
136 int attr)
137{
138 int nn = off + width;
139
140 if (nn > wp->w_width)
141 nn = wp->w_width;
142#ifdef FEAT_RIGHTLEFT
143 if (wp->w_p_rl)
144 {
145 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
146 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - off,
147 c1, c2, attr);
148 }
149 else
150#endif
151 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
152 wp->w_wincol + off, (int)wp->w_wincol + nn,
153 c1, c2, attr);
154 return nn;
155}
156
157/*
158 * Clear lines near the end the window and mark the unused lines with "c1".
159 * use "c2" as the filler character.
160 * When "draw_margin" is TRUE then draw the sign, fold and number columns.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200162 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100163win_draw_end(
164 win_T *wp,
165 int c1,
166 int c2,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100167 int draw_margin,
Bram Moolenaar05540972016-01-30 20:31:25 +0100168 int row,
169 int endrow,
170 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172 int n = 0;
Bram Moolenaar193ffd12019-05-25 22:57:30 +0200173 int attr = HL_ATTR(hl);
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200174 int wcr_attr = get_wcr_attr(wp);
Bram Moolenaar193ffd12019-05-25 22:57:30 +0200175
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200176 attr = hl_combine_attr(wcr_attr, attr);
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100177
178 if (draw_margin)
179 {
Bram Moolenaar1c934292015-01-27 16:39:29 +0100180#ifdef FEAT_FOLDING
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100181 int fdc = compute_foldcolumn(wp, 0);
182
183 if (fdc > 0)
184 // draw the fold column
185 n = screen_fill_end(wp, ' ', ' ', n, fdc,
Bram Moolenaar193ffd12019-05-25 22:57:30 +0200186 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_FC)));
Bram Moolenaar1c934292015-01-27 16:39:29 +0100187#endif
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100188#ifdef FEAT_SIGNS
189 if (signcolumn_on(wp))
190 // draw the sign column
191 n = screen_fill_end(wp, ' ', ' ', n, 2,
Bram Moolenaar193ffd12019-05-25 22:57:30 +0200192 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_SC)));
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100193#endif
194 if ((wp->w_p_nu || wp->w_p_rnu)
195 && vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
196 // draw the number column
197 n = screen_fill_end(wp, ' ', ' ', n, number_width(wp) + 1,
Bram Moolenaar193ffd12019-05-25 22:57:30 +0200198 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_N)));
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100199 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200
201#ifdef FEAT_RIGHTLEFT
202 if (wp->w_p_rl)
203 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100205 wp->w_wincol, W_ENDCOL(wp) - 1 - n,
Bram Moolenaar193ffd12019-05-25 22:57:30 +0200206 c2, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100208 W_ENDCOL(wp) - 1 - n, W_ENDCOL(wp) - n,
Bram Moolenaar193ffd12019-05-25 22:57:30 +0200209 c1, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210 }
211 else
212#endif
213 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100215 wp->w_wincol + n, (int)W_ENDCOL(wp),
Bram Moolenaar193ffd12019-05-25 22:57:30 +0200216 c1, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217 }
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100218
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219 set_empty_rows(wp, row);
220}
221
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200222#if defined(FEAT_FOLDING) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223/*
Bram Moolenaar1c934292015-01-27 16:39:29 +0100224 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
225 * space is available for window "wp", minus "col".
226 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200227 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100228compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +0100229{
230 int fdc = wp->w_p_fdc;
231 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
Bram Moolenaar02631462017-09-22 15:20:32 +0200232 int wwidth = wp->w_width;
Bram Moolenaar1c934292015-01-27 16:39:29 +0100233
234 if (fdc > wwidth - (col + wmw))
235 fdc = wwidth - (col + wmw);
236 return fdc;
237}
238
239/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000241 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100242 * Returns the number of bytes stored in 'p'. When non-multibyte characters are
243 * used for the fold column markers, this is equal to 'fdc' setting. Otherwise,
244 * this will be greater than 'fdc'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 */
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100246 size_t
Bram Moolenaar05540972016-01-30 20:31:25 +0100247fill_foldcolumn(
248 char_u *p,
249 win_T *wp,
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100250 int closed, // TRUE of FALSE
251 linenr_T lnum) // current line number
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252{
253 int i = 0;
254 int level;
255 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +0000256 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +0100257 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100258 size_t byte_counter = 0;
259 int symbol = 0;
260 int len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100262 // Init to all spaces.
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100263 vim_memset(p, ' ', MAX_MCO * fdc + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264
265 level = win_foldinfo.fi_level;
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100266 empty = (fdc == 1) ? 0 : 1;
267
268 // If the column is too narrow, we start at the lowest level that
269 // fits and use numbers to indicated the depth.
270 first_level = level - fdc - closed + 1 + empty;
271 if (first_level < 1)
272 first_level = 1;
273
274 for (i = 0; i < MIN(fdc, level); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275 {
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100276 if (win_foldinfo.fi_lnum == lnum
277 && first_level + i >= win_foldinfo.fi_low_level)
278 symbol = fill_foldopen;
279 else if (first_level == 1)
280 symbol = fill_foldsep;
281 else if (first_level + i <= 9)
282 symbol = '0' + first_level + i;
283 else
284 symbol = '>';
Bram Moolenaar578b49e2005-09-10 19:22:57 +0000285
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100286 len = utf_char2bytes(symbol, &p[byte_counter]);
287 byte_counter += len;
288 if (first_level + i >= level)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289 {
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100290 i++;
291 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292 }
293 }
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100294
Bram Moolenaar071d4272004-06-13 20:20:40 +0000295 if (closed)
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100296 {
297 if (symbol != 0)
298 // rollback length
299 byte_counter -= len;
300 symbol = fill_foldclosed;
301 len = utf_char2bytes(symbol, &p[byte_counter]);
302 byte_counter += len;
303 }
304
305 return MAX(byte_counter + (fdc - i), (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306}
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100307#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000309/*
310 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +0100311 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000312 */
313 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100314comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000315{
316 int i;
317
318 for (i = 0; i < Screen_mco; ++i)
319 {
320 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
321 return TRUE;
322 if (ScreenLinesC[i][off_from] == 0)
323 break;
324 }
325 return FALSE;
326}
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000327
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328/*
329 * Check whether the given character needs redrawing:
330 * - the (first byte of the) character is different
331 * - the attributes are different
332 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +0000333 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334 */
335 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100336char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337{
338 if (cols > 0
339 && ((ScreenLines[off_from] != ScreenLines[off_to]
340 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341 || (enc_dbcs != 0
342 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
343 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
344 ? ScreenLines2[off_from] != ScreenLines2[off_to]
345 : (cols > 1 && ScreenLines[off_from + 1]
346 != ScreenLines[off_to + 1])))
347 || (enc_utf8
348 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
349 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +0000350 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +0200351 || ((*mb_off2cells)(off_from, off_from + cols) > 1
352 && ScreenLines[off_from + 1]
Bram Moolenaara12a1612019-01-24 16:39:02 +0100353 != ScreenLines[off_to + 1])))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354 return TRUE;
355 return FALSE;
356}
357
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200358#if defined(FEAT_TERMINAL) || defined(PROTO)
359/*
360 * Return the index in ScreenLines[] for the current screen line.
361 */
362 int
363screen_get_current_line_off()
364{
365 return (int)(current_ScreenLine - ScreenLines);
366}
367#endif
368
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100369#ifdef FEAT_PROP_POPUP
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200370/*
371 * Return TRUE if this position has a higher level popup or this cell is
372 * transparent in the current popup.
373 */
374 static int
375blocked_by_popup(int row, int col)
376{
377 int off;
378
379 if (!popup_visible)
380 return FALSE;
381 off = row * screen_Columns + col;
382 return popup_mask[off] > screen_zindex || popup_transparent[off];
383}
384#endif
385
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386/*
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200387 * Reset the highlighting. Used before clearing the screen.
388 */
389 void
390reset_screen_attr(void)
391{
392#ifdef FEAT_GUI
393 if (gui.in_use)
394 // Use a code that will reset gui.highlight_mask in
395 // gui_stop_highlight().
396 screen_attr = HL_ALL + 1;
397 else
398#endif
399 // Use attributes that is very unlikely to appear in text.
400 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE | HL_STRIKETHROUGH;
401}
402
403/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404 * Move one "cooked" screen line to the screen, but only the characters that
405 * have actually changed. Handle insert/delete character.
406 * "coloff" gives the first column on the screen for this line.
407 * "endcol" gives the columns where valid characters are.
408 * "clear_width" is the width of the window. It's > 0 if the rest of the line
409 * needs to be cleared, negative otherwise.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200410 * "flags" can have bits:
411 * SLF_POPUP popup window
412 * SLF_RIGHTLEFT rightleft window:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
414 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
415 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200416 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100417screen_line(
418 int row,
419 int coloff,
420 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200421 int clear_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200422 int flags UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423{
424 unsigned off_from;
425 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +0000426 unsigned max_off_from;
427 unsigned max_off_to;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000429 int hl;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100430 int force = FALSE; // force update rest of the line
431 int redraw_this // bool: does character need redraw?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432#ifdef FEAT_GUI
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100433 = TRUE // For GUI when while-loop empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434#endif
435 ;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100436 int redraw_next; // redraw_this for next character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437 int clear_next = FALSE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100438 int char_cells; // 1: normal char
439 // 2: occupies two display cells
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440# define CHAR_CELLS char_cells
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100442 // Check for illegal row and col, just in case.
Bram Moolenaar5ad15df2012-03-16 19:07:58 +0100443 if (row >= Rows)
444 row = Rows - 1;
445 if (endcol > Columns)
446 endcol = Columns;
447
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448# ifdef FEAT_CLIPBOARD
449 clip_may_clear_selection(row, row);
450# endif
451
452 off_from = (unsigned)(current_ScreenLine - ScreenLines);
453 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +0000454 max_off_from = off_from + screen_Columns;
455 max_off_to = LineOffset[row] + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456
457#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200458 if (flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100460 // Clear rest first, because it's left of the text.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461 if (clear_width > 0)
462 {
463 while (col <= endcol && ScreenLines[off_to] == ' '
464 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +0100465 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466 {
467 ++off_to;
468 ++col;
469 }
470 if (col <= endcol)
471 screen_fill(row, row + 1, col + coloff,
472 endcol + coloff + 1, ' ', ' ', 0);
473 }
474 col = endcol + 1;
475 off_to = LineOffset[row] + col + coloff;
476 off_from += col;
477 endcol = (clear_width > 0 ? clear_width : -clear_width);
478 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100479#endif // FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100481#ifdef FEAT_PROP_POPUP
Bram Moolenaar92e25ab2019-11-26 22:39:10 +0100482 // First char of a popup window may go on top of the right half of a
483 // double-wide character. Clear the left half to avoid it getting the popup
484 // window background color.
Bram Moolenaar927495b2020-11-06 17:58:35 +0100485 if (coloff > 0 && enc_utf8
486 && ScreenLines[off_to] == 0
Bram Moolenaardc0cf1d2020-08-23 15:09:36 +0200487 && ScreenLinesUC[off_to - 1] != 0
488 && (*mb_char2cells)(ScreenLinesUC[off_to - 1]) > 1)
Bram Moolenaar92e25ab2019-11-26 22:39:10 +0100489 {
490 ScreenLines[off_to - 1] = ' ';
491 ScreenLinesUC[off_to - 1] = 0;
492 screen_char(off_to - 1, row, col + coloff - 1);
493 }
494#endif
495
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
497
498 while (col < endcol)
499 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +0000501 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 else
503 char_cells = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504
505 redraw_this = redraw_next;
506 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
507 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
508
509#ifdef FEAT_GUI
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100510 // If the next character was bold, then redraw the current character to
511 // remove any pixels that might have spilt over into us. This only
512 // happens in the GUI.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 if (redraw_next && gui.in_use)
514 {
515 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000516 if (hl > HL_ALL)
517 hl = syn_attr2attr(hl);
518 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 redraw_this = TRUE;
520 }
521#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100522#ifdef FEAT_PROP_POPUP
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200523 if (blocked_by_popup(row, col + coloff))
Bram Moolenaar33796b32019-06-08 16:01:13 +0200524 redraw_this = FALSE;
525#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526 if (redraw_this)
527 {
528 /*
529 * Special handling when 'xs' termcap flag set (hpterm):
530 * Attributes for characters are stored at the position where the
531 * cursor is when writing the highlighting code. The
532 * start-highlighting code must be written with the cursor on the
533 * first highlighted character. The stop-highlighting code must
534 * be written with the cursor just after the last highlighted
535 * character.
Bram Moolenaarc4568ab2018-11-16 16:21:05 +0100536 * Overwriting a character doesn't remove its highlighting. Need
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537 * to clear the rest of the line, and force redrawing it
538 * completely.
539 */
540 if ( p_wiv
541 && !force
542#ifdef FEAT_GUI
543 && !gui.in_use
544#endif
545 && ScreenAttrs[off_to] != 0
546 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
547 {
548 /*
549 * Need to remove highlighting attributes here.
550 */
551 windgoto(row, col + coloff);
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100552 out_str(T_CE); // clear rest of this screen line
553 screen_start(); // don't know where cursor is now
554 force = TRUE; // force redraw of rest of the line
555 redraw_next = TRUE; // or else next char would miss out
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556
557 /*
558 * If the previous character was highlighted, need to stop
559 * highlighting at this character.
560 */
561 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
562 {
563 screen_attr = ScreenAttrs[off_to - 1];
564 term_windgoto(row, col + coloff);
565 screen_stop_highlight();
566 }
567 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100568 screen_attr = 0; // highlighting has stopped
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 if (enc_dbcs != 0)
571 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100572 // Check if overwriting a double-byte with a single-byte or
573 // the other way around requires another character to be
574 // redrawn. For UTF-8 this isn't needed, because comparing
575 // ScreenLinesUC[] is sufficient.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576 if (char_cells == 1
577 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +0000578 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100580 // Writing a single-cell character over a double-cell
581 // character: need to redraw the next cell.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 ScreenLines[off_to + 1] = 0;
583 redraw_next = TRUE;
584 }
585 else if (char_cells == 2
586 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +0000587 && (*mb_off2cells)(off_to, max_off_to) == 1
588 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100590 // Writing the second half of a double-cell character over
591 // a double-cell character: need to redraw the second
592 // cell.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 ScreenLines[off_to + 2] = 0;
594 redraw_next = TRUE;
595 }
596
597 if (enc_dbcs == DBCS_JPNU)
598 ScreenLines2[off_to] = ScreenLines2[off_from];
599 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100600 // When writing a single-width character over a double-width
601 // character and at the end of the redrawn text, need to clear out
602 // the right halve of the old character.
603 // Also required when writing the right halve of a double-width
604 // char over the left halve of an existing one.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605 if (has_mbyte && col + char_cells == endcol
606 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +0000607 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +0000609 && (*mb_off2cells)(off_to, max_off_to) == 1
610 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611 clear_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612
613 ScreenLines[off_to] = ScreenLines[off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614 if (enc_utf8)
615 {
616 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
617 if (ScreenLinesUC[off_from] != 0)
618 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000619 int i;
620
621 for (i = 0; i < Screen_mco; ++i)
622 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 }
624 }
625 if (char_cells == 2)
626 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627
628#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100629 // The bold trick makes a single column of pixels appear in the
630 // next character. When a bold character is removed, the next
631 // character should be redrawn too. This happens for our own GUI
632 // and for some xterms.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 if (
634# ifdef FEAT_GUI
635 gui.in_use
636# endif
637# if defined(FEAT_GUI) && defined(UNIX)
638 ||
639# endif
640# ifdef UNIX
641 term_is_xterm
642# endif
643 )
644 {
645 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000646 if (hl > HL_ALL)
647 hl = syn_attr2attr(hl);
648 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 redraw_next = TRUE;
650 }
651#endif
652 ScreenAttrs[off_to] = ScreenAttrs[off_from];
Bram Moolenaara12a1612019-01-24 16:39:02 +0100653
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100654 // For simplicity set the attributes of second half of a
655 // double-wide character equal to the first half.
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000656 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000658
659 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 screen_char(off_to, row, col + coloff);
663 }
664 else if ( p_wiv
665#ifdef FEAT_GUI
666 && !gui.in_use
667#endif
668 && col + coloff > 0)
669 {
670 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
671 {
672 /*
673 * Don't output stop-highlight when moving the cursor, it will
674 * stop the highlighting when it should continue.
675 */
676 screen_attr = 0;
677 }
678 else if (screen_attr != 0)
679 screen_stop_highlight();
680 }
681
682 off_to += CHAR_CELLS;
683 off_from += CHAR_CELLS;
684 col += CHAR_CELLS;
685 }
686
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 if (clear_next)
688 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100689 // Clear the second half of a double-wide character of which the left
690 // half was overwritten with a single-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 ScreenLines[off_to] = ' ';
692 if (enc_utf8)
693 ScreenLinesUC[off_to] = 0;
694 screen_char(off_to, row, col + coloff);
695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696
697 if (clear_width > 0
698#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200699 && !(flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700#endif
701 )
702 {
703#ifdef FEAT_GUI
704 int startCol = col;
705#endif
706
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100707 // blank out the rest of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 while (col < clear_width && ScreenLines[off_to] == ' '
709 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +0100710 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 {
712 ++off_to;
713 ++col;
714 }
715 if (col < clear_width)
716 {
717#ifdef FEAT_GUI
718 /*
719 * In the GUI, clearing the rest of the line may leave pixels
720 * behind if the first character cleared was bold. Some bold
721 * fonts spill over the left. In this case we redraw the previous
722 * character too. If we didn't skip any blanks above, then we
723 * only redraw if the character wasn't already redrawn anyway.
724 */
Bram Moolenaar9c697322006-10-09 20:11:17 +0000725 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 {
727 hl = ScreenAttrs[off_to];
728 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +0000729 {
730 int prev_cells = 1;
Bram Moolenaara12a1612019-01-24 16:39:02 +0100731
Bram Moolenaar9c697322006-10-09 20:11:17 +0000732 if (enc_utf8)
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100733 // for utf-8, ScreenLines[char_offset + 1] == 0 means
734 // that its width is 2.
Bram Moolenaar9c697322006-10-09 20:11:17 +0000735 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
736 else if (enc_dbcs != 0)
737 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100738 // find previous character by counting from first
739 // column and get its width.
Bram Moolenaar9c697322006-10-09 20:11:17 +0000740 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +0000741 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +0000742
743 while (off < off_to)
744 {
Bram Moolenaar367329b2007-08-30 11:53:22 +0000745 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +0000746 off += prev_cells;
747 }
748 }
749
750 if (enc_dbcs != 0 && prev_cells > 1)
751 screen_char_2(off_to - prev_cells, row,
752 col + coloff - prev_cells);
753 else
Bram Moolenaar9c697322006-10-09 20:11:17 +0000754 screen_char(off_to - prev_cells, row,
755 col + coloff - prev_cells);
756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 }
758#endif
759 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
760 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 off_to += clear_width - col;
762 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 }
764 }
765
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200766 if (clear_width > 0
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100767#ifdef FEAT_PROP_POPUP
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200768 && !(flags & SLF_POPUP) // no separator for popup window
769#endif
770 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200772 // For a window that has a right neighbor, draw the separator char
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200773 // right of the window contents. But not on top of a popup window.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200774 if (coloff + col < Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 {
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100776#ifdef FEAT_PROP_POPUP
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200777 if (!blocked_by_popup(row, col + coloff))
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200778#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200780 int c;
781
782 c = fillchar_vsep(&hl);
783 if (ScreenLines[off_to] != (schar_T)c
784 || (enc_utf8 && (int)ScreenLinesUC[off_to]
785 != (c >= 0x80 ? c : 0))
786 || ScreenAttrs[off_to] != hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200788 ScreenLines[off_to] = c;
789 ScreenAttrs[off_to] = hl;
790 if (enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200792 if (c >= 0x80)
793 {
794 ScreenLinesUC[off_to] = c;
795 ScreenLinesC[0][off_to] = 0;
796 }
797 else
798 ScreenLinesUC[off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 }
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200800 screen_char(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 }
803 }
804 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 LineWraps[row] = FALSE;
806 }
807}
808
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000809#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000811 * Mirror text "str" for right-left displaying.
812 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000814 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100815rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816{
817 char_u *p1, *p2;
818 int t;
819
820 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
821 {
822 t = *p1;
823 *p1 = *p2;
824 *p2 = t;
825 }
826}
827#endif
828
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 * Draw the verticap separator right of window "wp" starting with line "row".
831 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200832 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100833draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834{
835 int hl;
836 int c;
837
838 if (wp->w_vsep_width)
839 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100840 // draw the vertical separator right of this window
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 c = fillchar_vsep(&hl);
842 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
843 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
844 c, ' ', hl);
845 }
846}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847
848#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100849static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850
851/*
Bram Moolenaar367329b2007-08-30 11:53:22 +0000852 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 */
854 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100855status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856{
857 int len = 0;
858
859#ifdef FEAT_MENU
860 int emenu = (xp->xp_context == EXPAND_MENUS
861 || xp->xp_context == EXPAND_MENUNAMES);
862
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100863 // Check for menu separators - replace with '|'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 if (emenu && menu_is_separator(s))
865 return 1;
866#endif
867
868 while (*s != NUL)
869 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +0000870 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +0000871 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100872 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 }
874
875 return len;
876}
877
878/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +0000879 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000880 * These are backslashes used for escaping. Do show backslashes in help tags.
881 */
882 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100883skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000884{
Bram Moolenaar7693ec62008-07-24 18:29:37 +0000885 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000886#ifdef FEAT_MENU
887 || ((xp->xp_context == EXPAND_MENUS
888 || xp->xp_context == EXPAND_MENUNAMES)
889 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
890#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +0000891 )
892 {
893#ifndef BACKSLASH_IN_FILENAME
894 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
895 return 2;
896#endif
897 return 1;
898 }
899 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000900}
901
902/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 * Show wildchar matches in the status line.
904 * Show at least the "match" item.
905 * We start at item 'first_match' in the list and show all matches that fit.
906 *
907 * If inversion is possible we use it. Else '=' characters are used.
908 */
909 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100910win_redr_status_matches(
911 expand_T *xp,
912 int num_matches,
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100913 char_u **matches, // list of matches
Bram Moolenaar05540972016-01-30 20:31:25 +0100914 int match,
915 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916{
917#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
918 int row;
919 char_u *buf;
920 int len;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100921 int clen; // length in screen cells
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 int fillchar;
923 int attr;
924 int i;
925 int highlight = TRUE;
926 char_u *selstart = NULL;
927 int selstart_col = 0;
928 char_u *selend = NULL;
929 static int first_match = 0;
930 int add_left = FALSE;
931 char_u *s;
932#ifdef FEAT_MENU
933 int emenu;
934#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100937 if (matches == NULL) // interrupted completion?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 return;
939
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000940 if (has_mbyte)
Bram Moolenaar964b3742019-05-24 18:54:09 +0200941 buf = alloc(Columns * MB_MAXBYTES + 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000942 else
Bram Moolenaar964b3742019-05-24 18:54:09 +0200943 buf = alloc(Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 if (buf == NULL)
945 return;
946
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100947 if (match == -1) // don't show match but original text
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 {
949 match = 0;
950 highlight = FALSE;
951 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100952 // count 1 for the ending ">"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 clen = status_match_len(xp, L_MATCH(match)) + 3;
954 if (match == 0)
955 first_match = 0;
956 else if (match < first_match)
957 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100958 // jumping left, as far as we can go
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 first_match = match;
960 add_left = TRUE;
961 }
962 else
963 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100964 // check if match fits on the screen
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 for (i = first_match; i < match; ++i)
966 clen += status_match_len(xp, L_MATCH(i)) + 2;
967 if (first_match > 0)
968 clen += 2;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100969 // jumping right, put match at the left
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 if ((long)clen > Columns)
971 {
972 first_match = match;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100973 // if showing the last match, we can add some on the left
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 clen = 2;
975 for (i = match; i < num_matches; ++i)
976 {
977 clen += status_match_len(xp, L_MATCH(i)) + 2;
978 if ((long)clen >= Columns)
979 break;
980 }
981 if (i == num_matches)
982 add_left = TRUE;
983 }
984 }
985 if (add_left)
986 while (first_match > 0)
987 {
988 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
989 if ((long)clen >= Columns)
990 break;
991 --first_match;
992 }
993
Bram Moolenaar3633cf52017-07-31 22:29:35 +0200994 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995
996 if (first_match == 0)
997 {
998 *buf = NUL;
999 len = 0;
1000 }
1001 else
1002 {
1003 STRCPY(buf, "< ");
1004 len = 2;
1005 }
1006 clen = len;
1007
1008 i = first_match;
1009 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
1010 {
1011 if (i == match)
1012 {
1013 selstart = buf + len;
1014 selstart_col = clen;
1015 }
1016
1017 s = L_MATCH(i);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001018 // Check for menu separators - replace with '|'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019#ifdef FEAT_MENU
1020 emenu = (xp->xp_context == EXPAND_MENUS
1021 || xp->xp_context == EXPAND_MENUNAMES);
1022 if (emenu && menu_is_separator(s))
1023 {
1024 STRCPY(buf + len, transchar('|'));
1025 l = (int)STRLEN(buf + len);
1026 len += l;
1027 clen += l;
1028 }
1029 else
1030#endif
1031 for ( ; *s != NUL; ++s)
1032 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00001033 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 clen += ptr2cells(s);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001035 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 {
1037 STRNCPY(buf + len, s, l);
1038 s += l - 1;
1039 len += l;
1040 }
1041 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 {
1043 STRCPY(buf + len, transchar_byte(*s));
1044 len += (int)STRLEN(buf + len);
1045 }
1046 }
1047 if (i == match)
1048 selend = buf + len;
1049
1050 *(buf + len++) = ' ';
1051 *(buf + len++) = ' ';
1052 clen += 2;
1053 if (++i == num_matches)
1054 break;
1055 }
1056
1057 if (i != num_matches)
1058 {
1059 *(buf + len++) = '>';
1060 ++clen;
1061 }
1062
1063 buf[len] = NUL;
1064
1065 row = cmdline_row - 1;
1066 if (row >= 0)
1067 {
1068 if (wild_menu_showing == 0)
1069 {
1070 if (msg_scrolled > 0)
1071 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001072 // Put the wildmenu just above the command line. If there is
1073 // no room, scroll the screen one line up.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 if (cmdline_row == Rows - 1)
1075 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001076 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 ++msg_scrolled;
1078 }
1079 else
1080 {
1081 ++cmdline_row;
1082 ++row;
1083 }
1084 wild_menu_showing = WM_SCROLLED;
1085 }
1086 else
1087 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001088 // Create status line if needed by setting 'laststatus' to 2.
1089 // Set 'winminheight' to zero to avoid that the window is
1090 // resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 if (lastwin->w_status_height == 0)
1092 {
1093 save_p_ls = p_ls;
1094 save_p_wmh = p_wmh;
1095 p_ls = 2;
1096 p_wmh = 0;
1097 last_status(FALSE);
1098 }
1099 wild_menu_showing = WM_SHOWN;
1100 }
1101 }
1102
1103 screen_puts(buf, row, 0, attr);
1104 if (selstart != NULL && highlight)
1105 {
1106 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001107 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 }
1109
1110 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
1111 }
1112
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 vim_free(buf);
1115}
1116#endif
1117
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 * Return TRUE if the status line of window "wp" is connected to the status
1120 * line of the window right of it. If not, then it's a vertical separator.
1121 * Only call if (wp->w_vsep_width != 0).
1122 */
1123 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001124stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125{
1126 frame_T *fr;
1127
1128 fr = wp->w_frame;
1129 while (fr->fr_parent != NULL)
1130 {
1131 if (fr->fr_parent->fr_layout == FR_COL)
1132 {
1133 if (fr->fr_next != NULL)
1134 break;
1135 }
1136 else
1137 {
1138 if (fr->fr_next != NULL)
1139 return TRUE;
1140 }
1141 fr = fr->fr_parent;
1142 }
1143 return FALSE;
1144}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147/*
1148 * Get the value to show for the language mappings, active 'keymap'.
1149 */
1150 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001151get_keymap_str(
1152 win_T *wp,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001153 char_u *fmt, // format string containing one %s item
1154 char_u *buf, // buffer for the result
1155 int len) // length of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156{
1157 char_u *p;
1158
1159 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
1160 return FALSE;
1161
1162 {
1163#ifdef FEAT_EVAL
1164 buf_T *old_curbuf = curbuf;
1165 win_T *old_curwin = curwin;
1166 char_u *s;
1167
1168 curbuf = wp->w_buffer;
1169 curwin = wp;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001170 STRCPY(buf, "b:keymap_name"); // must be writable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 ++emsg_skip;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001172 s = p = eval_to_string(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 --emsg_skip;
1174 curbuf = old_curbuf;
1175 curwin = old_curwin;
1176 if (p == NULL || *p == NUL)
1177#endif
1178 {
1179#ifdef FEAT_KEYMAP
1180 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
1181 p = wp->w_buffer->b_p_keymap;
1182 else
1183#endif
1184 p = (char_u *)"lang";
1185 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02001186 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 buf[0] = NUL;
1188#ifdef FEAT_EVAL
1189 vim_free(s);
1190#endif
1191 }
1192 return buf[0] != NUL;
1193}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194
1195#if defined(FEAT_STL_OPT) || defined(PROTO)
1196/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001197 * Redraw the status line or ruler of window "wp".
1198 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001200 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001201win_redr_custom(
1202 win_T *wp,
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001203 int draw_ruler) // TRUE or FALSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204{
Bram Moolenaar1d633412013-12-11 15:52:01 +01001205 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 int attr;
1207 int curattr;
1208 int row;
1209 int col = 0;
1210 int maxwidth;
1211 int width;
1212 int n;
1213 int len;
1214 int fillchar;
1215 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00001216 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 char_u *p;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01001218 stl_hlrec_T *hltab;
1219 stl_hlrec_T *tabtab;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001220 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01001221 win_T *ewp;
1222 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001224 // There is a tiny chance that this gets called recursively: When
1225 // redrawing a status line triggers redrawing the ruler or tabline.
1226 // Avoid trouble by not allowing recursion.
Bram Moolenaar1d633412013-12-11 15:52:01 +01001227 if (entered)
1228 return;
1229 entered = TRUE;
1230
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001231 // setup environment for the task at hand
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001232 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 {
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001234 // Use 'tabline'. Always at the first line of the screen.
Bram Moolenaar362f3562009-11-03 16:20:34 +00001235 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001236 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00001237 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01001238 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001239 maxwidth = Columns;
1240# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001241 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001242# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001244 else
1245 {
1246 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02001247 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02001248 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001249
1250 if (draw_ruler)
1251 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00001252 stl = p_ruf;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001253 // advance past any leading group spec - implicit in ru_col
Bram Moolenaar362f3562009-11-03 16:20:34 +00001254 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001255 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00001256 if (*++stl == '-')
1257 stl++;
1258 if (atoi((char *)stl))
1259 while (VIM_ISDIGIT(*stl))
1260 stl++;
1261 if (*stl++ != '(')
1262 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001263 }
Bram Moolenaar02631462017-09-22 15:20:32 +02001264 col = ru_col - (Columns - wp->w_width);
1265 if (col < (wp->w_width + 1) / 2)
1266 col = (wp->w_width + 1) / 2;
1267 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001268 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001269 {
1270 row = Rows - 1;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001271 --maxwidth; // writing in last column may cause scrolling
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001272 fillchar = ' ';
1273 attr = 0;
1274 }
1275
1276# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001277 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001278# endif
1279 }
1280 else
1281 {
1282 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00001283 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001284 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00001285 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001286# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001287 use_sandbox = was_set_insecurely((char_u *)"statusline",
1288 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001289# endif
1290 }
1291
Bram Moolenaar53f81742017-09-22 14:35:51 +02001292 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001293 }
1294
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01001296 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001298 // Temporarily reset 'cursorbind', we don't want a side effect from moving
1299 // the cursor away and back.
Bram Moolenaar61452852011-02-01 18:01:11 +01001300 ewp = wp == NULL ? curwin : wp;
1301 p_crb_save = ewp->w_p_crb;
1302 ewp->w_p_crb = FALSE;
1303
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001304 // Make a copy, because the statusline may include a function call that
1305 // might change the option value and free the memory.
Bram Moolenaar362f3562009-11-03 16:20:34 +00001306 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01001307 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00001308 stl, use_sandbox,
Bram Moolenaar8133cc62020-10-26 21:05:27 +01001309 fillchar, maxwidth, &hltab, &tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00001310 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01001311 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001313 // Make all characters printable.
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01001314 p = transstr(buf);
1315 if (p != NULL)
1316 {
1317 vim_strncpy(buf, p, sizeof(buf) - 1);
1318 vim_free(p);
1319 }
1320
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001321 // fill up with "fillchar"
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01001322 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00001323 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 len += (*mb_char2bytes)(fillchar, buf + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 ++width;
1327 }
1328 buf[len] = NUL;
1329
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001330 /*
1331 * Draw each snippet with the specified highlighting.
1332 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 curattr = attr;
1334 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001335 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001337 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 screen_puts_len(p, len, row, col, curattr);
1339 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001340 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001342 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001344 else if (hltab[n].userhl < 0)
1345 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02001346#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02001347 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
1348 && wp->w_status_height != 0)
1349 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02001350 else if (wp != NULL && bt_terminal(wp->w_buffer)
1351 && wp->w_status_height != 0)
1352 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02001353#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00001354 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001355 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001357 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 }
1359 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001360
1361 if (wp == NULL)
1362 {
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001363 // Fill the TabPageIdxs[] array for clicking in the tab pagesline.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001364 col = 0;
1365 len = 0;
1366 p = buf;
1367 fillchar = 0;
1368 for (n = 0; tabtab[n].start != NULL; n++)
1369 {
1370 len += vim_strnsize(p, (int)(tabtab[n].start - p));
1371 while (col < len)
1372 TabPageIdxs[col++] = fillchar;
1373 p = tabtab[n].start;
1374 fillchar = tabtab[n].userhl;
1375 }
1376 while (col < Columns)
1377 TabPageIdxs[col++] = fillchar;
1378 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01001379
1380theend:
1381 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382}
1383
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001384#endif // FEAT_STL_OPT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385
1386/*
1387 * Output a single character directly to the screen and update ScreenLines.
1388 */
1389 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001390screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 char_u buf[MB_MAXBYTES + 1];
1393
Bram Moolenaar9a920d82012-06-01 15:21:02 +02001394 if (has_mbyte)
1395 buf[(*mb_char2bytes)(c, buf)] = NUL;
1396 else
Bram Moolenaar9a920d82012-06-01 15:21:02 +02001397 {
1398 buf[0] = c;
1399 buf[1] = NUL;
1400 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 screen_puts(buf, row, col, attr);
1402}
1403
1404/*
1405 * Get a single character directly from ScreenLines into "bytes[]".
1406 * Also return its attribute in *attrp;
1407 */
1408 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001409screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410{
1411 unsigned off;
1412
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001413 // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
1415 {
1416 off = LineOffset[row] + col;
1417 *attrp = ScreenAttrs[off];
1418 bytes[0] = ScreenLines[off];
1419 bytes[1] = NUL;
1420
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 if (enc_utf8 && ScreenLinesUC[off] != 0)
1422 bytes[utfc_char2bytes(off, bytes)] = NUL;
1423 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
1424 {
1425 bytes[0] = ScreenLines[off];
1426 bytes[1] = ScreenLines2[off];
1427 bytes[2] = NUL;
1428 }
1429 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
1430 {
1431 bytes[1] = ScreenLines[off + 1];
1432 bytes[2] = NUL;
1433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 }
1435}
1436
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001437/*
1438 * Return TRUE if composing characters for screen posn "off" differs from
1439 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01001440 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001441 */
1442 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001443screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001444{
1445 int i;
1446
1447 for (i = 0; i < Screen_mco; ++i)
1448 {
1449 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
1450 return TRUE;
1451 if (u8cc[i] == 0)
1452 break;
1453 }
1454 return FALSE;
1455}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001456
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457/*
1458 * Put string '*text' on the screen at position 'row' and 'col', with
1459 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
1460 * Note: only outputs within one row, message is truncated at screen boundary!
1461 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
1462 */
1463 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001464screen_puts(
1465 char_u *text,
1466 int row,
1467 int col,
1468 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469{
1470 screen_puts_len(text, -1, row, col, attr);
1471}
1472
1473/*
1474 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
1475 * a NUL.
1476 */
1477 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001478screen_puts_len(
1479 char_u *text,
1480 int textlen,
1481 int row,
1482 int col,
1483 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484{
1485 unsigned off;
1486 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02001487 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 int c;
Bram Moolenaar367329b2007-08-30 11:53:22 +00001489 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 int mbyte_blen = 1;
1491 int mbyte_cells = 1;
1492 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001493 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 int clear_next_cell = FALSE;
Bram Moolenaara12a1612019-01-24 16:39:02 +01001495#ifdef FEAT_ARABIC
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001496 int prev_c = 0; // previous Arabic character
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001497 int pc, nc, nc1;
1498 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001500 int force_redraw_this;
1501 int force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001502 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503
Bram Moolenaar0b4c9ed2019-06-03 22:04:23 +02001504 // Safety check. The check for negative row and column is to fix issue
1505 // #4102. TODO: find out why row/col could be negative.
1506 if (ScreenLines == NULL
1507 || row >= screen_Rows || row < 0
1508 || col >= screen_Columns || col < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001510 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001512 // When drawing over the right halve of a double-wide char clear out the
1513 // left halve. Only needed in a terminal.
Bram Moolenaar7693ec62008-07-24 18:29:37 +00001514 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaara12a1612019-01-24 16:39:02 +01001515#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00001516 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01001517#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00001518 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001519 {
1520 ScreenLines[off - 1] = ' ';
1521 ScreenAttrs[off - 1] = 0;
1522 if (enc_utf8)
1523 {
1524 ScreenLinesUC[off - 1] = 0;
1525 ScreenLinesC[0][off - 1] = 0;
1526 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001527 // redraw the previous cell, make it empty
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001528 screen_char(off - 1, row, col - 1);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001529 // force the cell at "col" to be redrawn
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001530 force_redraw_next = TRUE;
1531 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00001532
Bram Moolenaar367329b2007-08-30 11:53:22 +00001533 max_off = LineOffset[row] + screen_Columns;
Bram Moolenaara064ac82007-08-05 18:10:54 +00001534 while (col < screen_Columns
1535 && (len < 0 || (int)(ptr - text) < len)
1536 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 {
1538 c = *ptr;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001539 // check if this is the first byte of a multibyte
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 if (has_mbyte)
1541 {
1542 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001543 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001545 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
1547 mbyte_cells = 1;
1548 else if (enc_dbcs != 0)
1549 mbyte_cells = mbyte_blen;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001550 else // enc_utf8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 {
1552 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001553 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 (int)((text + len) - ptr));
1555 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001556 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaara12a1612019-01-24 16:39:02 +01001558#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
1560 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001561 // Do Arabic shaping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
1563 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001564 // Past end of string to be displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 nc = NUL;
1566 nc1 = NUL;
1567 }
1568 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001569 {
Bram Moolenaar54620182009-11-11 16:07:20 +00001570 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
1571 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001572 nc1 = pcc[0];
1573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574 pc = prev_c;
1575 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001576 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 }
1578 else
1579 prev_c = u8c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01001580#endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01001581 if (col + mbyte_cells > screen_Columns)
1582 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001583 // Only 1 cell left, but character requires 2 cells:
1584 // display a '>' in the last column to avoid wrapping.
Bram Moolenaare4ebd292010-01-19 17:40:46 +01001585 c = '>';
1586 mbyte_cells = 1;
1587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 }
1589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001591 force_redraw_this = force_redraw_next;
1592 force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001593
1594 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 || (mbyte_cells == 2
1596 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
1597 || (enc_dbcs == DBCS_JPNU
1598 && c == 0x8e
1599 && ScreenLines2[off] != ptr[1])
1600 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01001601 && (ScreenLinesUC[off] !=
1602 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
1603 || (ScreenLinesUC[off] != 0
1604 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001606 || exmode_active;
1607
Bram Moolenaar24a5ac52019-06-08 19:01:18 +02001608 if ((need_redraw || force_redraw_this)
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001609#ifdef FEAT_PROP_POPUP
Bram Moolenaarc662ec92019-06-23 00:15:57 +02001610 && !blocked_by_popup(row, col)
Bram Moolenaar24a5ac52019-06-08 19:01:18 +02001611#endif
1612 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 {
1614#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001615 // The bold trick makes a single row of pixels appear in the next
1616 // character. When a bold character is removed, the next
1617 // character should be redrawn too. This happens for our own GUI
1618 // and for some xterms.
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001619 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620# ifdef FEAT_GUI
1621 gui.in_use
1622# endif
1623# if defined(FEAT_GUI) && defined(UNIX)
1624 ||
1625# endif
1626# ifdef UNIX
1627 term_is_xterm
1628# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001629 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001631 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001633 if (n > HL_ALL)
1634 n = syn_attr2attr(n);
1635 if (n & HL_BOLD)
1636 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 }
1638#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001639 // When at the end of the text and overwriting a two-cell
1640 // character with a one-cell character, need to clear the next
1641 // cell. Also when overwriting the left halve of a two-cell char
1642 // with the right halve of a two-cell char. Do this only once
1643 // (mb_off2cells() may return 2 on the right halve).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 if (clear_next_cell)
1645 clear_next_cell = FALSE;
1646 else if (has_mbyte
1647 && (len < 0 ? ptr[mbyte_blen] == NUL
1648 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00001649 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00001651 && (*mb_off2cells)(off, max_off) == 1
1652 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 clear_next_cell = TRUE;
1654
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001655 // Make sure we never leave a second byte of a double-byte behind,
1656 // it confuses mb_off2cells().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00001658 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00001660 && (*mb_off2cells)(off, max_off) == 1
1661 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 ScreenLines[off + mbyte_blen] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 ScreenLines[off] = c;
1664 ScreenAttrs[off] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 if (enc_utf8)
1666 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001667 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 ScreenLinesUC[off] = 0;
1669 else
1670 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001671 int i;
1672
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001674 for (i = 0; i < Screen_mco; ++i)
1675 {
1676 ScreenLinesC[i][off] = u8cc[i];
1677 if (u8cc[i] == 0)
1678 break;
1679 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 }
1681 if (mbyte_cells == 2)
1682 {
1683 ScreenLines[off + 1] = 0;
1684 ScreenAttrs[off + 1] = attr;
1685 }
1686 screen_char(off, row, col);
1687 }
1688 else if (mbyte_cells == 2)
1689 {
1690 ScreenLines[off + 1] = ptr[1];
1691 ScreenAttrs[off + 1] = attr;
1692 screen_char_2(off, row, col);
1693 }
1694 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
1695 {
1696 ScreenLines2[off] = ptr[1];
1697 screen_char(off, row, col);
1698 }
1699 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 screen_char(off, row, col);
1701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 if (has_mbyte)
1703 {
1704 off += mbyte_cells;
1705 col += mbyte_cells;
1706 ptr += mbyte_blen;
1707 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02001708 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001709 // This only happens at the end, display one space next.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02001711 len = -1;
1712 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 }
1714 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 {
1716 ++off;
1717 ++col;
1718 ++ptr;
1719 }
1720 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001721
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001722 // If we detected the next character needs to be redrawn, but the text
1723 // doesn't extend up to there, update the character here.
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001724 if (force_redraw_next && col < screen_Columns)
1725 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001726 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
1727 screen_char_2(off, row, col);
1728 else
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001729 screen_char(off, row, col);
1730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731}
1732
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001733#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001735 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001737 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001738start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739{
1740 if (p_hls && !no_hlsearch)
1741 {
Bram Moolenaar0b6849e2020-05-02 18:33:25 +02001742 end_search_hl(); // just in case it wasn't called before
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001743 last_pat_prog(&screen_search_hl.rm);
1744 screen_search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001745# ifdef FEAT_RELTIME
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001746 // Set the time limit to 'redrawtime'.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001747 profile_setlimit(p_rdt, &screen_search_hl.tm);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001748# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 }
1750}
1751
1752/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001753 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001755 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001756end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757{
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001758 if (screen_search_hl.rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 {
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001760 vim_regfree(screen_search_hl.rm.regprog);
1761 screen_search_hl.rm.regprog = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 }
1763}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02001764#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02001765
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001767screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768{
1769 attrentry_T *aep = NULL;
1770
1771 screen_attr = attr;
1772 if (full_screen
Bram Moolenaar4f974752019-02-17 17:44:42 +01001773#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 && termcap_active
1775#endif
1776 )
1777 {
1778#ifdef FEAT_GUI
1779 if (gui.in_use)
1780 {
1781 char buf[20];
1782
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001783 // The GUI handles this internally.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001784 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 OUT_STR(buf);
1786 }
1787 else
1788#endif
1789 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001790 if (attr > HL_ALL) // special HL attr.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001792 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 aep = syn_cterm_attr2entry(attr);
1794 else
1795 aep = syn_term_attr2entry(attr);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001796 if (aep == NULL) // did ":syntax clear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 attr = 0;
1798 else
1799 attr = aep->ae_attr;
1800 }
Bram Moolenaara050b942019-12-02 21:35:31 +01001801#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
1802 if (use_vtp())
1803 {
1804 guicolor_T defguifg, defguibg;
1805 int defctermfg, defctermbg;
1806
1807 // If FG and BG are unset, the color is undefined when
1808 // BOLD+INVERSE. Use Normal as the default value.
1809 get_default_console_color(&defctermfg, &defctermbg, &defguifg,
1810 &defguibg);
1811
1812 if (p_tgc)
1813 {
1814 if (aep == NULL || COLOR_INVALID(aep->ae_u.cterm.fg_rgb))
1815 term_fg_rgb_color(defguifg);
1816 if (aep == NULL || COLOR_INVALID(aep->ae_u.cterm.bg_rgb))
1817 term_bg_rgb_color(defguibg);
1818 }
1819 else if (t_colors >= 256)
1820 {
1821 if (aep == NULL || aep->ae_u.cterm.fg_color == 0)
1822 term_fg_color(defctermfg);
1823 if (aep == NULL || aep->ae_u.cterm.bg_color == 0)
1824 term_bg_color(defctermbg);
1825 }
1826 }
1827#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001828 if ((attr & HL_BOLD) && *T_MD != NUL) // bold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001830 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001831#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001832 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
1833 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
1834 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001835#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001836 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001837 // If the Normal FG color has BOLD attribute and the new HL
1838 // has a FG color defined, clear BOLD.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001839 out_str(T_ME);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001840 if ((attr & HL_STANDOUT) && *T_SO != NUL) // standout
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 out_str(T_SO);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001842 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) // undercurl
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01001843 out_str(T_UCS);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001844 if (((attr & HL_UNDERLINE) // underline or undercurl
Bram Moolenaar45a00002017-12-22 21:12:34 +01001845 || ((attr & HL_UNDERCURL) && *T_UCS == NUL))
1846 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 out_str(T_US);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001848 if ((attr & HL_ITALIC) && *T_CZH != NUL) // italic
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 out_str(T_CZH);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001850 if ((attr & HL_INVERSE) && *T_MR != NUL) // inverse (reverse)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 out_str(T_MR);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001852 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) // strike
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02001853 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854
1855 /*
1856 * Output the color or start string after bold etc., in case the
1857 * bold etc. override the color setting.
1858 */
1859 if (aep != NULL)
1860 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001861#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001862 // When 'termguicolors' is set but fg or bg is unset,
1863 // fall back to the cterm colors. This helps for SpellBad,
1864 // where the GUI uses a red undercurl.
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001865 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001867 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001868 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001869 }
1870 else
1871#endif
1872 if (t_colors > 1)
1873 {
1874 if (aep->ae_u.cterm.fg_color)
1875 term_fg_color(aep->ae_u.cterm.fg_color - 1);
1876 }
1877#ifdef FEAT_TERMGUICOLORS
1878 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
1879 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001880 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001881 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 }
1883 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001884#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001885 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001887 if (aep->ae_u.cterm.bg_color)
1888 term_bg_color(aep->ae_u.cterm.bg_color - 1);
1889 }
Bram Moolenaare023e882020-05-31 16:42:30 +02001890#ifdef FEAT_TERMGUICOLORS
1891 if (p_tgc && aep->ae_u.cterm.ul_rgb != CTERMCOLOR)
1892 {
1893 if (aep->ae_u.cterm.ul_rgb != INVALCOLOR)
1894 term_ul_rgb_color(aep->ae_u.cterm.ul_rgb);
1895 }
1896 else
1897#endif
1898 if (t_colors > 1)
1899 {
1900 if (aep->ae_u.cterm.ul_color)
1901 term_ul_color(aep->ae_u.cterm.ul_color - 1);
1902 }
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001903
Bram Moolenaarf708ac52018-03-12 21:48:32 +01001904 if (!IS_CTERM)
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001905 {
1906 if (aep->ae_u.term.start != NULL)
1907 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 }
1909 }
1910 }
1911 }
1912}
1913
1914 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001915screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001917 int do_ME = FALSE; // output T_ME code
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02001918#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Bram Moolenaar09307e32020-05-29 21:42:55 +02001919 int do_ME_fg = FALSE, do_ME_bg = FALSE;
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02001920#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921
1922 if (screen_attr != 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01001923#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 && termcap_active
1925#endif
1926 )
1927 {
1928#ifdef FEAT_GUI
1929 if (gui.in_use)
1930 {
1931 char buf[20];
1932
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001933 // use internal GUI code
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
1935 OUT_STR(buf);
1936 }
1937 else
1938#endif
1939 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001940 if (screen_attr > HL_ALL) // special HL attr.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 {
1942 attrentry_T *aep;
1943
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001944 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 {
1946 /*
1947 * Assume that t_me restores the original colors!
1948 */
1949 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001950 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001951#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001952 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
1953 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02001954# ifdef FEAT_VTP
1955 ? !(do_ME_fg = TRUE) : (do_ME_fg = FALSE)
1956# endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001957 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001958#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001959 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001960#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001961 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
1962 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02001963# ifdef FEAT_VTP
1964 ? !(do_ME_bg = TRUE) : (do_ME_bg = FALSE)
1965# endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001966 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001967#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001968 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 do_ME = TRUE;
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02001970#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
1971 if (use_vtp())
1972 {
1973 if (do_ME_fg && do_ME_bg)
1974 do_ME = TRUE;
1975
1976 // FG and BG cannot be separated in T_ME, which is not
1977 // efficient.
1978 if (!do_ME && do_ME_fg)
1979 out_str((char_u *)"\033|39m"); // restore FG
1980 if (!do_ME && do_ME_bg)
1981 out_str((char_u *)"\033|49m"); // restore BG
1982 }
1983 else
1984 {
1985 // Process FG and BG at once.
1986 if (!do_ME)
1987 do_ME = do_ME_fg | do_ME_bg;
1988 }
1989#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 }
1991 else
1992 {
1993 aep = syn_term_attr2entry(screen_attr);
1994 if (aep != NULL && aep->ae_u.term.stop != NULL)
1995 {
1996 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
1997 do_ME = TRUE;
1998 else
1999 out_str(aep->ae_u.term.stop);
2000 }
2001 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002002 if (aep == NULL) // did ":syntax clear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 screen_attr = 0;
2004 else
2005 screen_attr = aep->ae_attr;
2006 }
2007
2008 /*
2009 * Often all ending-codes are equal to T_ME. Avoid outputting the
2010 * same sequence several times.
2011 */
2012 if (screen_attr & HL_STANDOUT)
2013 {
2014 if (STRCMP(T_SE, T_ME) == 0)
2015 do_ME = TRUE;
2016 else
2017 out_str(T_SE);
2018 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01002019 if ((screen_attr & HL_UNDERCURL) && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01002020 {
2021 if (STRCMP(T_UCE, T_ME) == 0)
2022 do_ME = TRUE;
2023 else
2024 out_str(T_UCE);
2025 }
2026 if ((screen_attr & HL_UNDERLINE)
Bram Moolenaar45a00002017-12-22 21:12:34 +01002027 || ((screen_attr & HL_UNDERCURL) && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 {
2029 if (STRCMP(T_UE, T_ME) == 0)
2030 do_ME = TRUE;
2031 else
2032 out_str(T_UE);
2033 }
2034 if (screen_attr & HL_ITALIC)
2035 {
2036 if (STRCMP(T_CZR, T_ME) == 0)
2037 do_ME = TRUE;
2038 else
2039 out_str(T_CZR);
2040 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02002041 if (screen_attr & HL_STRIKETHROUGH)
2042 {
2043 if (STRCMP(T_STE, T_ME) == 0)
2044 do_ME = TRUE;
2045 else
2046 out_str(T_STE);
2047 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
2049 out_str(T_ME);
2050
Bram Moolenaar61be73b2016-04-29 22:59:22 +02002051#ifdef FEAT_TERMGUICOLORS
2052 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002054 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002055 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002056 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002057 term_bg_rgb_color(cterm_normal_bg_gui_color);
Bram Moolenaare023e882020-05-31 16:42:30 +02002058 if (cterm_normal_ul_gui_color != INVALCOLOR)
2059 term_ul_rgb_color(cterm_normal_ul_gui_color);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002060 }
2061 else
2062#endif
2063 {
2064 if (t_colors > 1)
2065 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002066 // set Normal cterm colors
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002067 if (cterm_normal_fg_color != 0)
2068 term_fg_color(cterm_normal_fg_color - 1);
2069 if (cterm_normal_bg_color != 0)
2070 term_bg_color(cterm_normal_bg_color - 1);
Bram Moolenaare023e882020-05-31 16:42:30 +02002071 if (cterm_normal_ul_color != 0)
2072 term_ul_color(cterm_normal_ul_color - 1);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002073 if (cterm_normal_fg_bold)
2074 out_str(T_MD);
2075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 }
2077 }
2078 }
2079 screen_attr = 0;
2080}
2081
2082/*
2083 * Reset the colors for a cterm. Used when leaving Vim.
2084 * The machine specific code may override this again.
2085 */
2086 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002087reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002089 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002091 // set Normal cterm colors
Bram Moolenaar61be73b2016-04-29 22:59:22 +02002092#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002093 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
2094 || cterm_normal_bg_gui_color != INVALCOLOR)
2095 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002096#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002098#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 {
2100 out_str(T_OP);
2101 screen_attr = -1;
2102 }
2103 if (cterm_normal_fg_bold)
2104 {
2105 out_str(T_ME);
2106 screen_attr = -1;
2107 }
2108 }
2109}
2110
2111/*
2112 * Put character ScreenLines["off"] on the screen at position "row" and "col",
2113 * using the attributes from ScreenAttrs["off"].
2114 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002115 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002116screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117{
2118 int attr;
2119
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002120 // Check for illegal values, just in case (could happen just after
2121 // resizing).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 if (row >= screen_Rows || col >= screen_Columns)
2123 return;
2124
Bram Moolenaar33796b32019-06-08 16:01:13 +02002125 // Skip if under the popup menu.
2126 // Popup windows with zindex higher than POPUPMENU_ZINDEX go on top.
2127 if (pum_under_menu(row, col)
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002128#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002129 && screen_zindex <= POPUPMENU_ZINDEX
Bram Moolenaare2c453d2019-08-21 14:37:09 +02002130#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02002131 )
Bram Moolenaarae654382019-01-17 21:09:05 +01002132 return;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002133#ifdef FEAT_PROP_POPUP
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002134 if (blocked_by_popup(row, col))
Bram Moolenaar33796b32019-06-08 16:01:13 +02002135 return;
2136#endif
2137
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002138 // Outputting a character in the last cell on the screen may scroll the
2139 // screen up. Only do it when the "xn" termcap property is set, otherwise
2140 // mark the character invalid (update it when scrolled up).
Bram Moolenaar494838a2015-02-10 19:20:37 +01002141 if (*T_XN == NUL
2142 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143#ifdef FEAT_RIGHTLEFT
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002144 // account for first command-line character in rightleft mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 && !cmdmsg_rl
2146#endif
2147 )
2148 {
2149 ScreenAttrs[off] = (sattr_T)-1;
2150 return;
2151 }
2152
2153 /*
2154 * Stop highlighting first, so it's easier to move the cursor.
2155 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 if (screen_char_attr != 0)
2157 attr = screen_char_attr;
2158 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 attr = ScreenAttrs[off];
2160 if (screen_attr != attr)
2161 screen_stop_highlight();
2162
2163 windgoto(row, col);
2164
2165 if (screen_attr != attr)
2166 screen_start_highlight(attr);
2167
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 if (enc_utf8 && ScreenLinesUC[off] != 0)
2169 {
2170 char_u buf[MB_MAXBYTES + 1];
2171
Bram Moolenaarcb070082016-04-02 22:14:51 +02002172 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002173 {
2174 if (*p_ambw == 'd'
Bram Moolenaara12a1612019-01-24 16:39:02 +01002175#ifdef FEAT_GUI
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002176 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01002177#endif
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002178 )
2179 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002180 // Clear the two screen cells. If the character is actually
2181 // single width it won't change the second cell.
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002182 out_str((char_u *)" ");
2183 term_windgoto(row, col);
2184 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002185 // not sure where the cursor is after drawing the ambiguous width
2186 // character
Bram Moolenaarcb070082016-04-02 22:14:51 +02002187 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002188 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02002189 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002191
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002192 // Convert the UTF-8 character to bytes and write it.
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002193 buf[utfc_char2bytes(off, buf)] = NUL;
2194 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 }
2196 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 out_char(ScreenLines[off]);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002200 // double-byte character in single-width cell
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2202 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 }
2204
2205 screen_cur_col++;
2206}
2207
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208/*
2209 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
2210 * on the screen at position 'row' and 'col'.
2211 * The attributes of the first byte is used for all. This is required to
2212 * output the two bytes of a double-byte character with nothing in between.
2213 */
2214 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002215screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002217 // Check for illegal values (could be wrong when screen was resized).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
2219 return;
2220
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002221 // Outputting the last character on the screen may scrollup the screen.
2222 // Don't to it! Mark the character invalid (update it when scrolled up)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
2224 {
2225 ScreenAttrs[off] = (sattr_T)-1;
2226 return;
2227 }
2228
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002229 // Output the first byte normally (positions the cursor), then write the
2230 // second byte directly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 screen_char(off, row, col);
2232 out_char(ScreenLines[off + 1]);
2233 ++screen_cur_col;
2234}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236/*
2237 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
2238 * This uses the contents of ScreenLines[] and doesn't change it.
2239 */
2240 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002241screen_draw_rectangle(
2242 int row,
2243 int col,
2244 int height,
2245 int width,
2246 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247{
2248 int r, c;
2249 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00002250 int max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002252 // Can't use ScreenLines unless initialized
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002253 if (ScreenLines == NULL)
2254 return;
2255
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 if (invert)
2257 screen_char_attr = HL_INVERSE;
2258 for (r = row; r < row + height; ++r)
2259 {
2260 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00002261 max_off = off + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 for (c = col; c < col + width; ++c)
2263 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00002264 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 {
2266 screen_char_2(off + c, r, c);
2267 ++c;
2268 }
2269 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 {
2271 screen_char(off + c, r, c);
Bram Moolenaar367329b2007-08-30 11:53:22 +00002272 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 ++c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 }
2275 }
2276 }
2277 screen_char_attr = 0;
2278}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280/*
2281 * Redraw the characters for a vertically split window.
2282 */
2283 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002284redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285{
2286 int col;
2287 int width;
2288
2289# ifdef FEAT_CLIPBOARD
2290 clip_may_clear_selection(row, end - 1);
2291# endif
2292
2293 if (wp == NULL)
2294 {
2295 col = 0;
2296 width = Columns;
2297 }
2298 else
2299 {
2300 col = wp->w_wincol;
2301 width = wp->w_width;
2302 }
2303 screen_draw_rectangle(row, col, end - row, width, FALSE);
2304}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002306 void
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002307space_to_screenline(int off, int attr)
2308{
2309 ScreenLines[off] = ' ';
2310 ScreenAttrs[off] = attr;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002311 if (enc_utf8)
2312 ScreenLinesUC[off] = 0;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002313}
2314
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315/*
2316 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
2317 * with character 'c1' in first column followed by 'c2' in the other columns.
2318 * Use attributes 'attr'.
2319 */
2320 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002321screen_fill(
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002322 int start_row,
2323 int end_row,
2324 int start_col,
2325 int end_col,
2326 int c1,
2327 int c2,
2328 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002330 int row;
2331 int col;
2332 int off;
2333 int end_off;
2334 int did_delete;
2335 int c;
2336 int norm_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002338 int force_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339#endif
2340
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002341 if (end_row > screen_Rows) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 end_row = screen_Rows;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002343 if (end_col > screen_Columns) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 end_col = screen_Columns;
2345 if (ScreenLines == NULL
2346 || start_row >= end_row
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002347 || start_col >= end_col) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 return;
2349
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002350 // it's a "normal" terminal when not in a GUI or cterm
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 norm_term = (
2352#ifdef FEAT_GUI
2353 !gui.in_use &&
2354#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002355 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 for (row = start_row; row < end_row; ++row)
2357 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00002358 if (has_mbyte
Bram Moolenaara12a1612019-01-24 16:39:02 +01002359#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00002360 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01002361#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00002362 )
2363 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002364 // When drawing over the right halve of a double-wide char clear
2365 // out the left halve. When drawing over the left halve of a
2366 // double wide-char clear out the right halve. Only needed in a
2367 // terminal.
Bram Moolenaar7693ec62008-07-24 18:29:37 +00002368 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00002369 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00002370 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00002371 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00002372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 /*
2374 * Try to use delete-line termcap code, when no attributes or in a
2375 * "normal" terminal, where a bold/italic space is just a
2376 * space.
2377 */
2378 did_delete = FALSE;
2379 if (c2 == ' '
2380 && end_col == Columns
2381 && can_clear(T_CE)
2382 && (attr == 0
2383 || (norm_term
2384 && attr <= HL_ALL
2385 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
2386 {
2387 /*
2388 * check if we really need to clear something
2389 */
2390 col = start_col;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002391 if (c1 != ' ') // don't clear first char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 ++col;
2393
2394 off = LineOffset[row] + col;
2395 end_off = LineOffset[row] + end_col;
2396
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002397 // skip blanks (used often, keep it fast!)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 if (enc_utf8)
2399 while (off < end_off && ScreenLines[off] == ' '
2400 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
2401 ++off;
2402 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 while (off < end_off && ScreenLines[off] == ' '
2404 && ScreenAttrs[off] == 0)
2405 ++off;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002406 if (off < end_off) // something to be cleared
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 {
2408 col = off - LineOffset[row];
2409 screen_stop_highlight();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002410 term_windgoto(row, col);// clear rest of this screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 out_str(T_CE);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002412 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 col = end_col - col;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002414 while (col--) // clear chars in ScreenLines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002416 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 ++off;
2418 }
2419 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002420 did_delete = TRUE; // the chars are cleared now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 }
2422
2423 off = LineOffset[row] + start_col;
2424 c = c1;
2425 for (col = start_col; col < end_col; ++col)
2426 {
Bram Moolenaar33796b32019-06-08 16:01:13 +02002427 if ((ScreenLines[off] != c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002428 || (enc_utf8 && (int)ScreenLinesUC[off]
2429 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 || ScreenAttrs[off] != attr
2431#if defined(FEAT_GUI) || defined(UNIX)
2432 || force_next
2433#endif
2434 )
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002435#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002436 // Skip if under a(nother) popup.
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002437 && !blocked_by_popup(row, col)
Bram Moolenaar33796b32019-06-08 16:01:13 +02002438#endif
2439 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 {
2441#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002442 // The bold trick may make a single row of pixels appear in
2443 // the next character. When a bold character is removed, the
2444 // next character should be redrawn too. This happens for our
2445 // own GUI and for some xterms.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 if (
2447# ifdef FEAT_GUI
2448 gui.in_use
2449# endif
2450# if defined(FEAT_GUI) && defined(UNIX)
2451 ||
2452# endif
2453# ifdef UNIX
2454 term_is_xterm
2455# endif
2456 )
2457 {
2458 if (ScreenLines[off] != ' '
2459 && (ScreenAttrs[off] > HL_ALL
2460 || ScreenAttrs[off] & HL_BOLD))
2461 force_next = TRUE;
2462 else
2463 force_next = FALSE;
2464 }
2465#endif
2466 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 if (enc_utf8)
2468 {
2469 if (c >= 0x80)
2470 {
2471 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002472 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 }
2474 else
2475 ScreenLinesUC[off] = 0;
2476 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 ScreenAttrs[off] = attr;
2478 if (!did_delete || c != ' ')
2479 screen_char(off, row, col);
2480 }
2481 ++off;
2482 if (col == start_col)
2483 {
2484 if (did_delete)
2485 break;
2486 c = c2;
2487 }
2488 }
2489 if (end_col == Columns)
2490 LineWraps[row] = FALSE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002491 if (row == Rows - 1) // overwritten the command line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 {
2493 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02002494 if (start_col == 0 && end_col == Columns
2495 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002496 clear_cmdline = FALSE; // command line has been cleared
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002497 if (start_col == 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002498 mode_displayed = FALSE; // mode cleared or overwritten
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 }
2500 }
2501}
2502
2503/*
2504 * Check if there should be a delay. Used before clearing or redrawing the
2505 * screen or the command line.
2506 */
2507 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002508check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509{
2510 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
2511 && !did_wait_return
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002512 && emsg_silent == 0
2513 && !in_assert_fails)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 {
2515 out_flush();
Bram Moolenaareda1da02019-11-17 17:06:33 +01002516 ui_delay(1006L, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 emsg_on_display = FALSE;
2518 if (check_msg_scroll)
2519 msg_scroll = FALSE;
2520 }
2521}
2522
2523/*
Bram Moolenaarca57ab52019-04-13 14:53:16 +02002524 * Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect.
2525 */
2526 static void
2527clear_TabPageIdxs(void)
2528{
2529 int scol;
2530
2531 for (scol = 0; scol < Columns; ++scol)
2532 TabPageIdxs[scol] = 0;
2533}
2534
2535/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002537 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 * Returns TRUE if there is a valid screen to write to.
2539 * Returns FALSE when starting up and screen not initialized yet.
2540 */
2541 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002542screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002544 screenalloc(doclear); // allocate screen buffers if size changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 return (ScreenLines != NULL);
2546}
2547
2548/*
2549 * Resize the shell to Rows and Columns.
2550 * Allocate ScreenLines[] and associated items.
2551 *
2552 * There may be some time between setting Rows and Columns and (re)allocating
2553 * ScreenLines[]. This happens when starting up and when (manually) changing
2554 * the shell size. Always use screen_Rows and screen_Columns to access items
2555 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
2556 * final size of the shell is needed.
2557 */
2558 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002559screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560{
2561 int new_row, old_row;
2562#ifdef FEAT_GUI
2563 int old_Rows;
2564#endif
2565 win_T *wp;
2566 int outofmem = FALSE;
2567 int len;
2568 schar_T *new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002570 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002572 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 sattr_T *new_ScreenAttrs;
2574 unsigned *new_LineOffset;
2575 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002576 short *new_TabPageIdxs;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002577#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002578 short *new_popup_mask;
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002579 short *new_popup_mask_next;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002580 char *new_popup_transparent;
Bram Moolenaar33796b32019-06-08 16:01:13 +02002581#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00002582 tabpage_T *tp;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002583 static int entered = FALSE; // avoid recursiveness
2584 static int done_outofmem_msg = FALSE; // did outofmem message
Bram Moolenaar87e817c2009-02-22 20:13:39 +00002585 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586
Bram Moolenaar87e817c2009-02-22 20:13:39 +00002587retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 /*
2589 * Allocation of the screen buffers is done only when the size changes and
2590 * when Rows and Columns have been set and we have started doing full
2591 * screen stuff.
2592 */
2593 if ((ScreenLines != NULL
2594 && Rows == screen_Rows
2595 && Columns == screen_Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 && enc_utf8 == (ScreenLinesUC != NULL)
2597 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaara12a1612019-01-24 16:39:02 +01002598 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 || Rows == 0
2600 || Columns == 0
2601 || (!full_screen && ScreenLines == NULL))
2602 return;
2603
2604 /*
2605 * It's possible that we produce an out-of-memory message below, which
2606 * will cause this function to be called again. To break the loop, just
2607 * return here.
2608 */
2609 if (entered)
2610 return;
2611 entered = TRUE;
2612
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00002613 /*
2614 * Note that the window sizes are updated before reallocating the arrays,
2615 * thus we must not redraw here!
2616 */
2617 ++RedrawingDisabled;
2618
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002619 win_new_shellsize(); // fit the windows in the new sized shell
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002621#ifdef FEAT_GUI_HAIKU
2622 vim_lock_screen(); // be safe, put it here
2623#endif
2624
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002625 comp_col(); // recompute columns for shown command and ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626
2627 /*
2628 * We're changing the size of the screen.
2629 * - Allocate new arrays for ScreenLines and ScreenAttrs.
2630 * - Move lines from the old arrays into the new arrays, clear extra
2631 * lines (unless the screen is going to be cleared).
2632 * - Free the old arrays.
2633 *
2634 * If anything fails, make ScreenLines NULL, so we don't do anything!
2635 * Continuing with the old ScreenLines may result in a crash, because the
2636 * size is wrong.
2637 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002638 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00002640 if (aucmd_win != NULL)
2641 win_free_lsize(aucmd_win);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002642#ifdef FEAT_PROP_POPUP
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002643 // global popup windows
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002644 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002645 win_free_lsize(wp);
2646 // tab-local popup windows
2647 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002648 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002649 win_free_lsize(wp);
2650#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002652 new_ScreenLines = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
Bram Moolenaar216b7102010-03-23 13:56:59 +01002653 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 if (enc_utf8)
2655 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002656 new_ScreenLinesUC = LALLOC_MULT(u8char_T, (Rows + 1) * Columns);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002657 for (i = 0; i < p_mco; ++i)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002658 new_ScreenLinesC[i] = LALLOC_CLEAR_MULT(u8char_T,
2659 (Rows + 1) * Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 }
2661 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002662 new_ScreenLines2 = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
2663 new_ScreenAttrs = LALLOC_MULT(sattr_T, (Rows + 1) * Columns);
2664 new_LineOffset = LALLOC_MULT(unsigned, Rows);
2665 new_LineWraps = LALLOC_MULT(char_u, Rows);
2666 new_TabPageIdxs = LALLOC_MULT(short, Columns);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002667#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002668 new_popup_mask = LALLOC_MULT(short, Rows * Columns);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002669 new_popup_mask_next = LALLOC_MULT(short, Rows * Columns);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002670 new_popup_transparent = LALLOC_MULT(char, Rows * Columns);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002671#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00002673 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 {
2675 if (win_alloc_lines(wp) == FAIL)
2676 {
2677 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00002678 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 }
2680 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00002681 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
2682 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002683 outofmem = TRUE;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002684#ifdef FEAT_PROP_POPUP
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002685 // global popup windows
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002686 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002687 if (win_alloc_lines(wp) == FAIL)
2688 {
2689 outofmem = TRUE;
2690 goto give_up;
2691 }
2692 // tab-local popup windows
2693 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002694 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002695 if (win_alloc_lines(wp) == FAIL)
2696 {
2697 outofmem = TRUE;
2698 goto give_up;
2699 }
2700#endif
2701
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00002702give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002704 for (i = 0; i < p_mco; ++i)
2705 if (new_ScreenLinesC[i] == NULL)
2706 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 if (new_ScreenLines == NULL
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002708 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 || new_ScreenAttrs == NULL
2711 || new_LineOffset == NULL
2712 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00002713 || new_TabPageIdxs == NULL
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002714#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002715 || new_popup_mask == NULL
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002716 || new_popup_mask_next == NULL
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002717 || new_popup_transparent == NULL
Bram Moolenaar33796b32019-06-08 16:01:13 +02002718#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 || outofmem)
2720 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002721 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002722 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002723 // guess the size
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002724 do_outofmem_msg((long_u)((Rows + 1) * Columns));
2725
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002726 // Remember we did this to avoid getting outofmem messages over
2727 // and over again.
Bram Moolenaar89d40322006-08-29 15:30:07 +00002728 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002729 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01002730 VIM_CLEAR(new_ScreenLines);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002731 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002732 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01002733 VIM_CLEAR(new_ScreenLinesC[i]);
2734 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002735 VIM_CLEAR(new_ScreenAttrs);
2736 VIM_CLEAR(new_LineOffset);
2737 VIM_CLEAR(new_LineWraps);
2738 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002739#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002740 VIM_CLEAR(new_popup_mask);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002741 VIM_CLEAR(new_popup_mask_next);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002742 VIM_CLEAR(new_popup_transparent);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002743#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 }
2745 else
2746 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002747 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002748
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 for (new_row = 0; new_row < Rows; ++new_row)
2750 {
2751 new_LineOffset[new_row] = new_row * Columns;
2752 new_LineWraps[new_row] = FALSE;
2753
2754 /*
2755 * If the screen is not going to be cleared, copy as much as
2756 * possible from the old screen to the new one and clear the rest
2757 * (used when resizing the window at the "--more--" prompt or when
2758 * executing an external command, for the GUI).
2759 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002760 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 {
2762 (void)vim_memset(new_ScreenLines + new_row * Columns,
2763 ' ', (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 if (enc_utf8)
2765 {
2766 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
2767 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002768 for (i = 0; i < p_mco; ++i)
2769 (void)vim_memset(new_ScreenLinesC[i]
2770 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 0, (size_t)Columns * sizeof(u8char_T));
2772 }
2773 if (enc_dbcs == DBCS_JPNU)
2774 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
2775 0, (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
2777 0, (size_t)Columns * sizeof(sattr_T));
2778 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002779 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 {
2781 if (screen_Columns < Columns)
2782 len = screen_Columns;
2783 else
2784 len = Columns;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002785 // When switching to utf-8 don't copy characters, they
2786 // may be invalid now. Also when p_mco changes.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002787 if (!(enc_utf8 && ScreenLinesUC == NULL)
2788 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002789 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
2790 ScreenLines + LineOffset[old_row],
2791 (size_t)len * sizeof(schar_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002792 if (enc_utf8 && ScreenLinesUC != NULL
2793 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 {
2795 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
2796 ScreenLinesUC + LineOffset[old_row],
2797 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002798 for (i = 0; i < p_mco; ++i)
2799 mch_memmove(new_ScreenLinesC[i]
2800 + new_LineOffset[new_row],
2801 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 (size_t)len * sizeof(u8char_T));
2803 }
2804 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
2805 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
2806 ScreenLines2 + LineOffset[old_row],
2807 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
2809 ScreenAttrs + LineOffset[old_row],
2810 (size_t)len * sizeof(sattr_T));
2811 }
2812 }
2813 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002814 // Use the last line of the screen for the current line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 current_ScreenLine = new_ScreenLines + Rows * Columns;
Bram Moolenaar6ace95e2019-08-13 23:09:49 +02002816
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002817#ifdef FEAT_PROP_POPUP
Bram Moolenaar6ace95e2019-08-13 23:09:49 +02002818 vim_memset(new_popup_mask, 0, Rows * Columns * sizeof(short));
2819 vim_memset(new_popup_transparent, 0, Rows * Columns * sizeof(char));
2820#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 }
2822
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002823 free_screenlines();
2824
Bram Moolenaar6ace95e2019-08-13 23:09:49 +02002825 // NOTE: this may result in all pointers to become NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 ScreenLines = new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002828 for (i = 0; i < p_mco; ++i)
2829 ScreenLinesC[i] = new_ScreenLinesC[i];
2830 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 ScreenLines2 = new_ScreenLines2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 ScreenAttrs = new_ScreenAttrs;
2833 LineOffset = new_LineOffset;
2834 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002835 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002836#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002837 popup_mask = new_popup_mask;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002838 popup_mask_next = new_popup_mask_next;
2839 popup_transparent = new_popup_transparent;
Bram Moolenaar33796b32019-06-08 16:01:13 +02002840 popup_mask_refresh = TRUE;
2841#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002843 // It's important that screen_Rows and screen_Columns reflect the actual
2844 // size of ScreenLines[]. Set them before calling anything.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845#ifdef FEAT_GUI
2846 old_Rows = screen_Rows;
2847#endif
2848 screen_Rows = Rows;
2849 screen_Columns = Columns;
2850
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002851 must_redraw = CLEAR; // need to clear the screen later
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002852 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 screenclear2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854#ifdef FEAT_GUI
2855 else if (gui.in_use
2856 && !gui.starting
2857 && ScreenLines != NULL
2858 && old_Rows != Rows)
2859 {
Bram Moolenaar7c003aa2020-03-28 20:44:41 +01002860 gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
2861
2862 // Adjust the position of the cursor, for when executing an external
2863 // command.
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002864 if (msg_row >= Rows) // Rows got smaller
2865 msg_row = Rows - 1; // put cursor at last row
2866 else if (Rows > old_Rows) // Rows got bigger
2867 msg_row += Rows - old_Rows; // put cursor in same place
2868 if (msg_col >= Columns) // Columns got smaller
2869 msg_col = Columns - 1; // put cursor at last column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 }
2871#endif
Bram Moolenaarca57ab52019-04-13 14:53:16 +02002872 clear_TabPageIdxs();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002874#ifdef FEAT_GUI_HAIKU
2875 vim_unlock_screen();
2876#endif
2877
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00002879 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00002880
Bram Moolenaar87e817c2009-02-22 20:13:39 +00002881 /*
2882 * Do not apply autocommands more than 3 times to avoid an endless loop
2883 * in case applying autocommands always changes Rows or Columns.
2884 */
2885 if (starting == 0 && ++retry_count <= 3)
2886 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00002887 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002888 // In rare cases, autocommands may have altered Rows or Columns,
2889 // jump back to check if we need to allocate the screen again.
Bram Moolenaar87e817c2009-02-22 20:13:39 +00002890 goto retry;
2891 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892}
2893
2894 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002895free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002896{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002897 int i;
2898
Bram Moolenaar33796b32019-06-08 16:01:13 +02002899 VIM_CLEAR(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002900 for (i = 0; i < Screen_mco; ++i)
Bram Moolenaar33796b32019-06-08 16:01:13 +02002901 VIM_CLEAR(ScreenLinesC[i]);
2902 VIM_CLEAR(ScreenLines2);
2903 VIM_CLEAR(ScreenLines);
2904 VIM_CLEAR(ScreenAttrs);
2905 VIM_CLEAR(LineOffset);
2906 VIM_CLEAR(LineWraps);
2907 VIM_CLEAR(TabPageIdxs);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002908#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002909 VIM_CLEAR(popup_mask);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002910 VIM_CLEAR(popup_mask_next);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002911 VIM_CLEAR(popup_transparent);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002912#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002913}
2914
2915 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002916screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917{
2918 check_for_delay(FALSE);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002919 screenalloc(FALSE); // allocate screen buffers if size changed
2920 screenclear2(); // clear the screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921}
2922
2923 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002924screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925{
2926 int i;
2927
2928 if (starting == NO_SCREEN || ScreenLines == NULL
2929#ifdef FEAT_GUI
2930 || (gui.in_use && gui.starting)
2931#endif
2932 )
2933 return;
2934
2935#ifdef FEAT_GUI
2936 if (!gui.in_use)
2937#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002938 screen_attr = -1; // force setting the Normal colors
2939 screen_stop_highlight(); // don't want highlighting here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940
2941#ifdef FEAT_CLIPBOARD
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002942 // disable selection without redrawing it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 clip_scroll_selection(9999);
2944#endif
2945
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002946 // blank out ScreenLines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 for (i = 0; i < Rows; ++i)
2948 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002949 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 LineWraps[i] = FALSE;
2951 }
2952
2953 if (can_clear(T_CL))
2954 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002955 out_str(T_CL); // clear the display
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002957 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 }
2959 else
2960 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002961 // can't clear the screen, mark all chars with invalid attributes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 for (i = 0; i < Rows; ++i)
2963 lineinvalid(LineOffset[i], (int)Columns);
2964 clear_cmdline = TRUE;
2965 }
2966
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002967 screen_cleared = TRUE; // can use contents of ScreenLines now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968
2969 win_rest_invalid(firstwin);
2970 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002971 redraw_tabline = TRUE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002972 if (must_redraw == CLEAR) // no need to clear again
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 must_redraw = NOT_VALID;
2974 compute_cmdrow();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002975 msg_row = cmdline_row; // put cursor on last line for messages
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 msg_col = 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002977 screen_start(); // don't know where cursor is now
2978 msg_scrolled = 0; // can't scroll back
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 msg_didany = FALSE;
2980 msg_didout = FALSE;
2981}
2982
2983/*
2984 * Clear one line in ScreenLines.
2985 */
2986 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002987lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988{
2989 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 if (enc_utf8)
2991 (void)vim_memset(ScreenLinesUC + off, 0,
2992 (size_t)width * sizeof(u8char_T));
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002993 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994}
2995
2996/*
2997 * Mark one line in ScreenLines invalid by setting the attributes to an
2998 * invalid value.
2999 */
3000 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003001lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002{
3003 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
3004}
3005
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006/*
Bram Moolenaar96916ac2020-07-08 23:09:28 +02003007 * To be called when characters were sent to the terminal directly, outputting
3008 * test on "screen_lnum".
3009 */
3010 void
3011line_was_clobbered(int screen_lnum)
3012{
3013 lineinvalid(LineOffset[screen_lnum], (int)Columns);
3014}
3015
3016/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 * Copy part of a Screenline for vertically split window "wp".
3018 */
3019 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003020linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021{
3022 unsigned off_to = LineOffset[to] + wp->w_wincol;
3023 unsigned off_from = LineOffset[from] + wp->w_wincol;
3024
3025 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
3026 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 if (enc_utf8)
3028 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003029 int i;
3030
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
3032 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003033 for (i = 0; i < p_mco; ++i)
3034 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
3035 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036 }
3037 if (enc_dbcs == DBCS_JPNU)
3038 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
3039 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
3041 wp->w_width * sizeof(sattr_T));
3042}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043
3044/*
3045 * Return TRUE if clearing with term string "p" would work.
3046 * It can't work when the string is empty or it won't set the right background.
Bram Moolenaar33796b32019-06-08 16:01:13 +02003047 * Don't clear to end-of-line when there are popups, it may cause flicker.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 */
3049 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003050can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051{
3052 return (*p != NUL && (t_colors <= 1
3053#ifdef FEAT_GUI
3054 || gui.in_use
3055#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003056#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003057 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02003058 || (!p_tgc && cterm_normal_bg_color == 0)
3059#else
3060 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003061#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02003062 || *T_UT != NUL)
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003063#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02003064 && !(p == T_CE && popup_visible)
3065#endif
3066 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067}
3068
3069/*
3070 * Reset cursor position. Use whenever cursor was moved because of outputting
3071 * something directly to the screen (shell commands) or a terminal control
3072 * code.
3073 */
3074 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003075screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076{
3077 screen_cur_row = screen_cur_col = 9999;
3078}
3079
3080/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 * Move the cursor to position "row","col" in the screen.
3082 * This tries to find the most efficient way to move, minimizing the number of
3083 * characters sent to the terminal.
3084 */
3085 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003086windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003088 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 int i;
3090 int plan;
3091 int cost;
3092 int wouldbe_col;
3093 int noinvcurs;
3094 char_u *bs;
3095 int goto_cost;
3096 int attr;
3097
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003098#define GOTO_COST 7 // assume a term_windgoto() takes about 7 chars
3099#define HIGHL_COST 5 // assume unhighlight takes 5 chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100
3101#define PLAN_LE 1
3102#define PLAN_CR 2
3103#define PLAN_NL 3
3104#define PLAN_WRITE 4
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003105 // Can't use ScreenLines unless initialized
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 if (ScreenLines == NULL)
3107 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 if (col != screen_cur_col || row != screen_cur_row)
3109 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003110 // Check for valid position.
3111 if (row < 0) // window without text lines?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 row = 0;
3113 if (row >= screen_Rows)
3114 row = screen_Rows - 1;
3115 if (col >= screen_Columns)
3116 col = screen_Columns - 1;
3117
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003118 // check if no cursor movement is allowed in highlight mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 if (screen_attr && *T_MS == NUL)
3120 noinvcurs = HIGHL_COST;
3121 else
3122 noinvcurs = 0;
3123 goto_cost = GOTO_COST + noinvcurs;
3124
3125 /*
3126 * Plan how to do the positioning:
3127 * 1. Use CR to move it to column 0, same row.
3128 * 2. Use T_LE to move it a few columns to the left.
3129 * 3. Use NL to move a few lines down, column 0.
3130 * 4. Move a few columns to the right with T_ND or by writing chars.
3131 *
3132 * Don't do this if the cursor went beyond the last column, the cursor
3133 * position is unknown then (some terminals wrap, some don't )
3134 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00003135 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 * characters to move the cursor to the right.
3137 */
3138 if (row >= screen_cur_row && screen_cur_col < Columns)
3139 {
3140 /*
3141 * If the cursor is in the same row, bigger col, we can use CR
3142 * or T_LE.
3143 */
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003144 bs = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 attr = screen_attr;
3146 if (row == screen_cur_row && col < screen_cur_col)
3147 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003148 // "le" is preferred over "bc", because "bc" is obsolete
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 if (*T_LE)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003150 bs = T_LE; // "cursor left"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003152 bs = T_BC; // "backspace character (old)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 if (*bs)
3154 cost = (screen_cur_col - col) * (int)STRLEN(bs);
3155 else
3156 cost = 999;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003157 if (col + 1 < cost) // using CR is less characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 {
3159 plan = PLAN_CR;
3160 wouldbe_col = 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003161 cost = 1; // CR is just one character
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 }
3163 else
3164 {
3165 plan = PLAN_LE;
3166 wouldbe_col = col;
3167 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003168 if (noinvcurs) // will stop highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 {
3170 cost += noinvcurs;
3171 attr = 0;
3172 }
3173 }
3174
3175 /*
3176 * If the cursor is above where we want to be, we can use CR LF.
3177 */
3178 else if (row > screen_cur_row)
3179 {
3180 plan = PLAN_NL;
3181 wouldbe_col = 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003182 cost = (row - screen_cur_row) * 2; // CR LF
3183 if (noinvcurs) // will stop highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 {
3185 cost += noinvcurs;
3186 attr = 0;
3187 }
3188 }
3189
3190 /*
3191 * If the cursor is in the same row, smaller col, just use write.
3192 */
3193 else
3194 {
3195 plan = PLAN_WRITE;
3196 wouldbe_col = screen_cur_col;
3197 cost = 0;
3198 }
3199
3200 /*
3201 * Check if any characters that need to be written have the
3202 * correct attributes. Also avoid UTF-8 characters.
3203 */
3204 i = col - wouldbe_col;
3205 if (i > 0)
3206 cost += i;
3207 if (cost < goto_cost && i > 0)
3208 {
3209 /*
3210 * Check if the attributes are correct without additionally
3211 * stopping highlighting.
3212 */
3213 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
3214 while (i && *p++ == attr)
3215 --i;
3216 if (i != 0)
3217 {
3218 /*
3219 * Try if it works when highlighting is stopped here.
3220 */
3221 if (*--p == 0)
3222 {
3223 cost += noinvcurs;
3224 while (i && *p++ == 0)
3225 --i;
3226 }
3227 if (i != 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003228 cost = 999; // different attributes, don't do it
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 if (enc_utf8)
3231 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003232 // Don't use an UTF-8 char for positioning, it's slow.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 for (i = wouldbe_col; i < col; ++i)
3234 if (ScreenLinesUC[LineOffset[row] + i] != 0)
3235 {
3236 cost = 999;
3237 break;
3238 }
3239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 }
3241
3242 /*
3243 * We can do it without term_windgoto()!
3244 */
3245 if (cost < goto_cost)
3246 {
3247 if (plan == PLAN_LE)
3248 {
3249 if (noinvcurs)
3250 screen_stop_highlight();
3251 while (screen_cur_col > col)
3252 {
3253 out_str(bs);
3254 --screen_cur_col;
3255 }
3256 }
3257 else if (plan == PLAN_CR)
3258 {
3259 if (noinvcurs)
3260 screen_stop_highlight();
3261 out_char('\r');
3262 screen_cur_col = 0;
3263 }
3264 else if (plan == PLAN_NL)
3265 {
3266 if (noinvcurs)
3267 screen_stop_highlight();
3268 while (screen_cur_row < row)
3269 {
3270 out_char('\n');
3271 ++screen_cur_row;
3272 }
3273 screen_cur_col = 0;
3274 }
3275
3276 i = col - screen_cur_col;
3277 if (i > 0)
3278 {
3279 /*
3280 * Use cursor-right if it's one character only. Avoids
3281 * removing a line of pixels from the last bold char, when
3282 * using the bold trick in the GUI.
3283 */
3284 if (T_ND[0] != NUL && T_ND[1] == NUL)
3285 {
3286 while (i-- > 0)
3287 out_char(*T_ND);
3288 }
3289 else
3290 {
3291 int off;
3292
3293 off = LineOffset[row] + screen_cur_col;
3294 while (i-- > 0)
3295 {
3296 if (ScreenAttrs[off] != screen_attr)
3297 screen_stop_highlight();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 if (enc_dbcs == DBCS_JPNU
3301 && ScreenLines[off] == 0x8e)
3302 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 ++off;
3304 }
3305 }
3306 }
3307 }
3308 }
3309 else
3310 cost = 999;
3311
3312 if (cost >= goto_cost)
3313 {
3314 if (noinvcurs)
3315 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02003316 if (row == screen_cur_row && (col > screen_cur_col)
3317 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 term_cursor_right(col - screen_cur_col);
3319 else
3320 term_windgoto(row, col);
3321 }
3322 screen_cur_row = row;
3323 screen_cur_col = col;
3324 }
3325}
3326
3327/*
3328 * Set cursor to its position in the current window.
3329 */
3330 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003331setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332{
Bram Moolenaar987723e2018-03-06 11:43:04 +01003333 setcursor_mayforce(FALSE);
3334}
3335
3336/*
3337 * Set cursor to its position in the current window.
3338 * When "force" is TRUE also when not redrawing.
3339 */
3340 void
3341setcursor_mayforce(int force)
3342{
3343 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 {
3345 validate_cursor();
3346 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02003347 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348#ifdef FEAT_RIGHTLEFT
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003349 // With 'rightleft' set and the cursor on a double-wide
3350 // character, position it on the leftmost column.
Bram Moolenaara12a1612019-01-24 16:39:02 +01003351 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol
3352 - ((has_mbyte
Bram Moolenaar561f9db2008-02-20 13:16:29 +00003353 && (*mb_ptr2cells)(ml_get_cursor()) == 2
Bram Moolenaara12a1612019-01-24 16:39:02 +01003354 && vim_isprintc(gchar_cursor())) ? 2 : 1)) :
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355#endif
3356 curwin->w_wcol));
3357 }
3358}
3359
3360
3361/*
Bram Moolenaar86033562017-07-12 20:24:41 +02003362 * Insert 'line_count' lines at 'row' in window 'wp'.
3363 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
3364 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 * scrolling.
3366 * Returns FAIL if the lines are not inserted, OK for success.
3367 */
3368 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003369win_ins_lines(
3370 win_T *wp,
3371 int row,
3372 int line_count,
3373 int invalid,
3374 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375{
3376 int did_delete;
3377 int nextrow;
3378 int lastrow;
3379 int retval;
3380
3381 if (invalid)
3382 wp->w_lines_valid = 0;
3383
3384 if (wp->w_height < 5)
3385 return FAIL;
3386
3387 if (line_count > wp->w_height - row)
3388 line_count = wp->w_height - row;
3389
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003390 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 if (retval != MAYBE)
3392 return retval;
3393
3394 /*
3395 * If there is a next window or a status line, we first try to delete the
3396 * lines at the bottom to avoid messing what is after the window.
Bram Moolenaarc363fe12019-08-04 18:13:46 +02003397 * If this fails and there are following windows, don't do anything to
3398 * avoid messing up those windows, better just redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 */
3400 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 if (wp->w_next != NULL || wp->w_status_height)
3402 {
3403 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003404 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 did_delete = TRUE;
3406 else if (wp->w_next)
3407 return FAIL;
3408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 /*
3410 * if no lines deleted, blank the lines that will end up below the window
3411 */
3412 if (!did_delete)
3413 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003416 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 lastrow = nextrow + line_count;
3418 if (lastrow > Rows)
3419 lastrow = Rows;
3420 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02003421 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 ' ', ' ', 0);
3423 }
3424
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003425 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 == FAIL)
3427 {
Bram Moolenaarc363fe12019-08-04 18:13:46 +02003428 // deletion will have messed up other windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 if (did_delete)
3430 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 win_rest_invalid(W_NEXT(wp));
3433 }
3434 return FAIL;
3435 }
3436
3437 return OK;
3438}
3439
3440/*
Bram Moolenaar86033562017-07-12 20:24:41 +02003441 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
3443 * If "mayclear" is TRUE the screen will be cleared if it is faster than
3444 * scrolling
3445 * Return OK for success, FAIL if the lines are not deleted.
3446 */
3447 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003448win_del_lines(
3449 win_T *wp,
3450 int row,
3451 int line_count,
3452 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003453 int mayclear,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003454 int clear_attr) // for clearing lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455{
3456 int retval;
3457
3458 if (invalid)
3459 wp->w_lines_valid = 0;
3460
3461 if (line_count > wp->w_height - row)
3462 line_count = wp->w_height - row;
3463
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003464 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 if (retval != MAYBE)
3466 return retval;
3467
3468 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003469 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 return FAIL;
3471
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 /*
3473 * If there are windows or status lines below, try to put them at the
3474 * correct place. If we can't do that, they have to be redrawn.
3475 */
3476 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
3477 {
3478 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003479 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 {
3481 wp->w_redr_status = TRUE;
3482 win_rest_invalid(wp->w_next);
3483 }
3484 }
3485 /*
3486 * If this is the last window and there is no status line, redraw the
3487 * command line later.
3488 */
3489 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 redraw_cmdline = TRUE;
3491 return OK;
3492}
3493
3494/*
3495 * Common code for win_ins_lines() and win_del_lines().
3496 * Returns OK or FAIL when the work has been done.
3497 * Returns MAYBE when not finished yet.
3498 */
3499 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003500win_do_lines(
3501 win_T *wp,
3502 int row,
3503 int line_count,
3504 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003505 int del,
3506 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507{
3508 int retval;
3509
3510 if (!redrawing() || line_count <= 0)
3511 return FAIL;
3512
Bram Moolenaar33796b32019-06-08 16:01:13 +02003513 // When inserting lines would result in loss of command output, just redraw
3514 // the lines.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003515 if (no_win_do_lines_ins && !del)
3516 return FAIL;
3517
Bram Moolenaar33796b32019-06-08 16:01:13 +02003518 // only a few lines left: redraw is faster
Bram Moolenaar4033c552017-09-16 20:54:51 +02003519 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003521 if (!no_win_do_lines_ins)
Bram Moolenaar33796b32019-06-08 16:01:13 +02003522 screenclear(); // will set wp->w_lines_valid to 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 return FAIL;
3524 }
3525
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003526#ifdef FEAT_PROP_POPUP
Bram Moolenaar4c063a02019-06-10 21:24:12 +02003527 // this doesn't work when there are popups visible
Bram Moolenaar33796b32019-06-08 16:01:13 +02003528 if (popup_visible)
3529 return FAIL;
3530#endif
3531
3532 // Delete all remaining lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 if (row + line_count >= wp->w_height)
3534 {
3535 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02003536 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 ' ', ' ', 0);
3538 return OK;
3539 }
3540
3541 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003542 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003544 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003546 if (!no_win_do_lines_ins)
3547 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548
3549 /*
3550 * If the terminal can set a scroll region, use that.
3551 * Always do this in a vertically split window. This will redraw from
3552 * ScreenLines[] when t_CV isn't defined. That's faster than using
3553 * win_line().
3554 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003555 * a character in the lower right corner of the scroll region may cause a
3556 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 */
Bram Moolenaar02631462017-09-22 15:20:32 +02003558 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 scroll_region_set(wp, row);
3562 if (del)
3563 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003564 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 else
3566 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003567 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 scroll_region_reset();
3570 return retval;
3571 }
3572
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003573 if (wp->w_next != NULL && p_tf) // don't delete/insert on fast terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575
3576 return MAYBE;
3577}
3578
3579/*
3580 * window 'wp' and everything after it is messed up, mark it for redraw
3581 */
3582 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003583win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 {
3587 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 wp->w_redr_status = TRUE;
3589 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 }
3591 redraw_cmdline = TRUE;
3592}
3593
3594/*
3595 * The rest of the routines in this file perform screen manipulations. The
3596 * given operation is performed physically on the screen. The corresponding
3597 * change is also made to the internal screen image. In this way, the editor
3598 * anticipates the effect of editing changes on the appearance of the screen.
3599 * That way, when we call screenupdate a complete redraw isn't usually
3600 * necessary. Another advantage is that we can keep adding code to anticipate
3601 * screen changes, and in the meantime, everything still works.
3602 */
3603
3604/*
3605 * types for inserting or deleting lines
3606 */
3607#define USE_T_CAL 1
3608#define USE_T_CDL 2
3609#define USE_T_AL 3
3610#define USE_T_CE 4
3611#define USE_T_DL 5
3612#define USE_T_SR 6
3613#define USE_NL 7
3614#define USE_T_CD 8
3615#define USE_REDRAW 9
3616
3617/*
3618 * insert lines on the screen and update ScreenLines[]
3619 * 'end' is the line after the scrolled part. Normally it is Rows.
3620 * When scrolling region used 'off' is the offset from the top for the region.
3621 * 'row' and 'end' are relative to the start of the region.
3622 *
3623 * return FAIL for failure, OK for success.
3624 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003625 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003626screen_ins_lines(
3627 int off,
3628 int row,
3629 int line_count,
3630 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003631 int clear_attr,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003632 win_T *wp) // NULL or window to use width from
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633{
3634 int i;
3635 int j;
3636 unsigned temp;
3637 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003638 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 int type;
3640 int result_empty;
3641 int can_ce = can_clear(T_CE);
3642
3643 /*
3644 * FAIL if
3645 * - there is no valid screen
3646 * - the screen has to be redrawn completely
3647 * - the line count is less than one
3648 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003649 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar33796b32019-06-08 16:01:13 +02003650 * - there is a popup window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 */
Bram Moolenaar33796b32019-06-08 16:01:13 +02003652 if (!screen_valid(TRUE)
3653 || line_count <= 0 || line_count > p_ttyscroll
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003654#ifdef FEAT_CLIPBOARD
3655 || (clip_star.state != SELECT_CLEARED
3656 && redrawing_for_callback > 0)
3657#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003658#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02003659 || popup_visible
3660#endif
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003661 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 return FAIL;
3663
3664 /*
3665 * There are seven ways to insert lines:
3666 * 0. When in a vertically split window and t_CV isn't set, redraw the
3667 * characters from ScreenLines[].
3668 * 1. Use T_CD (clear to end of display) if it exists and the result of
3669 * the insert is just empty lines
3670 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
3671 * present or line_count > 1. It looks better if we do all the inserts
3672 * at once.
3673 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
3674 * insert is just empty lines and T_CE is not present or line_count >
3675 * 1.
3676 * 4. Use T_AL (insert line) if it exists.
3677 * 5. Use T_CE (erase line) if it exists and the result of the insert is
3678 * just empty lines.
3679 * 6. Use T_DL (delete line) if it exists and the result of the insert is
3680 * just empty lines.
3681 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
3682 * the 'da' flag is not set or we have clear line capability.
3683 * 8. redraw the characters from ScreenLines[].
3684 *
3685 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
3686 * the scrollbar for the window. It does have insert line, use that if it
3687 * exists.
3688 */
3689 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
3691 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02003692 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 type = USE_T_CD;
3694 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
3695 type = USE_T_CAL;
3696 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
3697 type = USE_T_CDL;
3698 else if (*T_AL != NUL)
3699 type = USE_T_AL;
3700 else if (can_ce && result_empty)
3701 type = USE_T_CE;
3702 else if (*T_DL != NUL && result_empty)
3703 type = USE_T_DL;
3704 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
3705 type = USE_T_SR;
3706 else
3707 return FAIL;
3708
3709 /*
3710 * For clearing the lines screen_del_lines() is used. This will also take
3711 * care of t_db if necessary.
3712 */
3713 if (type == USE_T_CD || type == USE_T_CDL ||
3714 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003715 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716
3717 /*
3718 * If text is retained below the screen, first clear or delete as many
3719 * lines at the bottom of the window as are about to be inserted so that
3720 * the deleted lines won't later surface during a screen_del_lines.
3721 */
3722 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003723 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724
3725#ifdef FEAT_CLIPBOARD
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003726 // Remove a modeless selection when inserting lines halfway the screen
3727 // or not the full width of the screen.
Bram Moolenaar4033c552017-09-16 20:54:51 +02003728 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003729 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 else
3731 clip_scroll_selection(-line_count);
3732#endif
3733
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003734#ifdef FEAT_GUI_HAIKU
3735 vim_lock_screen();
3736#endif
3737
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738#ifdef FEAT_GUI
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003739 // Don't update the GUI cursor here, ScreenLines[] is invalid until the
3740 // scrolling is actually carried out.
Bram Moolenaar107abd22016-08-12 14:08:25 +02003741 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742#endif
3743
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003744 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
3745 cursor_col = wp->w_wincol;
3746
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003747 if (*T_CCS != NUL) // cursor relative to region
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 cursor_row = row;
3749 else
3750 cursor_row = row + off;
3751
3752 /*
3753 * Shift LineOffset[] line_count down to reflect the inserted lines.
3754 * Clear the inserted lines in ScreenLines[].
3755 */
3756 row += off;
3757 end += off;
3758 for (i = 0; i < line_count; ++i)
3759 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 if (wp != NULL && wp->w_width != Columns)
3761 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003762 // need to copy part of a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 j = end - 1 - i;
3764 while ((j -= line_count) >= row)
3765 linecopy(j + line_count, j, wp);
3766 j += line_count;
3767 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003768 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
3769 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 else
3771 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
3772 LineWraps[j] = FALSE;
3773 }
3774 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 {
3776 j = end - 1 - i;
3777 temp = LineOffset[j];
3778 while ((j -= line_count) >= row)
3779 {
3780 LineOffset[j + line_count] = LineOffset[j];
3781 LineWraps[j + line_count] = LineWraps[j];
3782 }
3783 LineOffset[j + line_count] = temp;
3784 LineWraps[j + line_count] = FALSE;
3785 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003786 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 else
3788 lineinvalid(temp, (int)Columns);
3789 }
3790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003792#ifdef FEAT_GUI_HAIKU
3793 vim_unlock_screen();
3794#endif
3795
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003797 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003798 if (clear_attr != 0)
3799 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003801 // redraw the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 if (type == USE_REDRAW)
3803 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02003804 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 {
3806 term_append_lines(line_count);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003807 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 }
3809 else
3810 {
3811 for (i = 0; i < line_count; i++)
3812 {
3813 if (type == USE_T_AL)
3814 {
3815 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003816 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 out_str(T_AL);
3818 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003819 else // type == USE_T_SR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 out_str(T_SR);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003821 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 }
3823 }
3824
3825 /*
3826 * With scroll-reverse and 'da' flag set we need to clear the lines that
3827 * have been scrolled down into the region.
3828 */
3829 if (type == USE_T_SR && *T_DA)
3830 {
3831 for (i = 0; i < line_count; ++i)
3832 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003833 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 out_str(T_CE);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003835 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 }
3837 }
3838
3839#ifdef FEAT_GUI
3840 gui_can_update_cursor();
3841 if (gui.in_use)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003842 out_flush(); // always flush after a scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843#endif
3844 return OK;
3845}
3846
3847/*
Bram Moolenaar107abd22016-08-12 14:08:25 +02003848 * Delete lines on the screen and update ScreenLines[].
3849 * "end" is the line after the scrolled part. Normally it is Rows.
3850 * When scrolling region used "off" is the offset from the top for the region.
3851 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 *
3853 * Return OK for success, FAIL if the lines are not deleted.
3854 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003856screen_del_lines(
3857 int off,
3858 int row,
3859 int line_count,
3860 int end,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003861 int force, // even when line_count > p_ttyscroll
3862 int clear_attr, // used for clearing lines
3863 win_T *wp UNUSED) // NULL or window to use width from
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864{
3865 int j;
3866 int i;
3867 unsigned temp;
3868 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003869 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 int cursor_end;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003871 int result_empty; // result is empty until end of region
3872 int can_delete; // deleting line codes can be used
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 int type;
3874
3875 /*
3876 * FAIL if
3877 * - there is no valid screen
3878 * - the screen has to be redrawn completely
3879 * - the line count is less than one
3880 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003881 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003883 if (!screen_valid(TRUE) || line_count <= 0
3884 || (!force && line_count > p_ttyscroll)
3885#ifdef FEAT_CLIPBOARD
3886 || (clip_star.state != SELECT_CLEARED
3887 && redrawing_for_callback > 0)
3888#endif
3889 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 return FAIL;
3891
3892 /*
3893 * Check if the rest of the current region will become empty.
3894 */
3895 result_empty = row + line_count >= end;
3896
3897 /*
3898 * We can delete lines only when 'db' flag not set or when 'ce' option
3899 * available.
3900 */
3901 can_delete = (*T_DB == NUL || can_clear(T_CE));
3902
3903 /*
3904 * There are six ways to delete lines:
3905 * 0. When in a vertically split window and t_CV isn't set, redraw the
3906 * characters from ScreenLines[].
3907 * 1. Use T_CD if it exists and the result is empty.
3908 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
3909 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
3910 * none of the other ways work.
3911 * 4. Use T_CE (erase line) if the result is empty.
3912 * 5. Use T_DL (delete line) if it exists.
3913 * 6. redraw the characters from ScreenLines[].
3914 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
3916 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02003917 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 type = USE_T_CD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 else if (row == 0 && (
3920#ifndef AMIGA
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003921 // On the Amiga, somehow '\n' on the last line doesn't always scroll
3922 // up, so use delete-line command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 line_count == 1 ||
3924#endif
3925 *T_CDL == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 type = USE_NL;
3927 else if (*T_CDL != NUL && line_count > 1 && can_delete)
3928 type = USE_T_CDL;
3929 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +02003930 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 type = USE_T_CE;
3932 else if (*T_DL != NUL && can_delete)
3933 type = USE_T_DL;
3934 else if (*T_CDL != NUL && can_delete)
3935 type = USE_T_CDL;
3936 else
3937 return FAIL;
3938
3939#ifdef FEAT_CLIPBOARD
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003940 // Remove a modeless selection when deleting lines halfway the screen or
3941 // not the full width of the screen.
Bram Moolenaar4033c552017-09-16 20:54:51 +02003942 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003943 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 else
3945 clip_scroll_selection(line_count);
3946#endif
3947
Bram Moolenaar92c461e2020-04-24 22:19:00 +02003948#ifdef FEAT_GUI_HAIKU
3949 vim_lock_screen();
3950#endif
3951
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952#ifdef FEAT_GUI
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003953 // Don't update the GUI cursor here, ScreenLines[] is invalid until the
3954 // scrolling is actually carried out.
Bram Moolenaar107abd22016-08-12 14:08:25 +02003955 gui_dont_update_cursor(gui.cursor_row >= row + off
3956 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957#endif
3958
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003959 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
3960 cursor_col = wp->w_wincol;
3961
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003962 if (*T_CCS != NUL) // cursor relative to region
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 {
3964 cursor_row = row;
3965 cursor_end = end;
3966 }
3967 else
3968 {
3969 cursor_row = row + off;
3970 cursor_end = end + off;
3971 }
3972
3973 /*
3974 * Now shift LineOffset[] line_count up to reflect the deleted lines.
3975 * Clear the inserted lines in ScreenLines[].
3976 */
3977 row += off;
3978 end += off;
3979 for (i = 0; i < line_count; ++i)
3980 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 if (wp != NULL && wp->w_width != Columns)
3982 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003983 // need to copy part of a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 j = row + i;
3985 while ((j += line_count) <= end - 1)
3986 linecopy(j - line_count, j, wp);
3987 j -= line_count;
3988 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003989 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
3990 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 else
3992 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
3993 LineWraps[j] = FALSE;
3994 }
3995 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003997 // whole width, moving the line pointers is faster
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 j = row + i;
3999 temp = LineOffset[j];
4000 while ((j += line_count) <= end - 1)
4001 {
4002 LineOffset[j - line_count] = LineOffset[j];
4003 LineWraps[j - line_count] = LineWraps[j];
4004 }
4005 LineOffset[j - line_count] = temp;
4006 LineWraps[j - line_count] = FALSE;
4007 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02004008 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 else
4010 lineinvalid(temp, (int)Columns);
4011 }
4012 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004014#ifdef FEAT_GUI_HAIKU
4015 vim_unlock_screen();
4016#endif
4017
Bram Moolenaarcfce7172017-08-17 20:31:48 +02004018 if (screen_attr != clear_attr)
4019 screen_stop_highlight();
4020 if (clear_attr != 0)
4021 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004023 // redraw the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 if (type == USE_REDRAW)
4025 redraw_block(row, end, wp);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004026 else if (type == USE_T_CD) // delete the lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02004028 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 out_str(T_CD);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004030 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 }
4032 else if (type == USE_T_CDL)
4033 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02004034 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 term_delete_lines(line_count);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004036 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 }
4038 /*
4039 * Deleting lines at top of the screen or scroll region: Just scroll
4040 * the whole screen (scroll region) up by outputting newlines on the
4041 * last line.
4042 */
4043 else if (type == USE_NL)
4044 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02004045 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 for (i = line_count; --i >= 0; )
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004047 out_char('\n'); // cursor will remain on same line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 }
4049 else
4050 {
4051 for (i = line_count; --i >= 0; )
4052 {
4053 if (type == USE_T_DL)
4054 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02004055 windgoto(cursor_row, cursor_col);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004056 out_str(T_DL); // delete a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004058 else // type == USE_T_CE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02004060 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004061 out_str(T_CE); // erase a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004063 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 }
4065 }
4066
4067 /*
4068 * If the 'db' flag is set, we need to clear the lines that have been
4069 * scrolled up at the bottom of the region.
4070 */
4071 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
4072 {
4073 for (i = line_count; i > 0; --i)
4074 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02004075 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004076 out_str(T_CE); // erase a line
4077 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 }
4079 }
4080
4081#ifdef FEAT_GUI
4082 gui_can_update_cursor();
4083 if (gui.in_use)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004084 out_flush(); // always flush after a scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085#endif
4086
4087 return OK;
4088}
4089
4090/*
Bram Moolenaarcb574f42019-01-25 22:29:57 +01004091 * Return TRUE when postponing displaying the mode message: when not redrawing
4092 * or inside a mapping.
4093 */
4094 int
4095skip_showmode()
4096{
4097 // Call char_avail() only when we are going to show something, because it
4098 // takes a bit of time. redrawing() may also call char_avail_avail().
4099 if (global_busy
4100 || msg_silent != 0
4101 || !redrawing()
4102 || (char_avail() && !KeyTyped))
4103 {
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004104 redraw_mode = TRUE; // show mode later
Bram Moolenaarcb574f42019-01-25 22:29:57 +01004105 return TRUE;
4106 }
4107 return FALSE;
4108}
4109
4110/*
Bram Moolenaar81226e02018-02-20 21:44:45 +01004111 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 *
4113 * If clear_cmdline is TRUE, clear the rest of the cmdline.
4114 * If clear_cmdline is FALSE there may be a message there that needs to be
4115 * cleared only if a mode is shown.
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004116 * If redraw_mode is TRUE show or clear the mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 * Return the length of the message (0 if no message).
4118 */
4119 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004120showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121{
4122 int need_clear;
4123 int length = 0;
4124 int do_mode;
4125 int attr;
4126 int nwr_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 int sub_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128
Bram Moolenaar7df351e2006-01-23 22:30:28 +00004129 do_mode = ((p_smd && msg_silent == 0)
4130 && ((State & INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +02004131 || restart_edit != NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004132 || VIsual_active));
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02004133 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 {
Bram Moolenaarcb574f42019-01-25 22:29:57 +01004135 if (skip_showmode())
4136 return 0; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137
4138 nwr_save = need_wait_return;
4139
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004140 // wait a bit before overwriting an important message
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 check_for_delay(FALSE);
4142
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004143 // if the cmdline is more than one line high, erase top lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 need_clear = clear_cmdline;
4145 if (clear_cmdline && cmdline_row < Rows - 1)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004146 msg_clr_cmdline(); // will reset clear_cmdline
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004148 // Position on the last line in the window, column 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 msg_pos_mode();
4150 cursor_off();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004151 attr = HL_ATTR(HLF_CM); // Highlight mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 if (do_mode)
4153 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004154 msg_puts_attr("--", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00004156 if (
Bram Moolenaar09092152010-08-08 16:38:42 +02004157# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +00004158 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +02004159# else
Bram Moolenaarc236c162008-07-13 17:41:49 +00004160 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +00004161# endif
Bram Moolenaar09092152010-08-08 16:38:42 +02004162 )
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004163# ifdef FEAT_GUI_GTK // most of the time, it's not XIM being used
Bram Moolenaar32526b32019-01-19 17:43:09 +01004164 msg_puts_attr(" IM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165# else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004166 msg_puts_attr(" XIM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167# endif
4168#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004169 // CTRL-X in Insert mode
Bram Moolenaarea389e92014-05-28 21:40:52 +02004170 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004172 // These messages can get long, avoid a wrap in a narrow
4173 // window. Prefer showing edit_submode_extra.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 length = (Rows - msg_row) * Columns - 3;
4175 if (edit_submode_extra != NULL)
4176 length -= vim_strsize(edit_submode_extra);
4177 if (length > 0)
4178 {
4179 if (edit_submode_pre != NULL)
4180 length -= vim_strsize(edit_submode_pre);
4181 if (length - vim_strsize(edit_submode) > 0)
4182 {
4183 if (edit_submode_pre != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004184 msg_puts_attr((char *)edit_submode_pre, attr);
4185 msg_puts_attr((char *)edit_submode, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 }
4187 if (edit_submode_extra != NULL)
4188 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004189 msg_puts_attr(" ", attr); // add a space in between
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004191 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 else
4193 sub_attr = attr;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004194 msg_puts_attr((char *)edit_submode_extra, sub_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 }
4196 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 }
4198 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 if (State & VREPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004201 msg_puts_attr(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02004202 else if (State & REPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004203 msg_puts_attr(_(" REPLACE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 else if (State & INSERT)
4205 {
4206#ifdef FEAT_RIGHTLEFT
4207 if (p_ri)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004208 msg_puts_attr(_(" REVERSE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01004210 msg_puts_attr(_(" INSERT"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01004212 else if (restart_edit == 'I' || restart_edit == 'i' ||
4213 restart_edit == 'a' || restart_edit == 'A')
Bram Moolenaar32526b32019-01-19 17:43:09 +01004214 msg_puts_attr(_(" (insert)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 else if (restart_edit == 'R')
Bram Moolenaar32526b32019-01-19 17:43:09 +01004216 msg_puts_attr(_(" (replace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 else if (restart_edit == 'V')
Bram Moolenaar32526b32019-01-19 17:43:09 +01004218 msg_puts_attr(_(" (vreplace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219#ifdef FEAT_RIGHTLEFT
4220 if (p_hkmap)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004221 msg_puts_attr(_(" Hebrew"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222#endif
4223#ifdef FEAT_KEYMAP
4224 if (State & LANGMAP)
4225 {
4226# ifdef FEAT_ARABIC
4227 if (curwin->w_p_arab)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004228 msg_puts_attr(_(" Arabic"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 else
4230# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02004231 if (get_keymap_str(curwin, (char_u *)" (%s)",
4232 NameBuff, MAXPATHL))
Bram Moolenaar32526b32019-01-19 17:43:09 +01004233 msg_puts_attr((char *)NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 }
4235#endif
4236 if ((State & INSERT) && p_paste)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004237 msg_puts_attr(_(" (paste)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 if (VIsual_active)
4240 {
4241 char *p;
4242
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004243 // Don't concatenate separate words to avoid translation
4244 // problems.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 switch ((VIsual_select ? 4 : 0)
4246 + (VIsual_mode == Ctrl_V) * 2
4247 + (VIsual_mode == 'V'))
4248 {
4249 case 0: p = N_(" VISUAL"); break;
4250 case 1: p = N_(" VISUAL LINE"); break;
4251 case 2: p = N_(" VISUAL BLOCK"); break;
4252 case 4: p = N_(" SELECT"); break;
4253 case 5: p = N_(" SELECT LINE"); break;
4254 default: p = N_(" SELECT BLOCK"); break;
4255 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01004256 msg_puts_attr(_(p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01004258 msg_puts_attr(" --", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004260
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 need_clear = TRUE;
4262 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02004263 if (reg_recording != 0
Bram Moolenaare2c453d2019-08-21 14:37:09 +02004264 && edit_submode == NULL) // otherwise it gets too long
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004266 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 need_clear = TRUE;
4268 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004269
4270 mode_displayed = TRUE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004271 if (need_clear || clear_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 msg_clr_eos();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004273 msg_didout = FALSE; // overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 length = msg_col;
4275 msg_col = 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004276 need_wait_return = nwr_save; // never ask for hit-return for this
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 }
4278 else if (clear_cmdline && msg_silent == 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004279 // Clear the whole command line. Will reset "clear_cmdline".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 msg_clr_cmdline();
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004281 else if (redraw_mode)
4282 {
4283 msg_pos_mode();
4284 msg_clr_eos();
4285 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286
4287#ifdef FEAT_CMDL_INFO
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004288 // In Visual mode the size of the selected area must be redrawn.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 if (VIsual_active)
4290 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004292 // If the last window has no status line, the ruler is after the mode
4293 // message and must be redrawn
Bram Moolenaar4033c552017-09-16 20:54:51 +02004294 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +02004295 win_redr_ruler(lastwin, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296#endif
4297 redraw_cmdline = FALSE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004298 redraw_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 clear_cmdline = FALSE;
4300
4301 return length;
4302}
4303
4304/*
4305 * Position for a mode message.
4306 */
4307 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004308msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309{
4310 msg_col = 0;
4311 msg_row = Rows - 1;
4312}
4313
4314/*
4315 * Delete mode message. Used when ESC is typed which is expected to end
4316 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004317 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 */
4319 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004320unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321{
4322 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +01004323 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 */
4325 if (!redrawing() || (!force && char_avail() && !KeyTyped))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004326 redraw_cmdline = TRUE; // delete mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +02004328 clearmode();
4329}
4330
4331/*
4332 * Clear the mode message.
4333 */
4334 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02004335clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +02004336{
Bram Moolenaar2abad542018-05-19 14:43:45 +02004337 int save_msg_row = msg_row;
4338 int save_msg_col = msg_col;
4339
Bram Moolenaarfd773e92016-04-02 19:39:16 +02004340 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02004341 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004342 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +02004343 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +02004344
4345 msg_col = save_msg_col;
4346 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347}
4348
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004349 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004350recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004351{
Bram Moolenaar32526b32019-01-19 17:43:09 +01004352 msg_puts_attr(_("recording"), attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004353 if (!shortmess(SHM_RECORDING))
4354 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004355 char s[4];
4356
4357 sprintf(s, " @%c", reg_recording);
4358 msg_puts_attr(s, attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004359 }
4360}
4361
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004362/*
4363 * Draw the tab pages line at the top of the Vim window.
4364 */
Bram Moolenaare12bab32019-01-08 22:02:56 +01004365 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004366draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004367{
4368 int tabcount = 0;
4369 tabpage_T *tp;
4370 int tabwidth;
4371 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00004372 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004373 int attr;
4374 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004375 win_T *cwp;
4376 int wincount;
4377 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004378 int c;
4379 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004380 int attr_sel = HL_ATTR(HLF_TPS);
4381 int attr_nosel = HL_ATTR(HLF_TP);
4382 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00004383 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004384 int room;
4385 int use_sep_chars = (t_colors < 8
4386#ifdef FEAT_GUI
4387 && !gui.in_use
4388#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02004389#ifdef FEAT_TERMGUICOLORS
4390 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +02004391#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004392 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004393
Bram Moolenaarc695cec2017-01-08 20:00:04 +01004394 if (ScreenLines == NULL)
4395 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00004396 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004397
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004398#ifdef FEAT_GUI_TABLINE
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004399 // Take care of a GUI tabline.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004400 if (gui_use_tabline())
4401 {
4402 gui_update_tabline();
4403 return;
4404 }
4405#endif
4406
4407 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004408 return;
4409
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004410#if defined(FEAT_STL_OPT)
Bram Moolenaarca57ab52019-04-13 14:53:16 +02004411 clear_TabPageIdxs();
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004412
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004413 // Use the 'tabline' option if it's set.
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004414 if (*p_tal != NUL)
4415 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +02004416 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +00004417
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004418 // Check for an error. If there is one we would loop in redrawing the
4419 // screen. Avoid that by making 'tabline' empty.
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +02004420 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004421 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +02004422 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +00004423 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004424 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +02004425 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004426 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00004427 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004428#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004429 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004430 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00004431 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004432
Bram Moolenaar238a5642006-02-21 22:12:05 +00004433 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
4434 if (tabwidth < 6)
4435 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004436
Bram Moolenaar238a5642006-02-21 22:12:05 +00004437 attr = attr_nosel;
4438 tabcount = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004439 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
4440 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00004441 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00004442 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004443
Bram Moolenaar238a5642006-02-21 22:12:05 +00004444 if (tp->tp_topframe == topframe)
4445 attr = attr_sel;
4446 if (use_sep_chars && col > 0)
4447 screen_putchar('|', 0, col++, attr);
4448
4449 if (tp->tp_topframe != topframe)
4450 attr = attr_nosel;
4451
4452 screen_putchar(' ', 0, col++, attr);
4453
4454 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00004455 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00004456 cwp = curwin;
4457 wp = firstwin;
4458 }
4459 else
4460 {
4461 cwp = tp->tp_curwin;
4462 wp = tp->tp_firstwin;
4463 }
4464
4465 modified = FALSE;
4466 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
4467 if (bufIsChanged(wp->w_buffer))
4468 modified = TRUE;
4469 if (modified || wincount > 1)
4470 {
4471 if (wincount > 1)
4472 {
4473 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004474 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004475 if (col + len >= Columns - 3)
4476 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +00004477 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004478#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004479 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004480#else
Bram Moolenaare0f14822014-08-06 13:20:56 +02004481 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004482#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00004483 );
4484 col += len;
4485 }
4486 if (modified)
4487 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
4488 screen_putchar(' ', 0, col++, attr);
4489 }
4490
4491 room = scol - col + tabwidth - 1;
4492 if (room > 0)
4493 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004494 // Get buffer name in NameBuff[]
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004495 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004496 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +00004497 len = vim_strsize(NameBuff);
4498 p = NameBuff;
Bram Moolenaar238a5642006-02-21 22:12:05 +00004499 if (has_mbyte)
4500 while (len > room)
4501 {
4502 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004503 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +00004504 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01004505 else if (len > room)
Bram Moolenaar238a5642006-02-21 22:12:05 +00004506 {
4507 p += len - room;
4508 len = room;
4509 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004510 if (len > Columns - col - 1)
4511 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +00004512
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004513 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +00004514 col += len;
4515 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00004516 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +00004517
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004518 // Store the tab page number in TabPageIdxs[], so that
4519 // jump_to_mouse() knows where each one is.
Bram Moolenaar238a5642006-02-21 22:12:05 +00004520 ++tabcount;
4521 while (scol < col)
4522 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004523 }
4524
Bram Moolenaar238a5642006-02-21 22:12:05 +00004525 if (use_sep_chars)
4526 c = '_';
4527 else
4528 c = ' ';
4529 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004530
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004531 // Put an "X" for closing the current tab if there are several.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004532 if (first_tabpage->tp_next != NULL)
4533 {
4534 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
4535 TabPageIdxs[Columns - 1] = -999;
4536 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004537 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +00004538
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004539 // Reset the flag here again, in case evaluating 'tabline' causes it to be
4540 // set.
Bram Moolenaarb21e5842006-04-16 18:30:08 +00004541 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004542}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004543
4544/*
4545 * Get buffer name for "buf" into NameBuff[].
4546 * Takes care of special buffer names and translates special characters.
4547 */
4548 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004549get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004550{
4551 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02004552 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004553 else
4554 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
4555 trans_characters(NameBuff, MAXPATHL);
4556}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004557
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558/*
4559 * Get the character to use in a status line. Get its attributes in "*attr".
4560 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02004561 int
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004562fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563{
4564 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004565
4566#ifdef FEAT_TERMINAL
4567 if (bt_terminal(wp->w_buffer))
4568 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004569 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02004570 {
4571 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004572 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02004573 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004574 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02004575 {
4576 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004577 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02004578 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004579 }
4580 else
4581#endif
4582 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004584 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 fill = fill_stl;
4586 }
4587 else
4588 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004589 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 fill = fill_stlnc;
4591 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004592 // Use fill when there is highlighting, and highlighting of current
4593 // window differs, or the fillchars differ, or this is not the
4594 // current window
Bram Moolenaar8820b482017-03-16 17:23:31 +01004595 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004596 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 || (fill_stl != fill_stlnc)))
4598 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004599 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 return '^';
4601 return '=';
4602}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604/*
4605 * Get the character to use in a separator between vertically split windows.
4606 * Get its attributes in "*attr".
4607 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02004608 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004609fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610{
Bram Moolenaar8820b482017-03-16 17:23:31 +01004611 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 if (*attr == 0 && fill_vert == ' ')
4613 return '|';
4614 else
4615 return fill_vert;
4616}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617
4618/*
4619 * Return TRUE if redrawing should currently be done.
4620 */
4621 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004622redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623{
Bram Moolenaareb992cb2017-03-09 18:20:16 +01004624#ifdef FEAT_EVAL
4625 if (disable_redraw_for_testing)
4626 return 0;
4627 else
4628#endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +02004629 return ((!RedrawingDisabled
4630#ifdef FEAT_EVAL
4631 || ignore_redraw_flag_for_testing
4632#endif
4633 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634}
4635
4636/*
4637 * Return TRUE if printing messages should currently be done.
4638 */
4639 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004640messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641{
4642 return (!(p_lz && char_avail() && !KeyTyped));
4643}
4644
Bram Moolenaare677df82019-09-02 22:31:11 +02004645/*
4646 * Compute columns for ruler and shown command. 'sc_col' is also used to
4647 * decide what the maximum length of a message on the status line can be.
4648 * If there is a status line for the last window, 'sc_col' is independent
4649 * of 'ru_col'.
4650 */
4651
4652#define COL_RULER 17 // columns needed by standard ruler
4653
4654 void
4655comp_col(void)
4656{
4657#if defined(FEAT_CMDL_INFO)
4658 int last_has_status = (p_ls == 2 || (p_ls == 1 && !ONE_WINDOW));
4659
4660 sc_col = 0;
4661 ru_col = 0;
4662 if (p_ru)
4663 {
4664# ifdef FEAT_STL_OPT
4665 ru_col = (ru_wid ? ru_wid : COL_RULER) + 1;
4666# else
4667 ru_col = COL_RULER + 1;
4668# endif
4669 // no last status line, adjust sc_col
4670 if (!last_has_status)
4671 sc_col = ru_col;
4672 }
4673 if (p_sc)
4674 {
4675 sc_col += SHOWCMD_COLS;
4676 if (!p_ru || last_has_status) // no need for separating space
4677 ++sc_col;
4678 }
4679 sc_col = Columns - sc_col;
4680 ru_col = Columns - ru_col;
4681 if (sc_col <= 0) // screen too narrow, will become a mess
4682 sc_col = 1;
4683 if (ru_col <= 0)
4684 ru_col = 1;
4685#else
4686 sc_col = Columns;
4687 ru_col = Columns;
4688#endif
4689#ifdef FEAT_EVAL
4690 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
4691#endif
4692}
4693
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004694#if defined(FEAT_LINEBREAK) || defined(PROTO)
4695/*
Bram Moolenaar64486672010-05-16 15:46:46 +02004696 * Return the width of the 'number' and 'relativenumber' column.
4697 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004698 * Otherwise it depends on 'numberwidth' and the line count.
4699 */
4700 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004701number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004702{
4703 int n;
4704 linenr_T lnum;
4705
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004706 if (wp->w_p_rnu && !wp->w_p_nu)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004707 // cursor line shows "0"
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004708 lnum = wp->w_height;
4709 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004710 // cursor line shows absolute line number
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004711 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +02004712
Bram Moolenaar6b314672015-03-20 15:42:10 +01004713 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004714 return wp->w_nrwidth_width;
4715 wp->w_nrwidth_line_count = lnum;
4716
4717 n = 0;
4718 do
4719 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004720 lnum /= 10;
4721 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004722 } while (lnum > 0);
4723
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004724 // 'numberwidth' gives the minimal width plus one
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004725 if (n < wp->w_p_nuw - 1)
4726 n = wp->w_p_nuw - 1;
4727
Bram Moolenaare4b407f2019-07-04 11:59:28 +02004728# ifdef FEAT_SIGNS
4729 // If 'signcolumn' is set to 'number' and there is a sign to display, then
4730 // the minimal width for the number column is 2.
Bram Moolenaar4eb7dae2019-11-12 22:33:45 +01004731 if (n < 2 && get_first_valid_sign(wp) != NULL
Bram Moolenaare4b407f2019-07-04 11:59:28 +02004732 && (*wp->w_p_scl == 'n' && *(wp->w_p_scl + 1) == 'u'))
4733 n = 2;
4734# endif
4735
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004736 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +01004737 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004738 return n;
4739}
4740#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +01004741
Bram Moolenaar113e1072019-01-20 15:30:40 +01004742#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9750bb12012-12-05 16:10:42 +01004743/*
4744 * Return the current cursor column. This is the actual position on the
4745 * screen. First column is 0.
4746 */
4747 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004748screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +01004749{
4750 return screen_cur_col;
4751}
4752
4753/*
4754 * Return the current cursor row. This is the actual position on the screen.
4755 * First row is 0.
4756 */
4757 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004758screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +01004759{
4760 return screen_cur_row;
4761}
Bram Moolenaar113e1072019-01-20 15:30:40 +01004762#endif
Bram Moolenaare677df82019-09-02 22:31:11 +02004763
4764/*
4765 * Handle setting 'listchars' or 'fillchars'.
Bram Moolenaareed9d462021-02-15 20:38:25 +01004766 * Assume monocell characters.
Bram Moolenaare677df82019-09-02 22:31:11 +02004767 * Returns error message, NULL if it's OK.
4768 */
4769 char *
Bram Moolenaareed9d462021-02-15 20:38:25 +01004770set_chars_option(win_T *wp, char_u **varp)
Bram Moolenaare677df82019-09-02 22:31:11 +02004771{
4772 int round, i, len, entries;
4773 char_u *p, *s;
4774 int c1 = 0, c2 = 0, c3 = 0;
4775 struct charstab
4776 {
4777 int *cp;
4778 char *name;
4779 };
4780 static struct charstab filltab[] =
4781 {
Bram Moolenaar3aca5a62021-02-17 13:14:07 +01004782 {&fill_stl, "stl"},
4783 {&fill_stlnc, "stlnc"},
4784 {&fill_vert, "vert"},
4785 {&fill_fold, "fold"},
4786 {&fill_foldopen, "foldopen"},
4787 {&fill_foldclosed, "foldclose"},
4788 {&fill_foldsep, "foldsep"},
4789 {&fill_diff, "diff"},
4790 {&fill_eob, "eob"},
Bram Moolenaare677df82019-09-02 22:31:11 +02004791 };
Bram Moolenaar333bd562021-02-16 22:22:13 +01004792 static lcs_chars_T lcs_chars;
Bram Moolenaareed9d462021-02-15 20:38:25 +01004793 struct charstab lcstab[] =
Bram Moolenaare677df82019-09-02 22:31:11 +02004794 {
Bram Moolenaar333bd562021-02-16 22:22:13 +01004795 {&lcs_chars.eol, "eol"},
4796 {&lcs_chars.ext, "extends"},
4797 {&lcs_chars.nbsp, "nbsp"},
4798 {&lcs_chars.prec, "precedes"},
4799 {&lcs_chars.space, "space"},
4800 {&lcs_chars.tab2, "tab"},
4801 {&lcs_chars.trail, "trail"},
4802 {&lcs_chars.lead, "lead"},
Bram Moolenaare677df82019-09-02 22:31:11 +02004803#ifdef FEAT_CONCEAL
Bram Moolenaar333bd562021-02-16 22:22:13 +01004804 {&lcs_chars.conceal, "conceal"},
Bram Moolenaare677df82019-09-02 22:31:11 +02004805#else
Bram Moolenaar333bd562021-02-16 22:22:13 +01004806 {NULL, "conceal"},
Bram Moolenaare677df82019-09-02 22:31:11 +02004807#endif
4808 };
4809 struct charstab *tab;
4810
Bram Moolenaareed9d462021-02-15 20:38:25 +01004811 if (varp == &p_lcs || varp == &wp->w_p_lcs)
Bram Moolenaare677df82019-09-02 22:31:11 +02004812 {
4813 tab = lcstab;
Bram Moolenaar333bd562021-02-16 22:22:13 +01004814 CLEAR_FIELD(lcs_chars);
Bram Moolenaare677df82019-09-02 22:31:11 +02004815 entries = sizeof(lcstab) / sizeof(struct charstab);
Bram Moolenaareed9d462021-02-15 20:38:25 +01004816 if (varp == &wp->w_p_lcs && wp->w_p_lcs[0] == NUL)
4817 varp = &p_lcs;
Bram Moolenaare677df82019-09-02 22:31:11 +02004818 }
4819 else
4820 {
4821 tab = filltab;
4822 entries = sizeof(filltab) / sizeof(struct charstab);
4823 }
4824
4825 // first round: check for valid value, second round: assign values
4826 for (round = 0; round <= 1; ++round)
4827 {
4828 if (round > 0)
4829 {
4830 // After checking that the value is valid: set defaults: space for
4831 // 'fillchars', NUL for 'listchars'
4832 for (i = 0; i < entries; ++i)
4833 if (tab[i].cp != NULL)
Bram Moolenaareed9d462021-02-15 20:38:25 +01004834 *(tab[i].cp) =
4835 ((varp == &p_lcs || varp == &wp->w_p_lcs) ? NUL : ' ');
Bram Moolenaare677df82019-09-02 22:31:11 +02004836
Bram Moolenaareed9d462021-02-15 20:38:25 +01004837 if (varp == &p_lcs || varp == &wp->w_p_lcs)
Bram Moolenaare677df82019-09-02 22:31:11 +02004838 {
Bram Moolenaar333bd562021-02-16 22:22:13 +01004839 lcs_chars.tab1 = NUL;
4840 lcs_chars.tab3 = NUL;
Bram Moolenaare677df82019-09-02 22:31:11 +02004841 }
4842 else
Bram Moolenaara98f8a22021-02-13 18:24:23 +01004843 {
Bram Moolenaare677df82019-09-02 22:31:11 +02004844 fill_diff = '-';
Bram Moolenaar3aca5a62021-02-17 13:14:07 +01004845 fill_foldopen = '-';
4846 fill_foldclosed = '+';
4847 fill_foldsep = '|';
Bram Moolenaara98f8a22021-02-13 18:24:23 +01004848 fill_eob = '~';
4849 }
Bram Moolenaare677df82019-09-02 22:31:11 +02004850 }
4851 p = *varp;
4852 while (*p)
4853 {
4854 for (i = 0; i < entries; ++i)
4855 {
4856 len = (int)STRLEN(tab[i].name);
4857 if (STRNCMP(p, tab[i].name, len) == 0
4858 && p[len] == ':'
4859 && p[len + 1] != NUL)
4860 {
4861 c2 = c3 = 0;
4862 s = p + len + 1;
4863 c1 = mb_ptr2char_adv(&s);
4864 if (mb_char2cells(c1) > 1)
4865 continue;
Bram Moolenaar333bd562021-02-16 22:22:13 +01004866 if (tab[i].cp == &lcs_chars.tab2)
Bram Moolenaare677df82019-09-02 22:31:11 +02004867 {
4868 if (*s == NUL)
4869 continue;
4870 c2 = mb_ptr2char_adv(&s);
4871 if (mb_char2cells(c2) > 1)
4872 continue;
4873 if (!(*s == ',' || *s == NUL))
4874 {
4875 c3 = mb_ptr2char_adv(&s);
4876 if (mb_char2cells(c3) > 1)
4877 continue;
4878 }
4879 }
4880
4881 if (*s == ',' || *s == NUL)
4882 {
4883 if (round)
4884 {
Bram Moolenaar333bd562021-02-16 22:22:13 +01004885 if (tab[i].cp == &lcs_chars.tab2)
Bram Moolenaare677df82019-09-02 22:31:11 +02004886 {
Bram Moolenaar333bd562021-02-16 22:22:13 +01004887 lcs_chars.tab1 = c1;
4888 lcs_chars.tab2 = c2;
4889 lcs_chars.tab3 = c3;
Bram Moolenaare677df82019-09-02 22:31:11 +02004890 }
4891 else if (tab[i].cp != NULL)
4892 *(tab[i].cp) = c1;
4893
4894 }
4895 p = s;
4896 break;
4897 }
4898 }
4899 }
4900
4901 if (i == entries)
4902 return e_invarg;
4903 if (*p == ',')
4904 ++p;
4905 }
4906 }
Bram Moolenaar333bd562021-02-16 22:22:13 +01004907 if (tab == lcstab)
4908 wp->w_lcs_chars = lcs_chars;
Bram Moolenaare677df82019-09-02 22:31:11 +02004909
4910 return NULL; // no error
4911}
Bram Moolenaar017ba072019-09-14 21:01:23 +02004912