blob: 01d6257b7cb345d1799833e8a4b4bf4651a6f1a7 [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.
467 if (coloff > 0 && ScreenLines[off_to] == 0)
468 {
469 ScreenLines[off_to - 1] = ' ';
470 ScreenLinesUC[off_to - 1] = 0;
471 screen_char(off_to - 1, row, col + coloff - 1);
472 }
473#endif
474
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
476
477 while (col < endcol)
478 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +0000480 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481 else
482 char_cells = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483
484 redraw_this = redraw_next;
485 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
486 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
487
488#ifdef FEAT_GUI
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100489 // If the next character was bold, then redraw the current character to
490 // remove any pixels that might have spilt over into us. This only
491 // happens in the GUI.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492 if (redraw_next && gui.in_use)
493 {
494 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000495 if (hl > HL_ALL)
496 hl = syn_attr2attr(hl);
497 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 redraw_this = TRUE;
499 }
500#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100501#ifdef FEAT_PROP_POPUP
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200502 if (blocked_by_popup(row, col + coloff))
Bram Moolenaar33796b32019-06-08 16:01:13 +0200503 redraw_this = FALSE;
504#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505 if (redraw_this)
506 {
507 /*
508 * Special handling when 'xs' termcap flag set (hpterm):
509 * Attributes for characters are stored at the position where the
510 * cursor is when writing the highlighting code. The
511 * start-highlighting code must be written with the cursor on the
512 * first highlighted character. The stop-highlighting code must
513 * be written with the cursor just after the last highlighted
514 * character.
Bram Moolenaarc4568ab2018-11-16 16:21:05 +0100515 * Overwriting a character doesn't remove its highlighting. Need
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516 * to clear the rest of the line, and force redrawing it
517 * completely.
518 */
519 if ( p_wiv
520 && !force
521#ifdef FEAT_GUI
522 && !gui.in_use
523#endif
524 && ScreenAttrs[off_to] != 0
525 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
526 {
527 /*
528 * Need to remove highlighting attributes here.
529 */
530 windgoto(row, col + coloff);
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100531 out_str(T_CE); // clear rest of this screen line
532 screen_start(); // don't know where cursor is now
533 force = TRUE; // force redraw of rest of the line
534 redraw_next = TRUE; // or else next char would miss out
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535
536 /*
537 * If the previous character was highlighted, need to stop
538 * highlighting at this character.
539 */
540 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
541 {
542 screen_attr = ScreenAttrs[off_to - 1];
543 term_windgoto(row, col + coloff);
544 screen_stop_highlight();
545 }
546 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100547 screen_attr = 0; // highlighting has stopped
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549 if (enc_dbcs != 0)
550 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100551 // Check if overwriting a double-byte with a single-byte or
552 // the other way around requires another character to be
553 // redrawn. For UTF-8 this isn't needed, because comparing
554 // ScreenLinesUC[] is sufficient.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 if (char_cells == 1
556 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +0000557 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100559 // Writing a single-cell character over a double-cell
560 // character: need to redraw the next cell.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 ScreenLines[off_to + 1] = 0;
562 redraw_next = TRUE;
563 }
564 else if (char_cells == 2
565 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +0000566 && (*mb_off2cells)(off_to, max_off_to) == 1
567 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100569 // Writing the second half of a double-cell character over
570 // a double-cell character: need to redraw the second
571 // cell.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 ScreenLines[off_to + 2] = 0;
573 redraw_next = TRUE;
574 }
575
576 if (enc_dbcs == DBCS_JPNU)
577 ScreenLines2[off_to] = ScreenLines2[off_from];
578 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100579 // When writing a single-width character over a double-width
580 // character and at the end of the redrawn text, need to clear out
581 // the right halve of the old character.
582 // Also required when writing the right halve of a double-width
583 // char over the left halve of an existing one.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 if (has_mbyte && col + char_cells == endcol
585 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +0000586 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +0000588 && (*mb_off2cells)(off_to, max_off_to) == 1
589 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 clear_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591
592 ScreenLines[off_to] = ScreenLines[off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 if (enc_utf8)
594 {
595 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
596 if (ScreenLinesUC[off_from] != 0)
597 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000598 int i;
599
600 for (i = 0; i < Screen_mco; ++i)
601 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 }
603 }
604 if (char_cells == 2)
605 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606
607#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100608 // The bold trick makes a single column of pixels appear in the
609 // next character. When a bold character is removed, the next
610 // character should be redrawn too. This happens for our own GUI
611 // and for some xterms.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 if (
613# ifdef FEAT_GUI
614 gui.in_use
615# endif
616# if defined(FEAT_GUI) && defined(UNIX)
617 ||
618# endif
619# ifdef UNIX
620 term_is_xterm
621# endif
622 )
623 {
624 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000625 if (hl > HL_ALL)
626 hl = syn_attr2attr(hl);
627 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 redraw_next = TRUE;
629 }
630#endif
631 ScreenAttrs[off_to] = ScreenAttrs[off_from];
Bram Moolenaara12a1612019-01-24 16:39:02 +0100632
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100633 // For simplicity set the attributes of second half of a
634 // double-wide character equal to the first half.
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000635 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000637
638 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 screen_char(off_to, row, col + coloff);
642 }
643 else if ( p_wiv
644#ifdef FEAT_GUI
645 && !gui.in_use
646#endif
647 && col + coloff > 0)
648 {
649 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
650 {
651 /*
652 * Don't output stop-highlight when moving the cursor, it will
653 * stop the highlighting when it should continue.
654 */
655 screen_attr = 0;
656 }
657 else if (screen_attr != 0)
658 screen_stop_highlight();
659 }
660
661 off_to += CHAR_CELLS;
662 off_from += CHAR_CELLS;
663 col += CHAR_CELLS;
664 }
665
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 if (clear_next)
667 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100668 // Clear the second half of a double-wide character of which the left
669 // half was overwritten with a single-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 ScreenLines[off_to] = ' ';
671 if (enc_utf8)
672 ScreenLinesUC[off_to] = 0;
673 screen_char(off_to, row, col + coloff);
674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675
676 if (clear_width > 0
677#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200678 && !(flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679#endif
680 )
681 {
682#ifdef FEAT_GUI
683 int startCol = col;
684#endif
685
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100686 // blank out the rest of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 while (col < clear_width && ScreenLines[off_to] == ' '
688 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +0100689 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 {
691 ++off_to;
692 ++col;
693 }
694 if (col < clear_width)
695 {
696#ifdef FEAT_GUI
697 /*
698 * In the GUI, clearing the rest of the line may leave pixels
699 * behind if the first character cleared was bold. Some bold
700 * fonts spill over the left. In this case we redraw the previous
701 * character too. If we didn't skip any blanks above, then we
702 * only redraw if the character wasn't already redrawn anyway.
703 */
Bram Moolenaar9c697322006-10-09 20:11:17 +0000704 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 {
706 hl = ScreenAttrs[off_to];
707 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +0000708 {
709 int prev_cells = 1;
Bram Moolenaara12a1612019-01-24 16:39:02 +0100710
Bram Moolenaar9c697322006-10-09 20:11:17 +0000711 if (enc_utf8)
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100712 // for utf-8, ScreenLines[char_offset + 1] == 0 means
713 // that its width is 2.
Bram Moolenaar9c697322006-10-09 20:11:17 +0000714 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
715 else if (enc_dbcs != 0)
716 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100717 // find previous character by counting from first
718 // column and get its width.
Bram Moolenaar9c697322006-10-09 20:11:17 +0000719 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +0000720 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +0000721
722 while (off < off_to)
723 {
Bram Moolenaar367329b2007-08-30 11:53:22 +0000724 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +0000725 off += prev_cells;
726 }
727 }
728
729 if (enc_dbcs != 0 && prev_cells > 1)
730 screen_char_2(off_to - prev_cells, row,
731 col + coloff - prev_cells);
732 else
Bram Moolenaar9c697322006-10-09 20:11:17 +0000733 screen_char(off_to - prev_cells, row,
734 col + coloff - prev_cells);
735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 }
737#endif
738 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
739 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 off_to += clear_width - col;
741 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 }
743 }
744
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200745 if (clear_width > 0
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100746#ifdef FEAT_PROP_POPUP
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200747 && !(flags & SLF_POPUP) // no separator for popup window
748#endif
749 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200751 // For a window that has a right neighbor, draw the separator char
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200752 // right of the window contents. But not on top of a popup window.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200753 if (coloff + col < Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 {
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100755#ifdef FEAT_PROP_POPUP
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200756 if (!blocked_by_popup(row, col + coloff))
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200757#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200759 int c;
760
761 c = fillchar_vsep(&hl);
762 if (ScreenLines[off_to] != (schar_T)c
763 || (enc_utf8 && (int)ScreenLinesUC[off_to]
764 != (c >= 0x80 ? c : 0))
765 || ScreenAttrs[off_to] != hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200767 ScreenLines[off_to] = c;
768 ScreenAttrs[off_to] = hl;
769 if (enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200771 if (c >= 0x80)
772 {
773 ScreenLinesUC[off_to] = c;
774 ScreenLinesC[0][off_to] = 0;
775 }
776 else
777 ScreenLinesUC[off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 }
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200779 screen_char(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 }
782 }
783 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 LineWraps[row] = FALSE;
785 }
786}
787
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000788#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000790 * Mirror text "str" for right-left displaying.
791 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000793 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100794rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795{
796 char_u *p1, *p2;
797 int t;
798
799 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
800 {
801 t = *p1;
802 *p1 = *p2;
803 *p2 = t;
804 }
805}
806#endif
807
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 * Draw the verticap separator right of window "wp" starting with line "row".
810 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200811 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100812draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813{
814 int hl;
815 int c;
816
817 if (wp->w_vsep_width)
818 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100819 // draw the vertical separator right of this window
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 c = fillchar_vsep(&hl);
821 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
822 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
823 c, ' ', hl);
824 }
825}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826
827#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100828static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829
830/*
Bram Moolenaar367329b2007-08-30 11:53:22 +0000831 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 */
833 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100834status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835{
836 int len = 0;
837
838#ifdef FEAT_MENU
839 int emenu = (xp->xp_context == EXPAND_MENUS
840 || xp->xp_context == EXPAND_MENUNAMES);
841
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100842 // Check for menu separators - replace with '|'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 if (emenu && menu_is_separator(s))
844 return 1;
845#endif
846
847 while (*s != NUL)
848 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +0000849 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +0000850 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100851 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 }
853
854 return len;
855}
856
857/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +0000858 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000859 * These are backslashes used for escaping. Do show backslashes in help tags.
860 */
861 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100862skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000863{
Bram Moolenaar7693ec62008-07-24 18:29:37 +0000864 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000865#ifdef FEAT_MENU
866 || ((xp->xp_context == EXPAND_MENUS
867 || xp->xp_context == EXPAND_MENUNAMES)
868 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
869#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +0000870 )
871 {
872#ifndef BACKSLASH_IN_FILENAME
873 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
874 return 2;
875#endif
876 return 1;
877 }
878 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000879}
880
881/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 * Show wildchar matches in the status line.
883 * Show at least the "match" item.
884 * We start at item 'first_match' in the list and show all matches that fit.
885 *
886 * If inversion is possible we use it. Else '=' characters are used.
887 */
888 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100889win_redr_status_matches(
890 expand_T *xp,
891 int num_matches,
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100892 char_u **matches, // list of matches
Bram Moolenaar05540972016-01-30 20:31:25 +0100893 int match,
894 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895{
896#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
897 int row;
898 char_u *buf;
899 int len;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100900 int clen; // length in screen cells
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 int fillchar;
902 int attr;
903 int i;
904 int highlight = TRUE;
905 char_u *selstart = NULL;
906 int selstart_col = 0;
907 char_u *selend = NULL;
908 static int first_match = 0;
909 int add_left = FALSE;
910 char_u *s;
911#ifdef FEAT_MENU
912 int emenu;
913#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100916 if (matches == NULL) // interrupted completion?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 return;
918
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000919 if (has_mbyte)
Bram Moolenaar964b3742019-05-24 18:54:09 +0200920 buf = alloc(Columns * MB_MAXBYTES + 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000921 else
Bram Moolenaar964b3742019-05-24 18:54:09 +0200922 buf = alloc(Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 if (buf == NULL)
924 return;
925
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100926 if (match == -1) // don't show match but original text
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 {
928 match = 0;
929 highlight = FALSE;
930 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100931 // count 1 for the ending ">"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 clen = status_match_len(xp, L_MATCH(match)) + 3;
933 if (match == 0)
934 first_match = 0;
935 else if (match < first_match)
936 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100937 // jumping left, as far as we can go
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 first_match = match;
939 add_left = TRUE;
940 }
941 else
942 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100943 // check if match fits on the screen
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 for (i = first_match; i < match; ++i)
945 clen += status_match_len(xp, L_MATCH(i)) + 2;
946 if (first_match > 0)
947 clen += 2;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100948 // jumping right, put match at the left
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 if ((long)clen > Columns)
950 {
951 first_match = match;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100952 // if showing the last match, we can add some on the left
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 clen = 2;
954 for (i = match; i < num_matches; ++i)
955 {
956 clen += status_match_len(xp, L_MATCH(i)) + 2;
957 if ((long)clen >= Columns)
958 break;
959 }
960 if (i == num_matches)
961 add_left = TRUE;
962 }
963 }
964 if (add_left)
965 while (first_match > 0)
966 {
967 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
968 if ((long)clen >= Columns)
969 break;
970 --first_match;
971 }
972
Bram Moolenaar3633cf52017-07-31 22:29:35 +0200973 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974
975 if (first_match == 0)
976 {
977 *buf = NUL;
978 len = 0;
979 }
980 else
981 {
982 STRCPY(buf, "< ");
983 len = 2;
984 }
985 clen = len;
986
987 i = first_match;
988 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
989 {
990 if (i == match)
991 {
992 selstart = buf + len;
993 selstart_col = clen;
994 }
995
996 s = L_MATCH(i);
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100997 // Check for menu separators - replace with '|'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998#ifdef FEAT_MENU
999 emenu = (xp->xp_context == EXPAND_MENUS
1000 || xp->xp_context == EXPAND_MENUNAMES);
1001 if (emenu && menu_is_separator(s))
1002 {
1003 STRCPY(buf + len, transchar('|'));
1004 l = (int)STRLEN(buf + len);
1005 len += l;
1006 clen += l;
1007 }
1008 else
1009#endif
1010 for ( ; *s != NUL; ++s)
1011 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00001012 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 clen += ptr2cells(s);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001014 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 {
1016 STRNCPY(buf + len, s, l);
1017 s += l - 1;
1018 len += l;
1019 }
1020 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 {
1022 STRCPY(buf + len, transchar_byte(*s));
1023 len += (int)STRLEN(buf + len);
1024 }
1025 }
1026 if (i == match)
1027 selend = buf + len;
1028
1029 *(buf + len++) = ' ';
1030 *(buf + len++) = ' ';
1031 clen += 2;
1032 if (++i == num_matches)
1033 break;
1034 }
1035
1036 if (i != num_matches)
1037 {
1038 *(buf + len++) = '>';
1039 ++clen;
1040 }
1041
1042 buf[len] = NUL;
1043
1044 row = cmdline_row - 1;
1045 if (row >= 0)
1046 {
1047 if (wild_menu_showing == 0)
1048 {
1049 if (msg_scrolled > 0)
1050 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001051 // Put the wildmenu just above the command line. If there is
1052 // no room, scroll the screen one line up.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 if (cmdline_row == Rows - 1)
1054 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001055 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 ++msg_scrolled;
1057 }
1058 else
1059 {
1060 ++cmdline_row;
1061 ++row;
1062 }
1063 wild_menu_showing = WM_SCROLLED;
1064 }
1065 else
1066 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001067 // Create status line if needed by setting 'laststatus' to 2.
1068 // Set 'winminheight' to zero to avoid that the window is
1069 // resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 if (lastwin->w_status_height == 0)
1071 {
1072 save_p_ls = p_ls;
1073 save_p_wmh = p_wmh;
1074 p_ls = 2;
1075 p_wmh = 0;
1076 last_status(FALSE);
1077 }
1078 wild_menu_showing = WM_SHOWN;
1079 }
1080 }
1081
1082 screen_puts(buf, row, 0, attr);
1083 if (selstart != NULL && highlight)
1084 {
1085 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001086 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 }
1088
1089 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
1090 }
1091
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 vim_free(buf);
1094}
1095#endif
1096
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 * Return TRUE if the status line of window "wp" is connected to the status
1099 * line of the window right of it. If not, then it's a vertical separator.
1100 * Only call if (wp->w_vsep_width != 0).
1101 */
1102 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001103stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104{
1105 frame_T *fr;
1106
1107 fr = wp->w_frame;
1108 while (fr->fr_parent != NULL)
1109 {
1110 if (fr->fr_parent->fr_layout == FR_COL)
1111 {
1112 if (fr->fr_next != NULL)
1113 break;
1114 }
1115 else
1116 {
1117 if (fr->fr_next != NULL)
1118 return TRUE;
1119 }
1120 fr = fr->fr_parent;
1121 }
1122 return FALSE;
1123}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126/*
1127 * Get the value to show for the language mappings, active 'keymap'.
1128 */
1129 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001130get_keymap_str(
1131 win_T *wp,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001132 char_u *fmt, // format string containing one %s item
1133 char_u *buf, // buffer for the result
1134 int len) // length of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135{
1136 char_u *p;
1137
1138 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
1139 return FALSE;
1140
1141 {
1142#ifdef FEAT_EVAL
1143 buf_T *old_curbuf = curbuf;
1144 win_T *old_curwin = curwin;
1145 char_u *s;
1146
1147 curbuf = wp->w_buffer;
1148 curwin = wp;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001149 STRCPY(buf, "b:keymap_name"); // must be writable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 ++emsg_skip;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001151 s = p = eval_to_string(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 --emsg_skip;
1153 curbuf = old_curbuf;
1154 curwin = old_curwin;
1155 if (p == NULL || *p == NUL)
1156#endif
1157 {
1158#ifdef FEAT_KEYMAP
1159 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
1160 p = wp->w_buffer->b_p_keymap;
1161 else
1162#endif
1163 p = (char_u *)"lang";
1164 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02001165 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 buf[0] = NUL;
1167#ifdef FEAT_EVAL
1168 vim_free(s);
1169#endif
1170 }
1171 return buf[0] != NUL;
1172}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173
1174#if defined(FEAT_STL_OPT) || defined(PROTO)
1175/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001176 * Redraw the status line or ruler of window "wp".
1177 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001179 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001180win_redr_custom(
1181 win_T *wp,
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001182 int draw_ruler) // TRUE or FALSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183{
Bram Moolenaar1d633412013-12-11 15:52:01 +01001184 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 int attr;
1186 int curattr;
1187 int row;
1188 int col = 0;
1189 int maxwidth;
1190 int width;
1191 int n;
1192 int len;
1193 int fillchar;
1194 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00001195 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001197 struct stl_hlrec hltab[STL_MAX_ITEM];
1198 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001199 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01001200 win_T *ewp;
1201 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001203 // There is a tiny chance that this gets called recursively: When
1204 // redrawing a status line triggers redrawing the ruler or tabline.
1205 // Avoid trouble by not allowing recursion.
Bram Moolenaar1d633412013-12-11 15:52:01 +01001206 if (entered)
1207 return;
1208 entered = TRUE;
1209
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001210 // setup environment for the task at hand
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001211 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 {
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001213 // Use 'tabline'. Always at the first line of the screen.
Bram Moolenaar362f3562009-11-03 16:20:34 +00001214 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001215 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00001216 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01001217 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001218 maxwidth = Columns;
1219# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001220 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001221# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001223 else
1224 {
1225 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02001226 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02001227 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001228
1229 if (draw_ruler)
1230 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00001231 stl = p_ruf;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001232 // advance past any leading group spec - implicit in ru_col
Bram Moolenaar362f3562009-11-03 16:20:34 +00001233 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001234 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00001235 if (*++stl == '-')
1236 stl++;
1237 if (atoi((char *)stl))
1238 while (VIM_ISDIGIT(*stl))
1239 stl++;
1240 if (*stl++ != '(')
1241 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001242 }
Bram Moolenaar02631462017-09-22 15:20:32 +02001243 col = ru_col - (Columns - wp->w_width);
1244 if (col < (wp->w_width + 1) / 2)
1245 col = (wp->w_width + 1) / 2;
1246 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001247 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001248 {
1249 row = Rows - 1;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001250 --maxwidth; // writing in last column may cause scrolling
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001251 fillchar = ' ';
1252 attr = 0;
1253 }
1254
1255# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001256 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001257# endif
1258 }
1259 else
1260 {
1261 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00001262 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001263 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00001264 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001265# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001266 use_sandbox = was_set_insecurely((char_u *)"statusline",
1267 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001268# endif
1269 }
1270
Bram Moolenaar53f81742017-09-22 14:35:51 +02001271 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001272 }
1273
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01001275 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001277 // Temporarily reset 'cursorbind', we don't want a side effect from moving
1278 // the cursor away and back.
Bram Moolenaar61452852011-02-01 18:01:11 +01001279 ewp = wp == NULL ? curwin : wp;
1280 p_crb_save = ewp->w_p_crb;
1281 ewp->w_p_crb = FALSE;
1282
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001283 // Make a copy, because the statusline may include a function call that
1284 // might change the option value and free the memory.
Bram Moolenaar362f3562009-11-03 16:20:34 +00001285 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01001286 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00001287 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001288 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00001289 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01001290 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001292 // Make all characters printable.
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01001293 p = transstr(buf);
1294 if (p != NULL)
1295 {
1296 vim_strncpy(buf, p, sizeof(buf) - 1);
1297 vim_free(p);
1298 }
1299
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001300 // fill up with "fillchar"
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01001301 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00001302 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 len += (*mb_char2bytes)(fillchar, buf + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 ++width;
1306 }
1307 buf[len] = NUL;
1308
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001309 /*
1310 * Draw each snippet with the specified highlighting.
1311 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 curattr = attr;
1313 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001314 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001316 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 screen_puts_len(p, len, row, col, curattr);
1318 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001319 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001321 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001323 else if (hltab[n].userhl < 0)
1324 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02001325#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02001326 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
1327 && wp->w_status_height != 0)
1328 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02001329 else if (wp != NULL && bt_terminal(wp->w_buffer)
1330 && wp->w_status_height != 0)
1331 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02001332#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00001333 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001334 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001336 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 }
1338 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001339
1340 if (wp == NULL)
1341 {
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001342 // Fill the TabPageIdxs[] array for clicking in the tab pagesline.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001343 col = 0;
1344 len = 0;
1345 p = buf;
1346 fillchar = 0;
1347 for (n = 0; tabtab[n].start != NULL; n++)
1348 {
1349 len += vim_strnsize(p, (int)(tabtab[n].start - p));
1350 while (col < len)
1351 TabPageIdxs[col++] = fillchar;
1352 p = tabtab[n].start;
1353 fillchar = tabtab[n].userhl;
1354 }
1355 while (col < Columns)
1356 TabPageIdxs[col++] = fillchar;
1357 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01001358
1359theend:
1360 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361}
1362
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001363#endif // FEAT_STL_OPT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364
1365/*
1366 * Output a single character directly to the screen and update ScreenLines.
1367 */
1368 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001369screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 char_u buf[MB_MAXBYTES + 1];
1372
Bram Moolenaar9a920d82012-06-01 15:21:02 +02001373 if (has_mbyte)
1374 buf[(*mb_char2bytes)(c, buf)] = NUL;
1375 else
Bram Moolenaar9a920d82012-06-01 15:21:02 +02001376 {
1377 buf[0] = c;
1378 buf[1] = NUL;
1379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 screen_puts(buf, row, col, attr);
1381}
1382
1383/*
1384 * Get a single character directly from ScreenLines into "bytes[]".
1385 * Also return its attribute in *attrp;
1386 */
1387 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001388screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389{
1390 unsigned off;
1391
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001392 // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
1394 {
1395 off = LineOffset[row] + col;
1396 *attrp = ScreenAttrs[off];
1397 bytes[0] = ScreenLines[off];
1398 bytes[1] = NUL;
1399
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 if (enc_utf8 && ScreenLinesUC[off] != 0)
1401 bytes[utfc_char2bytes(off, bytes)] = NUL;
1402 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
1403 {
1404 bytes[0] = ScreenLines[off];
1405 bytes[1] = ScreenLines2[off];
1406 bytes[2] = NUL;
1407 }
1408 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
1409 {
1410 bytes[1] = ScreenLines[off + 1];
1411 bytes[2] = NUL;
1412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 }
1414}
1415
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001416/*
1417 * Return TRUE if composing characters for screen posn "off" differs from
1418 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01001419 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001420 */
1421 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001422screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001423{
1424 int i;
1425
1426 for (i = 0; i < Screen_mco; ++i)
1427 {
1428 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
1429 return TRUE;
1430 if (u8cc[i] == 0)
1431 break;
1432 }
1433 return FALSE;
1434}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001435
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436/*
1437 * Put string '*text' on the screen at position 'row' and 'col', with
1438 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
1439 * Note: only outputs within one row, message is truncated at screen boundary!
1440 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
1441 */
1442 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001443screen_puts(
1444 char_u *text,
1445 int row,
1446 int col,
1447 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448{
1449 screen_puts_len(text, -1, row, col, attr);
1450}
1451
1452/*
1453 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
1454 * a NUL.
1455 */
1456 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001457screen_puts_len(
1458 char_u *text,
1459 int textlen,
1460 int row,
1461 int col,
1462 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463{
1464 unsigned off;
1465 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02001466 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 int c;
Bram Moolenaar367329b2007-08-30 11:53:22 +00001468 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 int mbyte_blen = 1;
1470 int mbyte_cells = 1;
1471 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001472 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 int clear_next_cell = FALSE;
Bram Moolenaara12a1612019-01-24 16:39:02 +01001474#ifdef FEAT_ARABIC
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001475 int prev_c = 0; // previous Arabic character
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001476 int pc, nc, nc1;
1477 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001479 int force_redraw_this;
1480 int force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001481 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482
Bram Moolenaar0b4c9ed2019-06-03 22:04:23 +02001483 // Safety check. The check for negative row and column is to fix issue
1484 // #4102. TODO: find out why row/col could be negative.
1485 if (ScreenLines == NULL
1486 || row >= screen_Rows || row < 0
1487 || col >= screen_Columns || col < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001489 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001491 // When drawing over the right halve of a double-wide char clear out the
1492 // left halve. Only needed in a terminal.
Bram Moolenaar7693ec62008-07-24 18:29:37 +00001493 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaara12a1612019-01-24 16:39:02 +01001494#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00001495 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01001496#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00001497 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001498 {
1499 ScreenLines[off - 1] = ' ';
1500 ScreenAttrs[off - 1] = 0;
1501 if (enc_utf8)
1502 {
1503 ScreenLinesUC[off - 1] = 0;
1504 ScreenLinesC[0][off - 1] = 0;
1505 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001506 // redraw the previous cell, make it empty
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001507 screen_char(off - 1, row, col - 1);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001508 // force the cell at "col" to be redrawn
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001509 force_redraw_next = TRUE;
1510 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00001511
Bram Moolenaar367329b2007-08-30 11:53:22 +00001512 max_off = LineOffset[row] + screen_Columns;
Bram Moolenaara064ac82007-08-05 18:10:54 +00001513 while (col < screen_Columns
1514 && (len < 0 || (int)(ptr - text) < len)
1515 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 {
1517 c = *ptr;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001518 // check if this is the first byte of a multibyte
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 if (has_mbyte)
1520 {
1521 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001522 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001524 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
1526 mbyte_cells = 1;
1527 else if (enc_dbcs != 0)
1528 mbyte_cells = mbyte_blen;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001529 else // enc_utf8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 {
1531 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001532 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 (int)((text + len) - ptr));
1534 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001535 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaara12a1612019-01-24 16:39:02 +01001537#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
1539 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001540 // Do Arabic shaping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
1542 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001543 // Past end of string to be displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 nc = NUL;
1545 nc1 = NUL;
1546 }
1547 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001548 {
Bram Moolenaar54620182009-11-11 16:07:20 +00001549 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
1550 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001551 nc1 = pcc[0];
1552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 pc = prev_c;
1554 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001555 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 }
1557 else
1558 prev_c = u8c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01001559#endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01001560 if (col + mbyte_cells > screen_Columns)
1561 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001562 // Only 1 cell left, but character requires 2 cells:
1563 // display a '>' in the last column to avoid wrapping.
Bram Moolenaare4ebd292010-01-19 17:40:46 +01001564 c = '>';
1565 mbyte_cells = 1;
1566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 }
1568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001570 force_redraw_this = force_redraw_next;
1571 force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001572
1573 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574 || (mbyte_cells == 2
1575 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
1576 || (enc_dbcs == DBCS_JPNU
1577 && c == 0x8e
1578 && ScreenLines2[off] != ptr[1])
1579 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01001580 && (ScreenLinesUC[off] !=
1581 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
1582 || (ScreenLinesUC[off] != 0
1583 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001585 || exmode_active;
1586
Bram Moolenaar24a5ac52019-06-08 19:01:18 +02001587 if ((need_redraw || force_redraw_this)
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001588#ifdef FEAT_PROP_POPUP
Bram Moolenaarc662ec92019-06-23 00:15:57 +02001589 && !blocked_by_popup(row, col)
Bram Moolenaar24a5ac52019-06-08 19:01:18 +02001590#endif
1591 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 {
1593#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001594 // The bold trick makes a single row of pixels appear in the next
1595 // character. When a bold character is removed, the next
1596 // character should be redrawn too. This happens for our own GUI
1597 // and for some xterms.
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001598 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599# ifdef FEAT_GUI
1600 gui.in_use
1601# endif
1602# if defined(FEAT_GUI) && defined(UNIX)
1603 ||
1604# endif
1605# ifdef UNIX
1606 term_is_xterm
1607# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001608 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001610 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001612 if (n > HL_ALL)
1613 n = syn_attr2attr(n);
1614 if (n & HL_BOLD)
1615 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 }
1617#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001618 // When at the end of the text and overwriting a two-cell
1619 // character with a one-cell character, need to clear the next
1620 // cell. Also when overwriting the left halve of a two-cell char
1621 // with the right halve of a two-cell char. Do this only once
1622 // (mb_off2cells() may return 2 on the right halve).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 if (clear_next_cell)
1624 clear_next_cell = FALSE;
1625 else if (has_mbyte
1626 && (len < 0 ? ptr[mbyte_blen] == NUL
1627 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00001628 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00001630 && (*mb_off2cells)(off, max_off) == 1
1631 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 clear_next_cell = TRUE;
1633
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001634 // Make sure we never leave a second byte of a double-byte behind,
1635 // it confuses mb_off2cells().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00001637 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00001639 && (*mb_off2cells)(off, max_off) == 1
1640 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 ScreenLines[off + mbyte_blen] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 ScreenLines[off] = c;
1643 ScreenAttrs[off] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 if (enc_utf8)
1645 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001646 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 ScreenLinesUC[off] = 0;
1648 else
1649 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001650 int i;
1651
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001653 for (i = 0; i < Screen_mco; ++i)
1654 {
1655 ScreenLinesC[i][off] = u8cc[i];
1656 if (u8cc[i] == 0)
1657 break;
1658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 }
1660 if (mbyte_cells == 2)
1661 {
1662 ScreenLines[off + 1] = 0;
1663 ScreenAttrs[off + 1] = attr;
1664 }
1665 screen_char(off, row, col);
1666 }
1667 else if (mbyte_cells == 2)
1668 {
1669 ScreenLines[off + 1] = ptr[1];
1670 ScreenAttrs[off + 1] = attr;
1671 screen_char_2(off, row, col);
1672 }
1673 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
1674 {
1675 ScreenLines2[off] = ptr[1];
1676 screen_char(off, row, col);
1677 }
1678 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 screen_char(off, row, col);
1680 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 if (has_mbyte)
1682 {
1683 off += mbyte_cells;
1684 col += mbyte_cells;
1685 ptr += mbyte_blen;
1686 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02001687 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001688 // This only happens at the end, display one space next.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02001690 len = -1;
1691 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 }
1693 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 {
1695 ++off;
1696 ++col;
1697 ++ptr;
1698 }
1699 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001700
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001701 // If we detected the next character needs to be redrawn, but the text
1702 // doesn't extend up to there, update the character here.
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001703 if (force_redraw_next && col < screen_Columns)
1704 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001705 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
1706 screen_char_2(off, row, col);
1707 else
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001708 screen_char(off, row, col);
1709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710}
1711
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001712#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001714 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001716 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001717start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718{
1719 if (p_hls && !no_hlsearch)
1720 {
Bram Moolenaar0b6849e2020-05-02 18:33:25 +02001721 end_search_hl(); // just in case it wasn't called before
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001722 last_pat_prog(&screen_search_hl.rm);
1723 screen_search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001724# ifdef FEAT_RELTIME
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001725 // Set the time limit to 'redrawtime'.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001726 profile_setlimit(p_rdt, &screen_search_hl.tm);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001727# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 }
1729}
1730
1731/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001732 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001734 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001735end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736{
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001737 if (screen_search_hl.rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 {
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001739 vim_regfree(screen_search_hl.rm.regprog);
1740 screen_search_hl.rm.regprog = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 }
1742}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02001743#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02001744
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001746screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747{
1748 attrentry_T *aep = NULL;
1749
1750 screen_attr = attr;
1751 if (full_screen
Bram Moolenaar4f974752019-02-17 17:44:42 +01001752#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 && termcap_active
1754#endif
1755 )
1756 {
1757#ifdef FEAT_GUI
1758 if (gui.in_use)
1759 {
1760 char buf[20];
1761
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001762 // The GUI handles this internally.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001763 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 OUT_STR(buf);
1765 }
1766 else
1767#endif
1768 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001769 if (attr > HL_ALL) // special HL attr.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001771 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772 aep = syn_cterm_attr2entry(attr);
1773 else
1774 aep = syn_term_attr2entry(attr);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001775 if (aep == NULL) // did ":syntax clear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 attr = 0;
1777 else
1778 attr = aep->ae_attr;
1779 }
Bram Moolenaara050b942019-12-02 21:35:31 +01001780#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
1781 if (use_vtp())
1782 {
1783 guicolor_T defguifg, defguibg;
1784 int defctermfg, defctermbg;
1785
1786 // If FG and BG are unset, the color is undefined when
1787 // BOLD+INVERSE. Use Normal as the default value.
1788 get_default_console_color(&defctermfg, &defctermbg, &defguifg,
1789 &defguibg);
1790
1791 if (p_tgc)
1792 {
1793 if (aep == NULL || COLOR_INVALID(aep->ae_u.cterm.fg_rgb))
1794 term_fg_rgb_color(defguifg);
1795 if (aep == NULL || COLOR_INVALID(aep->ae_u.cterm.bg_rgb))
1796 term_bg_rgb_color(defguibg);
1797 }
1798 else if (t_colors >= 256)
1799 {
1800 if (aep == NULL || aep->ae_u.cterm.fg_color == 0)
1801 term_fg_color(defctermfg);
1802 if (aep == NULL || aep->ae_u.cterm.bg_color == 0)
1803 term_bg_color(defctermbg);
1804 }
1805 }
1806#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001807 if ((attr & HL_BOLD) && *T_MD != NUL) // bold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001809 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001810#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001811 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
1812 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
1813 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001814#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001815 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001816 // If the Normal FG color has BOLD attribute and the new HL
1817 // has a FG color defined, clear BOLD.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001818 out_str(T_ME);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001819 if ((attr & HL_STANDOUT) && *T_SO != NUL) // standout
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 out_str(T_SO);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001821 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) // undercurl
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01001822 out_str(T_UCS);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001823 if (((attr & HL_UNDERLINE) // underline or undercurl
Bram Moolenaar45a00002017-12-22 21:12:34 +01001824 || ((attr & HL_UNDERCURL) && *T_UCS == NUL))
1825 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 out_str(T_US);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001827 if ((attr & HL_ITALIC) && *T_CZH != NUL) // italic
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 out_str(T_CZH);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001829 if ((attr & HL_INVERSE) && *T_MR != NUL) // inverse (reverse)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 out_str(T_MR);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001831 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) // strike
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02001832 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833
1834 /*
1835 * Output the color or start string after bold etc., in case the
1836 * bold etc. override the color setting.
1837 */
1838 if (aep != NULL)
1839 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001840#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001841 // When 'termguicolors' is set but fg or bg is unset,
1842 // fall back to the cterm colors. This helps for SpellBad,
1843 // where the GUI uses a red undercurl.
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001844 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001846 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001847 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001848 }
1849 else
1850#endif
1851 if (t_colors > 1)
1852 {
1853 if (aep->ae_u.cterm.fg_color)
1854 term_fg_color(aep->ae_u.cterm.fg_color - 1);
1855 }
1856#ifdef FEAT_TERMGUICOLORS
1857 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
1858 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001859 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001860 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 }
1862 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001863#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001864 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001866 if (aep->ae_u.cterm.bg_color)
1867 term_bg_color(aep->ae_u.cterm.bg_color - 1);
1868 }
Bram Moolenaare023e882020-05-31 16:42:30 +02001869#ifdef FEAT_TERMGUICOLORS
1870 if (p_tgc && aep->ae_u.cterm.ul_rgb != CTERMCOLOR)
1871 {
1872 if (aep->ae_u.cterm.ul_rgb != INVALCOLOR)
1873 term_ul_rgb_color(aep->ae_u.cterm.ul_rgb);
1874 }
1875 else
1876#endif
1877 if (t_colors > 1)
1878 {
1879 if (aep->ae_u.cterm.ul_color)
1880 term_ul_color(aep->ae_u.cterm.ul_color - 1);
1881 }
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001882
Bram Moolenaarf708ac52018-03-12 21:48:32 +01001883 if (!IS_CTERM)
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001884 {
1885 if (aep->ae_u.term.start != NULL)
1886 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 }
1888 }
1889 }
1890 }
1891}
1892
1893 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001894screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001896 int do_ME = FALSE; // output T_ME code
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02001897#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Bram Moolenaar09307e32020-05-29 21:42:55 +02001898 int do_ME_fg = FALSE, do_ME_bg = FALSE;
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02001899#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900
1901 if (screen_attr != 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01001902#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 && termcap_active
1904#endif
1905 )
1906 {
1907#ifdef FEAT_GUI
1908 if (gui.in_use)
1909 {
1910 char buf[20];
1911
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001912 // use internal GUI code
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
1914 OUT_STR(buf);
1915 }
1916 else
1917#endif
1918 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001919 if (screen_attr > HL_ALL) // special HL attr.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 {
1921 attrentry_T *aep;
1922
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001923 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 {
1925 /*
1926 * Assume that t_me restores the original colors!
1927 */
1928 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001929 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001930#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001931 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
1932 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02001933# ifdef FEAT_VTP
1934 ? !(do_ME_fg = TRUE) : (do_ME_fg = FALSE)
1935# endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001936 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001937#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001938 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001939#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001940 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
1941 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02001942# ifdef FEAT_VTP
1943 ? !(do_ME_bg = TRUE) : (do_ME_bg = FALSE)
1944# endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001945 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001946#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001947 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 do_ME = TRUE;
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02001949#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
1950 if (use_vtp())
1951 {
1952 if (do_ME_fg && do_ME_bg)
1953 do_ME = TRUE;
1954
1955 // FG and BG cannot be separated in T_ME, which is not
1956 // efficient.
1957 if (!do_ME && do_ME_fg)
1958 out_str((char_u *)"\033|39m"); // restore FG
1959 if (!do_ME && do_ME_bg)
1960 out_str((char_u *)"\033|49m"); // restore BG
1961 }
1962 else
1963 {
1964 // Process FG and BG at once.
1965 if (!do_ME)
1966 do_ME = do_ME_fg | do_ME_bg;
1967 }
1968#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 }
1970 else
1971 {
1972 aep = syn_term_attr2entry(screen_attr);
1973 if (aep != NULL && aep->ae_u.term.stop != NULL)
1974 {
1975 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
1976 do_ME = TRUE;
1977 else
1978 out_str(aep->ae_u.term.stop);
1979 }
1980 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001981 if (aep == NULL) // did ":syntax clear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 screen_attr = 0;
1983 else
1984 screen_attr = aep->ae_attr;
1985 }
1986
1987 /*
1988 * Often all ending-codes are equal to T_ME. Avoid outputting the
1989 * same sequence several times.
1990 */
1991 if (screen_attr & HL_STANDOUT)
1992 {
1993 if (STRCMP(T_SE, T_ME) == 0)
1994 do_ME = TRUE;
1995 else
1996 out_str(T_SE);
1997 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01001998 if ((screen_attr & HL_UNDERCURL) && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01001999 {
2000 if (STRCMP(T_UCE, T_ME) == 0)
2001 do_ME = TRUE;
2002 else
2003 out_str(T_UCE);
2004 }
2005 if ((screen_attr & HL_UNDERLINE)
Bram Moolenaar45a00002017-12-22 21:12:34 +01002006 || ((screen_attr & HL_UNDERCURL) && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 {
2008 if (STRCMP(T_UE, T_ME) == 0)
2009 do_ME = TRUE;
2010 else
2011 out_str(T_UE);
2012 }
2013 if (screen_attr & HL_ITALIC)
2014 {
2015 if (STRCMP(T_CZR, T_ME) == 0)
2016 do_ME = TRUE;
2017 else
2018 out_str(T_CZR);
2019 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02002020 if (screen_attr & HL_STRIKETHROUGH)
2021 {
2022 if (STRCMP(T_STE, T_ME) == 0)
2023 do_ME = TRUE;
2024 else
2025 out_str(T_STE);
2026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
2028 out_str(T_ME);
2029
Bram Moolenaar61be73b2016-04-29 22:59:22 +02002030#ifdef FEAT_TERMGUICOLORS
2031 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002033 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002034 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002035 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002036 term_bg_rgb_color(cterm_normal_bg_gui_color);
Bram Moolenaare023e882020-05-31 16:42:30 +02002037 if (cterm_normal_ul_gui_color != INVALCOLOR)
2038 term_ul_rgb_color(cterm_normal_ul_gui_color);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002039 }
2040 else
2041#endif
2042 {
2043 if (t_colors > 1)
2044 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002045 // set Normal cterm colors
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002046 if (cterm_normal_fg_color != 0)
2047 term_fg_color(cterm_normal_fg_color - 1);
2048 if (cterm_normal_bg_color != 0)
2049 term_bg_color(cterm_normal_bg_color - 1);
Bram Moolenaare023e882020-05-31 16:42:30 +02002050 if (cterm_normal_ul_color != 0)
2051 term_ul_color(cterm_normal_ul_color - 1);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002052 if (cterm_normal_fg_bold)
2053 out_str(T_MD);
2054 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 }
2056 }
2057 }
2058 screen_attr = 0;
2059}
2060
2061/*
2062 * Reset the colors for a cterm. Used when leaving Vim.
2063 * The machine specific code may override this again.
2064 */
2065 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002066reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002068 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002070 // set Normal cterm colors
Bram Moolenaar61be73b2016-04-29 22:59:22 +02002071#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002072 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
2073 || cterm_normal_bg_gui_color != INVALCOLOR)
2074 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002075#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002077#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 {
2079 out_str(T_OP);
2080 screen_attr = -1;
2081 }
2082 if (cterm_normal_fg_bold)
2083 {
2084 out_str(T_ME);
2085 screen_attr = -1;
2086 }
2087 }
2088}
2089
2090/*
2091 * Put character ScreenLines["off"] on the screen at position "row" and "col",
2092 * using the attributes from ScreenAttrs["off"].
2093 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002094 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002095screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096{
2097 int attr;
2098
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002099 // Check for illegal values, just in case (could happen just after
2100 // resizing).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 if (row >= screen_Rows || col >= screen_Columns)
2102 return;
2103
Bram Moolenaar33796b32019-06-08 16:01:13 +02002104 // Skip if under the popup menu.
2105 // Popup windows with zindex higher than POPUPMENU_ZINDEX go on top.
2106 if (pum_under_menu(row, col)
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002107#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002108 && screen_zindex <= POPUPMENU_ZINDEX
Bram Moolenaare2c453d2019-08-21 14:37:09 +02002109#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02002110 )
Bram Moolenaarae654382019-01-17 21:09:05 +01002111 return;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002112#ifdef FEAT_PROP_POPUP
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002113 if (blocked_by_popup(row, col))
Bram Moolenaar33796b32019-06-08 16:01:13 +02002114 return;
2115#endif
2116
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002117 // Outputting a character in the last cell on the screen may scroll the
2118 // screen up. Only do it when the "xn" termcap property is set, otherwise
2119 // mark the character invalid (update it when scrolled up).
Bram Moolenaar494838a2015-02-10 19:20:37 +01002120 if (*T_XN == NUL
2121 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122#ifdef FEAT_RIGHTLEFT
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002123 // account for first command-line character in rightleft mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 && !cmdmsg_rl
2125#endif
2126 )
2127 {
2128 ScreenAttrs[off] = (sattr_T)-1;
2129 return;
2130 }
2131
2132 /*
2133 * Stop highlighting first, so it's easier to move the cursor.
2134 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 if (screen_char_attr != 0)
2136 attr = screen_char_attr;
2137 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 attr = ScreenAttrs[off];
2139 if (screen_attr != attr)
2140 screen_stop_highlight();
2141
2142 windgoto(row, col);
2143
2144 if (screen_attr != attr)
2145 screen_start_highlight(attr);
2146
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 if (enc_utf8 && ScreenLinesUC[off] != 0)
2148 {
2149 char_u buf[MB_MAXBYTES + 1];
2150
Bram Moolenaarcb070082016-04-02 22:14:51 +02002151 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002152 {
2153 if (*p_ambw == 'd'
Bram Moolenaara12a1612019-01-24 16:39:02 +01002154#ifdef FEAT_GUI
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002155 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01002156#endif
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002157 )
2158 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002159 // Clear the two screen cells. If the character is actually
2160 // single width it won't change the second cell.
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002161 out_str((char_u *)" ");
2162 term_windgoto(row, col);
2163 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002164 // not sure where the cursor is after drawing the ambiguous width
2165 // character
Bram Moolenaarcb070082016-04-02 22:14:51 +02002166 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002167 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02002168 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002170
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002171 // Convert the UTF-8 character to bytes and write it.
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002172 buf[utfc_char2bytes(off, buf)] = NUL;
2173 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 }
2175 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 out_char(ScreenLines[off]);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002179 // double-byte character in single-width cell
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2181 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 }
2183
2184 screen_cur_col++;
2185}
2186
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187/*
2188 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
2189 * on the screen at position 'row' and 'col'.
2190 * The attributes of the first byte is used for all. This is required to
2191 * output the two bytes of a double-byte character with nothing in between.
2192 */
2193 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002194screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002196 // Check for illegal values (could be wrong when screen was resized).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
2198 return;
2199
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002200 // Outputting the last character on the screen may scrollup the screen.
2201 // Don't to it! Mark the character invalid (update it when scrolled up)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
2203 {
2204 ScreenAttrs[off] = (sattr_T)-1;
2205 return;
2206 }
2207
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002208 // Output the first byte normally (positions the cursor), then write the
2209 // second byte directly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 screen_char(off, row, col);
2211 out_char(ScreenLines[off + 1]);
2212 ++screen_cur_col;
2213}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215/*
2216 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
2217 * This uses the contents of ScreenLines[] and doesn't change it.
2218 */
2219 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002220screen_draw_rectangle(
2221 int row,
2222 int col,
2223 int height,
2224 int width,
2225 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226{
2227 int r, c;
2228 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00002229 int max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002231 // Can't use ScreenLines unless initialized
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002232 if (ScreenLines == NULL)
2233 return;
2234
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 if (invert)
2236 screen_char_attr = HL_INVERSE;
2237 for (r = row; r < row + height; ++r)
2238 {
2239 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00002240 max_off = off + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 for (c = col; c < col + width; ++c)
2242 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00002243 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 {
2245 screen_char_2(off + c, r, c);
2246 ++c;
2247 }
2248 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 {
2250 screen_char(off + c, r, c);
Bram Moolenaar367329b2007-08-30 11:53:22 +00002251 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 ++c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 }
2254 }
2255 }
2256 screen_char_attr = 0;
2257}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259/*
2260 * Redraw the characters for a vertically split window.
2261 */
2262 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002263redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264{
2265 int col;
2266 int width;
2267
2268# ifdef FEAT_CLIPBOARD
2269 clip_may_clear_selection(row, end - 1);
2270# endif
2271
2272 if (wp == NULL)
2273 {
2274 col = 0;
2275 width = Columns;
2276 }
2277 else
2278 {
2279 col = wp->w_wincol;
2280 width = wp->w_width;
2281 }
2282 screen_draw_rectangle(row, col, end - row, width, FALSE);
2283}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002285 void
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002286space_to_screenline(int off, int attr)
2287{
2288 ScreenLines[off] = ' ';
2289 ScreenAttrs[off] = attr;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002290 if (enc_utf8)
2291 ScreenLinesUC[off] = 0;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002292}
2293
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294/*
2295 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
2296 * with character 'c1' in first column followed by 'c2' in the other columns.
2297 * Use attributes 'attr'.
2298 */
2299 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002300screen_fill(
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002301 int start_row,
2302 int end_row,
2303 int start_col,
2304 int end_col,
2305 int c1,
2306 int c2,
2307 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002309 int row;
2310 int col;
2311 int off;
2312 int end_off;
2313 int did_delete;
2314 int c;
2315 int norm_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002317 int force_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318#endif
2319
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002320 if (end_row > screen_Rows) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 end_row = screen_Rows;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002322 if (end_col > screen_Columns) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 end_col = screen_Columns;
2324 if (ScreenLines == NULL
2325 || start_row >= end_row
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002326 || start_col >= end_col) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 return;
2328
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002329 // it's a "normal" terminal when not in a GUI or cterm
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 norm_term = (
2331#ifdef FEAT_GUI
2332 !gui.in_use &&
2333#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002334 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 for (row = start_row; row < end_row; ++row)
2336 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00002337 if (has_mbyte
Bram Moolenaara12a1612019-01-24 16:39:02 +01002338#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00002339 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01002340#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00002341 )
2342 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002343 // When drawing over the right halve of a double-wide char clear
2344 // out the left halve. When drawing over the left halve of a
2345 // double wide-char clear out the right halve. Only needed in a
2346 // terminal.
Bram Moolenaar7693ec62008-07-24 18:29:37 +00002347 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00002348 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00002349 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00002350 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00002351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 /*
2353 * Try to use delete-line termcap code, when no attributes or in a
2354 * "normal" terminal, where a bold/italic space is just a
2355 * space.
2356 */
2357 did_delete = FALSE;
2358 if (c2 == ' '
2359 && end_col == Columns
2360 && can_clear(T_CE)
2361 && (attr == 0
2362 || (norm_term
2363 && attr <= HL_ALL
2364 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
2365 {
2366 /*
2367 * check if we really need to clear something
2368 */
2369 col = start_col;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002370 if (c1 != ' ') // don't clear first char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 ++col;
2372
2373 off = LineOffset[row] + col;
2374 end_off = LineOffset[row] + end_col;
2375
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002376 // skip blanks (used often, keep it fast!)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 if (enc_utf8)
2378 while (off < end_off && ScreenLines[off] == ' '
2379 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
2380 ++off;
2381 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 while (off < end_off && ScreenLines[off] == ' '
2383 && ScreenAttrs[off] == 0)
2384 ++off;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002385 if (off < end_off) // something to be cleared
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 {
2387 col = off - LineOffset[row];
2388 screen_stop_highlight();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002389 term_windgoto(row, col);// clear rest of this screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 out_str(T_CE);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002391 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 col = end_col - col;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002393 while (col--) // clear chars in ScreenLines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002395 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 ++off;
2397 }
2398 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002399 did_delete = TRUE; // the chars are cleared now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 }
2401
2402 off = LineOffset[row] + start_col;
2403 c = c1;
2404 for (col = start_col; col < end_col; ++col)
2405 {
Bram Moolenaar33796b32019-06-08 16:01:13 +02002406 if ((ScreenLines[off] != c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002407 || (enc_utf8 && (int)ScreenLinesUC[off]
2408 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 || ScreenAttrs[off] != attr
2410#if defined(FEAT_GUI) || defined(UNIX)
2411 || force_next
2412#endif
2413 )
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002414#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002415 // Skip if under a(nother) popup.
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002416 && !blocked_by_popup(row, col)
Bram Moolenaar33796b32019-06-08 16:01:13 +02002417#endif
2418 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 {
2420#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002421 // The bold trick may make a single row of pixels appear in
2422 // the next character. When a bold character is removed, the
2423 // next character should be redrawn too. This happens for our
2424 // own GUI and for some xterms.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 if (
2426# ifdef FEAT_GUI
2427 gui.in_use
2428# endif
2429# if defined(FEAT_GUI) && defined(UNIX)
2430 ||
2431# endif
2432# ifdef UNIX
2433 term_is_xterm
2434# endif
2435 )
2436 {
2437 if (ScreenLines[off] != ' '
2438 && (ScreenAttrs[off] > HL_ALL
2439 || ScreenAttrs[off] & HL_BOLD))
2440 force_next = TRUE;
2441 else
2442 force_next = FALSE;
2443 }
2444#endif
2445 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 if (enc_utf8)
2447 {
2448 if (c >= 0x80)
2449 {
2450 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002451 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 }
2453 else
2454 ScreenLinesUC[off] = 0;
2455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 ScreenAttrs[off] = attr;
2457 if (!did_delete || c != ' ')
2458 screen_char(off, row, col);
2459 }
2460 ++off;
2461 if (col == start_col)
2462 {
2463 if (did_delete)
2464 break;
2465 c = c2;
2466 }
2467 }
2468 if (end_col == Columns)
2469 LineWraps[row] = FALSE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002470 if (row == Rows - 1) // overwritten the command line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 {
2472 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02002473 if (start_col == 0 && end_col == Columns
2474 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002475 clear_cmdline = FALSE; // command line has been cleared
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002476 if (start_col == 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002477 mode_displayed = FALSE; // mode cleared or overwritten
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 }
2479 }
2480}
2481
2482/*
2483 * Check if there should be a delay. Used before clearing or redrawing the
2484 * screen or the command line.
2485 */
2486 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002487check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488{
2489 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
2490 && !did_wait_return
2491 && emsg_silent == 0)
2492 {
2493 out_flush();
Bram Moolenaareda1da02019-11-17 17:06:33 +01002494 ui_delay(1006L, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 emsg_on_display = FALSE;
2496 if (check_msg_scroll)
2497 msg_scroll = FALSE;
2498 }
2499}
2500
2501/*
Bram Moolenaarca57ab52019-04-13 14:53:16 +02002502 * Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect.
2503 */
2504 static void
2505clear_TabPageIdxs(void)
2506{
2507 int scol;
2508
2509 for (scol = 0; scol < Columns; ++scol)
2510 TabPageIdxs[scol] = 0;
2511}
2512
2513/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002515 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 * Returns TRUE if there is a valid screen to write to.
2517 * Returns FALSE when starting up and screen not initialized yet.
2518 */
2519 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002520screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002522 screenalloc(doclear); // allocate screen buffers if size changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 return (ScreenLines != NULL);
2524}
2525
2526/*
2527 * Resize the shell to Rows and Columns.
2528 * Allocate ScreenLines[] and associated items.
2529 *
2530 * There may be some time between setting Rows and Columns and (re)allocating
2531 * ScreenLines[]. This happens when starting up and when (manually) changing
2532 * the shell size. Always use screen_Rows and screen_Columns to access items
2533 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
2534 * final size of the shell is needed.
2535 */
2536 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002537screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538{
2539 int new_row, old_row;
2540#ifdef FEAT_GUI
2541 int old_Rows;
2542#endif
2543 win_T *wp;
2544 int outofmem = FALSE;
2545 int len;
2546 schar_T *new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002548 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002550 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 sattr_T *new_ScreenAttrs;
2552 unsigned *new_LineOffset;
2553 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002554 short *new_TabPageIdxs;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002555#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002556 short *new_popup_mask;
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002557 short *new_popup_mask_next;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002558 char *new_popup_transparent;
Bram Moolenaar33796b32019-06-08 16:01:13 +02002559#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00002560 tabpage_T *tp;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002561 static int entered = FALSE; // avoid recursiveness
2562 static int done_outofmem_msg = FALSE; // did outofmem message
Bram Moolenaar87e817c2009-02-22 20:13:39 +00002563 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564
Bram Moolenaar87e817c2009-02-22 20:13:39 +00002565retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 /*
2567 * Allocation of the screen buffers is done only when the size changes and
2568 * when Rows and Columns have been set and we have started doing full
2569 * screen stuff.
2570 */
2571 if ((ScreenLines != NULL
2572 && Rows == screen_Rows
2573 && Columns == screen_Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 && enc_utf8 == (ScreenLinesUC != NULL)
2575 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaara12a1612019-01-24 16:39:02 +01002576 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 || Rows == 0
2578 || Columns == 0
2579 || (!full_screen && ScreenLines == NULL))
2580 return;
2581
2582 /*
2583 * It's possible that we produce an out-of-memory message below, which
2584 * will cause this function to be called again. To break the loop, just
2585 * return here.
2586 */
2587 if (entered)
2588 return;
2589 entered = TRUE;
2590
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00002591 /*
2592 * Note that the window sizes are updated before reallocating the arrays,
2593 * thus we must not redraw here!
2594 */
2595 ++RedrawingDisabled;
2596
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002597 win_new_shellsize(); // fit the windows in the new sized shell
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002599#ifdef FEAT_GUI_HAIKU
2600 vim_lock_screen(); // be safe, put it here
2601#endif
2602
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002603 comp_col(); // recompute columns for shown command and ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604
2605 /*
2606 * We're changing the size of the screen.
2607 * - Allocate new arrays for ScreenLines and ScreenAttrs.
2608 * - Move lines from the old arrays into the new arrays, clear extra
2609 * lines (unless the screen is going to be cleared).
2610 * - Free the old arrays.
2611 *
2612 * If anything fails, make ScreenLines NULL, so we don't do anything!
2613 * Continuing with the old ScreenLines may result in a crash, because the
2614 * size is wrong.
2615 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002616 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00002618 if (aucmd_win != NULL)
2619 win_free_lsize(aucmd_win);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002620#ifdef FEAT_PROP_POPUP
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002621 // global popup windows
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002622 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002623 win_free_lsize(wp);
2624 // tab-local popup windows
2625 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002626 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002627 win_free_lsize(wp);
2628#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002630 new_ScreenLines = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
Bram Moolenaar216b7102010-03-23 13:56:59 +01002631 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 if (enc_utf8)
2633 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002634 new_ScreenLinesUC = LALLOC_MULT(u8char_T, (Rows + 1) * Columns);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002635 for (i = 0; i < p_mco; ++i)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002636 new_ScreenLinesC[i] = LALLOC_CLEAR_MULT(u8char_T,
2637 (Rows + 1) * Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 }
2639 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002640 new_ScreenLines2 = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
2641 new_ScreenAttrs = LALLOC_MULT(sattr_T, (Rows + 1) * Columns);
2642 new_LineOffset = LALLOC_MULT(unsigned, Rows);
2643 new_LineWraps = LALLOC_MULT(char_u, Rows);
2644 new_TabPageIdxs = LALLOC_MULT(short, Columns);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002645#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002646 new_popup_mask = LALLOC_MULT(short, Rows * Columns);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002647 new_popup_mask_next = LALLOC_MULT(short, Rows * Columns);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002648 new_popup_transparent = LALLOC_MULT(char, Rows * Columns);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002649#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00002651 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 {
2653 if (win_alloc_lines(wp) == FAIL)
2654 {
2655 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00002656 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 }
2658 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00002659 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
2660 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002661 outofmem = TRUE;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002662#ifdef FEAT_PROP_POPUP
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002663 // global popup windows
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002664 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002665 if (win_alloc_lines(wp) == FAIL)
2666 {
2667 outofmem = TRUE;
2668 goto give_up;
2669 }
2670 // tab-local popup windows
2671 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002672 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002673 if (win_alloc_lines(wp) == FAIL)
2674 {
2675 outofmem = TRUE;
2676 goto give_up;
2677 }
2678#endif
2679
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00002680give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002682 for (i = 0; i < p_mco; ++i)
2683 if (new_ScreenLinesC[i] == NULL)
2684 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 if (new_ScreenLines == NULL
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002686 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 || new_ScreenAttrs == NULL
2689 || new_LineOffset == NULL
2690 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00002691 || new_TabPageIdxs == NULL
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002692#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002693 || new_popup_mask == NULL
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002694 || new_popup_mask_next == NULL
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002695 || new_popup_transparent == NULL
Bram Moolenaar33796b32019-06-08 16:01:13 +02002696#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 || outofmem)
2698 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002699 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002700 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002701 // guess the size
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002702 do_outofmem_msg((long_u)((Rows + 1) * Columns));
2703
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002704 // Remember we did this to avoid getting outofmem messages over
2705 // and over again.
Bram Moolenaar89d40322006-08-29 15:30:07 +00002706 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002707 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01002708 VIM_CLEAR(new_ScreenLines);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002709 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002710 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01002711 VIM_CLEAR(new_ScreenLinesC[i]);
2712 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002713 VIM_CLEAR(new_ScreenAttrs);
2714 VIM_CLEAR(new_LineOffset);
2715 VIM_CLEAR(new_LineWraps);
2716 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002717#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002718 VIM_CLEAR(new_popup_mask);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002719 VIM_CLEAR(new_popup_mask_next);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002720 VIM_CLEAR(new_popup_transparent);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002721#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 }
2723 else
2724 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002725 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002726
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 for (new_row = 0; new_row < Rows; ++new_row)
2728 {
2729 new_LineOffset[new_row] = new_row * Columns;
2730 new_LineWraps[new_row] = FALSE;
2731
2732 /*
2733 * If the screen is not going to be cleared, copy as much as
2734 * possible from the old screen to the new one and clear the rest
2735 * (used when resizing the window at the "--more--" prompt or when
2736 * executing an external command, for the GUI).
2737 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002738 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 {
2740 (void)vim_memset(new_ScreenLines + new_row * Columns,
2741 ' ', (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 if (enc_utf8)
2743 {
2744 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
2745 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002746 for (i = 0; i < p_mco; ++i)
2747 (void)vim_memset(new_ScreenLinesC[i]
2748 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 0, (size_t)Columns * sizeof(u8char_T));
2750 }
2751 if (enc_dbcs == DBCS_JPNU)
2752 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
2753 0, (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
2755 0, (size_t)Columns * sizeof(sattr_T));
2756 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002757 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 {
2759 if (screen_Columns < Columns)
2760 len = screen_Columns;
2761 else
2762 len = Columns;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002763 // When switching to utf-8 don't copy characters, they
2764 // may be invalid now. Also when p_mco changes.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002765 if (!(enc_utf8 && ScreenLinesUC == NULL)
2766 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002767 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
2768 ScreenLines + LineOffset[old_row],
2769 (size_t)len * sizeof(schar_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002770 if (enc_utf8 && ScreenLinesUC != NULL
2771 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 {
2773 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
2774 ScreenLinesUC + LineOffset[old_row],
2775 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002776 for (i = 0; i < p_mco; ++i)
2777 mch_memmove(new_ScreenLinesC[i]
2778 + new_LineOffset[new_row],
2779 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 (size_t)len * sizeof(u8char_T));
2781 }
2782 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
2783 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
2784 ScreenLines2 + LineOffset[old_row],
2785 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
2787 ScreenAttrs + LineOffset[old_row],
2788 (size_t)len * sizeof(sattr_T));
2789 }
2790 }
2791 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002792 // Use the last line of the screen for the current line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 current_ScreenLine = new_ScreenLines + Rows * Columns;
Bram Moolenaar6ace95e2019-08-13 23:09:49 +02002794
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002795#ifdef FEAT_PROP_POPUP
Bram Moolenaar6ace95e2019-08-13 23:09:49 +02002796 vim_memset(new_popup_mask, 0, Rows * Columns * sizeof(short));
2797 vim_memset(new_popup_transparent, 0, Rows * Columns * sizeof(char));
2798#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 }
2800
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002801 free_screenlines();
2802
Bram Moolenaar6ace95e2019-08-13 23:09:49 +02002803 // NOTE: this may result in all pointers to become NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 ScreenLines = new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002806 for (i = 0; i < p_mco; ++i)
2807 ScreenLinesC[i] = new_ScreenLinesC[i];
2808 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 ScreenLines2 = new_ScreenLines2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 ScreenAttrs = new_ScreenAttrs;
2811 LineOffset = new_LineOffset;
2812 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002813 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002814#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002815 popup_mask = new_popup_mask;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002816 popup_mask_next = new_popup_mask_next;
2817 popup_transparent = new_popup_transparent;
Bram Moolenaar33796b32019-06-08 16:01:13 +02002818 popup_mask_refresh = TRUE;
2819#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002821 // It's important that screen_Rows and screen_Columns reflect the actual
2822 // size of ScreenLines[]. Set them before calling anything.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823#ifdef FEAT_GUI
2824 old_Rows = screen_Rows;
2825#endif
2826 screen_Rows = Rows;
2827 screen_Columns = Columns;
2828
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002829 must_redraw = CLEAR; // need to clear the screen later
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002830 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 screenclear2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832#ifdef FEAT_GUI
2833 else if (gui.in_use
2834 && !gui.starting
2835 && ScreenLines != NULL
2836 && old_Rows != Rows)
2837 {
Bram Moolenaar7c003aa2020-03-28 20:44:41 +01002838 gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
2839
2840 // Adjust the position of the cursor, for when executing an external
2841 // command.
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002842 if (msg_row >= Rows) // Rows got smaller
2843 msg_row = Rows - 1; // put cursor at last row
2844 else if (Rows > old_Rows) // Rows got bigger
2845 msg_row += Rows - old_Rows; // put cursor in same place
2846 if (msg_col >= Columns) // Columns got smaller
2847 msg_col = Columns - 1; // put cursor at last column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 }
2849#endif
Bram Moolenaarca57ab52019-04-13 14:53:16 +02002850 clear_TabPageIdxs();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002852#ifdef FEAT_GUI_HAIKU
2853 vim_unlock_screen();
2854#endif
2855
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00002857 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00002858
Bram Moolenaar87e817c2009-02-22 20:13:39 +00002859 /*
2860 * Do not apply autocommands more than 3 times to avoid an endless loop
2861 * in case applying autocommands always changes Rows or Columns.
2862 */
2863 if (starting == 0 && ++retry_count <= 3)
2864 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00002865 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002866 // In rare cases, autocommands may have altered Rows or Columns,
2867 // jump back to check if we need to allocate the screen again.
Bram Moolenaar87e817c2009-02-22 20:13:39 +00002868 goto retry;
2869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870}
2871
2872 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002873free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002874{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002875 int i;
2876
Bram Moolenaar33796b32019-06-08 16:01:13 +02002877 VIM_CLEAR(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002878 for (i = 0; i < Screen_mco; ++i)
Bram Moolenaar33796b32019-06-08 16:01:13 +02002879 VIM_CLEAR(ScreenLinesC[i]);
2880 VIM_CLEAR(ScreenLines2);
2881 VIM_CLEAR(ScreenLines);
2882 VIM_CLEAR(ScreenAttrs);
2883 VIM_CLEAR(LineOffset);
2884 VIM_CLEAR(LineWraps);
2885 VIM_CLEAR(TabPageIdxs);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002886#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002887 VIM_CLEAR(popup_mask);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002888 VIM_CLEAR(popup_mask_next);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002889 VIM_CLEAR(popup_transparent);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002890#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002891}
2892
2893 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002894screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895{
2896 check_for_delay(FALSE);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002897 screenalloc(FALSE); // allocate screen buffers if size changed
2898 screenclear2(); // clear the screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899}
2900
2901 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002902screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903{
2904 int i;
2905
2906 if (starting == NO_SCREEN || ScreenLines == NULL
2907#ifdef FEAT_GUI
2908 || (gui.in_use && gui.starting)
2909#endif
2910 )
2911 return;
2912
2913#ifdef FEAT_GUI
2914 if (!gui.in_use)
2915#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002916 screen_attr = -1; // force setting the Normal colors
2917 screen_stop_highlight(); // don't want highlighting here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918
2919#ifdef FEAT_CLIPBOARD
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002920 // disable selection without redrawing it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 clip_scroll_selection(9999);
2922#endif
2923
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002924 // blank out ScreenLines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 for (i = 0; i < Rows; ++i)
2926 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002927 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 LineWraps[i] = FALSE;
2929 }
2930
2931 if (can_clear(T_CL))
2932 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002933 out_str(T_CL); // clear the display
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002935 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 }
2937 else
2938 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002939 // can't clear the screen, mark all chars with invalid attributes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 for (i = 0; i < Rows; ++i)
2941 lineinvalid(LineOffset[i], (int)Columns);
2942 clear_cmdline = TRUE;
2943 }
2944
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002945 screen_cleared = TRUE; // can use contents of ScreenLines now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946
2947 win_rest_invalid(firstwin);
2948 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002949 redraw_tabline = TRUE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002950 if (must_redraw == CLEAR) // no need to clear again
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 must_redraw = NOT_VALID;
2952 compute_cmdrow();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002953 msg_row = cmdline_row; // put cursor on last line for messages
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 msg_col = 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002955 screen_start(); // don't know where cursor is now
2956 msg_scrolled = 0; // can't scroll back
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 msg_didany = FALSE;
2958 msg_didout = FALSE;
2959}
2960
2961/*
2962 * Clear one line in ScreenLines.
2963 */
2964 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002965lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966{
2967 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 if (enc_utf8)
2969 (void)vim_memset(ScreenLinesUC + off, 0,
2970 (size_t)width * sizeof(u8char_T));
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002971 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972}
2973
2974/*
2975 * Mark one line in ScreenLines invalid by setting the attributes to an
2976 * invalid value.
2977 */
2978 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002979lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980{
2981 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
2982}
2983
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984/*
2985 * Copy part of a Screenline for vertically split window "wp".
2986 */
2987 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002988linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989{
2990 unsigned off_to = LineOffset[to] + wp->w_wincol;
2991 unsigned off_from = LineOffset[from] + wp->w_wincol;
2992
2993 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
2994 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 if (enc_utf8)
2996 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002997 int i;
2998
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
3000 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003001 for (i = 0; i < p_mco; ++i)
3002 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
3003 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 }
3005 if (enc_dbcs == DBCS_JPNU)
3006 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
3007 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
3009 wp->w_width * sizeof(sattr_T));
3010}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011
3012/*
3013 * Return TRUE if clearing with term string "p" would work.
3014 * It can't work when the string is empty or it won't set the right background.
Bram Moolenaar33796b32019-06-08 16:01:13 +02003015 * Don't clear to end-of-line when there are popups, it may cause flicker.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 */
3017 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003018can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019{
3020 return (*p != NUL && (t_colors <= 1
3021#ifdef FEAT_GUI
3022 || gui.in_use
3023#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003024#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003025 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02003026 || (!p_tgc && cterm_normal_bg_color == 0)
3027#else
3028 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003029#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02003030 || *T_UT != NUL)
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003031#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02003032 && !(p == T_CE && popup_visible)
3033#endif
3034 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035}
3036
3037/*
3038 * Reset cursor position. Use whenever cursor was moved because of outputting
3039 * something directly to the screen (shell commands) or a terminal control
3040 * code.
3041 */
3042 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003043screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044{
3045 screen_cur_row = screen_cur_col = 9999;
3046}
3047
3048/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 * Move the cursor to position "row","col" in the screen.
3050 * This tries to find the most efficient way to move, minimizing the number of
3051 * characters sent to the terminal.
3052 */
3053 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003054windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003056 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 int i;
3058 int plan;
3059 int cost;
3060 int wouldbe_col;
3061 int noinvcurs;
3062 char_u *bs;
3063 int goto_cost;
3064 int attr;
3065
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003066#define GOTO_COST 7 // assume a term_windgoto() takes about 7 chars
3067#define HIGHL_COST 5 // assume unhighlight takes 5 chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068
3069#define PLAN_LE 1
3070#define PLAN_CR 2
3071#define PLAN_NL 3
3072#define PLAN_WRITE 4
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003073 // Can't use ScreenLines unless initialized
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 if (ScreenLines == NULL)
3075 return;
3076
3077 if (col != screen_cur_col || row != screen_cur_row)
3078 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003079 // Check for valid position.
3080 if (row < 0) // window without text lines?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 row = 0;
3082 if (row >= screen_Rows)
3083 row = screen_Rows - 1;
3084 if (col >= screen_Columns)
3085 col = screen_Columns - 1;
3086
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003087 // check if no cursor movement is allowed in highlight mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 if (screen_attr && *T_MS == NUL)
3089 noinvcurs = HIGHL_COST;
3090 else
3091 noinvcurs = 0;
3092 goto_cost = GOTO_COST + noinvcurs;
3093
3094 /*
3095 * Plan how to do the positioning:
3096 * 1. Use CR to move it to column 0, same row.
3097 * 2. Use T_LE to move it a few columns to the left.
3098 * 3. Use NL to move a few lines down, column 0.
3099 * 4. Move a few columns to the right with T_ND or by writing chars.
3100 *
3101 * Don't do this if the cursor went beyond the last column, the cursor
3102 * position is unknown then (some terminals wrap, some don't )
3103 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00003104 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 * characters to move the cursor to the right.
3106 */
3107 if (row >= screen_cur_row && screen_cur_col < Columns)
3108 {
3109 /*
3110 * If the cursor is in the same row, bigger col, we can use CR
3111 * or T_LE.
3112 */
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003113 bs = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 attr = screen_attr;
3115 if (row == screen_cur_row && col < screen_cur_col)
3116 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003117 // "le" is preferred over "bc", because "bc" is obsolete
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 if (*T_LE)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003119 bs = T_LE; // "cursor left"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003121 bs = T_BC; // "backspace character (old)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 if (*bs)
3123 cost = (screen_cur_col - col) * (int)STRLEN(bs);
3124 else
3125 cost = 999;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003126 if (col + 1 < cost) // using CR is less characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 {
3128 plan = PLAN_CR;
3129 wouldbe_col = 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003130 cost = 1; // CR is just one character
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 }
3132 else
3133 {
3134 plan = PLAN_LE;
3135 wouldbe_col = col;
3136 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003137 if (noinvcurs) // will stop highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 {
3139 cost += noinvcurs;
3140 attr = 0;
3141 }
3142 }
3143
3144 /*
3145 * If the cursor is above where we want to be, we can use CR LF.
3146 */
3147 else if (row > screen_cur_row)
3148 {
3149 plan = PLAN_NL;
3150 wouldbe_col = 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003151 cost = (row - screen_cur_row) * 2; // CR LF
3152 if (noinvcurs) // will stop highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 {
3154 cost += noinvcurs;
3155 attr = 0;
3156 }
3157 }
3158
3159 /*
3160 * If the cursor is in the same row, smaller col, just use write.
3161 */
3162 else
3163 {
3164 plan = PLAN_WRITE;
3165 wouldbe_col = screen_cur_col;
3166 cost = 0;
3167 }
3168
3169 /*
3170 * Check if any characters that need to be written have the
3171 * correct attributes. Also avoid UTF-8 characters.
3172 */
3173 i = col - wouldbe_col;
3174 if (i > 0)
3175 cost += i;
3176 if (cost < goto_cost && i > 0)
3177 {
3178 /*
3179 * Check if the attributes are correct without additionally
3180 * stopping highlighting.
3181 */
3182 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
3183 while (i && *p++ == attr)
3184 --i;
3185 if (i != 0)
3186 {
3187 /*
3188 * Try if it works when highlighting is stopped here.
3189 */
3190 if (*--p == 0)
3191 {
3192 cost += noinvcurs;
3193 while (i && *p++ == 0)
3194 --i;
3195 }
3196 if (i != 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003197 cost = 999; // different attributes, don't do it
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 if (enc_utf8)
3200 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003201 // Don't use an UTF-8 char for positioning, it's slow.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 for (i = wouldbe_col; i < col; ++i)
3203 if (ScreenLinesUC[LineOffset[row] + i] != 0)
3204 {
3205 cost = 999;
3206 break;
3207 }
3208 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 }
3210
3211 /*
3212 * We can do it without term_windgoto()!
3213 */
3214 if (cost < goto_cost)
3215 {
3216 if (plan == PLAN_LE)
3217 {
3218 if (noinvcurs)
3219 screen_stop_highlight();
3220 while (screen_cur_col > col)
3221 {
3222 out_str(bs);
3223 --screen_cur_col;
3224 }
3225 }
3226 else if (plan == PLAN_CR)
3227 {
3228 if (noinvcurs)
3229 screen_stop_highlight();
3230 out_char('\r');
3231 screen_cur_col = 0;
3232 }
3233 else if (plan == PLAN_NL)
3234 {
3235 if (noinvcurs)
3236 screen_stop_highlight();
3237 while (screen_cur_row < row)
3238 {
3239 out_char('\n');
3240 ++screen_cur_row;
3241 }
3242 screen_cur_col = 0;
3243 }
3244
3245 i = col - screen_cur_col;
3246 if (i > 0)
3247 {
3248 /*
3249 * Use cursor-right if it's one character only. Avoids
3250 * removing a line of pixels from the last bold char, when
3251 * using the bold trick in the GUI.
3252 */
3253 if (T_ND[0] != NUL && T_ND[1] == NUL)
3254 {
3255 while (i-- > 0)
3256 out_char(*T_ND);
3257 }
3258 else
3259 {
3260 int off;
3261
3262 off = LineOffset[row] + screen_cur_col;
3263 while (i-- > 0)
3264 {
3265 if (ScreenAttrs[off] != screen_attr)
3266 screen_stop_highlight();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 if (enc_dbcs == DBCS_JPNU
3270 && ScreenLines[off] == 0x8e)
3271 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 ++off;
3273 }
3274 }
3275 }
3276 }
3277 }
3278 else
3279 cost = 999;
3280
3281 if (cost >= goto_cost)
3282 {
3283 if (noinvcurs)
3284 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02003285 if (row == screen_cur_row && (col > screen_cur_col)
3286 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 term_cursor_right(col - screen_cur_col);
3288 else
3289 term_windgoto(row, col);
3290 }
3291 screen_cur_row = row;
3292 screen_cur_col = col;
3293 }
3294}
3295
3296/*
3297 * Set cursor to its position in the current window.
3298 */
3299 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003300setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301{
Bram Moolenaar987723e2018-03-06 11:43:04 +01003302 setcursor_mayforce(FALSE);
3303}
3304
3305/*
3306 * Set cursor to its position in the current window.
3307 * When "force" is TRUE also when not redrawing.
3308 */
3309 void
3310setcursor_mayforce(int force)
3311{
3312 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 {
3314 validate_cursor();
3315 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02003316 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317#ifdef FEAT_RIGHTLEFT
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003318 // With 'rightleft' set and the cursor on a double-wide
3319 // character, position it on the leftmost column.
Bram Moolenaara12a1612019-01-24 16:39:02 +01003320 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol
3321 - ((has_mbyte
Bram Moolenaar561f9db2008-02-20 13:16:29 +00003322 && (*mb_ptr2cells)(ml_get_cursor()) == 2
Bram Moolenaara12a1612019-01-24 16:39:02 +01003323 && vim_isprintc(gchar_cursor())) ? 2 : 1)) :
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324#endif
3325 curwin->w_wcol));
3326 }
3327}
3328
3329
3330/*
Bram Moolenaar86033562017-07-12 20:24:41 +02003331 * Insert 'line_count' lines at 'row' in window 'wp'.
3332 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
3333 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 * scrolling.
3335 * Returns FAIL if the lines are not inserted, OK for success.
3336 */
3337 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003338win_ins_lines(
3339 win_T *wp,
3340 int row,
3341 int line_count,
3342 int invalid,
3343 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344{
3345 int did_delete;
3346 int nextrow;
3347 int lastrow;
3348 int retval;
3349
3350 if (invalid)
3351 wp->w_lines_valid = 0;
3352
3353 if (wp->w_height < 5)
3354 return FAIL;
3355
3356 if (line_count > wp->w_height - row)
3357 line_count = wp->w_height - row;
3358
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003359 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 if (retval != MAYBE)
3361 return retval;
3362
3363 /*
3364 * If there is a next window or a status line, we first try to delete the
3365 * lines at the bottom to avoid messing what is after the window.
Bram Moolenaarc363fe12019-08-04 18:13:46 +02003366 * If this fails and there are following windows, don't do anything to
3367 * avoid messing up those windows, better just redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 */
3369 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 if (wp->w_next != NULL || wp->w_status_height)
3371 {
3372 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003373 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 did_delete = TRUE;
3375 else if (wp->w_next)
3376 return FAIL;
3377 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 /*
3379 * if no lines deleted, blank the lines that will end up below the window
3380 */
3381 if (!did_delete)
3382 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003385 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 lastrow = nextrow + line_count;
3387 if (lastrow > Rows)
3388 lastrow = Rows;
3389 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02003390 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 ' ', ' ', 0);
3392 }
3393
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003394 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 == FAIL)
3396 {
Bram Moolenaarc363fe12019-08-04 18:13:46 +02003397 // deletion will have messed up other windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 if (did_delete)
3399 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 win_rest_invalid(W_NEXT(wp));
3402 }
3403 return FAIL;
3404 }
3405
3406 return OK;
3407}
3408
3409/*
Bram Moolenaar86033562017-07-12 20:24:41 +02003410 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
3412 * If "mayclear" is TRUE the screen will be cleared if it is faster than
3413 * scrolling
3414 * Return OK for success, FAIL if the lines are not deleted.
3415 */
3416 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003417win_del_lines(
3418 win_T *wp,
3419 int row,
3420 int line_count,
3421 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003422 int mayclear,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003423 int clear_attr) // for clearing lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424{
3425 int retval;
3426
3427 if (invalid)
3428 wp->w_lines_valid = 0;
3429
3430 if (line_count > wp->w_height - row)
3431 line_count = wp->w_height - row;
3432
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003433 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 if (retval != MAYBE)
3435 return retval;
3436
3437 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003438 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 return FAIL;
3440
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 /*
3442 * If there are windows or status lines below, try to put them at the
3443 * correct place. If we can't do that, they have to be redrawn.
3444 */
3445 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
3446 {
3447 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003448 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 {
3450 wp->w_redr_status = TRUE;
3451 win_rest_invalid(wp->w_next);
3452 }
3453 }
3454 /*
3455 * If this is the last window and there is no status line, redraw the
3456 * command line later.
3457 */
3458 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 redraw_cmdline = TRUE;
3460 return OK;
3461}
3462
3463/*
3464 * Common code for win_ins_lines() and win_del_lines().
3465 * Returns OK or FAIL when the work has been done.
3466 * Returns MAYBE when not finished yet.
3467 */
3468 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003469win_do_lines(
3470 win_T *wp,
3471 int row,
3472 int line_count,
3473 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003474 int del,
3475 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476{
3477 int retval;
3478
3479 if (!redrawing() || line_count <= 0)
3480 return FAIL;
3481
Bram Moolenaar33796b32019-06-08 16:01:13 +02003482 // When inserting lines would result in loss of command output, just redraw
3483 // the lines.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003484 if (no_win_do_lines_ins && !del)
3485 return FAIL;
3486
Bram Moolenaar33796b32019-06-08 16:01:13 +02003487 // only a few lines left: redraw is faster
Bram Moolenaar4033c552017-09-16 20:54:51 +02003488 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003490 if (!no_win_do_lines_ins)
Bram Moolenaar33796b32019-06-08 16:01:13 +02003491 screenclear(); // will set wp->w_lines_valid to 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 return FAIL;
3493 }
3494
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003495#ifdef FEAT_PROP_POPUP
Bram Moolenaar4c063a02019-06-10 21:24:12 +02003496 // this doesn't work when there are popups visible
Bram Moolenaar33796b32019-06-08 16:01:13 +02003497 if (popup_visible)
3498 return FAIL;
3499#endif
3500
3501 // Delete all remaining lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 if (row + line_count >= wp->w_height)
3503 {
3504 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02003505 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 ' ', ' ', 0);
3507 return OK;
3508 }
3509
3510 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003511 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003513 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003515 if (!no_win_do_lines_ins)
3516 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517
3518 /*
3519 * If the terminal can set a scroll region, use that.
3520 * Always do this in a vertically split window. This will redraw from
3521 * ScreenLines[] when t_CV isn't defined. That's faster than using
3522 * win_line().
3523 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003524 * a character in the lower right corner of the scroll region may cause a
3525 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 */
Bram Moolenaar02631462017-09-22 15:20:32 +02003527 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 scroll_region_set(wp, row);
3531 if (del)
3532 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003533 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 else
3535 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003536 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 scroll_region_reset();
3539 return retval;
3540 }
3541
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003542 if (wp->w_next != NULL && p_tf) // don't delete/insert on fast terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544
3545 return MAYBE;
3546}
3547
3548/*
3549 * window 'wp' and everything after it is messed up, mark it for redraw
3550 */
3551 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003552win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 {
3556 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 wp->w_redr_status = TRUE;
3558 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 }
3560 redraw_cmdline = TRUE;
3561}
3562
3563/*
3564 * The rest of the routines in this file perform screen manipulations. The
3565 * given operation is performed physically on the screen. The corresponding
3566 * change is also made to the internal screen image. In this way, the editor
3567 * anticipates the effect of editing changes on the appearance of the screen.
3568 * That way, when we call screenupdate a complete redraw isn't usually
3569 * necessary. Another advantage is that we can keep adding code to anticipate
3570 * screen changes, and in the meantime, everything still works.
3571 */
3572
3573/*
3574 * types for inserting or deleting lines
3575 */
3576#define USE_T_CAL 1
3577#define USE_T_CDL 2
3578#define USE_T_AL 3
3579#define USE_T_CE 4
3580#define USE_T_DL 5
3581#define USE_T_SR 6
3582#define USE_NL 7
3583#define USE_T_CD 8
3584#define USE_REDRAW 9
3585
3586/*
3587 * insert lines on the screen and update ScreenLines[]
3588 * 'end' is the line after the scrolled part. Normally it is Rows.
3589 * When scrolling region used 'off' is the offset from the top for the region.
3590 * 'row' and 'end' are relative to the start of the region.
3591 *
3592 * return FAIL for failure, OK for success.
3593 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003594 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003595screen_ins_lines(
3596 int off,
3597 int row,
3598 int line_count,
3599 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003600 int clear_attr,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003601 win_T *wp) // NULL or window to use width from
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602{
3603 int i;
3604 int j;
3605 unsigned temp;
3606 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003607 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 int type;
3609 int result_empty;
3610 int can_ce = can_clear(T_CE);
3611
3612 /*
3613 * FAIL if
3614 * - there is no valid screen
3615 * - the screen has to be redrawn completely
3616 * - the line count is less than one
3617 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003618 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar33796b32019-06-08 16:01:13 +02003619 * - there is a popup window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 */
Bram Moolenaar33796b32019-06-08 16:01:13 +02003621 if (!screen_valid(TRUE)
3622 || line_count <= 0 || line_count > p_ttyscroll
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003623#ifdef FEAT_CLIPBOARD
3624 || (clip_star.state != SELECT_CLEARED
3625 && redrawing_for_callback > 0)
3626#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003627#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02003628 || popup_visible
3629#endif
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003630 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 return FAIL;
3632
3633 /*
3634 * There are seven ways to insert lines:
3635 * 0. When in a vertically split window and t_CV isn't set, redraw the
3636 * characters from ScreenLines[].
3637 * 1. Use T_CD (clear to end of display) if it exists and the result of
3638 * the insert is just empty lines
3639 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
3640 * present or line_count > 1. It looks better if we do all the inserts
3641 * at once.
3642 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
3643 * insert is just empty lines and T_CE is not present or line_count >
3644 * 1.
3645 * 4. Use T_AL (insert line) if it exists.
3646 * 5. Use T_CE (erase line) if it exists and the result of the insert is
3647 * just empty lines.
3648 * 6. Use T_DL (delete line) if it exists and the result of the insert is
3649 * just empty lines.
3650 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
3651 * the 'da' flag is not set or we have clear line capability.
3652 * 8. redraw the characters from ScreenLines[].
3653 *
3654 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
3655 * the scrollbar for the window. It does have insert line, use that if it
3656 * exists.
3657 */
3658 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
3660 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02003661 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 type = USE_T_CD;
3663 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
3664 type = USE_T_CAL;
3665 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
3666 type = USE_T_CDL;
3667 else if (*T_AL != NUL)
3668 type = USE_T_AL;
3669 else if (can_ce && result_empty)
3670 type = USE_T_CE;
3671 else if (*T_DL != NUL && result_empty)
3672 type = USE_T_DL;
3673 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
3674 type = USE_T_SR;
3675 else
3676 return FAIL;
3677
3678 /*
3679 * For clearing the lines screen_del_lines() is used. This will also take
3680 * care of t_db if necessary.
3681 */
3682 if (type == USE_T_CD || type == USE_T_CDL ||
3683 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003684 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685
3686 /*
3687 * If text is retained below the screen, first clear or delete as many
3688 * lines at the bottom of the window as are about to be inserted so that
3689 * the deleted lines won't later surface during a screen_del_lines.
3690 */
3691 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003692 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693
3694#ifdef FEAT_CLIPBOARD
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003695 // Remove a modeless selection when inserting lines halfway the screen
3696 // or not the full width of the screen.
Bram Moolenaar4033c552017-09-16 20:54:51 +02003697 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003698 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 else
3700 clip_scroll_selection(-line_count);
3701#endif
3702
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003703#ifdef FEAT_GUI_HAIKU
3704 vim_lock_screen();
3705#endif
3706
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707#ifdef FEAT_GUI
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003708 // Don't update the GUI cursor here, ScreenLines[] is invalid until the
3709 // scrolling is actually carried out.
Bram Moolenaar107abd22016-08-12 14:08:25 +02003710 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711#endif
3712
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003713 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
3714 cursor_col = wp->w_wincol;
3715
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003716 if (*T_CCS != NUL) // cursor relative to region
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 cursor_row = row;
3718 else
3719 cursor_row = row + off;
3720
3721 /*
3722 * Shift LineOffset[] line_count down to reflect the inserted lines.
3723 * Clear the inserted lines in ScreenLines[].
3724 */
3725 row += off;
3726 end += off;
3727 for (i = 0; i < line_count; ++i)
3728 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 if (wp != NULL && wp->w_width != Columns)
3730 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003731 // need to copy part of a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 j = end - 1 - i;
3733 while ((j -= line_count) >= row)
3734 linecopy(j + line_count, j, wp);
3735 j += line_count;
3736 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003737 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
3738 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 else
3740 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
3741 LineWraps[j] = FALSE;
3742 }
3743 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 {
3745 j = end - 1 - i;
3746 temp = LineOffset[j];
3747 while ((j -= line_count) >= row)
3748 {
3749 LineOffset[j + line_count] = LineOffset[j];
3750 LineWraps[j + line_count] = LineWraps[j];
3751 }
3752 LineOffset[j + line_count] = temp;
3753 LineWraps[j + line_count] = FALSE;
3754 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003755 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 else
3757 lineinvalid(temp, (int)Columns);
3758 }
3759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003761#ifdef FEAT_GUI_HAIKU
3762 vim_unlock_screen();
3763#endif
3764
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003766 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003767 if (clear_attr != 0)
3768 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003770 // redraw the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 if (type == USE_REDRAW)
3772 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02003773 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 {
3775 term_append_lines(line_count);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003776 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 }
3778 else
3779 {
3780 for (i = 0; i < line_count; i++)
3781 {
3782 if (type == USE_T_AL)
3783 {
3784 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003785 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 out_str(T_AL);
3787 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003788 else // type == USE_T_SR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 out_str(T_SR);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003790 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 }
3792 }
3793
3794 /*
3795 * With scroll-reverse and 'da' flag set we need to clear the lines that
3796 * have been scrolled down into the region.
3797 */
3798 if (type == USE_T_SR && *T_DA)
3799 {
3800 for (i = 0; i < line_count; ++i)
3801 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003802 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 out_str(T_CE);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003804 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 }
3806 }
3807
3808#ifdef FEAT_GUI
3809 gui_can_update_cursor();
3810 if (gui.in_use)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003811 out_flush(); // always flush after a scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812#endif
3813 return OK;
3814}
3815
3816/*
Bram Moolenaar107abd22016-08-12 14:08:25 +02003817 * Delete lines on the screen and update ScreenLines[].
3818 * "end" is the line after the scrolled part. Normally it is Rows.
3819 * When scrolling region used "off" is the offset from the top for the region.
3820 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 *
3822 * Return OK for success, FAIL if the lines are not deleted.
3823 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003825screen_del_lines(
3826 int off,
3827 int row,
3828 int line_count,
3829 int end,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003830 int force, // even when line_count > p_ttyscroll
3831 int clear_attr, // used for clearing lines
3832 win_T *wp UNUSED) // NULL or window to use width from
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833{
3834 int j;
3835 int i;
3836 unsigned temp;
3837 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003838 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 int cursor_end;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003840 int result_empty; // result is empty until end of region
3841 int can_delete; // deleting line codes can be used
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 int type;
3843
3844 /*
3845 * FAIL if
3846 * - there is no valid screen
3847 * - the screen has to be redrawn completely
3848 * - the line count is less than one
3849 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003850 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003852 if (!screen_valid(TRUE) || line_count <= 0
3853 || (!force && line_count > p_ttyscroll)
3854#ifdef FEAT_CLIPBOARD
3855 || (clip_star.state != SELECT_CLEARED
3856 && redrawing_for_callback > 0)
3857#endif
3858 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 return FAIL;
3860
3861 /*
3862 * Check if the rest of the current region will become empty.
3863 */
3864 result_empty = row + line_count >= end;
3865
3866 /*
3867 * We can delete lines only when 'db' flag not set or when 'ce' option
3868 * available.
3869 */
3870 can_delete = (*T_DB == NUL || can_clear(T_CE));
3871
3872 /*
3873 * There are six ways to delete lines:
3874 * 0. When in a vertically split window and t_CV isn't set, redraw the
3875 * characters from ScreenLines[].
3876 * 1. Use T_CD if it exists and the result is empty.
3877 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
3878 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
3879 * none of the other ways work.
3880 * 4. Use T_CE (erase line) if the result is empty.
3881 * 5. Use T_DL (delete line) if it exists.
3882 * 6. redraw the characters from ScreenLines[].
3883 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
3885 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02003886 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 type = USE_T_CD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 else if (row == 0 && (
3889#ifndef AMIGA
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003890 // On the Amiga, somehow '\n' on the last line doesn't always scroll
3891 // up, so use delete-line command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 line_count == 1 ||
3893#endif
3894 *T_CDL == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 type = USE_NL;
3896 else if (*T_CDL != NUL && line_count > 1 && can_delete)
3897 type = USE_T_CDL;
3898 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +02003899 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 type = USE_T_CE;
3901 else if (*T_DL != NUL && can_delete)
3902 type = USE_T_DL;
3903 else if (*T_CDL != NUL && can_delete)
3904 type = USE_T_CDL;
3905 else
3906 return FAIL;
3907
3908#ifdef FEAT_CLIPBOARD
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003909 // Remove a modeless selection when deleting lines halfway the screen or
3910 // not the full width of the screen.
Bram Moolenaar4033c552017-09-16 20:54:51 +02003911 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003912 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 else
3914 clip_scroll_selection(line_count);
3915#endif
3916
Bram Moolenaar92c461e2020-04-24 22:19:00 +02003917#ifdef FEAT_GUI_HAIKU
3918 vim_lock_screen();
3919#endif
3920
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921#ifdef FEAT_GUI
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003922 // Don't update the GUI cursor here, ScreenLines[] is invalid until the
3923 // scrolling is actually carried out.
Bram Moolenaar107abd22016-08-12 14:08:25 +02003924 gui_dont_update_cursor(gui.cursor_row >= row + off
3925 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926#endif
3927
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003928 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
3929 cursor_col = wp->w_wincol;
3930
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003931 if (*T_CCS != NUL) // cursor relative to region
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 {
3933 cursor_row = row;
3934 cursor_end = end;
3935 }
3936 else
3937 {
3938 cursor_row = row + off;
3939 cursor_end = end + off;
3940 }
3941
3942 /*
3943 * Now shift LineOffset[] line_count up to reflect the deleted lines.
3944 * Clear the inserted lines in ScreenLines[].
3945 */
3946 row += off;
3947 end += off;
3948 for (i = 0; i < line_count; ++i)
3949 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 if (wp != NULL && wp->w_width != Columns)
3951 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003952 // need to copy part of a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 j = row + i;
3954 while ((j += line_count) <= end - 1)
3955 linecopy(j - line_count, j, wp);
3956 j -= line_count;
3957 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003958 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
3959 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 else
3961 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
3962 LineWraps[j] = FALSE;
3963 }
3964 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003966 // whole width, moving the line pointers is faster
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 j = row + i;
3968 temp = LineOffset[j];
3969 while ((j += line_count) <= end - 1)
3970 {
3971 LineOffset[j - line_count] = LineOffset[j];
3972 LineWraps[j - line_count] = LineWraps[j];
3973 }
3974 LineOffset[j - line_count] = temp;
3975 LineWraps[j - line_count] = FALSE;
3976 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003977 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 else
3979 lineinvalid(temp, (int)Columns);
3980 }
3981 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003983#ifdef FEAT_GUI_HAIKU
3984 vim_unlock_screen();
3985#endif
3986
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003987 if (screen_attr != clear_attr)
3988 screen_stop_highlight();
3989 if (clear_attr != 0)
3990 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003992 // redraw the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 if (type == USE_REDRAW)
3994 redraw_block(row, end, wp);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003995 else if (type == USE_T_CD) // delete the lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003997 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 out_str(T_CD);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003999 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 }
4001 else if (type == USE_T_CDL)
4002 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02004003 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 term_delete_lines(line_count);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004005 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 }
4007 /*
4008 * Deleting lines at top of the screen or scroll region: Just scroll
4009 * the whole screen (scroll region) up by outputting newlines on the
4010 * last line.
4011 */
4012 else if (type == USE_NL)
4013 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02004014 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 for (i = line_count; --i >= 0; )
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004016 out_char('\n'); // cursor will remain on same line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 }
4018 else
4019 {
4020 for (i = line_count; --i >= 0; )
4021 {
4022 if (type == USE_T_DL)
4023 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02004024 windgoto(cursor_row, cursor_col);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004025 out_str(T_DL); // delete a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004027 else // type == USE_T_CE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02004029 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004030 out_str(T_CE); // erase a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004032 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 }
4034 }
4035
4036 /*
4037 * If the 'db' flag is set, we need to clear the lines that have been
4038 * scrolled up at the bottom of the region.
4039 */
4040 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
4041 {
4042 for (i = line_count; i > 0; --i)
4043 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02004044 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004045 out_str(T_CE); // erase a line
4046 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 }
4048 }
4049
4050#ifdef FEAT_GUI
4051 gui_can_update_cursor();
4052 if (gui.in_use)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004053 out_flush(); // always flush after a scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054#endif
4055
4056 return OK;
4057}
4058
4059/*
Bram Moolenaarcb574f42019-01-25 22:29:57 +01004060 * Return TRUE when postponing displaying the mode message: when not redrawing
4061 * or inside a mapping.
4062 */
4063 int
4064skip_showmode()
4065{
4066 // Call char_avail() only when we are going to show something, because it
4067 // takes a bit of time. redrawing() may also call char_avail_avail().
4068 if (global_busy
4069 || msg_silent != 0
4070 || !redrawing()
4071 || (char_avail() && !KeyTyped))
4072 {
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004073 redraw_mode = TRUE; // show mode later
Bram Moolenaarcb574f42019-01-25 22:29:57 +01004074 return TRUE;
4075 }
4076 return FALSE;
4077}
4078
4079/*
Bram Moolenaar81226e02018-02-20 21:44:45 +01004080 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 *
4082 * If clear_cmdline is TRUE, clear the rest of the cmdline.
4083 * If clear_cmdline is FALSE there may be a message there that needs to be
4084 * cleared only if a mode is shown.
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004085 * If redraw_mode is TRUE show or clear the mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 * Return the length of the message (0 if no message).
4087 */
4088 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004089showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090{
4091 int need_clear;
4092 int length = 0;
4093 int do_mode;
4094 int attr;
4095 int nwr_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 int sub_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097
Bram Moolenaar7df351e2006-01-23 22:30:28 +00004098 do_mode = ((p_smd && msg_silent == 0)
4099 && ((State & INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +02004100 || restart_edit != NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004101 || VIsual_active));
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02004102 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 {
Bram Moolenaarcb574f42019-01-25 22:29:57 +01004104 if (skip_showmode())
4105 return 0; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106
4107 nwr_save = need_wait_return;
4108
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004109 // wait a bit before overwriting an important message
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 check_for_delay(FALSE);
4111
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004112 // if the cmdline is more than one line high, erase top lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 need_clear = clear_cmdline;
4114 if (clear_cmdline && cmdline_row < Rows - 1)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004115 msg_clr_cmdline(); // will reset clear_cmdline
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004117 // Position on the last line in the window, column 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 msg_pos_mode();
4119 cursor_off();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004120 attr = HL_ATTR(HLF_CM); // Highlight mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 if (do_mode)
4122 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004123 msg_puts_attr("--", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00004125 if (
Bram Moolenaar09092152010-08-08 16:38:42 +02004126# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +00004127 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +02004128# else
Bram Moolenaarc236c162008-07-13 17:41:49 +00004129 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +00004130# endif
Bram Moolenaar09092152010-08-08 16:38:42 +02004131 )
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004132# ifdef FEAT_GUI_GTK // most of the time, it's not XIM being used
Bram Moolenaar32526b32019-01-19 17:43:09 +01004133 msg_puts_attr(" IM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134# else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004135 msg_puts_attr(" XIM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136# endif
4137#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004138 // CTRL-X in Insert mode
Bram Moolenaarea389e92014-05-28 21:40:52 +02004139 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004141 // These messages can get long, avoid a wrap in a narrow
4142 // window. Prefer showing edit_submode_extra.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 length = (Rows - msg_row) * Columns - 3;
4144 if (edit_submode_extra != NULL)
4145 length -= vim_strsize(edit_submode_extra);
4146 if (length > 0)
4147 {
4148 if (edit_submode_pre != NULL)
4149 length -= vim_strsize(edit_submode_pre);
4150 if (length - vim_strsize(edit_submode) > 0)
4151 {
4152 if (edit_submode_pre != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004153 msg_puts_attr((char *)edit_submode_pre, attr);
4154 msg_puts_attr((char *)edit_submode, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 }
4156 if (edit_submode_extra != NULL)
4157 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004158 msg_puts_attr(" ", attr); // add a space in between
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004160 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 else
4162 sub_attr = attr;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004163 msg_puts_attr((char *)edit_submode_extra, sub_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 }
4165 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 }
4167 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 if (State & VREPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004170 msg_puts_attr(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02004171 else if (State & REPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004172 msg_puts_attr(_(" REPLACE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 else if (State & INSERT)
4174 {
4175#ifdef FEAT_RIGHTLEFT
4176 if (p_ri)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004177 msg_puts_attr(_(" REVERSE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01004179 msg_puts_attr(_(" INSERT"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 }
Bram Moolenaar942b4542018-06-17 16:23:34 +02004181 else if (restart_edit == 'I' || restart_edit == 'A')
Bram Moolenaar32526b32019-01-19 17:43:09 +01004182 msg_puts_attr(_(" (insert)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 else if (restart_edit == 'R')
Bram Moolenaar32526b32019-01-19 17:43:09 +01004184 msg_puts_attr(_(" (replace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 else if (restart_edit == 'V')
Bram Moolenaar32526b32019-01-19 17:43:09 +01004186 msg_puts_attr(_(" (vreplace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187#ifdef FEAT_RIGHTLEFT
4188 if (p_hkmap)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004189 msg_puts_attr(_(" Hebrew"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190#endif
4191#ifdef FEAT_KEYMAP
4192 if (State & LANGMAP)
4193 {
4194# ifdef FEAT_ARABIC
4195 if (curwin->w_p_arab)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004196 msg_puts_attr(_(" Arabic"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 else
4198# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02004199 if (get_keymap_str(curwin, (char_u *)" (%s)",
4200 NameBuff, MAXPATHL))
Bram Moolenaar32526b32019-01-19 17:43:09 +01004201 msg_puts_attr((char *)NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 }
4203#endif
4204 if ((State & INSERT) && p_paste)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004205 msg_puts_attr(_(" (paste)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 if (VIsual_active)
4208 {
4209 char *p;
4210
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004211 // Don't concatenate separate words to avoid translation
4212 // problems.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 switch ((VIsual_select ? 4 : 0)
4214 + (VIsual_mode == Ctrl_V) * 2
4215 + (VIsual_mode == 'V'))
4216 {
4217 case 0: p = N_(" VISUAL"); break;
4218 case 1: p = N_(" VISUAL LINE"); break;
4219 case 2: p = N_(" VISUAL BLOCK"); break;
4220 case 4: p = N_(" SELECT"); break;
4221 case 5: p = N_(" SELECT LINE"); break;
4222 default: p = N_(" SELECT BLOCK"); break;
4223 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01004224 msg_puts_attr(_(p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01004226 msg_puts_attr(" --", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004228
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 need_clear = TRUE;
4230 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02004231 if (reg_recording != 0
Bram Moolenaare2c453d2019-08-21 14:37:09 +02004232 && edit_submode == NULL) // otherwise it gets too long
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004234 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 need_clear = TRUE;
4236 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004237
4238 mode_displayed = TRUE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004239 if (need_clear || clear_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 msg_clr_eos();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004241 msg_didout = FALSE; // overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 length = msg_col;
4243 msg_col = 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004244 need_wait_return = nwr_save; // never ask for hit-return for this
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 }
4246 else if (clear_cmdline && msg_silent == 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004247 // Clear the whole command line. Will reset "clear_cmdline".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 msg_clr_cmdline();
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004249 else if (redraw_mode)
4250 {
4251 msg_pos_mode();
4252 msg_clr_eos();
4253 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254
4255#ifdef FEAT_CMDL_INFO
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004256 // In Visual mode the size of the selected area must be redrawn.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 if (VIsual_active)
4258 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004260 // If the last window has no status line, the ruler is after the mode
4261 // message and must be redrawn
Bram Moolenaar4033c552017-09-16 20:54:51 +02004262 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +02004263 win_redr_ruler(lastwin, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264#endif
4265 redraw_cmdline = FALSE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004266 redraw_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 clear_cmdline = FALSE;
4268
4269 return length;
4270}
4271
4272/*
4273 * Position for a mode message.
4274 */
4275 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004276msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277{
4278 msg_col = 0;
4279 msg_row = Rows - 1;
4280}
4281
4282/*
4283 * Delete mode message. Used when ESC is typed which is expected to end
4284 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004285 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 */
4287 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004288unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289{
4290 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +01004291 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 */
4293 if (!redrawing() || (!force && char_avail() && !KeyTyped))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004294 redraw_cmdline = TRUE; // delete mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +02004296 clearmode();
4297}
4298
4299/*
4300 * Clear the mode message.
4301 */
4302 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02004303clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +02004304{
Bram Moolenaar2abad542018-05-19 14:43:45 +02004305 int save_msg_row = msg_row;
4306 int save_msg_col = msg_col;
4307
Bram Moolenaarfd773e92016-04-02 19:39:16 +02004308 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02004309 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004310 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +02004311 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +02004312
4313 msg_col = save_msg_col;
4314 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315}
4316
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004317 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004318recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004319{
Bram Moolenaar32526b32019-01-19 17:43:09 +01004320 msg_puts_attr(_("recording"), attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004321 if (!shortmess(SHM_RECORDING))
4322 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004323 char s[4];
4324
4325 sprintf(s, " @%c", reg_recording);
4326 msg_puts_attr(s, attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004327 }
4328}
4329
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004330/*
4331 * Draw the tab pages line at the top of the Vim window.
4332 */
Bram Moolenaare12bab32019-01-08 22:02:56 +01004333 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004334draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004335{
4336 int tabcount = 0;
4337 tabpage_T *tp;
4338 int tabwidth;
4339 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00004340 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004341 int attr;
4342 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004343 win_T *cwp;
4344 int wincount;
4345 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004346 int c;
4347 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004348 int attr_sel = HL_ATTR(HLF_TPS);
4349 int attr_nosel = HL_ATTR(HLF_TP);
4350 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00004351 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004352 int room;
4353 int use_sep_chars = (t_colors < 8
4354#ifdef FEAT_GUI
4355 && !gui.in_use
4356#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02004357#ifdef FEAT_TERMGUICOLORS
4358 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +02004359#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004360 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004361
Bram Moolenaarc695cec2017-01-08 20:00:04 +01004362 if (ScreenLines == NULL)
4363 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00004364 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004365
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004366#ifdef FEAT_GUI_TABLINE
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004367 // Take care of a GUI tabline.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004368 if (gui_use_tabline())
4369 {
4370 gui_update_tabline();
4371 return;
4372 }
4373#endif
4374
4375 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004376 return;
4377
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004378#if defined(FEAT_STL_OPT)
Bram Moolenaarca57ab52019-04-13 14:53:16 +02004379 clear_TabPageIdxs();
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004380
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004381 // Use the 'tabline' option if it's set.
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004382 if (*p_tal != NUL)
4383 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +02004384 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +00004385
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004386 // Check for an error. If there is one we would loop in redrawing the
4387 // screen. Avoid that by making 'tabline' empty.
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +02004388 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004389 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +02004390 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +00004391 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004392 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +02004393 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004394 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00004395 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004396#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004397 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004398 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00004399 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004400
Bram Moolenaar238a5642006-02-21 22:12:05 +00004401 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
4402 if (tabwidth < 6)
4403 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004404
Bram Moolenaar238a5642006-02-21 22:12:05 +00004405 attr = attr_nosel;
4406 tabcount = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004407 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
4408 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00004409 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00004410 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004411
Bram Moolenaar238a5642006-02-21 22:12:05 +00004412 if (tp->tp_topframe == topframe)
4413 attr = attr_sel;
4414 if (use_sep_chars && col > 0)
4415 screen_putchar('|', 0, col++, attr);
4416
4417 if (tp->tp_topframe != topframe)
4418 attr = attr_nosel;
4419
4420 screen_putchar(' ', 0, col++, attr);
4421
4422 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00004423 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00004424 cwp = curwin;
4425 wp = firstwin;
4426 }
4427 else
4428 {
4429 cwp = tp->tp_curwin;
4430 wp = tp->tp_firstwin;
4431 }
4432
4433 modified = FALSE;
4434 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
4435 if (bufIsChanged(wp->w_buffer))
4436 modified = TRUE;
4437 if (modified || wincount > 1)
4438 {
4439 if (wincount > 1)
4440 {
4441 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004442 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004443 if (col + len >= Columns - 3)
4444 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +00004445 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004446#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004447 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004448#else
Bram Moolenaare0f14822014-08-06 13:20:56 +02004449 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004450#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00004451 );
4452 col += len;
4453 }
4454 if (modified)
4455 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
4456 screen_putchar(' ', 0, col++, attr);
4457 }
4458
4459 room = scol - col + tabwidth - 1;
4460 if (room > 0)
4461 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004462 // Get buffer name in NameBuff[]
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004463 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004464 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +00004465 len = vim_strsize(NameBuff);
4466 p = NameBuff;
Bram Moolenaar238a5642006-02-21 22:12:05 +00004467 if (has_mbyte)
4468 while (len > room)
4469 {
4470 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004471 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +00004472 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01004473 else if (len > room)
Bram Moolenaar238a5642006-02-21 22:12:05 +00004474 {
4475 p += len - room;
4476 len = room;
4477 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004478 if (len > Columns - col - 1)
4479 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +00004480
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004481 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +00004482 col += len;
4483 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00004484 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +00004485
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004486 // Store the tab page number in TabPageIdxs[], so that
4487 // jump_to_mouse() knows where each one is.
Bram Moolenaar238a5642006-02-21 22:12:05 +00004488 ++tabcount;
4489 while (scol < col)
4490 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004491 }
4492
Bram Moolenaar238a5642006-02-21 22:12:05 +00004493 if (use_sep_chars)
4494 c = '_';
4495 else
4496 c = ' ';
4497 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004498
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004499 // Put an "X" for closing the current tab if there are several.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004500 if (first_tabpage->tp_next != NULL)
4501 {
4502 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
4503 TabPageIdxs[Columns - 1] = -999;
4504 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004505 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +00004506
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004507 // Reset the flag here again, in case evaluating 'tabline' causes it to be
4508 // set.
Bram Moolenaarb21e5842006-04-16 18:30:08 +00004509 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004510}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004511
4512/*
4513 * Get buffer name for "buf" into NameBuff[].
4514 * Takes care of special buffer names and translates special characters.
4515 */
4516 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004517get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004518{
4519 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02004520 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004521 else
4522 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
4523 trans_characters(NameBuff, MAXPATHL);
4524}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004525
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526/*
4527 * Get the character to use in a status line. Get its attributes in "*attr".
4528 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02004529 int
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004530fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531{
4532 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004533
4534#ifdef FEAT_TERMINAL
4535 if (bt_terminal(wp->w_buffer))
4536 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004537 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02004538 {
4539 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004540 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02004541 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004542 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02004543 {
4544 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004545 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02004546 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004547 }
4548 else
4549#endif
4550 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004552 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 fill = fill_stl;
4554 }
4555 else
4556 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004557 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 fill = fill_stlnc;
4559 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004560 // Use fill when there is highlighting, and highlighting of current
4561 // window differs, or the fillchars differ, or this is not the
4562 // current window
Bram Moolenaar8820b482017-03-16 17:23:31 +01004563 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004564 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 || (fill_stl != fill_stlnc)))
4566 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004567 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 return '^';
4569 return '=';
4570}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572/*
4573 * Get the character to use in a separator between vertically split windows.
4574 * Get its attributes in "*attr".
4575 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02004576 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004577fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578{
Bram Moolenaar8820b482017-03-16 17:23:31 +01004579 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 if (*attr == 0 && fill_vert == ' ')
4581 return '|';
4582 else
4583 return fill_vert;
4584}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585
4586/*
4587 * Return TRUE if redrawing should currently be done.
4588 */
4589 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004590redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591{
Bram Moolenaareb992cb2017-03-09 18:20:16 +01004592#ifdef FEAT_EVAL
4593 if (disable_redraw_for_testing)
4594 return 0;
4595 else
4596#endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +02004597 return ((!RedrawingDisabled
4598#ifdef FEAT_EVAL
4599 || ignore_redraw_flag_for_testing
4600#endif
4601 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602}
4603
4604/*
4605 * Return TRUE if printing messages should currently be done.
4606 */
4607 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004608messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609{
4610 return (!(p_lz && char_avail() && !KeyTyped));
4611}
4612
Bram Moolenaare677df82019-09-02 22:31:11 +02004613/*
4614 * Compute columns for ruler and shown command. 'sc_col' is also used to
4615 * decide what the maximum length of a message on the status line can be.
4616 * If there is a status line for the last window, 'sc_col' is independent
4617 * of 'ru_col'.
4618 */
4619
4620#define COL_RULER 17 // columns needed by standard ruler
4621
4622 void
4623comp_col(void)
4624{
4625#if defined(FEAT_CMDL_INFO)
4626 int last_has_status = (p_ls == 2 || (p_ls == 1 && !ONE_WINDOW));
4627
4628 sc_col = 0;
4629 ru_col = 0;
4630 if (p_ru)
4631 {
4632# ifdef FEAT_STL_OPT
4633 ru_col = (ru_wid ? ru_wid : COL_RULER) + 1;
4634# else
4635 ru_col = COL_RULER + 1;
4636# endif
4637 // no last status line, adjust sc_col
4638 if (!last_has_status)
4639 sc_col = ru_col;
4640 }
4641 if (p_sc)
4642 {
4643 sc_col += SHOWCMD_COLS;
4644 if (!p_ru || last_has_status) // no need for separating space
4645 ++sc_col;
4646 }
4647 sc_col = Columns - sc_col;
4648 ru_col = Columns - ru_col;
4649 if (sc_col <= 0) // screen too narrow, will become a mess
4650 sc_col = 1;
4651 if (ru_col <= 0)
4652 ru_col = 1;
4653#else
4654 sc_col = Columns;
4655 ru_col = Columns;
4656#endif
4657#ifdef FEAT_EVAL
4658 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
4659#endif
4660}
4661
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004662#if defined(FEAT_LINEBREAK) || defined(PROTO)
4663/*
Bram Moolenaar64486672010-05-16 15:46:46 +02004664 * Return the width of the 'number' and 'relativenumber' column.
4665 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004666 * Otherwise it depends on 'numberwidth' and the line count.
4667 */
4668 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004669number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004670{
4671 int n;
4672 linenr_T lnum;
4673
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004674 if (wp->w_p_rnu && !wp->w_p_nu)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004675 // cursor line shows "0"
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004676 lnum = wp->w_height;
4677 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004678 // cursor line shows absolute line number
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004679 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +02004680
Bram Moolenaar6b314672015-03-20 15:42:10 +01004681 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004682 return wp->w_nrwidth_width;
4683 wp->w_nrwidth_line_count = lnum;
4684
4685 n = 0;
4686 do
4687 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004688 lnum /= 10;
4689 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004690 } while (lnum > 0);
4691
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004692 // 'numberwidth' gives the minimal width plus one
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004693 if (n < wp->w_p_nuw - 1)
4694 n = wp->w_p_nuw - 1;
4695
Bram Moolenaare4b407f2019-07-04 11:59:28 +02004696# ifdef FEAT_SIGNS
4697 // If 'signcolumn' is set to 'number' and there is a sign to display, then
4698 // the minimal width for the number column is 2.
Bram Moolenaar4eb7dae2019-11-12 22:33:45 +01004699 if (n < 2 && get_first_valid_sign(wp) != NULL
Bram Moolenaare4b407f2019-07-04 11:59:28 +02004700 && (*wp->w_p_scl == 'n' && *(wp->w_p_scl + 1) == 'u'))
4701 n = 2;
4702# endif
4703
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004704 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +01004705 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004706 return n;
4707}
4708#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +01004709
Bram Moolenaar113e1072019-01-20 15:30:40 +01004710#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9750bb12012-12-05 16:10:42 +01004711/*
4712 * Return the current cursor column. This is the actual position on the
4713 * screen. First column is 0.
4714 */
4715 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004716screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +01004717{
4718 return screen_cur_col;
4719}
4720
4721/*
4722 * Return the current cursor row. This is the actual position on the screen.
4723 * First row is 0.
4724 */
4725 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004726screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +01004727{
4728 return screen_cur_row;
4729}
Bram Moolenaar113e1072019-01-20 15:30:40 +01004730#endif
Bram Moolenaare677df82019-09-02 22:31:11 +02004731
4732/*
4733 * Handle setting 'listchars' or 'fillchars'.
4734 * Returns error message, NULL if it's OK.
4735 */
4736 char *
4737set_chars_option(char_u **varp)
4738{
4739 int round, i, len, entries;
4740 char_u *p, *s;
4741 int c1 = 0, c2 = 0, c3 = 0;
4742 struct charstab
4743 {
4744 int *cp;
4745 char *name;
4746 };
4747 static struct charstab filltab[] =
4748 {
4749 {&fill_stl, "stl"},
4750 {&fill_stlnc, "stlnc"},
4751 {&fill_vert, "vert"},
4752 {&fill_fold, "fold"},
4753 {&fill_diff, "diff"},
4754 };
4755 static struct charstab lcstab[] =
4756 {
4757 {&lcs_eol, "eol"},
4758 {&lcs_ext, "extends"},
4759 {&lcs_nbsp, "nbsp"},
4760 {&lcs_prec, "precedes"},
4761 {&lcs_space, "space"},
4762 {&lcs_tab2, "tab"},
4763 {&lcs_trail, "trail"},
4764#ifdef FEAT_CONCEAL
4765 {&lcs_conceal, "conceal"},
4766#else
4767 {NULL, "conceal"},
4768#endif
4769 };
4770 struct charstab *tab;
4771
4772 if (varp == &p_lcs)
4773 {
4774 tab = lcstab;
4775 entries = sizeof(lcstab) / sizeof(struct charstab);
4776 }
4777 else
4778 {
4779 tab = filltab;
4780 entries = sizeof(filltab) / sizeof(struct charstab);
4781 }
4782
4783 // first round: check for valid value, second round: assign values
4784 for (round = 0; round <= 1; ++round)
4785 {
4786 if (round > 0)
4787 {
4788 // After checking that the value is valid: set defaults: space for
4789 // 'fillchars', NUL for 'listchars'
4790 for (i = 0; i < entries; ++i)
4791 if (tab[i].cp != NULL)
4792 *(tab[i].cp) = (varp == &p_lcs ? NUL : ' ');
4793
4794 if (varp == &p_lcs)
4795 {
4796 lcs_tab1 = NUL;
4797 lcs_tab3 = NUL;
4798 }
4799 else
4800 fill_diff = '-';
4801 }
4802 p = *varp;
4803 while (*p)
4804 {
4805 for (i = 0; i < entries; ++i)
4806 {
4807 len = (int)STRLEN(tab[i].name);
4808 if (STRNCMP(p, tab[i].name, len) == 0
4809 && p[len] == ':'
4810 && p[len + 1] != NUL)
4811 {
4812 c2 = c3 = 0;
4813 s = p + len + 1;
4814 c1 = mb_ptr2char_adv(&s);
4815 if (mb_char2cells(c1) > 1)
4816 continue;
4817 if (tab[i].cp == &lcs_tab2)
4818 {
4819 if (*s == NUL)
4820 continue;
4821 c2 = mb_ptr2char_adv(&s);
4822 if (mb_char2cells(c2) > 1)
4823 continue;
4824 if (!(*s == ',' || *s == NUL))
4825 {
4826 c3 = mb_ptr2char_adv(&s);
4827 if (mb_char2cells(c3) > 1)
4828 continue;
4829 }
4830 }
4831
4832 if (*s == ',' || *s == NUL)
4833 {
4834 if (round)
4835 {
4836 if (tab[i].cp == &lcs_tab2)
4837 {
4838 lcs_tab1 = c1;
4839 lcs_tab2 = c2;
4840 lcs_tab3 = c3;
4841 }
4842 else if (tab[i].cp != NULL)
4843 *(tab[i].cp) = c1;
4844
4845 }
4846 p = s;
4847 break;
4848 }
4849 }
4850 }
4851
4852 if (i == entries)
4853 return e_invarg;
4854 if (*p == ',')
4855 ++p;
4856 }
4857 }
4858
4859 return NULL; // no error
4860}
Bram Moolenaar017ba072019-09-14 21:01:23 +02004861