blob: 0b117cd849faec5c6c51e81796dd63412a9d9132 [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 Moolenaar071d4272004-06-13 20:20:40 +0000242 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200243 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100244fill_foldcolumn(
245 char_u *p,
246 win_T *wp,
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100247 int closed, // TRUE of FALSE
248 linenr_T lnum) // current line number
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249{
250 int i = 0;
251 int level;
252 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +0000253 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +0100254 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100256 // Init to all spaces.
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200257 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258
259 level = win_foldinfo.fi_level;
260 if (level > 0)
261 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100262 // If there is only one column put more info in it.
Bram Moolenaar1c934292015-01-27 16:39:29 +0100263 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +0000264
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100265 // If the column is too narrow, we start at the lowest level that
266 // fits and use numbers to indicated the depth.
Bram Moolenaar1c934292015-01-27 16:39:29 +0100267 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268 if (first_level < 1)
269 first_level = 1;
270
Bram Moolenaar1c934292015-01-27 16:39:29 +0100271 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272 {
273 if (win_foldinfo.fi_lnum == lnum
274 && first_level + i >= win_foldinfo.fi_low_level)
275 p[i] = '-';
276 else if (first_level == 1)
277 p[i] = '|';
278 else if (first_level + i <= 9)
279 p[i] = '0' + first_level + i;
280 else
281 p[i] = '>';
282 if (first_level + i == level)
283 break;
284 }
285 }
286 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +0100287 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288}
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100289#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000291/*
292 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +0100293 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000294 */
295 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100296comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000297{
298 int i;
299
300 for (i = 0; i < Screen_mco; ++i)
301 {
302 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
303 return TRUE;
304 if (ScreenLinesC[i][off_from] == 0)
305 break;
306 }
307 return FALSE;
308}
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000309
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310/*
311 * Check whether the given character needs redrawing:
312 * - the (first byte of the) character is different
313 * - the attributes are different
314 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +0000315 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316 */
317 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100318char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319{
320 if (cols > 0
321 && ((ScreenLines[off_from] != ScreenLines[off_to]
322 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323 || (enc_dbcs != 0
324 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
325 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
326 ? ScreenLines2[off_from] != ScreenLines2[off_to]
327 : (cols > 1 && ScreenLines[off_from + 1]
328 != ScreenLines[off_to + 1])))
329 || (enc_utf8
330 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
331 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +0000332 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +0200333 || ((*mb_off2cells)(off_from, off_from + cols) > 1
334 && ScreenLines[off_from + 1]
Bram Moolenaara12a1612019-01-24 16:39:02 +0100335 != ScreenLines[off_to + 1])))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336 return TRUE;
337 return FALSE;
338}
339
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200340#if defined(FEAT_TERMINAL) || defined(PROTO)
341/*
342 * Return the index in ScreenLines[] for the current screen line.
343 */
344 int
345screen_get_current_line_off()
346{
347 return (int)(current_ScreenLine - ScreenLines);
348}
349#endif
350
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100351#ifdef FEAT_PROP_POPUP
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200352/*
353 * Return TRUE if this position has a higher level popup or this cell is
354 * transparent in the current popup.
355 */
356 static int
357blocked_by_popup(int row, int col)
358{
359 int off;
360
361 if (!popup_visible)
362 return FALSE;
363 off = row * screen_Columns + col;
364 return popup_mask[off] > screen_zindex || popup_transparent[off];
365}
366#endif
367
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368/*
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200369 * Reset the highlighting. Used before clearing the screen.
370 */
371 void
372reset_screen_attr(void)
373{
374#ifdef FEAT_GUI
375 if (gui.in_use)
376 // Use a code that will reset gui.highlight_mask in
377 // gui_stop_highlight().
378 screen_attr = HL_ALL + 1;
379 else
380#endif
381 // Use attributes that is very unlikely to appear in text.
382 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE | HL_STRIKETHROUGH;
383}
384
385/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386 * Move one "cooked" screen line to the screen, but only the characters that
387 * have actually changed. Handle insert/delete character.
388 * "coloff" gives the first column on the screen for this line.
389 * "endcol" gives the columns where valid characters are.
390 * "clear_width" is the width of the window. It's > 0 if the rest of the line
391 * needs to be cleared, negative otherwise.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200392 * "flags" can have bits:
393 * SLF_POPUP popup window
394 * SLF_RIGHTLEFT rightleft window:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
396 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
397 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200398 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100399screen_line(
400 int row,
401 int coloff,
402 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200403 int clear_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200404 int flags UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405{
406 unsigned off_from;
407 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +0000408 unsigned max_off_from;
409 unsigned max_off_to;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411 int hl;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100412 int force = FALSE; // force update rest of the line
413 int redraw_this // bool: does character need redraw?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414#ifdef FEAT_GUI
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100415 = TRUE // For GUI when while-loop empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416#endif
417 ;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100418 int redraw_next; // redraw_this for next character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419 int clear_next = FALSE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100420 int char_cells; // 1: normal char
421 // 2: occupies two display cells
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422# define CHAR_CELLS char_cells
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100424 // Check for illegal row and col, just in case.
Bram Moolenaar5ad15df2012-03-16 19:07:58 +0100425 if (row >= Rows)
426 row = Rows - 1;
427 if (endcol > Columns)
428 endcol = Columns;
429
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430# ifdef FEAT_CLIPBOARD
431 clip_may_clear_selection(row, row);
432# endif
433
434 off_from = (unsigned)(current_ScreenLine - ScreenLines);
435 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +0000436 max_off_from = off_from + screen_Columns;
437 max_off_to = LineOffset[row] + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438
439#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200440 if (flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100442 // Clear rest first, because it's left of the text.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443 if (clear_width > 0)
444 {
445 while (col <= endcol && ScreenLines[off_to] == ' '
446 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +0100447 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448 {
449 ++off_to;
450 ++col;
451 }
452 if (col <= endcol)
453 screen_fill(row, row + 1, col + coloff,
454 endcol + coloff + 1, ' ', ' ', 0);
455 }
456 col = endcol + 1;
457 off_to = LineOffset[row] + col + coloff;
458 off_from += col;
459 endcol = (clear_width > 0 ? clear_width : -clear_width);
460 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100461#endif // FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100463#ifdef FEAT_PROP_POPUP
Bram Moolenaar92e25ab2019-11-26 22:39:10 +0100464 // First char of a popup window may go on top of the right half of a
465 // double-wide character. Clear the left half to avoid it getting the popup
466 // window background color.
Bram Moolenaar9943b3d2020-08-22 17:21:14 +0200467 if (coloff > 0 && ScreenLines[off_to] == 0
Bram Moolenaardc0cf1d2020-08-23 15:09:36 +0200468 && ScreenLinesUC[off_to - 1] != 0
469 && (*mb_char2cells)(ScreenLinesUC[off_to - 1]) > 1)
Bram Moolenaar92e25ab2019-11-26 22:39:10 +0100470 {
471 ScreenLines[off_to - 1] = ' ';
472 ScreenLinesUC[off_to - 1] = 0;
473 screen_char(off_to - 1, row, col + coloff - 1);
474 }
475#endif
476
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
478
479 while (col < endcol)
480 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +0000482 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483 else
484 char_cells = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485
486 redraw_this = redraw_next;
487 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
488 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
489
490#ifdef FEAT_GUI
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100491 // If the next character was bold, then redraw the current character to
492 // remove any pixels that might have spilt over into us. This only
493 // happens in the GUI.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494 if (redraw_next && gui.in_use)
495 {
496 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000497 if (hl > HL_ALL)
498 hl = syn_attr2attr(hl);
499 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500 redraw_this = TRUE;
501 }
502#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100503#ifdef FEAT_PROP_POPUP
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200504 if (blocked_by_popup(row, col + coloff))
Bram Moolenaar33796b32019-06-08 16:01:13 +0200505 redraw_this = FALSE;
506#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 if (redraw_this)
508 {
509 /*
510 * Special handling when 'xs' termcap flag set (hpterm):
511 * Attributes for characters are stored at the position where the
512 * cursor is when writing the highlighting code. The
513 * start-highlighting code must be written with the cursor on the
514 * first highlighted character. The stop-highlighting code must
515 * be written with the cursor just after the last highlighted
516 * character.
Bram Moolenaarc4568ab2018-11-16 16:21:05 +0100517 * Overwriting a character doesn't remove its highlighting. Need
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518 * to clear the rest of the line, and force redrawing it
519 * completely.
520 */
521 if ( p_wiv
522 && !force
523#ifdef FEAT_GUI
524 && !gui.in_use
525#endif
526 && ScreenAttrs[off_to] != 0
527 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
528 {
529 /*
530 * Need to remove highlighting attributes here.
531 */
532 windgoto(row, col + coloff);
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100533 out_str(T_CE); // clear rest of this screen line
534 screen_start(); // don't know where cursor is now
535 force = TRUE; // force redraw of rest of the line
536 redraw_next = TRUE; // or else next char would miss out
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537
538 /*
539 * If the previous character was highlighted, need to stop
540 * highlighting at this character.
541 */
542 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
543 {
544 screen_attr = ScreenAttrs[off_to - 1];
545 term_windgoto(row, col + coloff);
546 screen_stop_highlight();
547 }
548 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100549 screen_attr = 0; // highlighting has stopped
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 if (enc_dbcs != 0)
552 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100553 // Check if overwriting a double-byte with a single-byte or
554 // the other way around requires another character to be
555 // redrawn. For UTF-8 this isn't needed, because comparing
556 // ScreenLinesUC[] is sufficient.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557 if (char_cells == 1
558 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +0000559 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100561 // Writing a single-cell character over a double-cell
562 // character: need to redraw the next cell.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 ScreenLines[off_to + 1] = 0;
564 redraw_next = TRUE;
565 }
566 else if (char_cells == 2
567 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +0000568 && (*mb_off2cells)(off_to, max_off_to) == 1
569 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100571 // Writing the second half of a double-cell character over
572 // a double-cell character: need to redraw the second
573 // cell.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 ScreenLines[off_to + 2] = 0;
575 redraw_next = TRUE;
576 }
577
578 if (enc_dbcs == DBCS_JPNU)
579 ScreenLines2[off_to] = ScreenLines2[off_from];
580 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100581 // When writing a single-width character over a double-width
582 // character and at the end of the redrawn text, need to clear out
583 // the right halve of the old character.
584 // Also required when writing the right halve of a double-width
585 // char over the left halve of an existing one.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 if (has_mbyte && col + char_cells == endcol
587 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +0000588 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +0000590 && (*mb_off2cells)(off_to, max_off_to) == 1
591 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 clear_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593
594 ScreenLines[off_to] = ScreenLines[off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 if (enc_utf8)
596 {
597 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
598 if (ScreenLinesUC[off_from] != 0)
599 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000600 int i;
601
602 for (i = 0; i < Screen_mco; ++i)
603 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 }
605 }
606 if (char_cells == 2)
607 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608
609#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100610 // The bold trick makes a single column of pixels appear in the
611 // next character. When a bold character is removed, the next
612 // character should be redrawn too. This happens for our own GUI
613 // and for some xterms.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614 if (
615# ifdef FEAT_GUI
616 gui.in_use
617# endif
618# if defined(FEAT_GUI) && defined(UNIX)
619 ||
620# endif
621# ifdef UNIX
622 term_is_xterm
623# endif
624 )
625 {
626 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000627 if (hl > HL_ALL)
628 hl = syn_attr2attr(hl);
629 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 redraw_next = TRUE;
631 }
632#endif
633 ScreenAttrs[off_to] = ScreenAttrs[off_from];
Bram Moolenaara12a1612019-01-24 16:39:02 +0100634
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100635 // For simplicity set the attributes of second half of a
636 // double-wide character equal to the first half.
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000637 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000639
640 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 screen_char(off_to, row, col + coloff);
644 }
645 else if ( p_wiv
646#ifdef FEAT_GUI
647 && !gui.in_use
648#endif
649 && col + coloff > 0)
650 {
651 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
652 {
653 /*
654 * Don't output stop-highlight when moving the cursor, it will
655 * stop the highlighting when it should continue.
656 */
657 screen_attr = 0;
658 }
659 else if (screen_attr != 0)
660 screen_stop_highlight();
661 }
662
663 off_to += CHAR_CELLS;
664 off_from += CHAR_CELLS;
665 col += CHAR_CELLS;
666 }
667
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 if (clear_next)
669 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100670 // Clear the second half of a double-wide character of which the left
671 // half was overwritten with a single-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 ScreenLines[off_to] = ' ';
673 if (enc_utf8)
674 ScreenLinesUC[off_to] = 0;
675 screen_char(off_to, row, col + coloff);
676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677
678 if (clear_width > 0
679#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200680 && !(flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681#endif
682 )
683 {
684#ifdef FEAT_GUI
685 int startCol = col;
686#endif
687
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100688 // blank out the rest of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 while (col < clear_width && ScreenLines[off_to] == ' '
690 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +0100691 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 {
693 ++off_to;
694 ++col;
695 }
696 if (col < clear_width)
697 {
698#ifdef FEAT_GUI
699 /*
700 * In the GUI, clearing the rest of the line may leave pixels
701 * behind if the first character cleared was bold. Some bold
702 * fonts spill over the left. In this case we redraw the previous
703 * character too. If we didn't skip any blanks above, then we
704 * only redraw if the character wasn't already redrawn anyway.
705 */
Bram Moolenaar9c697322006-10-09 20:11:17 +0000706 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 {
708 hl = ScreenAttrs[off_to];
709 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +0000710 {
711 int prev_cells = 1;
Bram Moolenaara12a1612019-01-24 16:39:02 +0100712
Bram Moolenaar9c697322006-10-09 20:11:17 +0000713 if (enc_utf8)
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100714 // for utf-8, ScreenLines[char_offset + 1] == 0 means
715 // that its width is 2.
Bram Moolenaar9c697322006-10-09 20:11:17 +0000716 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
717 else if (enc_dbcs != 0)
718 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100719 // find previous character by counting from first
720 // column and get its width.
Bram Moolenaar9c697322006-10-09 20:11:17 +0000721 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +0000722 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +0000723
724 while (off < off_to)
725 {
Bram Moolenaar367329b2007-08-30 11:53:22 +0000726 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +0000727 off += prev_cells;
728 }
729 }
730
731 if (enc_dbcs != 0 && prev_cells > 1)
732 screen_char_2(off_to - prev_cells, row,
733 col + coloff - prev_cells);
734 else
Bram Moolenaar9c697322006-10-09 20:11:17 +0000735 screen_char(off_to - prev_cells, row,
736 col + coloff - prev_cells);
737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 }
739#endif
740 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
741 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 off_to += clear_width - col;
743 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 }
745 }
746
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200747 if (clear_width > 0
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100748#ifdef FEAT_PROP_POPUP
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200749 && !(flags & SLF_POPUP) // no separator for popup window
750#endif
751 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200753 // For a window that has a right neighbor, draw the separator char
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200754 // right of the window contents. But not on top of a popup window.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200755 if (coloff + col < Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 {
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100757#ifdef FEAT_PROP_POPUP
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200758 if (!blocked_by_popup(row, col + coloff))
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200759#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200761 int c;
762
763 c = fillchar_vsep(&hl);
764 if (ScreenLines[off_to] != (schar_T)c
765 || (enc_utf8 && (int)ScreenLinesUC[off_to]
766 != (c >= 0x80 ? c : 0))
767 || ScreenAttrs[off_to] != hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200769 ScreenLines[off_to] = c;
770 ScreenAttrs[off_to] = hl;
771 if (enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200773 if (c >= 0x80)
774 {
775 ScreenLinesUC[off_to] = c;
776 ScreenLinesC[0][off_to] = 0;
777 }
778 else
779 ScreenLinesUC[off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 }
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200781 screen_char(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 }
784 }
785 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 LineWraps[row] = FALSE;
787 }
788}
789
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000790#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000792 * Mirror text "str" for right-left displaying.
793 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000795 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100796rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797{
798 char_u *p1, *p2;
799 int t;
800
801 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
802 {
803 t = *p1;
804 *p1 = *p2;
805 *p2 = t;
806 }
807}
808#endif
809
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 * Draw the verticap separator right of window "wp" starting with line "row".
812 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200813 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100814draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815{
816 int hl;
817 int c;
818
819 if (wp->w_vsep_width)
820 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100821 // draw the vertical separator right of this window
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 c = fillchar_vsep(&hl);
823 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
824 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
825 c, ' ', hl);
826 }
827}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828
829#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100830static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831
832/*
Bram Moolenaar367329b2007-08-30 11:53:22 +0000833 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 */
835 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100836status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837{
838 int len = 0;
839
840#ifdef FEAT_MENU
841 int emenu = (xp->xp_context == EXPAND_MENUS
842 || xp->xp_context == EXPAND_MENUNAMES);
843
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100844 // Check for menu separators - replace with '|'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 if (emenu && menu_is_separator(s))
846 return 1;
847#endif
848
849 while (*s != NUL)
850 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +0000851 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +0000852 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100853 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 }
855
856 return len;
857}
858
859/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +0000860 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000861 * These are backslashes used for escaping. Do show backslashes in help tags.
862 */
863 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100864skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000865{
Bram Moolenaar7693ec62008-07-24 18:29:37 +0000866 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000867#ifdef FEAT_MENU
868 || ((xp->xp_context == EXPAND_MENUS
869 || xp->xp_context == EXPAND_MENUNAMES)
870 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
871#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +0000872 )
873 {
874#ifndef BACKSLASH_IN_FILENAME
875 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
876 return 2;
877#endif
878 return 1;
879 }
880 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000881}
882
883/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 * Show wildchar matches in the status line.
885 * Show at least the "match" item.
886 * We start at item 'first_match' in the list and show all matches that fit.
887 *
888 * If inversion is possible we use it. Else '=' characters are used.
889 */
890 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100891win_redr_status_matches(
892 expand_T *xp,
893 int num_matches,
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100894 char_u **matches, // list of matches
Bram Moolenaar05540972016-01-30 20:31:25 +0100895 int match,
896 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897{
898#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
899 int row;
900 char_u *buf;
901 int len;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100902 int clen; // length in screen cells
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 int fillchar;
904 int attr;
905 int i;
906 int highlight = TRUE;
907 char_u *selstart = NULL;
908 int selstart_col = 0;
909 char_u *selend = NULL;
910 static int first_match = 0;
911 int add_left = FALSE;
912 char_u *s;
913#ifdef FEAT_MENU
914 int emenu;
915#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100918 if (matches == NULL) // interrupted completion?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 return;
920
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000921 if (has_mbyte)
Bram Moolenaar964b3742019-05-24 18:54:09 +0200922 buf = alloc(Columns * MB_MAXBYTES + 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000923 else
Bram Moolenaar964b3742019-05-24 18:54:09 +0200924 buf = alloc(Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 if (buf == NULL)
926 return;
927
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100928 if (match == -1) // don't show match but original text
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 {
930 match = 0;
931 highlight = FALSE;
932 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100933 // count 1 for the ending ">"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 clen = status_match_len(xp, L_MATCH(match)) + 3;
935 if (match == 0)
936 first_match = 0;
937 else if (match < first_match)
938 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100939 // jumping left, as far as we can go
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 first_match = match;
941 add_left = TRUE;
942 }
943 else
944 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100945 // check if match fits on the screen
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 for (i = first_match; i < match; ++i)
947 clen += status_match_len(xp, L_MATCH(i)) + 2;
948 if (first_match > 0)
949 clen += 2;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100950 // jumping right, put match at the left
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 if ((long)clen > Columns)
952 {
953 first_match = match;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100954 // if showing the last match, we can add some on the left
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 clen = 2;
956 for (i = match; i < num_matches; ++i)
957 {
958 clen += status_match_len(xp, L_MATCH(i)) + 2;
959 if ((long)clen >= Columns)
960 break;
961 }
962 if (i == num_matches)
963 add_left = TRUE;
964 }
965 }
966 if (add_left)
967 while (first_match > 0)
968 {
969 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
970 if ((long)clen >= Columns)
971 break;
972 --first_match;
973 }
974
Bram Moolenaar3633cf52017-07-31 22:29:35 +0200975 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976
977 if (first_match == 0)
978 {
979 *buf = NUL;
980 len = 0;
981 }
982 else
983 {
984 STRCPY(buf, "< ");
985 len = 2;
986 }
987 clen = len;
988
989 i = first_match;
990 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
991 {
992 if (i == match)
993 {
994 selstart = buf + len;
995 selstart_col = clen;
996 }
997
998 s = L_MATCH(i);
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100999 // Check for menu separators - replace with '|'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000#ifdef FEAT_MENU
1001 emenu = (xp->xp_context == EXPAND_MENUS
1002 || xp->xp_context == EXPAND_MENUNAMES);
1003 if (emenu && menu_is_separator(s))
1004 {
1005 STRCPY(buf + len, transchar('|'));
1006 l = (int)STRLEN(buf + len);
1007 len += l;
1008 clen += l;
1009 }
1010 else
1011#endif
1012 for ( ; *s != NUL; ++s)
1013 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00001014 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 clen += ptr2cells(s);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001016 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 {
1018 STRNCPY(buf + len, s, l);
1019 s += l - 1;
1020 len += l;
1021 }
1022 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 {
1024 STRCPY(buf + len, transchar_byte(*s));
1025 len += (int)STRLEN(buf + len);
1026 }
1027 }
1028 if (i == match)
1029 selend = buf + len;
1030
1031 *(buf + len++) = ' ';
1032 *(buf + len++) = ' ';
1033 clen += 2;
1034 if (++i == num_matches)
1035 break;
1036 }
1037
1038 if (i != num_matches)
1039 {
1040 *(buf + len++) = '>';
1041 ++clen;
1042 }
1043
1044 buf[len] = NUL;
1045
1046 row = cmdline_row - 1;
1047 if (row >= 0)
1048 {
1049 if (wild_menu_showing == 0)
1050 {
1051 if (msg_scrolled > 0)
1052 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001053 // Put the wildmenu just above the command line. If there is
1054 // no room, scroll the screen one line up.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 if (cmdline_row == Rows - 1)
1056 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001057 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 ++msg_scrolled;
1059 }
1060 else
1061 {
1062 ++cmdline_row;
1063 ++row;
1064 }
1065 wild_menu_showing = WM_SCROLLED;
1066 }
1067 else
1068 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001069 // Create status line if needed by setting 'laststatus' to 2.
1070 // Set 'winminheight' to zero to avoid that the window is
1071 // resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 if (lastwin->w_status_height == 0)
1073 {
1074 save_p_ls = p_ls;
1075 save_p_wmh = p_wmh;
1076 p_ls = 2;
1077 p_wmh = 0;
1078 last_status(FALSE);
1079 }
1080 wild_menu_showing = WM_SHOWN;
1081 }
1082 }
1083
1084 screen_puts(buf, row, 0, attr);
1085 if (selstart != NULL && highlight)
1086 {
1087 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001088 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 }
1090
1091 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
1092 }
1093
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 vim_free(buf);
1096}
1097#endif
1098
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 * Return TRUE if the status line of window "wp" is connected to the status
1101 * line of the window right of it. If not, then it's a vertical separator.
1102 * Only call if (wp->w_vsep_width != 0).
1103 */
1104 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001105stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106{
1107 frame_T *fr;
1108
1109 fr = wp->w_frame;
1110 while (fr->fr_parent != NULL)
1111 {
1112 if (fr->fr_parent->fr_layout == FR_COL)
1113 {
1114 if (fr->fr_next != NULL)
1115 break;
1116 }
1117 else
1118 {
1119 if (fr->fr_next != NULL)
1120 return TRUE;
1121 }
1122 fr = fr->fr_parent;
1123 }
1124 return FALSE;
1125}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128/*
1129 * Get the value to show for the language mappings, active 'keymap'.
1130 */
1131 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001132get_keymap_str(
1133 win_T *wp,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001134 char_u *fmt, // format string containing one %s item
1135 char_u *buf, // buffer for the result
1136 int len) // length of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137{
1138 char_u *p;
1139
1140 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
1141 return FALSE;
1142
1143 {
1144#ifdef FEAT_EVAL
1145 buf_T *old_curbuf = curbuf;
1146 win_T *old_curwin = curwin;
1147 char_u *s;
1148
1149 curbuf = wp->w_buffer;
1150 curwin = wp;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001151 STRCPY(buf, "b:keymap_name"); // must be writable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 ++emsg_skip;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001153 s = p = eval_to_string(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 --emsg_skip;
1155 curbuf = old_curbuf;
1156 curwin = old_curwin;
1157 if (p == NULL || *p == NUL)
1158#endif
1159 {
1160#ifdef FEAT_KEYMAP
1161 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
1162 p = wp->w_buffer->b_p_keymap;
1163 else
1164#endif
1165 p = (char_u *)"lang";
1166 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02001167 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 buf[0] = NUL;
1169#ifdef FEAT_EVAL
1170 vim_free(s);
1171#endif
1172 }
1173 return buf[0] != NUL;
1174}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175
1176#if defined(FEAT_STL_OPT) || defined(PROTO)
1177/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001178 * Redraw the status line or ruler of window "wp".
1179 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001181 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001182win_redr_custom(
1183 win_T *wp,
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001184 int draw_ruler) // TRUE or FALSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185{
Bram Moolenaar1d633412013-12-11 15:52:01 +01001186 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 int attr;
1188 int curattr;
1189 int row;
1190 int col = 0;
1191 int maxwidth;
1192 int width;
1193 int n;
1194 int len;
1195 int fillchar;
1196 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00001197 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 char_u *p;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01001199 stl_hlrec_T *hltab;
1200 stl_hlrec_T *tabtab;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001201 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01001202 win_T *ewp;
1203 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001205 // There is a tiny chance that this gets called recursively: When
1206 // redrawing a status line triggers redrawing the ruler or tabline.
1207 // Avoid trouble by not allowing recursion.
Bram Moolenaar1d633412013-12-11 15:52:01 +01001208 if (entered)
1209 return;
1210 entered = TRUE;
1211
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001212 // setup environment for the task at hand
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001213 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 {
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001215 // Use 'tabline'. Always at the first line of the screen.
Bram Moolenaar362f3562009-11-03 16:20:34 +00001216 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001217 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00001218 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01001219 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001220 maxwidth = Columns;
1221# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001222 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001223# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001225 else
1226 {
1227 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02001228 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02001229 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001230
1231 if (draw_ruler)
1232 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00001233 stl = p_ruf;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001234 // advance past any leading group spec - implicit in ru_col
Bram Moolenaar362f3562009-11-03 16:20:34 +00001235 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001236 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00001237 if (*++stl == '-')
1238 stl++;
1239 if (atoi((char *)stl))
1240 while (VIM_ISDIGIT(*stl))
1241 stl++;
1242 if (*stl++ != '(')
1243 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001244 }
Bram Moolenaar02631462017-09-22 15:20:32 +02001245 col = ru_col - (Columns - wp->w_width);
1246 if (col < (wp->w_width + 1) / 2)
1247 col = (wp->w_width + 1) / 2;
1248 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001249 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001250 {
1251 row = Rows - 1;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001252 --maxwidth; // writing in last column may cause scrolling
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001253 fillchar = ' ';
1254 attr = 0;
1255 }
1256
1257# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001258 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001259# endif
1260 }
1261 else
1262 {
1263 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00001264 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001265 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00001266 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001267# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001268 use_sandbox = was_set_insecurely((char_u *)"statusline",
1269 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001270# endif
1271 }
1272
Bram Moolenaar53f81742017-09-22 14:35:51 +02001273 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001274 }
1275
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01001277 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001279 // Temporarily reset 'cursorbind', we don't want a side effect from moving
1280 // the cursor away and back.
Bram Moolenaar61452852011-02-01 18:01:11 +01001281 ewp = wp == NULL ? curwin : wp;
1282 p_crb_save = ewp->w_p_crb;
1283 ewp->w_p_crb = FALSE;
1284
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001285 // Make a copy, because the statusline may include a function call that
1286 // might change the option value and free the memory.
Bram Moolenaar362f3562009-11-03 16:20:34 +00001287 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01001288 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00001289 stl, use_sandbox,
Bram Moolenaar8133cc62020-10-26 21:05:27 +01001290 fillchar, maxwidth, &hltab, &tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00001291 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01001292 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001294 // Make all characters printable.
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01001295 p = transstr(buf);
1296 if (p != NULL)
1297 {
1298 vim_strncpy(buf, p, sizeof(buf) - 1);
1299 vim_free(p);
1300 }
1301
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001302 // fill up with "fillchar"
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01001303 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00001304 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 len += (*mb_char2bytes)(fillchar, buf + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 ++width;
1308 }
1309 buf[len] = NUL;
1310
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001311 /*
1312 * Draw each snippet with the specified highlighting.
1313 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 curattr = attr;
1315 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001316 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001318 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 screen_puts_len(p, len, row, col, curattr);
1320 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001321 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001323 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001325 else if (hltab[n].userhl < 0)
1326 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02001327#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02001328 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
1329 && wp->w_status_height != 0)
1330 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02001331 else if (wp != NULL && bt_terminal(wp->w_buffer)
1332 && wp->w_status_height != 0)
1333 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02001334#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00001335 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001336 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001338 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 }
1340 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001341
1342 if (wp == NULL)
1343 {
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001344 // Fill the TabPageIdxs[] array for clicking in the tab pagesline.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001345 col = 0;
1346 len = 0;
1347 p = buf;
1348 fillchar = 0;
1349 for (n = 0; tabtab[n].start != NULL; n++)
1350 {
1351 len += vim_strnsize(p, (int)(tabtab[n].start - p));
1352 while (col < len)
1353 TabPageIdxs[col++] = fillchar;
1354 p = tabtab[n].start;
1355 fillchar = tabtab[n].userhl;
1356 }
1357 while (col < Columns)
1358 TabPageIdxs[col++] = fillchar;
1359 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01001360
1361theend:
1362 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363}
1364
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001365#endif // FEAT_STL_OPT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366
1367/*
1368 * Output a single character directly to the screen and update ScreenLines.
1369 */
1370 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001371screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 char_u buf[MB_MAXBYTES + 1];
1374
Bram Moolenaar9a920d82012-06-01 15:21:02 +02001375 if (has_mbyte)
1376 buf[(*mb_char2bytes)(c, buf)] = NUL;
1377 else
Bram Moolenaar9a920d82012-06-01 15:21:02 +02001378 {
1379 buf[0] = c;
1380 buf[1] = NUL;
1381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 screen_puts(buf, row, col, attr);
1383}
1384
1385/*
1386 * Get a single character directly from ScreenLines into "bytes[]".
1387 * Also return its attribute in *attrp;
1388 */
1389 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001390screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391{
1392 unsigned off;
1393
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001394 // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
1396 {
1397 off = LineOffset[row] + col;
1398 *attrp = ScreenAttrs[off];
1399 bytes[0] = ScreenLines[off];
1400 bytes[1] = NUL;
1401
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 if (enc_utf8 && ScreenLinesUC[off] != 0)
1403 bytes[utfc_char2bytes(off, bytes)] = NUL;
1404 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
1405 {
1406 bytes[0] = ScreenLines[off];
1407 bytes[1] = ScreenLines2[off];
1408 bytes[2] = NUL;
1409 }
1410 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
1411 {
1412 bytes[1] = ScreenLines[off + 1];
1413 bytes[2] = NUL;
1414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 }
1416}
1417
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001418/*
1419 * Return TRUE if composing characters for screen posn "off" differs from
1420 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01001421 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001422 */
1423 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001424screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001425{
1426 int i;
1427
1428 for (i = 0; i < Screen_mco; ++i)
1429 {
1430 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
1431 return TRUE;
1432 if (u8cc[i] == 0)
1433 break;
1434 }
1435 return FALSE;
1436}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001437
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438/*
1439 * Put string '*text' on the screen at position 'row' and 'col', with
1440 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
1441 * Note: only outputs within one row, message is truncated at screen boundary!
1442 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
1443 */
1444 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001445screen_puts(
1446 char_u *text,
1447 int row,
1448 int col,
1449 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450{
1451 screen_puts_len(text, -1, row, col, attr);
1452}
1453
1454/*
1455 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
1456 * a NUL.
1457 */
1458 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001459screen_puts_len(
1460 char_u *text,
1461 int textlen,
1462 int row,
1463 int col,
1464 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465{
1466 unsigned off;
1467 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02001468 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 int c;
Bram Moolenaar367329b2007-08-30 11:53:22 +00001470 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 int mbyte_blen = 1;
1472 int mbyte_cells = 1;
1473 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001474 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 int clear_next_cell = FALSE;
Bram Moolenaara12a1612019-01-24 16:39:02 +01001476#ifdef FEAT_ARABIC
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001477 int prev_c = 0; // previous Arabic character
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001478 int pc, nc, nc1;
1479 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001481 int force_redraw_this;
1482 int force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001483 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484
Bram Moolenaar0b4c9ed2019-06-03 22:04:23 +02001485 // Safety check. The check for negative row and column is to fix issue
1486 // #4102. TODO: find out why row/col could be negative.
1487 if (ScreenLines == NULL
1488 || row >= screen_Rows || row < 0
1489 || col >= screen_Columns || col < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001491 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001493 // When drawing over the right halve of a double-wide char clear out the
1494 // left halve. Only needed in a terminal.
Bram Moolenaar7693ec62008-07-24 18:29:37 +00001495 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaara12a1612019-01-24 16:39:02 +01001496#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00001497 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01001498#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00001499 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001500 {
1501 ScreenLines[off - 1] = ' ';
1502 ScreenAttrs[off - 1] = 0;
1503 if (enc_utf8)
1504 {
1505 ScreenLinesUC[off - 1] = 0;
1506 ScreenLinesC[0][off - 1] = 0;
1507 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001508 // redraw the previous cell, make it empty
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001509 screen_char(off - 1, row, col - 1);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001510 // force the cell at "col" to be redrawn
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001511 force_redraw_next = TRUE;
1512 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00001513
Bram Moolenaar367329b2007-08-30 11:53:22 +00001514 max_off = LineOffset[row] + screen_Columns;
Bram Moolenaara064ac82007-08-05 18:10:54 +00001515 while (col < screen_Columns
1516 && (len < 0 || (int)(ptr - text) < len)
1517 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 {
1519 c = *ptr;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001520 // check if this is the first byte of a multibyte
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 if (has_mbyte)
1522 {
1523 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001524 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001526 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
1528 mbyte_cells = 1;
1529 else if (enc_dbcs != 0)
1530 mbyte_cells = mbyte_blen;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001531 else // enc_utf8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 {
1533 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001534 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 (int)((text + len) - ptr));
1536 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001537 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaara12a1612019-01-24 16:39:02 +01001539#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
1541 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001542 // Do Arabic shaping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
1544 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001545 // Past end of string to be displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 nc = NUL;
1547 nc1 = NUL;
1548 }
1549 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001550 {
Bram Moolenaar54620182009-11-11 16:07:20 +00001551 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
1552 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001553 nc1 = pcc[0];
1554 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 pc = prev_c;
1556 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001557 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 }
1559 else
1560 prev_c = u8c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01001561#endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01001562 if (col + mbyte_cells > screen_Columns)
1563 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001564 // Only 1 cell left, but character requires 2 cells:
1565 // display a '>' in the last column to avoid wrapping.
Bram Moolenaare4ebd292010-01-19 17:40:46 +01001566 c = '>';
1567 mbyte_cells = 1;
1568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 }
1570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001572 force_redraw_this = force_redraw_next;
1573 force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001574
1575 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 || (mbyte_cells == 2
1577 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
1578 || (enc_dbcs == DBCS_JPNU
1579 && c == 0x8e
1580 && ScreenLines2[off] != ptr[1])
1581 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01001582 && (ScreenLinesUC[off] !=
1583 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
1584 || (ScreenLinesUC[off] != 0
1585 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001587 || exmode_active;
1588
Bram Moolenaar24a5ac52019-06-08 19:01:18 +02001589 if ((need_redraw || force_redraw_this)
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001590#ifdef FEAT_PROP_POPUP
Bram Moolenaarc662ec92019-06-23 00:15:57 +02001591 && !blocked_by_popup(row, col)
Bram Moolenaar24a5ac52019-06-08 19:01:18 +02001592#endif
1593 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 {
1595#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001596 // The bold trick makes a single row of pixels appear in the next
1597 // character. When a bold character is removed, the next
1598 // character should be redrawn too. This happens for our own GUI
1599 // and for some xterms.
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001600 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601# ifdef FEAT_GUI
1602 gui.in_use
1603# endif
1604# if defined(FEAT_GUI) && defined(UNIX)
1605 ||
1606# endif
1607# ifdef UNIX
1608 term_is_xterm
1609# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001610 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001612 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001614 if (n > HL_ALL)
1615 n = syn_attr2attr(n);
1616 if (n & HL_BOLD)
1617 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 }
1619#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001620 // When at the end of the text and overwriting a two-cell
1621 // character with a one-cell character, need to clear the next
1622 // cell. Also when overwriting the left halve of a two-cell char
1623 // with the right halve of a two-cell char. Do this only once
1624 // (mb_off2cells() may return 2 on the right halve).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 if (clear_next_cell)
1626 clear_next_cell = FALSE;
1627 else if (has_mbyte
1628 && (len < 0 ? ptr[mbyte_blen] == NUL
1629 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00001630 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00001632 && (*mb_off2cells)(off, max_off) == 1
1633 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 clear_next_cell = TRUE;
1635
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001636 // Make sure we never leave a second byte of a double-byte behind,
1637 // it confuses mb_off2cells().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00001639 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00001641 && (*mb_off2cells)(off, max_off) == 1
1642 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 ScreenLines[off + mbyte_blen] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 ScreenLines[off] = c;
1645 ScreenAttrs[off] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 if (enc_utf8)
1647 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001648 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 ScreenLinesUC[off] = 0;
1650 else
1651 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001652 int i;
1653
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001655 for (i = 0; i < Screen_mco; ++i)
1656 {
1657 ScreenLinesC[i][off] = u8cc[i];
1658 if (u8cc[i] == 0)
1659 break;
1660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 }
1662 if (mbyte_cells == 2)
1663 {
1664 ScreenLines[off + 1] = 0;
1665 ScreenAttrs[off + 1] = attr;
1666 }
1667 screen_char(off, row, col);
1668 }
1669 else if (mbyte_cells == 2)
1670 {
1671 ScreenLines[off + 1] = ptr[1];
1672 ScreenAttrs[off + 1] = attr;
1673 screen_char_2(off, row, col);
1674 }
1675 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
1676 {
1677 ScreenLines2[off] = ptr[1];
1678 screen_char(off, row, col);
1679 }
1680 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 screen_char(off, row, col);
1682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 if (has_mbyte)
1684 {
1685 off += mbyte_cells;
1686 col += mbyte_cells;
1687 ptr += mbyte_blen;
1688 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02001689 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001690 // This only happens at the end, display one space next.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02001692 len = -1;
1693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 }
1695 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 {
1697 ++off;
1698 ++col;
1699 ++ptr;
1700 }
1701 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001702
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001703 // If we detected the next character needs to be redrawn, but the text
1704 // doesn't extend up to there, update the character here.
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001705 if (force_redraw_next && col < screen_Columns)
1706 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001707 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
1708 screen_char_2(off, row, col);
1709 else
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001710 screen_char(off, row, col);
1711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712}
1713
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001714#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001716 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001718 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001719start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720{
1721 if (p_hls && !no_hlsearch)
1722 {
Bram Moolenaar0b6849e2020-05-02 18:33:25 +02001723 end_search_hl(); // just in case it wasn't called before
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001724 last_pat_prog(&screen_search_hl.rm);
1725 screen_search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001726# ifdef FEAT_RELTIME
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001727 // Set the time limit to 'redrawtime'.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001728 profile_setlimit(p_rdt, &screen_search_hl.tm);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001729# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 }
1731}
1732
1733/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001734 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001736 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001737end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738{
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001739 if (screen_search_hl.rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 {
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001741 vim_regfree(screen_search_hl.rm.regprog);
1742 screen_search_hl.rm.regprog = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 }
1744}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02001745#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02001746
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001748screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749{
1750 attrentry_T *aep = NULL;
1751
1752 screen_attr = attr;
1753 if (full_screen
Bram Moolenaar4f974752019-02-17 17:44:42 +01001754#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 && termcap_active
1756#endif
1757 )
1758 {
1759#ifdef FEAT_GUI
1760 if (gui.in_use)
1761 {
1762 char buf[20];
1763
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001764 // The GUI handles this internally.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001765 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 OUT_STR(buf);
1767 }
1768 else
1769#endif
1770 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001771 if (attr > HL_ALL) // special HL attr.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001773 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 aep = syn_cterm_attr2entry(attr);
1775 else
1776 aep = syn_term_attr2entry(attr);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001777 if (aep == NULL) // did ":syntax clear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 attr = 0;
1779 else
1780 attr = aep->ae_attr;
1781 }
Bram Moolenaara050b942019-12-02 21:35:31 +01001782#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
1783 if (use_vtp())
1784 {
1785 guicolor_T defguifg, defguibg;
1786 int defctermfg, defctermbg;
1787
1788 // If FG and BG are unset, the color is undefined when
1789 // BOLD+INVERSE. Use Normal as the default value.
1790 get_default_console_color(&defctermfg, &defctermbg, &defguifg,
1791 &defguibg);
1792
1793 if (p_tgc)
1794 {
1795 if (aep == NULL || COLOR_INVALID(aep->ae_u.cterm.fg_rgb))
1796 term_fg_rgb_color(defguifg);
1797 if (aep == NULL || COLOR_INVALID(aep->ae_u.cterm.bg_rgb))
1798 term_bg_rgb_color(defguibg);
1799 }
1800 else if (t_colors >= 256)
1801 {
1802 if (aep == NULL || aep->ae_u.cterm.fg_color == 0)
1803 term_fg_color(defctermfg);
1804 if (aep == NULL || aep->ae_u.cterm.bg_color == 0)
1805 term_bg_color(defctermbg);
1806 }
1807 }
1808#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001809 if ((attr & HL_BOLD) && *T_MD != NUL) // bold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001811 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001812#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001813 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
1814 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
1815 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001816#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001817 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001818 // If the Normal FG color has BOLD attribute and the new HL
1819 // has a FG color defined, clear BOLD.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001820 out_str(T_ME);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001821 if ((attr & HL_STANDOUT) && *T_SO != NUL) // standout
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 out_str(T_SO);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001823 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) // undercurl
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01001824 out_str(T_UCS);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001825 if (((attr & HL_UNDERLINE) // underline or undercurl
Bram Moolenaar45a00002017-12-22 21:12:34 +01001826 || ((attr & HL_UNDERCURL) && *T_UCS == NUL))
1827 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 out_str(T_US);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001829 if ((attr & HL_ITALIC) && *T_CZH != NUL) // italic
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 out_str(T_CZH);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001831 if ((attr & HL_INVERSE) && *T_MR != NUL) // inverse (reverse)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 out_str(T_MR);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001833 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) // strike
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02001834 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835
1836 /*
1837 * Output the color or start string after bold etc., in case the
1838 * bold etc. override the color setting.
1839 */
1840 if (aep != NULL)
1841 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001842#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001843 // When 'termguicolors' is set but fg or bg is unset,
1844 // fall back to the cterm colors. This helps for SpellBad,
1845 // where the GUI uses a red undercurl.
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001846 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001848 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001849 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001850 }
1851 else
1852#endif
1853 if (t_colors > 1)
1854 {
1855 if (aep->ae_u.cterm.fg_color)
1856 term_fg_color(aep->ae_u.cterm.fg_color - 1);
1857 }
1858#ifdef FEAT_TERMGUICOLORS
1859 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
1860 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001861 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001862 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 }
1864 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001865#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001866 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001868 if (aep->ae_u.cterm.bg_color)
1869 term_bg_color(aep->ae_u.cterm.bg_color - 1);
1870 }
Bram Moolenaare023e882020-05-31 16:42:30 +02001871#ifdef FEAT_TERMGUICOLORS
1872 if (p_tgc && aep->ae_u.cterm.ul_rgb != CTERMCOLOR)
1873 {
1874 if (aep->ae_u.cterm.ul_rgb != INVALCOLOR)
1875 term_ul_rgb_color(aep->ae_u.cterm.ul_rgb);
1876 }
1877 else
1878#endif
1879 if (t_colors > 1)
1880 {
1881 if (aep->ae_u.cterm.ul_color)
1882 term_ul_color(aep->ae_u.cterm.ul_color - 1);
1883 }
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001884
Bram Moolenaarf708ac52018-03-12 21:48:32 +01001885 if (!IS_CTERM)
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001886 {
1887 if (aep->ae_u.term.start != NULL)
1888 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 }
1890 }
1891 }
1892 }
1893}
1894
1895 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001896screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001898 int do_ME = FALSE; // output T_ME code
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02001899#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Bram Moolenaar09307e32020-05-29 21:42:55 +02001900 int do_ME_fg = FALSE, do_ME_bg = FALSE;
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02001901#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902
1903 if (screen_attr != 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01001904#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 && termcap_active
1906#endif
1907 )
1908 {
1909#ifdef FEAT_GUI
1910 if (gui.in_use)
1911 {
1912 char buf[20];
1913
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001914 // use internal GUI code
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
1916 OUT_STR(buf);
1917 }
1918 else
1919#endif
1920 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001921 if (screen_attr > HL_ALL) // special HL attr.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 {
1923 attrentry_T *aep;
1924
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001925 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 {
1927 /*
1928 * Assume that t_me restores the original colors!
1929 */
1930 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001931 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001932#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001933 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
1934 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02001935# ifdef FEAT_VTP
1936 ? !(do_ME_fg = TRUE) : (do_ME_fg = FALSE)
1937# endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001938 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001939#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001940 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001941#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001942 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
1943 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02001944# ifdef FEAT_VTP
1945 ? !(do_ME_bg = TRUE) : (do_ME_bg = FALSE)
1946# endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001947 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001948#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001949 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 do_ME = TRUE;
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02001951#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
1952 if (use_vtp())
1953 {
1954 if (do_ME_fg && do_ME_bg)
1955 do_ME = TRUE;
1956
1957 // FG and BG cannot be separated in T_ME, which is not
1958 // efficient.
1959 if (!do_ME && do_ME_fg)
1960 out_str((char_u *)"\033|39m"); // restore FG
1961 if (!do_ME && do_ME_bg)
1962 out_str((char_u *)"\033|49m"); // restore BG
1963 }
1964 else
1965 {
1966 // Process FG and BG at once.
1967 if (!do_ME)
1968 do_ME = do_ME_fg | do_ME_bg;
1969 }
1970#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 }
1972 else
1973 {
1974 aep = syn_term_attr2entry(screen_attr);
1975 if (aep != NULL && aep->ae_u.term.stop != NULL)
1976 {
1977 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
1978 do_ME = TRUE;
1979 else
1980 out_str(aep->ae_u.term.stop);
1981 }
1982 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001983 if (aep == NULL) // did ":syntax clear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 screen_attr = 0;
1985 else
1986 screen_attr = aep->ae_attr;
1987 }
1988
1989 /*
1990 * Often all ending-codes are equal to T_ME. Avoid outputting the
1991 * same sequence several times.
1992 */
1993 if (screen_attr & HL_STANDOUT)
1994 {
1995 if (STRCMP(T_SE, T_ME) == 0)
1996 do_ME = TRUE;
1997 else
1998 out_str(T_SE);
1999 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01002000 if ((screen_attr & HL_UNDERCURL) && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01002001 {
2002 if (STRCMP(T_UCE, T_ME) == 0)
2003 do_ME = TRUE;
2004 else
2005 out_str(T_UCE);
2006 }
2007 if ((screen_attr & HL_UNDERLINE)
Bram Moolenaar45a00002017-12-22 21:12:34 +01002008 || ((screen_attr & HL_UNDERCURL) && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 {
2010 if (STRCMP(T_UE, T_ME) == 0)
2011 do_ME = TRUE;
2012 else
2013 out_str(T_UE);
2014 }
2015 if (screen_attr & HL_ITALIC)
2016 {
2017 if (STRCMP(T_CZR, T_ME) == 0)
2018 do_ME = TRUE;
2019 else
2020 out_str(T_CZR);
2021 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02002022 if (screen_attr & HL_STRIKETHROUGH)
2023 {
2024 if (STRCMP(T_STE, T_ME) == 0)
2025 do_ME = TRUE;
2026 else
2027 out_str(T_STE);
2028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
2030 out_str(T_ME);
2031
Bram Moolenaar61be73b2016-04-29 22:59:22 +02002032#ifdef FEAT_TERMGUICOLORS
2033 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002035 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002036 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002037 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002038 term_bg_rgb_color(cterm_normal_bg_gui_color);
Bram Moolenaare023e882020-05-31 16:42:30 +02002039 if (cterm_normal_ul_gui_color != INVALCOLOR)
2040 term_ul_rgb_color(cterm_normal_ul_gui_color);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002041 }
2042 else
2043#endif
2044 {
2045 if (t_colors > 1)
2046 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002047 // set Normal cterm colors
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002048 if (cterm_normal_fg_color != 0)
2049 term_fg_color(cterm_normal_fg_color - 1);
2050 if (cterm_normal_bg_color != 0)
2051 term_bg_color(cterm_normal_bg_color - 1);
Bram Moolenaare023e882020-05-31 16:42:30 +02002052 if (cterm_normal_ul_color != 0)
2053 term_ul_color(cterm_normal_ul_color - 1);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002054 if (cterm_normal_fg_bold)
2055 out_str(T_MD);
2056 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 }
2058 }
2059 }
2060 screen_attr = 0;
2061}
2062
2063/*
2064 * Reset the colors for a cterm. Used when leaving Vim.
2065 * The machine specific code may override this again.
2066 */
2067 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002068reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002070 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002072 // set Normal cterm colors
Bram Moolenaar61be73b2016-04-29 22:59:22 +02002073#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002074 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
2075 || cterm_normal_bg_gui_color != INVALCOLOR)
2076 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002077#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002079#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 {
2081 out_str(T_OP);
2082 screen_attr = -1;
2083 }
2084 if (cterm_normal_fg_bold)
2085 {
2086 out_str(T_ME);
2087 screen_attr = -1;
2088 }
2089 }
2090}
2091
2092/*
2093 * Put character ScreenLines["off"] on the screen at position "row" and "col",
2094 * using the attributes from ScreenAttrs["off"].
2095 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002096 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002097screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098{
2099 int attr;
2100
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002101 // Check for illegal values, just in case (could happen just after
2102 // resizing).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 if (row >= screen_Rows || col >= screen_Columns)
2104 return;
2105
Bram Moolenaar33796b32019-06-08 16:01:13 +02002106 // Skip if under the popup menu.
2107 // Popup windows with zindex higher than POPUPMENU_ZINDEX go on top.
2108 if (pum_under_menu(row, col)
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002109#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002110 && screen_zindex <= POPUPMENU_ZINDEX
Bram Moolenaare2c453d2019-08-21 14:37:09 +02002111#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02002112 )
Bram Moolenaarae654382019-01-17 21:09:05 +01002113 return;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002114#ifdef FEAT_PROP_POPUP
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002115 if (blocked_by_popup(row, col))
Bram Moolenaar33796b32019-06-08 16:01:13 +02002116 return;
2117#endif
2118
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002119 // Outputting a character in the last cell on the screen may scroll the
2120 // screen up. Only do it when the "xn" termcap property is set, otherwise
2121 // mark the character invalid (update it when scrolled up).
Bram Moolenaar494838a2015-02-10 19:20:37 +01002122 if (*T_XN == NUL
2123 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124#ifdef FEAT_RIGHTLEFT
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002125 // account for first command-line character in rightleft mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 && !cmdmsg_rl
2127#endif
2128 )
2129 {
2130 ScreenAttrs[off] = (sattr_T)-1;
2131 return;
2132 }
2133
2134 /*
2135 * Stop highlighting first, so it's easier to move the cursor.
2136 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 if (screen_char_attr != 0)
2138 attr = screen_char_attr;
2139 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 attr = ScreenAttrs[off];
2141 if (screen_attr != attr)
2142 screen_stop_highlight();
2143
2144 windgoto(row, col);
2145
2146 if (screen_attr != attr)
2147 screen_start_highlight(attr);
2148
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 if (enc_utf8 && ScreenLinesUC[off] != 0)
2150 {
2151 char_u buf[MB_MAXBYTES + 1];
2152
Bram Moolenaarcb070082016-04-02 22:14:51 +02002153 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002154 {
2155 if (*p_ambw == 'd'
Bram Moolenaara12a1612019-01-24 16:39:02 +01002156#ifdef FEAT_GUI
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002157 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01002158#endif
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002159 )
2160 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002161 // Clear the two screen cells. If the character is actually
2162 // single width it won't change the second cell.
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002163 out_str((char_u *)" ");
2164 term_windgoto(row, col);
2165 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002166 // not sure where the cursor is after drawing the ambiguous width
2167 // character
Bram Moolenaarcb070082016-04-02 22:14:51 +02002168 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002169 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02002170 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002172
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002173 // Convert the UTF-8 character to bytes and write it.
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002174 buf[utfc_char2bytes(off, buf)] = NUL;
2175 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 }
2177 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 out_char(ScreenLines[off]);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002181 // double-byte character in single-width cell
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2183 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 }
2185
2186 screen_cur_col++;
2187}
2188
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189/*
2190 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
2191 * on the screen at position 'row' and 'col'.
2192 * The attributes of the first byte is used for all. This is required to
2193 * output the two bytes of a double-byte character with nothing in between.
2194 */
2195 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002196screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002198 // Check for illegal values (could be wrong when screen was resized).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
2200 return;
2201
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002202 // Outputting the last character on the screen may scrollup the screen.
2203 // Don't to it! Mark the character invalid (update it when scrolled up)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
2205 {
2206 ScreenAttrs[off] = (sattr_T)-1;
2207 return;
2208 }
2209
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002210 // Output the first byte normally (positions the cursor), then write the
2211 // second byte directly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 screen_char(off, row, col);
2213 out_char(ScreenLines[off + 1]);
2214 ++screen_cur_col;
2215}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217/*
2218 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
2219 * This uses the contents of ScreenLines[] and doesn't change it.
2220 */
2221 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002222screen_draw_rectangle(
2223 int row,
2224 int col,
2225 int height,
2226 int width,
2227 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228{
2229 int r, c;
2230 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00002231 int max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002233 // Can't use ScreenLines unless initialized
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002234 if (ScreenLines == NULL)
2235 return;
2236
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 if (invert)
2238 screen_char_attr = HL_INVERSE;
2239 for (r = row; r < row + height; ++r)
2240 {
2241 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00002242 max_off = off + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 for (c = col; c < col + width; ++c)
2244 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00002245 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 {
2247 screen_char_2(off + c, r, c);
2248 ++c;
2249 }
2250 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 {
2252 screen_char(off + c, r, c);
Bram Moolenaar367329b2007-08-30 11:53:22 +00002253 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 ++c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 }
2256 }
2257 }
2258 screen_char_attr = 0;
2259}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261/*
2262 * Redraw the characters for a vertically split window.
2263 */
2264 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002265redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266{
2267 int col;
2268 int width;
2269
2270# ifdef FEAT_CLIPBOARD
2271 clip_may_clear_selection(row, end - 1);
2272# endif
2273
2274 if (wp == NULL)
2275 {
2276 col = 0;
2277 width = Columns;
2278 }
2279 else
2280 {
2281 col = wp->w_wincol;
2282 width = wp->w_width;
2283 }
2284 screen_draw_rectangle(row, col, end - row, width, FALSE);
2285}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002287 void
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002288space_to_screenline(int off, int attr)
2289{
2290 ScreenLines[off] = ' ';
2291 ScreenAttrs[off] = attr;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002292 if (enc_utf8)
2293 ScreenLinesUC[off] = 0;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002294}
2295
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296/*
2297 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
2298 * with character 'c1' in first column followed by 'c2' in the other columns.
2299 * Use attributes 'attr'.
2300 */
2301 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002302screen_fill(
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002303 int start_row,
2304 int end_row,
2305 int start_col,
2306 int end_col,
2307 int c1,
2308 int c2,
2309 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002311 int row;
2312 int col;
2313 int off;
2314 int end_off;
2315 int did_delete;
2316 int c;
2317 int norm_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002319 int force_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320#endif
2321
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002322 if (end_row > screen_Rows) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 end_row = screen_Rows;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002324 if (end_col > screen_Columns) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 end_col = screen_Columns;
2326 if (ScreenLines == NULL
2327 || start_row >= end_row
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002328 || start_col >= end_col) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 return;
2330
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002331 // it's a "normal" terminal when not in a GUI or cterm
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 norm_term = (
2333#ifdef FEAT_GUI
2334 !gui.in_use &&
2335#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002336 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 for (row = start_row; row < end_row; ++row)
2338 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00002339 if (has_mbyte
Bram Moolenaara12a1612019-01-24 16:39:02 +01002340#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00002341 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01002342#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00002343 )
2344 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002345 // When drawing over the right halve of a double-wide char clear
2346 // out the left halve. When drawing over the left halve of a
2347 // double wide-char clear out the right halve. Only needed in a
2348 // terminal.
Bram Moolenaar7693ec62008-07-24 18:29:37 +00002349 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00002350 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00002351 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00002352 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00002353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 /*
2355 * Try to use delete-line termcap code, when no attributes or in a
2356 * "normal" terminal, where a bold/italic space is just a
2357 * space.
2358 */
2359 did_delete = FALSE;
2360 if (c2 == ' '
2361 && end_col == Columns
2362 && can_clear(T_CE)
2363 && (attr == 0
2364 || (norm_term
2365 && attr <= HL_ALL
2366 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
2367 {
2368 /*
2369 * check if we really need to clear something
2370 */
2371 col = start_col;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002372 if (c1 != ' ') // don't clear first char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 ++col;
2374
2375 off = LineOffset[row] + col;
2376 end_off = LineOffset[row] + end_col;
2377
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002378 // skip blanks (used often, keep it fast!)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 if (enc_utf8)
2380 while (off < end_off && ScreenLines[off] == ' '
2381 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
2382 ++off;
2383 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 while (off < end_off && ScreenLines[off] == ' '
2385 && ScreenAttrs[off] == 0)
2386 ++off;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002387 if (off < end_off) // something to be cleared
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 {
2389 col = off - LineOffset[row];
2390 screen_stop_highlight();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002391 term_windgoto(row, col);// clear rest of this screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 out_str(T_CE);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002393 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 col = end_col - col;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002395 while (col--) // clear chars in ScreenLines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002397 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 ++off;
2399 }
2400 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002401 did_delete = TRUE; // the chars are cleared now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 }
2403
2404 off = LineOffset[row] + start_col;
2405 c = c1;
2406 for (col = start_col; col < end_col; ++col)
2407 {
Bram Moolenaar33796b32019-06-08 16:01:13 +02002408 if ((ScreenLines[off] != c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002409 || (enc_utf8 && (int)ScreenLinesUC[off]
2410 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 || ScreenAttrs[off] != attr
2412#if defined(FEAT_GUI) || defined(UNIX)
2413 || force_next
2414#endif
2415 )
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002416#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002417 // Skip if under a(nother) popup.
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002418 && !blocked_by_popup(row, col)
Bram Moolenaar33796b32019-06-08 16:01:13 +02002419#endif
2420 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 {
2422#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002423 // The bold trick may make a single row of pixels appear in
2424 // the next character. When a bold character is removed, the
2425 // next character should be redrawn too. This happens for our
2426 // own GUI and for some xterms.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 if (
2428# ifdef FEAT_GUI
2429 gui.in_use
2430# endif
2431# if defined(FEAT_GUI) && defined(UNIX)
2432 ||
2433# endif
2434# ifdef UNIX
2435 term_is_xterm
2436# endif
2437 )
2438 {
2439 if (ScreenLines[off] != ' '
2440 && (ScreenAttrs[off] > HL_ALL
2441 || ScreenAttrs[off] & HL_BOLD))
2442 force_next = TRUE;
2443 else
2444 force_next = FALSE;
2445 }
2446#endif
2447 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 if (enc_utf8)
2449 {
2450 if (c >= 0x80)
2451 {
2452 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002453 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 }
2455 else
2456 ScreenLinesUC[off] = 0;
2457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 ScreenAttrs[off] = attr;
2459 if (!did_delete || c != ' ')
2460 screen_char(off, row, col);
2461 }
2462 ++off;
2463 if (col == start_col)
2464 {
2465 if (did_delete)
2466 break;
2467 c = c2;
2468 }
2469 }
2470 if (end_col == Columns)
2471 LineWraps[row] = FALSE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002472 if (row == Rows - 1) // overwritten the command line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 {
2474 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02002475 if (start_col == 0 && end_col == Columns
2476 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002477 clear_cmdline = FALSE; // command line has been cleared
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002478 if (start_col == 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002479 mode_displayed = FALSE; // mode cleared or overwritten
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 }
2481 }
2482}
2483
2484/*
2485 * Check if there should be a delay. Used before clearing or redrawing the
2486 * screen or the command line.
2487 */
2488 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002489check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490{
2491 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
2492 && !did_wait_return
2493 && emsg_silent == 0)
2494 {
2495 out_flush();
Bram Moolenaareda1da02019-11-17 17:06:33 +01002496 ui_delay(1006L, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 emsg_on_display = FALSE;
2498 if (check_msg_scroll)
2499 msg_scroll = FALSE;
2500 }
2501}
2502
2503/*
Bram Moolenaarca57ab52019-04-13 14:53:16 +02002504 * Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect.
2505 */
2506 static void
2507clear_TabPageIdxs(void)
2508{
2509 int scol;
2510
2511 for (scol = 0; scol < Columns; ++scol)
2512 TabPageIdxs[scol] = 0;
2513}
2514
2515/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002517 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 * Returns TRUE if there is a valid screen to write to.
2519 * Returns FALSE when starting up and screen not initialized yet.
2520 */
2521 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002522screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002524 screenalloc(doclear); // allocate screen buffers if size changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 return (ScreenLines != NULL);
2526}
2527
2528/*
2529 * Resize the shell to Rows and Columns.
2530 * Allocate ScreenLines[] and associated items.
2531 *
2532 * There may be some time between setting Rows and Columns and (re)allocating
2533 * ScreenLines[]. This happens when starting up and when (manually) changing
2534 * the shell size. Always use screen_Rows and screen_Columns to access items
2535 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
2536 * final size of the shell is needed.
2537 */
2538 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002539screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540{
2541 int new_row, old_row;
2542#ifdef FEAT_GUI
2543 int old_Rows;
2544#endif
2545 win_T *wp;
2546 int outofmem = FALSE;
2547 int len;
2548 schar_T *new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002550 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002552 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 sattr_T *new_ScreenAttrs;
2554 unsigned *new_LineOffset;
2555 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002556 short *new_TabPageIdxs;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002557#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002558 short *new_popup_mask;
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002559 short *new_popup_mask_next;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002560 char *new_popup_transparent;
Bram Moolenaar33796b32019-06-08 16:01:13 +02002561#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00002562 tabpage_T *tp;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002563 static int entered = FALSE; // avoid recursiveness
2564 static int done_outofmem_msg = FALSE; // did outofmem message
Bram Moolenaar87e817c2009-02-22 20:13:39 +00002565 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566
Bram Moolenaar87e817c2009-02-22 20:13:39 +00002567retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 /*
2569 * Allocation of the screen buffers is done only when the size changes and
2570 * when Rows and Columns have been set and we have started doing full
2571 * screen stuff.
2572 */
2573 if ((ScreenLines != NULL
2574 && Rows == screen_Rows
2575 && Columns == screen_Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 && enc_utf8 == (ScreenLinesUC != NULL)
2577 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaara12a1612019-01-24 16:39:02 +01002578 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 || Rows == 0
2580 || Columns == 0
2581 || (!full_screen && ScreenLines == NULL))
2582 return;
2583
2584 /*
2585 * It's possible that we produce an out-of-memory message below, which
2586 * will cause this function to be called again. To break the loop, just
2587 * return here.
2588 */
2589 if (entered)
2590 return;
2591 entered = TRUE;
2592
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00002593 /*
2594 * Note that the window sizes are updated before reallocating the arrays,
2595 * thus we must not redraw here!
2596 */
2597 ++RedrawingDisabled;
2598
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002599 win_new_shellsize(); // fit the windows in the new sized shell
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002601#ifdef FEAT_GUI_HAIKU
2602 vim_lock_screen(); // be safe, put it here
2603#endif
2604
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002605 comp_col(); // recompute columns for shown command and ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606
2607 /*
2608 * We're changing the size of the screen.
2609 * - Allocate new arrays for ScreenLines and ScreenAttrs.
2610 * - Move lines from the old arrays into the new arrays, clear extra
2611 * lines (unless the screen is going to be cleared).
2612 * - Free the old arrays.
2613 *
2614 * If anything fails, make ScreenLines NULL, so we don't do anything!
2615 * Continuing with the old ScreenLines may result in a crash, because the
2616 * size is wrong.
2617 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002618 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00002620 if (aucmd_win != NULL)
2621 win_free_lsize(aucmd_win);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002622#ifdef FEAT_PROP_POPUP
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002623 // global popup windows
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002624 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002625 win_free_lsize(wp);
2626 // tab-local popup windows
2627 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002628 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002629 win_free_lsize(wp);
2630#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002632 new_ScreenLines = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
Bram Moolenaar216b7102010-03-23 13:56:59 +01002633 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 if (enc_utf8)
2635 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002636 new_ScreenLinesUC = LALLOC_MULT(u8char_T, (Rows + 1) * Columns);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002637 for (i = 0; i < p_mco; ++i)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002638 new_ScreenLinesC[i] = LALLOC_CLEAR_MULT(u8char_T,
2639 (Rows + 1) * Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 }
2641 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002642 new_ScreenLines2 = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
2643 new_ScreenAttrs = LALLOC_MULT(sattr_T, (Rows + 1) * Columns);
2644 new_LineOffset = LALLOC_MULT(unsigned, Rows);
2645 new_LineWraps = LALLOC_MULT(char_u, Rows);
2646 new_TabPageIdxs = LALLOC_MULT(short, Columns);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002647#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002648 new_popup_mask = LALLOC_MULT(short, Rows * Columns);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002649 new_popup_mask_next = LALLOC_MULT(short, Rows * Columns);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002650 new_popup_transparent = LALLOC_MULT(char, Rows * Columns);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002651#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00002653 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 {
2655 if (win_alloc_lines(wp) == FAIL)
2656 {
2657 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00002658 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 }
2660 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00002661 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
2662 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002663 outofmem = TRUE;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002664#ifdef FEAT_PROP_POPUP
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002665 // global popup windows
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002666 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002667 if (win_alloc_lines(wp) == FAIL)
2668 {
2669 outofmem = TRUE;
2670 goto give_up;
2671 }
2672 // tab-local popup windows
2673 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002674 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002675 if (win_alloc_lines(wp) == FAIL)
2676 {
2677 outofmem = TRUE;
2678 goto give_up;
2679 }
2680#endif
2681
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00002682give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002684 for (i = 0; i < p_mco; ++i)
2685 if (new_ScreenLinesC[i] == NULL)
2686 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 if (new_ScreenLines == NULL
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002688 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 || new_ScreenAttrs == NULL
2691 || new_LineOffset == NULL
2692 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00002693 || new_TabPageIdxs == NULL
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002694#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002695 || new_popup_mask == NULL
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002696 || new_popup_mask_next == NULL
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002697 || new_popup_transparent == NULL
Bram Moolenaar33796b32019-06-08 16:01:13 +02002698#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 || outofmem)
2700 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002701 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002702 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002703 // guess the size
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002704 do_outofmem_msg((long_u)((Rows + 1) * Columns));
2705
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002706 // Remember we did this to avoid getting outofmem messages over
2707 // and over again.
Bram Moolenaar89d40322006-08-29 15:30:07 +00002708 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002709 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01002710 VIM_CLEAR(new_ScreenLines);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002711 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002712 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01002713 VIM_CLEAR(new_ScreenLinesC[i]);
2714 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002715 VIM_CLEAR(new_ScreenAttrs);
2716 VIM_CLEAR(new_LineOffset);
2717 VIM_CLEAR(new_LineWraps);
2718 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002719#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002720 VIM_CLEAR(new_popup_mask);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002721 VIM_CLEAR(new_popup_mask_next);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002722 VIM_CLEAR(new_popup_transparent);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002723#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 }
2725 else
2726 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002727 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002728
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 for (new_row = 0; new_row < Rows; ++new_row)
2730 {
2731 new_LineOffset[new_row] = new_row * Columns;
2732 new_LineWraps[new_row] = FALSE;
2733
2734 /*
2735 * If the screen is not going to be cleared, copy as much as
2736 * possible from the old screen to the new one and clear the rest
2737 * (used when resizing the window at the "--more--" prompt or when
2738 * executing an external command, for the GUI).
2739 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002740 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 {
2742 (void)vim_memset(new_ScreenLines + new_row * Columns,
2743 ' ', (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 if (enc_utf8)
2745 {
2746 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
2747 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002748 for (i = 0; i < p_mco; ++i)
2749 (void)vim_memset(new_ScreenLinesC[i]
2750 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 0, (size_t)Columns * sizeof(u8char_T));
2752 }
2753 if (enc_dbcs == DBCS_JPNU)
2754 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
2755 0, (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
2757 0, (size_t)Columns * sizeof(sattr_T));
2758 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002759 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 {
2761 if (screen_Columns < Columns)
2762 len = screen_Columns;
2763 else
2764 len = Columns;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002765 // When switching to utf-8 don't copy characters, they
2766 // may be invalid now. Also when p_mco changes.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002767 if (!(enc_utf8 && ScreenLinesUC == NULL)
2768 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002769 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
2770 ScreenLines + LineOffset[old_row],
2771 (size_t)len * sizeof(schar_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002772 if (enc_utf8 && ScreenLinesUC != NULL
2773 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 {
2775 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
2776 ScreenLinesUC + LineOffset[old_row],
2777 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002778 for (i = 0; i < p_mco; ++i)
2779 mch_memmove(new_ScreenLinesC[i]
2780 + new_LineOffset[new_row],
2781 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 (size_t)len * sizeof(u8char_T));
2783 }
2784 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
2785 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
2786 ScreenLines2 + LineOffset[old_row],
2787 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
2789 ScreenAttrs + LineOffset[old_row],
2790 (size_t)len * sizeof(sattr_T));
2791 }
2792 }
2793 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002794 // Use the last line of the screen for the current line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 current_ScreenLine = new_ScreenLines + Rows * Columns;
Bram Moolenaar6ace95e2019-08-13 23:09:49 +02002796
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002797#ifdef FEAT_PROP_POPUP
Bram Moolenaar6ace95e2019-08-13 23:09:49 +02002798 vim_memset(new_popup_mask, 0, Rows * Columns * sizeof(short));
2799 vim_memset(new_popup_transparent, 0, Rows * Columns * sizeof(char));
2800#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 }
2802
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002803 free_screenlines();
2804
Bram Moolenaar6ace95e2019-08-13 23:09:49 +02002805 // NOTE: this may result in all pointers to become NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 ScreenLines = new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002808 for (i = 0; i < p_mco; ++i)
2809 ScreenLinesC[i] = new_ScreenLinesC[i];
2810 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 ScreenLines2 = new_ScreenLines2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 ScreenAttrs = new_ScreenAttrs;
2813 LineOffset = new_LineOffset;
2814 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002815 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002816#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002817 popup_mask = new_popup_mask;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002818 popup_mask_next = new_popup_mask_next;
2819 popup_transparent = new_popup_transparent;
Bram Moolenaar33796b32019-06-08 16:01:13 +02002820 popup_mask_refresh = TRUE;
2821#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002823 // It's important that screen_Rows and screen_Columns reflect the actual
2824 // size of ScreenLines[]. Set them before calling anything.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825#ifdef FEAT_GUI
2826 old_Rows = screen_Rows;
2827#endif
2828 screen_Rows = Rows;
2829 screen_Columns = Columns;
2830
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002831 must_redraw = CLEAR; // need to clear the screen later
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002832 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 screenclear2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834#ifdef FEAT_GUI
2835 else if (gui.in_use
2836 && !gui.starting
2837 && ScreenLines != NULL
2838 && old_Rows != Rows)
2839 {
Bram Moolenaar7c003aa2020-03-28 20:44:41 +01002840 gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
2841
2842 // Adjust the position of the cursor, for when executing an external
2843 // command.
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002844 if (msg_row >= Rows) // Rows got smaller
2845 msg_row = Rows - 1; // put cursor at last row
2846 else if (Rows > old_Rows) // Rows got bigger
2847 msg_row += Rows - old_Rows; // put cursor in same place
2848 if (msg_col >= Columns) // Columns got smaller
2849 msg_col = Columns - 1; // put cursor at last column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 }
2851#endif
Bram Moolenaarca57ab52019-04-13 14:53:16 +02002852 clear_TabPageIdxs();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002854#ifdef FEAT_GUI_HAIKU
2855 vim_unlock_screen();
2856#endif
2857
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00002859 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00002860
Bram Moolenaar87e817c2009-02-22 20:13:39 +00002861 /*
2862 * Do not apply autocommands more than 3 times to avoid an endless loop
2863 * in case applying autocommands always changes Rows or Columns.
2864 */
2865 if (starting == 0 && ++retry_count <= 3)
2866 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00002867 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002868 // In rare cases, autocommands may have altered Rows or Columns,
2869 // jump back to check if we need to allocate the screen again.
Bram Moolenaar87e817c2009-02-22 20:13:39 +00002870 goto retry;
2871 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872}
2873
2874 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002875free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002876{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002877 int i;
2878
Bram Moolenaar33796b32019-06-08 16:01:13 +02002879 VIM_CLEAR(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002880 for (i = 0; i < Screen_mco; ++i)
Bram Moolenaar33796b32019-06-08 16:01:13 +02002881 VIM_CLEAR(ScreenLinesC[i]);
2882 VIM_CLEAR(ScreenLines2);
2883 VIM_CLEAR(ScreenLines);
2884 VIM_CLEAR(ScreenAttrs);
2885 VIM_CLEAR(LineOffset);
2886 VIM_CLEAR(LineWraps);
2887 VIM_CLEAR(TabPageIdxs);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002888#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002889 VIM_CLEAR(popup_mask);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002890 VIM_CLEAR(popup_mask_next);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002891 VIM_CLEAR(popup_transparent);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002892#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002893}
2894
2895 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002896screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897{
2898 check_for_delay(FALSE);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002899 screenalloc(FALSE); // allocate screen buffers if size changed
2900 screenclear2(); // clear the screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901}
2902
2903 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002904screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905{
2906 int i;
2907
2908 if (starting == NO_SCREEN || ScreenLines == NULL
2909#ifdef FEAT_GUI
2910 || (gui.in_use && gui.starting)
2911#endif
2912 )
2913 return;
2914
2915#ifdef FEAT_GUI
2916 if (!gui.in_use)
2917#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002918 screen_attr = -1; // force setting the Normal colors
2919 screen_stop_highlight(); // don't want highlighting here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920
2921#ifdef FEAT_CLIPBOARD
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002922 // disable selection without redrawing it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 clip_scroll_selection(9999);
2924#endif
2925
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002926 // blank out ScreenLines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 for (i = 0; i < Rows; ++i)
2928 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002929 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 LineWraps[i] = FALSE;
2931 }
2932
2933 if (can_clear(T_CL))
2934 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002935 out_str(T_CL); // clear the display
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002937 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 }
2939 else
2940 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002941 // can't clear the screen, mark all chars with invalid attributes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 for (i = 0; i < Rows; ++i)
2943 lineinvalid(LineOffset[i], (int)Columns);
2944 clear_cmdline = TRUE;
2945 }
2946
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002947 screen_cleared = TRUE; // can use contents of ScreenLines now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948
2949 win_rest_invalid(firstwin);
2950 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002951 redraw_tabline = TRUE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002952 if (must_redraw == CLEAR) // no need to clear again
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 must_redraw = NOT_VALID;
2954 compute_cmdrow();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002955 msg_row = cmdline_row; // put cursor on last line for messages
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 msg_col = 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002957 screen_start(); // don't know where cursor is now
2958 msg_scrolled = 0; // can't scroll back
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 msg_didany = FALSE;
2960 msg_didout = FALSE;
2961}
2962
2963/*
2964 * Clear one line in ScreenLines.
2965 */
2966 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002967lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968{
2969 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 if (enc_utf8)
2971 (void)vim_memset(ScreenLinesUC + off, 0,
2972 (size_t)width * sizeof(u8char_T));
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002973 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974}
2975
2976/*
2977 * Mark one line in ScreenLines invalid by setting the attributes to an
2978 * invalid value.
2979 */
2980 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002981lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982{
2983 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
2984}
2985
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986/*
Bram Moolenaar96916ac2020-07-08 23:09:28 +02002987 * To be called when characters were sent to the terminal directly, outputting
2988 * test on "screen_lnum".
2989 */
2990 void
2991line_was_clobbered(int screen_lnum)
2992{
2993 lineinvalid(LineOffset[screen_lnum], (int)Columns);
2994}
2995
2996/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997 * Copy part of a Screenline for vertically split window "wp".
2998 */
2999 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003000linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001{
3002 unsigned off_to = LineOffset[to] + wp->w_wincol;
3003 unsigned off_from = LineOffset[from] + wp->w_wincol;
3004
3005 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
3006 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 if (enc_utf8)
3008 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003009 int i;
3010
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
3012 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003013 for (i = 0; i < p_mco; ++i)
3014 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
3015 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 }
3017 if (enc_dbcs == DBCS_JPNU)
3018 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
3019 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
3021 wp->w_width * sizeof(sattr_T));
3022}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023
3024/*
3025 * Return TRUE if clearing with term string "p" would work.
3026 * It can't work when the string is empty or it won't set the right background.
Bram Moolenaar33796b32019-06-08 16:01:13 +02003027 * Don't clear to end-of-line when there are popups, it may cause flicker.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 */
3029 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003030can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031{
3032 return (*p != NUL && (t_colors <= 1
3033#ifdef FEAT_GUI
3034 || gui.in_use
3035#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003036#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003037 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02003038 || (!p_tgc && cterm_normal_bg_color == 0)
3039#else
3040 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003041#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02003042 || *T_UT != NUL)
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003043#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02003044 && !(p == T_CE && popup_visible)
3045#endif
3046 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047}
3048
3049/*
3050 * Reset cursor position. Use whenever cursor was moved because of outputting
3051 * something directly to the screen (shell commands) or a terminal control
3052 * code.
3053 */
3054 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003055screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056{
3057 screen_cur_row = screen_cur_col = 9999;
3058}
3059
3060/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 * Move the cursor to position "row","col" in the screen.
3062 * This tries to find the most efficient way to move, minimizing the number of
3063 * characters sent to the terminal.
3064 */
3065 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003066windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003068 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 int i;
3070 int plan;
3071 int cost;
3072 int wouldbe_col;
3073 int noinvcurs;
3074 char_u *bs;
3075 int goto_cost;
3076 int attr;
3077
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003078#define GOTO_COST 7 // assume a term_windgoto() takes about 7 chars
3079#define HIGHL_COST 5 // assume unhighlight takes 5 chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080
3081#define PLAN_LE 1
3082#define PLAN_CR 2
3083#define PLAN_NL 3
3084#define PLAN_WRITE 4
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003085 // Can't use ScreenLines unless initialized
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 if (ScreenLines == NULL)
3087 return;
3088
3089 if (col != screen_cur_col || row != screen_cur_row)
3090 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003091 // Check for valid position.
3092 if (row < 0) // window without text lines?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 row = 0;
3094 if (row >= screen_Rows)
3095 row = screen_Rows - 1;
3096 if (col >= screen_Columns)
3097 col = screen_Columns - 1;
3098
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003099 // check if no cursor movement is allowed in highlight mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 if (screen_attr && *T_MS == NUL)
3101 noinvcurs = HIGHL_COST;
3102 else
3103 noinvcurs = 0;
3104 goto_cost = GOTO_COST + noinvcurs;
3105
3106 /*
3107 * Plan how to do the positioning:
3108 * 1. Use CR to move it to column 0, same row.
3109 * 2. Use T_LE to move it a few columns to the left.
3110 * 3. Use NL to move a few lines down, column 0.
3111 * 4. Move a few columns to the right with T_ND or by writing chars.
3112 *
3113 * Don't do this if the cursor went beyond the last column, the cursor
3114 * position is unknown then (some terminals wrap, some don't )
3115 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00003116 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 * characters to move the cursor to the right.
3118 */
3119 if (row >= screen_cur_row && screen_cur_col < Columns)
3120 {
3121 /*
3122 * If the cursor is in the same row, bigger col, we can use CR
3123 * or T_LE.
3124 */
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003125 bs = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 attr = screen_attr;
3127 if (row == screen_cur_row && col < screen_cur_col)
3128 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003129 // "le" is preferred over "bc", because "bc" is obsolete
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 if (*T_LE)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003131 bs = T_LE; // "cursor left"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003133 bs = T_BC; // "backspace character (old)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 if (*bs)
3135 cost = (screen_cur_col - col) * (int)STRLEN(bs);
3136 else
3137 cost = 999;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003138 if (col + 1 < cost) // using CR is less characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 {
3140 plan = PLAN_CR;
3141 wouldbe_col = 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003142 cost = 1; // CR is just one character
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 }
3144 else
3145 {
3146 plan = PLAN_LE;
3147 wouldbe_col = col;
3148 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003149 if (noinvcurs) // will stop highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 {
3151 cost += noinvcurs;
3152 attr = 0;
3153 }
3154 }
3155
3156 /*
3157 * If the cursor is above where we want to be, we can use CR LF.
3158 */
3159 else if (row > screen_cur_row)
3160 {
3161 plan = PLAN_NL;
3162 wouldbe_col = 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003163 cost = (row - screen_cur_row) * 2; // CR LF
3164 if (noinvcurs) // will stop highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 {
3166 cost += noinvcurs;
3167 attr = 0;
3168 }
3169 }
3170
3171 /*
3172 * If the cursor is in the same row, smaller col, just use write.
3173 */
3174 else
3175 {
3176 plan = PLAN_WRITE;
3177 wouldbe_col = screen_cur_col;
3178 cost = 0;
3179 }
3180
3181 /*
3182 * Check if any characters that need to be written have the
3183 * correct attributes. Also avoid UTF-8 characters.
3184 */
3185 i = col - wouldbe_col;
3186 if (i > 0)
3187 cost += i;
3188 if (cost < goto_cost && i > 0)
3189 {
3190 /*
3191 * Check if the attributes are correct without additionally
3192 * stopping highlighting.
3193 */
3194 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
3195 while (i && *p++ == attr)
3196 --i;
3197 if (i != 0)
3198 {
3199 /*
3200 * Try if it works when highlighting is stopped here.
3201 */
3202 if (*--p == 0)
3203 {
3204 cost += noinvcurs;
3205 while (i && *p++ == 0)
3206 --i;
3207 }
3208 if (i != 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003209 cost = 999; // different attributes, don't do it
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 if (enc_utf8)
3212 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003213 // Don't use an UTF-8 char for positioning, it's slow.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 for (i = wouldbe_col; i < col; ++i)
3215 if (ScreenLinesUC[LineOffset[row] + i] != 0)
3216 {
3217 cost = 999;
3218 break;
3219 }
3220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 }
3222
3223 /*
3224 * We can do it without term_windgoto()!
3225 */
3226 if (cost < goto_cost)
3227 {
3228 if (plan == PLAN_LE)
3229 {
3230 if (noinvcurs)
3231 screen_stop_highlight();
3232 while (screen_cur_col > col)
3233 {
3234 out_str(bs);
3235 --screen_cur_col;
3236 }
3237 }
3238 else if (plan == PLAN_CR)
3239 {
3240 if (noinvcurs)
3241 screen_stop_highlight();
3242 out_char('\r');
3243 screen_cur_col = 0;
3244 }
3245 else if (plan == PLAN_NL)
3246 {
3247 if (noinvcurs)
3248 screen_stop_highlight();
3249 while (screen_cur_row < row)
3250 {
3251 out_char('\n');
3252 ++screen_cur_row;
3253 }
3254 screen_cur_col = 0;
3255 }
3256
3257 i = col - screen_cur_col;
3258 if (i > 0)
3259 {
3260 /*
3261 * Use cursor-right if it's one character only. Avoids
3262 * removing a line of pixels from the last bold char, when
3263 * using the bold trick in the GUI.
3264 */
3265 if (T_ND[0] != NUL && T_ND[1] == NUL)
3266 {
3267 while (i-- > 0)
3268 out_char(*T_ND);
3269 }
3270 else
3271 {
3272 int off;
3273
3274 off = LineOffset[row] + screen_cur_col;
3275 while (i-- > 0)
3276 {
3277 if (ScreenAttrs[off] != screen_attr)
3278 screen_stop_highlight();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 if (enc_dbcs == DBCS_JPNU
3282 && ScreenLines[off] == 0x8e)
3283 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 ++off;
3285 }
3286 }
3287 }
3288 }
3289 }
3290 else
3291 cost = 999;
3292
3293 if (cost >= goto_cost)
3294 {
3295 if (noinvcurs)
3296 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02003297 if (row == screen_cur_row && (col > screen_cur_col)
3298 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 term_cursor_right(col - screen_cur_col);
3300 else
3301 term_windgoto(row, col);
3302 }
3303 screen_cur_row = row;
3304 screen_cur_col = col;
3305 }
3306}
3307
3308/*
3309 * Set cursor to its position in the current window.
3310 */
3311 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003312setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313{
Bram Moolenaar987723e2018-03-06 11:43:04 +01003314 setcursor_mayforce(FALSE);
3315}
3316
3317/*
3318 * Set cursor to its position in the current window.
3319 * When "force" is TRUE also when not redrawing.
3320 */
3321 void
3322setcursor_mayforce(int force)
3323{
3324 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 {
3326 validate_cursor();
3327 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02003328 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329#ifdef FEAT_RIGHTLEFT
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003330 // With 'rightleft' set and the cursor on a double-wide
3331 // character, position it on the leftmost column.
Bram Moolenaara12a1612019-01-24 16:39:02 +01003332 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol
3333 - ((has_mbyte
Bram Moolenaar561f9db2008-02-20 13:16:29 +00003334 && (*mb_ptr2cells)(ml_get_cursor()) == 2
Bram Moolenaara12a1612019-01-24 16:39:02 +01003335 && vim_isprintc(gchar_cursor())) ? 2 : 1)) :
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336#endif
3337 curwin->w_wcol));
3338 }
3339}
3340
3341
3342/*
Bram Moolenaar86033562017-07-12 20:24:41 +02003343 * Insert 'line_count' lines at 'row' in window 'wp'.
3344 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
3345 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 * scrolling.
3347 * Returns FAIL if the lines are not inserted, OK for success.
3348 */
3349 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003350win_ins_lines(
3351 win_T *wp,
3352 int row,
3353 int line_count,
3354 int invalid,
3355 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356{
3357 int did_delete;
3358 int nextrow;
3359 int lastrow;
3360 int retval;
3361
3362 if (invalid)
3363 wp->w_lines_valid = 0;
3364
3365 if (wp->w_height < 5)
3366 return FAIL;
3367
3368 if (line_count > wp->w_height - row)
3369 line_count = wp->w_height - row;
3370
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003371 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 if (retval != MAYBE)
3373 return retval;
3374
3375 /*
3376 * If there is a next window or a status line, we first try to delete the
3377 * lines at the bottom to avoid messing what is after the window.
Bram Moolenaarc363fe12019-08-04 18:13:46 +02003378 * If this fails and there are following windows, don't do anything to
3379 * avoid messing up those windows, better just redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 */
3381 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 if (wp->w_next != NULL || wp->w_status_height)
3383 {
3384 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003385 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 did_delete = TRUE;
3387 else if (wp->w_next)
3388 return FAIL;
3389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 /*
3391 * if no lines deleted, blank the lines that will end up below the window
3392 */
3393 if (!did_delete)
3394 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003397 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 lastrow = nextrow + line_count;
3399 if (lastrow > Rows)
3400 lastrow = Rows;
3401 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02003402 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 ' ', ' ', 0);
3404 }
3405
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003406 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 == FAIL)
3408 {
Bram Moolenaarc363fe12019-08-04 18:13:46 +02003409 // deletion will have messed up other windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 if (did_delete)
3411 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 win_rest_invalid(W_NEXT(wp));
3414 }
3415 return FAIL;
3416 }
3417
3418 return OK;
3419}
3420
3421/*
Bram Moolenaar86033562017-07-12 20:24:41 +02003422 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
3424 * If "mayclear" is TRUE the screen will be cleared if it is faster than
3425 * scrolling
3426 * Return OK for success, FAIL if the lines are not deleted.
3427 */
3428 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003429win_del_lines(
3430 win_T *wp,
3431 int row,
3432 int line_count,
3433 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003434 int mayclear,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003435 int clear_attr) // for clearing lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436{
3437 int retval;
3438
3439 if (invalid)
3440 wp->w_lines_valid = 0;
3441
3442 if (line_count > wp->w_height - row)
3443 line_count = wp->w_height - row;
3444
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003445 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 if (retval != MAYBE)
3447 return retval;
3448
3449 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003450 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 return FAIL;
3452
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 /*
3454 * If there are windows or status lines below, try to put them at the
3455 * correct place. If we can't do that, they have to be redrawn.
3456 */
3457 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
3458 {
3459 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003460 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 {
3462 wp->w_redr_status = TRUE;
3463 win_rest_invalid(wp->w_next);
3464 }
3465 }
3466 /*
3467 * If this is the last window and there is no status line, redraw the
3468 * command line later.
3469 */
3470 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 redraw_cmdline = TRUE;
3472 return OK;
3473}
3474
3475/*
3476 * Common code for win_ins_lines() and win_del_lines().
3477 * Returns OK or FAIL when the work has been done.
3478 * Returns MAYBE when not finished yet.
3479 */
3480 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003481win_do_lines(
3482 win_T *wp,
3483 int row,
3484 int line_count,
3485 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003486 int del,
3487 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488{
3489 int retval;
3490
3491 if (!redrawing() || line_count <= 0)
3492 return FAIL;
3493
Bram Moolenaar33796b32019-06-08 16:01:13 +02003494 // When inserting lines would result in loss of command output, just redraw
3495 // the lines.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003496 if (no_win_do_lines_ins && !del)
3497 return FAIL;
3498
Bram Moolenaar33796b32019-06-08 16:01:13 +02003499 // only a few lines left: redraw is faster
Bram Moolenaar4033c552017-09-16 20:54:51 +02003500 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003502 if (!no_win_do_lines_ins)
Bram Moolenaar33796b32019-06-08 16:01:13 +02003503 screenclear(); // will set wp->w_lines_valid to 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 return FAIL;
3505 }
3506
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003507#ifdef FEAT_PROP_POPUP
Bram Moolenaar4c063a02019-06-10 21:24:12 +02003508 // this doesn't work when there are popups visible
Bram Moolenaar33796b32019-06-08 16:01:13 +02003509 if (popup_visible)
3510 return FAIL;
3511#endif
3512
3513 // Delete all remaining lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 if (row + line_count >= wp->w_height)
3515 {
3516 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02003517 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 ' ', ' ', 0);
3519 return OK;
3520 }
3521
3522 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003523 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003525 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003527 if (!no_win_do_lines_ins)
3528 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529
3530 /*
3531 * If the terminal can set a scroll region, use that.
3532 * Always do this in a vertically split window. This will redraw from
3533 * ScreenLines[] when t_CV isn't defined. That's faster than using
3534 * win_line().
3535 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003536 * a character in the lower right corner of the scroll region may cause a
3537 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 */
Bram Moolenaar02631462017-09-22 15:20:32 +02003539 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 scroll_region_set(wp, row);
3543 if (del)
3544 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003545 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 else
3547 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003548 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 scroll_region_reset();
3551 return retval;
3552 }
3553
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003554 if (wp->w_next != NULL && p_tf) // don't delete/insert on fast terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556
3557 return MAYBE;
3558}
3559
3560/*
3561 * window 'wp' and everything after it is messed up, mark it for redraw
3562 */
3563 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003564win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 {
3568 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 wp->w_redr_status = TRUE;
3570 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 }
3572 redraw_cmdline = TRUE;
3573}
3574
3575/*
3576 * The rest of the routines in this file perform screen manipulations. The
3577 * given operation is performed physically on the screen. The corresponding
3578 * change is also made to the internal screen image. In this way, the editor
3579 * anticipates the effect of editing changes on the appearance of the screen.
3580 * That way, when we call screenupdate a complete redraw isn't usually
3581 * necessary. Another advantage is that we can keep adding code to anticipate
3582 * screen changes, and in the meantime, everything still works.
3583 */
3584
3585/*
3586 * types for inserting or deleting lines
3587 */
3588#define USE_T_CAL 1
3589#define USE_T_CDL 2
3590#define USE_T_AL 3
3591#define USE_T_CE 4
3592#define USE_T_DL 5
3593#define USE_T_SR 6
3594#define USE_NL 7
3595#define USE_T_CD 8
3596#define USE_REDRAW 9
3597
3598/*
3599 * insert lines on the screen and update ScreenLines[]
3600 * 'end' is the line after the scrolled part. Normally it is Rows.
3601 * When scrolling region used 'off' is the offset from the top for the region.
3602 * 'row' and 'end' are relative to the start of the region.
3603 *
3604 * return FAIL for failure, OK for success.
3605 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003606 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003607screen_ins_lines(
3608 int off,
3609 int row,
3610 int line_count,
3611 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003612 int clear_attr,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003613 win_T *wp) // NULL or window to use width from
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614{
3615 int i;
3616 int j;
3617 unsigned temp;
3618 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003619 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 int type;
3621 int result_empty;
3622 int can_ce = can_clear(T_CE);
3623
3624 /*
3625 * FAIL if
3626 * - there is no valid screen
3627 * - the screen has to be redrawn completely
3628 * - the line count is less than one
3629 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003630 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar33796b32019-06-08 16:01:13 +02003631 * - there is a popup window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 */
Bram Moolenaar33796b32019-06-08 16:01:13 +02003633 if (!screen_valid(TRUE)
3634 || line_count <= 0 || line_count > p_ttyscroll
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003635#ifdef FEAT_CLIPBOARD
3636 || (clip_star.state != SELECT_CLEARED
3637 && redrawing_for_callback > 0)
3638#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003639#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02003640 || popup_visible
3641#endif
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003642 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 return FAIL;
3644
3645 /*
3646 * There are seven ways to insert lines:
3647 * 0. When in a vertically split window and t_CV isn't set, redraw the
3648 * characters from ScreenLines[].
3649 * 1. Use T_CD (clear to end of display) if it exists and the result of
3650 * the insert is just empty lines
3651 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
3652 * present or line_count > 1. It looks better if we do all the inserts
3653 * at once.
3654 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
3655 * insert is just empty lines and T_CE is not present or line_count >
3656 * 1.
3657 * 4. Use T_AL (insert line) if it exists.
3658 * 5. Use T_CE (erase line) if it exists and the result of the insert is
3659 * just empty lines.
3660 * 6. Use T_DL (delete line) if it exists and the result of the insert is
3661 * just empty lines.
3662 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
3663 * the 'da' flag is not set or we have clear line capability.
3664 * 8. redraw the characters from ScreenLines[].
3665 *
3666 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
3667 * the scrollbar for the window. It does have insert line, use that if it
3668 * exists.
3669 */
3670 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
3672 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02003673 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 type = USE_T_CD;
3675 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
3676 type = USE_T_CAL;
3677 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
3678 type = USE_T_CDL;
3679 else if (*T_AL != NUL)
3680 type = USE_T_AL;
3681 else if (can_ce && result_empty)
3682 type = USE_T_CE;
3683 else if (*T_DL != NUL && result_empty)
3684 type = USE_T_DL;
3685 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
3686 type = USE_T_SR;
3687 else
3688 return FAIL;
3689
3690 /*
3691 * For clearing the lines screen_del_lines() is used. This will also take
3692 * care of t_db if necessary.
3693 */
3694 if (type == USE_T_CD || type == USE_T_CDL ||
3695 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003696 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697
3698 /*
3699 * If text is retained below the screen, first clear or delete as many
3700 * lines at the bottom of the window as are about to be inserted so that
3701 * the deleted lines won't later surface during a screen_del_lines.
3702 */
3703 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003704 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705
3706#ifdef FEAT_CLIPBOARD
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003707 // Remove a modeless selection when inserting lines halfway the screen
3708 // or not the full width of the screen.
Bram Moolenaar4033c552017-09-16 20:54:51 +02003709 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003710 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 else
3712 clip_scroll_selection(-line_count);
3713#endif
3714
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003715#ifdef FEAT_GUI_HAIKU
3716 vim_lock_screen();
3717#endif
3718
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719#ifdef FEAT_GUI
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003720 // Don't update the GUI cursor here, ScreenLines[] is invalid until the
3721 // scrolling is actually carried out.
Bram Moolenaar107abd22016-08-12 14:08:25 +02003722 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723#endif
3724
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003725 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
3726 cursor_col = wp->w_wincol;
3727
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003728 if (*T_CCS != NUL) // cursor relative to region
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 cursor_row = row;
3730 else
3731 cursor_row = row + off;
3732
3733 /*
3734 * Shift LineOffset[] line_count down to reflect the inserted lines.
3735 * Clear the inserted lines in ScreenLines[].
3736 */
3737 row += off;
3738 end += off;
3739 for (i = 0; i < line_count; ++i)
3740 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 if (wp != NULL && wp->w_width != Columns)
3742 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003743 // need to copy part of a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 j = end - 1 - i;
3745 while ((j -= line_count) >= row)
3746 linecopy(j + line_count, j, wp);
3747 j += line_count;
3748 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003749 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
3750 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 else
3752 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
3753 LineWraps[j] = FALSE;
3754 }
3755 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 {
3757 j = end - 1 - i;
3758 temp = LineOffset[j];
3759 while ((j -= line_count) >= row)
3760 {
3761 LineOffset[j + line_count] = LineOffset[j];
3762 LineWraps[j + line_count] = LineWraps[j];
3763 }
3764 LineOffset[j + line_count] = temp;
3765 LineWraps[j + line_count] = FALSE;
3766 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003767 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 else
3769 lineinvalid(temp, (int)Columns);
3770 }
3771 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003773#ifdef FEAT_GUI_HAIKU
3774 vim_unlock_screen();
3775#endif
3776
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003778 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003779 if (clear_attr != 0)
3780 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003782 // redraw the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 if (type == USE_REDRAW)
3784 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02003785 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 {
3787 term_append_lines(line_count);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003788 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 }
3790 else
3791 {
3792 for (i = 0; i < line_count; i++)
3793 {
3794 if (type == USE_T_AL)
3795 {
3796 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003797 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 out_str(T_AL);
3799 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003800 else // type == USE_T_SR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 out_str(T_SR);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003802 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 }
3804 }
3805
3806 /*
3807 * With scroll-reverse and 'da' flag set we need to clear the lines that
3808 * have been scrolled down into the region.
3809 */
3810 if (type == USE_T_SR && *T_DA)
3811 {
3812 for (i = 0; i < line_count; ++i)
3813 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003814 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 out_str(T_CE);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003816 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 }
3818 }
3819
3820#ifdef FEAT_GUI
3821 gui_can_update_cursor();
3822 if (gui.in_use)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003823 out_flush(); // always flush after a scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824#endif
3825 return OK;
3826}
3827
3828/*
Bram Moolenaar107abd22016-08-12 14:08:25 +02003829 * Delete lines on the screen and update ScreenLines[].
3830 * "end" is the line after the scrolled part. Normally it is Rows.
3831 * When scrolling region used "off" is the offset from the top for the region.
3832 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 *
3834 * Return OK for success, FAIL if the lines are not deleted.
3835 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003837screen_del_lines(
3838 int off,
3839 int row,
3840 int line_count,
3841 int end,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003842 int force, // even when line_count > p_ttyscroll
3843 int clear_attr, // used for clearing lines
3844 win_T *wp UNUSED) // NULL or window to use width from
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845{
3846 int j;
3847 int i;
3848 unsigned temp;
3849 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003850 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 int cursor_end;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003852 int result_empty; // result is empty until end of region
3853 int can_delete; // deleting line codes can be used
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 int type;
3855
3856 /*
3857 * FAIL if
3858 * - there is no valid screen
3859 * - the screen has to be redrawn completely
3860 * - the line count is less than one
3861 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003862 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003864 if (!screen_valid(TRUE) || line_count <= 0
3865 || (!force && line_count > p_ttyscroll)
3866#ifdef FEAT_CLIPBOARD
3867 || (clip_star.state != SELECT_CLEARED
3868 && redrawing_for_callback > 0)
3869#endif
3870 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 return FAIL;
3872
3873 /*
3874 * Check if the rest of the current region will become empty.
3875 */
3876 result_empty = row + line_count >= end;
3877
3878 /*
3879 * We can delete lines only when 'db' flag not set or when 'ce' option
3880 * available.
3881 */
3882 can_delete = (*T_DB == NUL || can_clear(T_CE));
3883
3884 /*
3885 * There are six ways to delete lines:
3886 * 0. When in a vertically split window and t_CV isn't set, redraw the
3887 * characters from ScreenLines[].
3888 * 1. Use T_CD if it exists and the result is empty.
3889 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
3890 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
3891 * none of the other ways work.
3892 * 4. Use T_CE (erase line) if the result is empty.
3893 * 5. Use T_DL (delete line) if it exists.
3894 * 6. redraw the characters from ScreenLines[].
3895 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
3897 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02003898 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 type = USE_T_CD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 else if (row == 0 && (
3901#ifndef AMIGA
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003902 // On the Amiga, somehow '\n' on the last line doesn't always scroll
3903 // up, so use delete-line command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 line_count == 1 ||
3905#endif
3906 *T_CDL == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 type = USE_NL;
3908 else if (*T_CDL != NUL && line_count > 1 && can_delete)
3909 type = USE_T_CDL;
3910 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +02003911 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 type = USE_T_CE;
3913 else if (*T_DL != NUL && can_delete)
3914 type = USE_T_DL;
3915 else if (*T_CDL != NUL && can_delete)
3916 type = USE_T_CDL;
3917 else
3918 return FAIL;
3919
3920#ifdef FEAT_CLIPBOARD
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003921 // Remove a modeless selection when deleting lines halfway the screen or
3922 // not the full width of the screen.
Bram Moolenaar4033c552017-09-16 20:54:51 +02003923 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003924 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 else
3926 clip_scroll_selection(line_count);
3927#endif
3928
Bram Moolenaar92c461e2020-04-24 22:19:00 +02003929#ifdef FEAT_GUI_HAIKU
3930 vim_lock_screen();
3931#endif
3932
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933#ifdef FEAT_GUI
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003934 // Don't update the GUI cursor here, ScreenLines[] is invalid until the
3935 // scrolling is actually carried out.
Bram Moolenaar107abd22016-08-12 14:08:25 +02003936 gui_dont_update_cursor(gui.cursor_row >= row + off
3937 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938#endif
3939
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003940 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
3941 cursor_col = wp->w_wincol;
3942
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003943 if (*T_CCS != NUL) // cursor relative to region
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 {
3945 cursor_row = row;
3946 cursor_end = end;
3947 }
3948 else
3949 {
3950 cursor_row = row + off;
3951 cursor_end = end + off;
3952 }
3953
3954 /*
3955 * Now shift LineOffset[] line_count up to reflect the deleted lines.
3956 * Clear the inserted lines in ScreenLines[].
3957 */
3958 row += off;
3959 end += off;
3960 for (i = 0; i < line_count; ++i)
3961 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 if (wp != NULL && wp->w_width != Columns)
3963 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003964 // need to copy part of a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 j = row + i;
3966 while ((j += line_count) <= end - 1)
3967 linecopy(j - line_count, j, wp);
3968 j -= line_count;
3969 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003970 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
3971 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 else
3973 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
3974 LineWraps[j] = FALSE;
3975 }
3976 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003978 // whole width, moving the line pointers is faster
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 j = row + i;
3980 temp = LineOffset[j];
3981 while ((j += line_count) <= end - 1)
3982 {
3983 LineOffset[j - line_count] = LineOffset[j];
3984 LineWraps[j - line_count] = LineWraps[j];
3985 }
3986 LineOffset[j - line_count] = temp;
3987 LineWraps[j - line_count] = FALSE;
3988 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003989 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 else
3991 lineinvalid(temp, (int)Columns);
3992 }
3993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003995#ifdef FEAT_GUI_HAIKU
3996 vim_unlock_screen();
3997#endif
3998
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003999 if (screen_attr != clear_attr)
4000 screen_stop_highlight();
4001 if (clear_attr != 0)
4002 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004004 // redraw the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 if (type == USE_REDRAW)
4006 redraw_block(row, end, wp);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004007 else if (type == USE_T_CD) // delete the lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02004009 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 out_str(T_CD);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004011 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 }
4013 else if (type == USE_T_CDL)
4014 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02004015 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 term_delete_lines(line_count);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004017 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 }
4019 /*
4020 * Deleting lines at top of the screen or scroll region: Just scroll
4021 * the whole screen (scroll region) up by outputting newlines on the
4022 * last line.
4023 */
4024 else if (type == USE_NL)
4025 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02004026 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 for (i = line_count; --i >= 0; )
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004028 out_char('\n'); // cursor will remain on same line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 }
4030 else
4031 {
4032 for (i = line_count; --i >= 0; )
4033 {
4034 if (type == USE_T_DL)
4035 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02004036 windgoto(cursor_row, cursor_col);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004037 out_str(T_DL); // delete a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004039 else // type == USE_T_CE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02004041 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004042 out_str(T_CE); // erase a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004044 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 }
4046 }
4047
4048 /*
4049 * If the 'db' flag is set, we need to clear the lines that have been
4050 * scrolled up at the bottom of the region.
4051 */
4052 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
4053 {
4054 for (i = line_count; i > 0; --i)
4055 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02004056 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004057 out_str(T_CE); // erase a line
4058 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 }
4060 }
4061
4062#ifdef FEAT_GUI
4063 gui_can_update_cursor();
4064 if (gui.in_use)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004065 out_flush(); // always flush after a scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066#endif
4067
4068 return OK;
4069}
4070
4071/*
Bram Moolenaarcb574f42019-01-25 22:29:57 +01004072 * Return TRUE when postponing displaying the mode message: when not redrawing
4073 * or inside a mapping.
4074 */
4075 int
4076skip_showmode()
4077{
4078 // Call char_avail() only when we are going to show something, because it
4079 // takes a bit of time. redrawing() may also call char_avail_avail().
4080 if (global_busy
4081 || msg_silent != 0
4082 || !redrawing()
4083 || (char_avail() && !KeyTyped))
4084 {
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004085 redraw_mode = TRUE; // show mode later
Bram Moolenaarcb574f42019-01-25 22:29:57 +01004086 return TRUE;
4087 }
4088 return FALSE;
4089}
4090
4091/*
Bram Moolenaar81226e02018-02-20 21:44:45 +01004092 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 *
4094 * If clear_cmdline is TRUE, clear the rest of the cmdline.
4095 * If clear_cmdline is FALSE there may be a message there that needs to be
4096 * cleared only if a mode is shown.
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004097 * If redraw_mode is TRUE show or clear the mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 * Return the length of the message (0 if no message).
4099 */
4100 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004101showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102{
4103 int need_clear;
4104 int length = 0;
4105 int do_mode;
4106 int attr;
4107 int nwr_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 int sub_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109
Bram Moolenaar7df351e2006-01-23 22:30:28 +00004110 do_mode = ((p_smd && msg_silent == 0)
4111 && ((State & INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +02004112 || restart_edit != NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004113 || VIsual_active));
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02004114 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 {
Bram Moolenaarcb574f42019-01-25 22:29:57 +01004116 if (skip_showmode())
4117 return 0; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118
4119 nwr_save = need_wait_return;
4120
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004121 // wait a bit before overwriting an important message
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 check_for_delay(FALSE);
4123
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004124 // if the cmdline is more than one line high, erase top lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 need_clear = clear_cmdline;
4126 if (clear_cmdline && cmdline_row < Rows - 1)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004127 msg_clr_cmdline(); // will reset clear_cmdline
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004129 // Position on the last line in the window, column 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 msg_pos_mode();
4131 cursor_off();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004132 attr = HL_ATTR(HLF_CM); // Highlight mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 if (do_mode)
4134 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004135 msg_puts_attr("--", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00004137 if (
Bram Moolenaar09092152010-08-08 16:38:42 +02004138# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +00004139 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +02004140# else
Bram Moolenaarc236c162008-07-13 17:41:49 +00004141 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +00004142# endif
Bram Moolenaar09092152010-08-08 16:38:42 +02004143 )
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004144# ifdef FEAT_GUI_GTK // most of the time, it's not XIM being used
Bram Moolenaar32526b32019-01-19 17:43:09 +01004145 msg_puts_attr(" IM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146# else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004147 msg_puts_attr(" XIM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148# endif
4149#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004150 // CTRL-X in Insert mode
Bram Moolenaarea389e92014-05-28 21:40:52 +02004151 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004153 // These messages can get long, avoid a wrap in a narrow
4154 // window. Prefer showing edit_submode_extra.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 length = (Rows - msg_row) * Columns - 3;
4156 if (edit_submode_extra != NULL)
4157 length -= vim_strsize(edit_submode_extra);
4158 if (length > 0)
4159 {
4160 if (edit_submode_pre != NULL)
4161 length -= vim_strsize(edit_submode_pre);
4162 if (length - vim_strsize(edit_submode) > 0)
4163 {
4164 if (edit_submode_pre != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004165 msg_puts_attr((char *)edit_submode_pre, attr);
4166 msg_puts_attr((char *)edit_submode, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 }
4168 if (edit_submode_extra != NULL)
4169 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004170 msg_puts_attr(" ", attr); // add a space in between
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004172 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 else
4174 sub_attr = attr;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004175 msg_puts_attr((char *)edit_submode_extra, sub_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 }
4177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 }
4179 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 if (State & VREPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004182 msg_puts_attr(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02004183 else if (State & REPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004184 msg_puts_attr(_(" REPLACE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 else if (State & INSERT)
4186 {
4187#ifdef FEAT_RIGHTLEFT
4188 if (p_ri)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004189 msg_puts_attr(_(" REVERSE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01004191 msg_puts_attr(_(" INSERT"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 }
Bram Moolenaar942b4542018-06-17 16:23:34 +02004193 else if (restart_edit == 'I' || restart_edit == 'A')
Bram Moolenaar32526b32019-01-19 17:43:09 +01004194 msg_puts_attr(_(" (insert)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 else if (restart_edit == 'R')
Bram Moolenaar32526b32019-01-19 17:43:09 +01004196 msg_puts_attr(_(" (replace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 else if (restart_edit == 'V')
Bram Moolenaar32526b32019-01-19 17:43:09 +01004198 msg_puts_attr(_(" (vreplace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199#ifdef FEAT_RIGHTLEFT
4200 if (p_hkmap)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004201 msg_puts_attr(_(" Hebrew"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202#endif
4203#ifdef FEAT_KEYMAP
4204 if (State & LANGMAP)
4205 {
4206# ifdef FEAT_ARABIC
4207 if (curwin->w_p_arab)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004208 msg_puts_attr(_(" Arabic"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 else
4210# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02004211 if (get_keymap_str(curwin, (char_u *)" (%s)",
4212 NameBuff, MAXPATHL))
Bram Moolenaar32526b32019-01-19 17:43:09 +01004213 msg_puts_attr((char *)NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 }
4215#endif
4216 if ((State & INSERT) && p_paste)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004217 msg_puts_attr(_(" (paste)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 if (VIsual_active)
4220 {
4221 char *p;
4222
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004223 // Don't concatenate separate words to avoid translation
4224 // problems.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 switch ((VIsual_select ? 4 : 0)
4226 + (VIsual_mode == Ctrl_V) * 2
4227 + (VIsual_mode == 'V'))
4228 {
4229 case 0: p = N_(" VISUAL"); break;
4230 case 1: p = N_(" VISUAL LINE"); break;
4231 case 2: p = N_(" VISUAL BLOCK"); break;
4232 case 4: p = N_(" SELECT"); break;
4233 case 5: p = N_(" SELECT LINE"); break;
4234 default: p = N_(" SELECT BLOCK"); break;
4235 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01004236 msg_puts_attr(_(p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01004238 msg_puts_attr(" --", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004240
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 need_clear = TRUE;
4242 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02004243 if (reg_recording != 0
Bram Moolenaare2c453d2019-08-21 14:37:09 +02004244 && edit_submode == NULL) // otherwise it gets too long
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004246 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 need_clear = TRUE;
4248 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004249
4250 mode_displayed = TRUE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004251 if (need_clear || clear_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 msg_clr_eos();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004253 msg_didout = FALSE; // overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 length = msg_col;
4255 msg_col = 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004256 need_wait_return = nwr_save; // never ask for hit-return for this
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 }
4258 else if (clear_cmdline && msg_silent == 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004259 // Clear the whole command line. Will reset "clear_cmdline".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 msg_clr_cmdline();
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004261 else if (redraw_mode)
4262 {
4263 msg_pos_mode();
4264 msg_clr_eos();
4265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266
4267#ifdef FEAT_CMDL_INFO
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004268 // In Visual mode the size of the selected area must be redrawn.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 if (VIsual_active)
4270 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004272 // If the last window has no status line, the ruler is after the mode
4273 // message and must be redrawn
Bram Moolenaar4033c552017-09-16 20:54:51 +02004274 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +02004275 win_redr_ruler(lastwin, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276#endif
4277 redraw_cmdline = FALSE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004278 redraw_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 clear_cmdline = FALSE;
4280
4281 return length;
4282}
4283
4284/*
4285 * Position for a mode message.
4286 */
4287 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004288msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289{
4290 msg_col = 0;
4291 msg_row = Rows - 1;
4292}
4293
4294/*
4295 * Delete mode message. Used when ESC is typed which is expected to end
4296 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004297 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 */
4299 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004300unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301{
4302 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +01004303 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 */
4305 if (!redrawing() || (!force && char_avail() && !KeyTyped))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004306 redraw_cmdline = TRUE; // delete mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +02004308 clearmode();
4309}
4310
4311/*
4312 * Clear the mode message.
4313 */
4314 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02004315clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +02004316{
Bram Moolenaar2abad542018-05-19 14:43:45 +02004317 int save_msg_row = msg_row;
4318 int save_msg_col = msg_col;
4319
Bram Moolenaarfd773e92016-04-02 19:39:16 +02004320 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02004321 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004322 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +02004323 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +02004324
4325 msg_col = save_msg_col;
4326 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327}
4328
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004329 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004330recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004331{
Bram Moolenaar32526b32019-01-19 17:43:09 +01004332 msg_puts_attr(_("recording"), attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004333 if (!shortmess(SHM_RECORDING))
4334 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004335 char s[4];
4336
4337 sprintf(s, " @%c", reg_recording);
4338 msg_puts_attr(s, attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004339 }
4340}
4341
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004342/*
4343 * Draw the tab pages line at the top of the Vim window.
4344 */
Bram Moolenaare12bab32019-01-08 22:02:56 +01004345 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004346draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004347{
4348 int tabcount = 0;
4349 tabpage_T *tp;
4350 int tabwidth;
4351 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00004352 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004353 int attr;
4354 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004355 win_T *cwp;
4356 int wincount;
4357 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004358 int c;
4359 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004360 int attr_sel = HL_ATTR(HLF_TPS);
4361 int attr_nosel = HL_ATTR(HLF_TP);
4362 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00004363 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004364 int room;
4365 int use_sep_chars = (t_colors < 8
4366#ifdef FEAT_GUI
4367 && !gui.in_use
4368#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02004369#ifdef FEAT_TERMGUICOLORS
4370 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +02004371#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004372 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004373
Bram Moolenaarc695cec2017-01-08 20:00:04 +01004374 if (ScreenLines == NULL)
4375 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00004376 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004377
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004378#ifdef FEAT_GUI_TABLINE
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004379 // Take care of a GUI tabline.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004380 if (gui_use_tabline())
4381 {
4382 gui_update_tabline();
4383 return;
4384 }
4385#endif
4386
4387 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004388 return;
4389
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004390#if defined(FEAT_STL_OPT)
Bram Moolenaarca57ab52019-04-13 14:53:16 +02004391 clear_TabPageIdxs();
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004392
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004393 // Use the 'tabline' option if it's set.
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004394 if (*p_tal != NUL)
4395 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +02004396 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +00004397
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004398 // Check for an error. If there is one we would loop in redrawing the
4399 // screen. Avoid that by making 'tabline' empty.
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +02004400 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004401 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +02004402 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +00004403 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004404 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +02004405 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004406 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00004407 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004408#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004409 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004410 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00004411 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004412
Bram Moolenaar238a5642006-02-21 22:12:05 +00004413 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
4414 if (tabwidth < 6)
4415 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004416
Bram Moolenaar238a5642006-02-21 22:12:05 +00004417 attr = attr_nosel;
4418 tabcount = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004419 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
4420 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00004421 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00004422 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004423
Bram Moolenaar238a5642006-02-21 22:12:05 +00004424 if (tp->tp_topframe == topframe)
4425 attr = attr_sel;
4426 if (use_sep_chars && col > 0)
4427 screen_putchar('|', 0, col++, attr);
4428
4429 if (tp->tp_topframe != topframe)
4430 attr = attr_nosel;
4431
4432 screen_putchar(' ', 0, col++, attr);
4433
4434 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00004435 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00004436 cwp = curwin;
4437 wp = firstwin;
4438 }
4439 else
4440 {
4441 cwp = tp->tp_curwin;
4442 wp = tp->tp_firstwin;
4443 }
4444
4445 modified = FALSE;
4446 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
4447 if (bufIsChanged(wp->w_buffer))
4448 modified = TRUE;
4449 if (modified || wincount > 1)
4450 {
4451 if (wincount > 1)
4452 {
4453 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004454 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004455 if (col + len >= Columns - 3)
4456 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +00004457 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004458#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004459 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004460#else
Bram Moolenaare0f14822014-08-06 13:20:56 +02004461 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004462#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00004463 );
4464 col += len;
4465 }
4466 if (modified)
4467 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
4468 screen_putchar(' ', 0, col++, attr);
4469 }
4470
4471 room = scol - col + tabwidth - 1;
4472 if (room > 0)
4473 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004474 // Get buffer name in NameBuff[]
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004475 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004476 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +00004477 len = vim_strsize(NameBuff);
4478 p = NameBuff;
Bram Moolenaar238a5642006-02-21 22:12:05 +00004479 if (has_mbyte)
4480 while (len > room)
4481 {
4482 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004483 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +00004484 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01004485 else if (len > room)
Bram Moolenaar238a5642006-02-21 22:12:05 +00004486 {
4487 p += len - room;
4488 len = room;
4489 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004490 if (len > Columns - col - 1)
4491 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +00004492
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004493 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +00004494 col += len;
4495 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00004496 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +00004497
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004498 // Store the tab page number in TabPageIdxs[], so that
4499 // jump_to_mouse() knows where each one is.
Bram Moolenaar238a5642006-02-21 22:12:05 +00004500 ++tabcount;
4501 while (scol < col)
4502 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004503 }
4504
Bram Moolenaar238a5642006-02-21 22:12:05 +00004505 if (use_sep_chars)
4506 c = '_';
4507 else
4508 c = ' ';
4509 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004510
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004511 // Put an "X" for closing the current tab if there are several.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004512 if (first_tabpage->tp_next != NULL)
4513 {
4514 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
4515 TabPageIdxs[Columns - 1] = -999;
4516 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004517 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +00004518
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004519 // Reset the flag here again, in case evaluating 'tabline' causes it to be
4520 // set.
Bram Moolenaarb21e5842006-04-16 18:30:08 +00004521 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004522}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004523
4524/*
4525 * Get buffer name for "buf" into NameBuff[].
4526 * Takes care of special buffer names and translates special characters.
4527 */
4528 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004529get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004530{
4531 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02004532 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004533 else
4534 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
4535 trans_characters(NameBuff, MAXPATHL);
4536}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004537
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538/*
4539 * Get the character to use in a status line. Get its attributes in "*attr".
4540 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02004541 int
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004542fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543{
4544 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004545
4546#ifdef FEAT_TERMINAL
4547 if (bt_terminal(wp->w_buffer))
4548 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004549 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02004550 {
4551 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004552 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02004553 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004554 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02004555 {
4556 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004557 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02004558 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004559 }
4560 else
4561#endif
4562 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004564 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 fill = fill_stl;
4566 }
4567 else
4568 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004569 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 fill = fill_stlnc;
4571 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004572 // Use fill when there is highlighting, and highlighting of current
4573 // window differs, or the fillchars differ, or this is not the
4574 // current window
Bram Moolenaar8820b482017-03-16 17:23:31 +01004575 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004576 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 || (fill_stl != fill_stlnc)))
4578 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004579 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 return '^';
4581 return '=';
4582}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584/*
4585 * Get the character to use in a separator between vertically split windows.
4586 * Get its attributes in "*attr".
4587 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02004588 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004589fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590{
Bram Moolenaar8820b482017-03-16 17:23:31 +01004591 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 if (*attr == 0 && fill_vert == ' ')
4593 return '|';
4594 else
4595 return fill_vert;
4596}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597
4598/*
4599 * Return TRUE if redrawing should currently be done.
4600 */
4601 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004602redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603{
Bram Moolenaareb992cb2017-03-09 18:20:16 +01004604#ifdef FEAT_EVAL
4605 if (disable_redraw_for_testing)
4606 return 0;
4607 else
4608#endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +02004609 return ((!RedrawingDisabled
4610#ifdef FEAT_EVAL
4611 || ignore_redraw_flag_for_testing
4612#endif
4613 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614}
4615
4616/*
4617 * Return TRUE if printing messages should currently be done.
4618 */
4619 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004620messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621{
4622 return (!(p_lz && char_avail() && !KeyTyped));
4623}
4624
Bram Moolenaare677df82019-09-02 22:31:11 +02004625/*
4626 * Compute columns for ruler and shown command. 'sc_col' is also used to
4627 * decide what the maximum length of a message on the status line can be.
4628 * If there is a status line for the last window, 'sc_col' is independent
4629 * of 'ru_col'.
4630 */
4631
4632#define COL_RULER 17 // columns needed by standard ruler
4633
4634 void
4635comp_col(void)
4636{
4637#if defined(FEAT_CMDL_INFO)
4638 int last_has_status = (p_ls == 2 || (p_ls == 1 && !ONE_WINDOW));
4639
4640 sc_col = 0;
4641 ru_col = 0;
4642 if (p_ru)
4643 {
4644# ifdef FEAT_STL_OPT
4645 ru_col = (ru_wid ? ru_wid : COL_RULER) + 1;
4646# else
4647 ru_col = COL_RULER + 1;
4648# endif
4649 // no last status line, adjust sc_col
4650 if (!last_has_status)
4651 sc_col = ru_col;
4652 }
4653 if (p_sc)
4654 {
4655 sc_col += SHOWCMD_COLS;
4656 if (!p_ru || last_has_status) // no need for separating space
4657 ++sc_col;
4658 }
4659 sc_col = Columns - sc_col;
4660 ru_col = Columns - ru_col;
4661 if (sc_col <= 0) // screen too narrow, will become a mess
4662 sc_col = 1;
4663 if (ru_col <= 0)
4664 ru_col = 1;
4665#else
4666 sc_col = Columns;
4667 ru_col = Columns;
4668#endif
4669#ifdef FEAT_EVAL
4670 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
4671#endif
4672}
4673
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004674#if defined(FEAT_LINEBREAK) || defined(PROTO)
4675/*
Bram Moolenaar64486672010-05-16 15:46:46 +02004676 * Return the width of the 'number' and 'relativenumber' column.
4677 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004678 * Otherwise it depends on 'numberwidth' and the line count.
4679 */
4680 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004681number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004682{
4683 int n;
4684 linenr_T lnum;
4685
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004686 if (wp->w_p_rnu && !wp->w_p_nu)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004687 // cursor line shows "0"
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004688 lnum = wp->w_height;
4689 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004690 // cursor line shows absolute line number
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004691 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +02004692
Bram Moolenaar6b314672015-03-20 15:42:10 +01004693 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004694 return wp->w_nrwidth_width;
4695 wp->w_nrwidth_line_count = lnum;
4696
4697 n = 0;
4698 do
4699 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004700 lnum /= 10;
4701 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004702 } while (lnum > 0);
4703
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004704 // 'numberwidth' gives the minimal width plus one
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004705 if (n < wp->w_p_nuw - 1)
4706 n = wp->w_p_nuw - 1;
4707
Bram Moolenaare4b407f2019-07-04 11:59:28 +02004708# ifdef FEAT_SIGNS
4709 // If 'signcolumn' is set to 'number' and there is a sign to display, then
4710 // the minimal width for the number column is 2.
Bram Moolenaar4eb7dae2019-11-12 22:33:45 +01004711 if (n < 2 && get_first_valid_sign(wp) != NULL
Bram Moolenaare4b407f2019-07-04 11:59:28 +02004712 && (*wp->w_p_scl == 'n' && *(wp->w_p_scl + 1) == 'u'))
4713 n = 2;
4714# endif
4715
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004716 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +01004717 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004718 return n;
4719}
4720#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +01004721
Bram Moolenaar113e1072019-01-20 15:30:40 +01004722#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9750bb12012-12-05 16:10:42 +01004723/*
4724 * Return the current cursor column. This is the actual position on the
4725 * screen. First column is 0.
4726 */
4727 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004728screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +01004729{
4730 return screen_cur_col;
4731}
4732
4733/*
4734 * Return the current cursor row. This is the actual position on the screen.
4735 * First row is 0.
4736 */
4737 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004738screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +01004739{
4740 return screen_cur_row;
4741}
Bram Moolenaar113e1072019-01-20 15:30:40 +01004742#endif
Bram Moolenaare677df82019-09-02 22:31:11 +02004743
4744/*
4745 * Handle setting 'listchars' or 'fillchars'.
4746 * Returns error message, NULL if it's OK.
4747 */
4748 char *
4749set_chars_option(char_u **varp)
4750{
4751 int round, i, len, entries;
4752 char_u *p, *s;
4753 int c1 = 0, c2 = 0, c3 = 0;
4754 struct charstab
4755 {
4756 int *cp;
4757 char *name;
4758 };
4759 static struct charstab filltab[] =
4760 {
4761 {&fill_stl, "stl"},
4762 {&fill_stlnc, "stlnc"},
4763 {&fill_vert, "vert"},
4764 {&fill_fold, "fold"},
4765 {&fill_diff, "diff"},
4766 };
4767 static struct charstab lcstab[] =
4768 {
4769 {&lcs_eol, "eol"},
4770 {&lcs_ext, "extends"},
4771 {&lcs_nbsp, "nbsp"},
4772 {&lcs_prec, "precedes"},
4773 {&lcs_space, "space"},
4774 {&lcs_tab2, "tab"},
4775 {&lcs_trail, "trail"},
4776#ifdef FEAT_CONCEAL
4777 {&lcs_conceal, "conceal"},
4778#else
4779 {NULL, "conceal"},
4780#endif
4781 };
4782 struct charstab *tab;
4783
4784 if (varp == &p_lcs)
4785 {
4786 tab = lcstab;
4787 entries = sizeof(lcstab) / sizeof(struct charstab);
4788 }
4789 else
4790 {
4791 tab = filltab;
4792 entries = sizeof(filltab) / sizeof(struct charstab);
4793 }
4794
4795 // first round: check for valid value, second round: assign values
4796 for (round = 0; round <= 1; ++round)
4797 {
4798 if (round > 0)
4799 {
4800 // After checking that the value is valid: set defaults: space for
4801 // 'fillchars', NUL for 'listchars'
4802 for (i = 0; i < entries; ++i)
4803 if (tab[i].cp != NULL)
4804 *(tab[i].cp) = (varp == &p_lcs ? NUL : ' ');
4805
4806 if (varp == &p_lcs)
4807 {
4808 lcs_tab1 = NUL;
4809 lcs_tab3 = NUL;
4810 }
4811 else
4812 fill_diff = '-';
4813 }
4814 p = *varp;
4815 while (*p)
4816 {
4817 for (i = 0; i < entries; ++i)
4818 {
4819 len = (int)STRLEN(tab[i].name);
4820 if (STRNCMP(p, tab[i].name, len) == 0
4821 && p[len] == ':'
4822 && p[len + 1] != NUL)
4823 {
4824 c2 = c3 = 0;
4825 s = p + len + 1;
4826 c1 = mb_ptr2char_adv(&s);
4827 if (mb_char2cells(c1) > 1)
4828 continue;
4829 if (tab[i].cp == &lcs_tab2)
4830 {
4831 if (*s == NUL)
4832 continue;
4833 c2 = mb_ptr2char_adv(&s);
4834 if (mb_char2cells(c2) > 1)
4835 continue;
4836 if (!(*s == ',' || *s == NUL))
4837 {
4838 c3 = mb_ptr2char_adv(&s);
4839 if (mb_char2cells(c3) > 1)
4840 continue;
4841 }
4842 }
4843
4844 if (*s == ',' || *s == NUL)
4845 {
4846 if (round)
4847 {
4848 if (tab[i].cp == &lcs_tab2)
4849 {
4850 lcs_tab1 = c1;
4851 lcs_tab2 = c2;
4852 lcs_tab3 = c3;
4853 }
4854 else if (tab[i].cp != NULL)
4855 *(tab[i].cp) = c1;
4856
4857 }
4858 p = s;
4859 break;
4860 }
4861 }
4862 }
4863
4864 if (i == entries)
4865 return e_invarg;
4866 if (*p == ',')
4867 ++p;
4868 }
4869 }
4870
4871 return NULL; // no error
4872}
Bram Moolenaar017ba072019-09-14 21:01:23 +02004873