blob: 763d7a82fe2159da133784cf4429079f69abc6c2 [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 Moolenaar071d4272004-06-13 20:20:40 +000056/* Ugly global: overrule attribute used by screen_char() */
57static 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;
93 /* Need to recompute cursor column, e.g., when starting Visual mode
94 * without concealing. */
95 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);
111#ifdef FEAT_TEXT_PROP
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,
247 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
256 /* 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 Moolenaar578b49e2005-09-10 19:22:57 +0000262 /* 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 Moolenaar071d4272004-06-13 20:20:40 +0000265 /* 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}
289#endif /* FEAT_FOLDING */
290
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 Moolenaarc662ec92019-06-23 00:15:57 +0200351#ifdef FEAT_TEXT_PROP
352/*
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 Moolenaar071d4272004-06-13 20:20:40 +0000412 int force = FALSE; /* force update rest of the line */
413 int redraw_this /* bool: does character need redraw? */
414#ifdef FEAT_GUI
415 = TRUE /* For GUI when while-loop empty */
416#endif
417 ;
418 int redraw_next; /* redraw_this for next character */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419 int clear_next = FALSE;
420 int char_cells; /* 1: normal char */
421 /* 2: occupies two display cells */
422# define CHAR_CELLS char_cells
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423
Bram Moolenaar5ad15df2012-03-16 19:07:58 +0100424 /* Check for illegal row and col, just in case. */
425 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 {
442 /* Clear rest first, because it's left of the text. */
443 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 }
461#endif /* FEAT_RIGHTLEFT */
462
Bram Moolenaar92e25ab2019-11-26 22:39:10 +0100463#ifdef FEAT_TEXT_PROP
464 // 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
489 /* 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.
492 */
493 if (redraw_next && gui.in_use)
494 {
495 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000496 if (hl > HL_ALL)
497 hl = syn_attr2attr(hl);
498 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499 redraw_this = TRUE;
500 }
501#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +0200502#ifdef FEAT_TEXT_PROP
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200503 if (blocked_by_popup(row, col + coloff))
Bram Moolenaar33796b32019-06-08 16:01:13 +0200504 redraw_this = FALSE;
505#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506 if (redraw_this)
507 {
508 /*
509 * Special handling when 'xs' termcap flag set (hpterm):
510 * Attributes for characters are stored at the position where the
511 * cursor is when writing the highlighting code. The
512 * start-highlighting code must be written with the cursor on the
513 * first highlighted character. The stop-highlighting code must
514 * be written with the cursor just after the last highlighted
515 * character.
Bram Moolenaarc4568ab2018-11-16 16:21:05 +0100516 * Overwriting a character doesn't remove its highlighting. Need
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 * to clear the rest of the line, and force redrawing it
518 * completely.
519 */
520 if ( p_wiv
521 && !force
522#ifdef FEAT_GUI
523 && !gui.in_use
524#endif
525 && ScreenAttrs[off_to] != 0
526 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
527 {
528 /*
529 * Need to remove highlighting attributes here.
530 */
531 windgoto(row, col + coloff);
532 out_str(T_CE); /* clear rest of this screen line */
533 screen_start(); /* don't know where cursor is now */
534 force = TRUE; /* force redraw of rest of the line */
535 redraw_next = TRUE; /* or else next char would miss out */
536
537 /*
538 * If the previous character was highlighted, need to stop
539 * highlighting at this character.
540 */
541 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
542 {
543 screen_attr = ScreenAttrs[off_to - 1];
544 term_windgoto(row, col + coloff);
545 screen_stop_highlight();
546 }
547 else
548 screen_attr = 0; /* highlighting has stopped */
549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 if (enc_dbcs != 0)
551 {
552 /* Check if overwriting a double-byte with a single-byte or
553 * the other way around requires another character to be
554 * redrawn. For UTF-8 this isn't needed, because comparing
555 * ScreenLinesUC[] is sufficient. */
556 if (char_cells == 1
557 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +0000558 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559 {
560 /* Writing a single-cell character over a double-cell
561 * character: need to redraw the next cell. */
562 ScreenLines[off_to + 1] = 0;
563 redraw_next = TRUE;
564 }
565 else if (char_cells == 2
566 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +0000567 && (*mb_off2cells)(off_to, max_off_to) == 1
568 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 {
570 /* Writing the second half of a double-cell character over
571 * a double-cell character: need to redraw the second
572 * cell. */
573 ScreenLines[off_to + 2] = 0;
574 redraw_next = TRUE;
575 }
576
577 if (enc_dbcs == DBCS_JPNU)
578 ScreenLines2[off_to] = ScreenLines2[off_from];
579 }
580 /* When writing a single-width character over a double-width
581 * character and at the end of the redrawn text, need to clear out
582 * the right halve of the old character.
583 * Also required when writing the right halve of a double-width
584 * char over the left halve of an existing one. */
585 if (has_mbyte && col + char_cells == endcol
586 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +0000587 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +0000589 && (*mb_off2cells)(off_to, max_off_to) == 1
590 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 clear_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592
593 ScreenLines[off_to] = ScreenLines[off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 if (enc_utf8)
595 {
596 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
597 if (ScreenLinesUC[off_from] != 0)
598 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000599 int i;
600
601 for (i = 0; i < Screen_mco; ++i)
602 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 }
604 }
605 if (char_cells == 2)
606 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607
608#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +0000609 /* The bold trick makes a single column of pixels appear in the
610 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611 * character should be redrawn too. This happens for our own GUI
612 * and for some xterms. */
613 if (
614# ifdef FEAT_GUI
615 gui.in_use
616# endif
617# if defined(FEAT_GUI) && defined(UNIX)
618 ||
619# endif
620# ifdef UNIX
621 term_is_xterm
622# endif
623 )
624 {
625 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000626 if (hl > HL_ALL)
627 hl = syn_attr2attr(hl);
628 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 redraw_next = TRUE;
630 }
631#endif
632 ScreenAttrs[off_to] = ScreenAttrs[off_from];
Bram Moolenaara12a1612019-01-24 16:39:02 +0100633
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000634 /* For simplicity set the attributes of second half of a
635 * double-wide character equal to the first half. */
636 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000638
639 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 screen_char(off_to, row, col + coloff);
643 }
644 else if ( p_wiv
645#ifdef FEAT_GUI
646 && !gui.in_use
647#endif
648 && col + coloff > 0)
649 {
650 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
651 {
652 /*
653 * Don't output stop-highlight when moving the cursor, it will
654 * stop the highlighting when it should continue.
655 */
656 screen_attr = 0;
657 }
658 else if (screen_attr != 0)
659 screen_stop_highlight();
660 }
661
662 off_to += CHAR_CELLS;
663 off_from += CHAR_CELLS;
664 col += CHAR_CELLS;
665 }
666
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 if (clear_next)
668 {
669 /* Clear the second half of a double-wide character of which the left
670 * half was overwritten with a single-wide character. */
671 ScreenLines[off_to] = ' ';
672 if (enc_utf8)
673 ScreenLinesUC[off_to] = 0;
674 screen_char(off_to, row, col + coloff);
675 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676
677 if (clear_width > 0
678#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200679 && !(flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680#endif
681 )
682 {
683#ifdef FEAT_GUI
684 int startCol = col;
685#endif
686
687 /* blank out the rest of the line */
688 while (col < clear_width && ScreenLines[off_to] == ' '
689 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +0100690 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 {
692 ++off_to;
693 ++col;
694 }
695 if (col < clear_width)
696 {
697#ifdef FEAT_GUI
698 /*
699 * In the GUI, clearing the rest of the line may leave pixels
700 * behind if the first character cleared was bold. Some bold
701 * fonts spill over the left. In this case we redraw the previous
702 * character too. If we didn't skip any blanks above, then we
703 * only redraw if the character wasn't already redrawn anyway.
704 */
Bram Moolenaar9c697322006-10-09 20:11:17 +0000705 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 {
707 hl = ScreenAttrs[off_to];
708 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +0000709 {
710 int prev_cells = 1;
Bram Moolenaara12a1612019-01-24 16:39:02 +0100711
Bram Moolenaar9c697322006-10-09 20:11:17 +0000712 if (enc_utf8)
713 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
714 * that its width is 2. */
715 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
716 else if (enc_dbcs != 0)
717 {
718 /* find previous character by counting from first
719 * column and get its width. */
720 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +0000721 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +0000722
723 while (off < off_to)
724 {
Bram Moolenaar367329b2007-08-30 11:53:22 +0000725 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +0000726 off += prev_cells;
727 }
728 }
729
730 if (enc_dbcs != 0 && prev_cells > 1)
731 screen_char_2(off_to - prev_cells, row,
732 col + coloff - prev_cells);
733 else
Bram Moolenaar9c697322006-10-09 20:11:17 +0000734 screen_char(off_to - prev_cells, row,
735 col + coloff - prev_cells);
736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 }
738#endif
739 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
740 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 off_to += clear_width - col;
742 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 }
744 }
745
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200746 if (clear_width > 0
747#ifdef FEAT_TEXT_PROP
748 && !(flags & SLF_POPUP) // no separator for popup window
749#endif
750 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200752 // For a window that has a right neighbor, draw the separator char
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200753 // right of the window contents. But not on top of a popup window.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200754 if (coloff + col < Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200756#ifdef FEAT_TEXT_PROP
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200757 if (!blocked_by_popup(row, col + coloff))
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200758#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200760 int c;
761
762 c = fillchar_vsep(&hl);
763 if (ScreenLines[off_to] != (schar_T)c
764 || (enc_utf8 && (int)ScreenLinesUC[off_to]
765 != (c >= 0x80 ? c : 0))
766 || ScreenAttrs[off_to] != hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200768 ScreenLines[off_to] = c;
769 ScreenAttrs[off_to] = hl;
770 if (enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200772 if (c >= 0x80)
773 {
774 ScreenLinesUC[off_to] = c;
775 ScreenLinesC[0][off_to] = 0;
776 }
777 else
778 ScreenLinesUC[off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 }
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200780 screen_char(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 }
783 }
784 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 LineWraps[row] = FALSE;
786 }
787}
788
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000789#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000791 * Mirror text "str" for right-left displaying.
792 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000794 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100795rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796{
797 char_u *p1, *p2;
798 int t;
799
800 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
801 {
802 t = *p1;
803 *p1 = *p2;
804 *p2 = t;
805 }
806}
807#endif
808
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 * Draw the verticap separator right of window "wp" starting with line "row".
811 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200812 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100813draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814{
815 int hl;
816 int c;
817
818 if (wp->w_vsep_width)
819 {
820 /* draw the vertical separator right of this window */
821 c = fillchar_vsep(&hl);
822 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
823 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
824 c, ' ', hl);
825 }
826}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827
828#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100829static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830
831/*
Bram Moolenaar367329b2007-08-30 11:53:22 +0000832 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 */
834 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100835status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836{
837 int len = 0;
838
839#ifdef FEAT_MENU
840 int emenu = (xp->xp_context == EXPAND_MENUS
841 || xp->xp_context == EXPAND_MENUNAMES);
842
843 /* Check for menu separators - replace with '|'. */
844 if (emenu && menu_is_separator(s))
845 return 1;
846#endif
847
848 while (*s != NUL)
849 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +0000850 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +0000851 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100852 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 }
854
855 return len;
856}
857
858/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +0000859 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000860 * These are backslashes used for escaping. Do show backslashes in help tags.
861 */
862 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100863skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000864{
Bram Moolenaar7693ec62008-07-24 18:29:37 +0000865 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000866#ifdef FEAT_MENU
867 || ((xp->xp_context == EXPAND_MENUS
868 || xp->xp_context == EXPAND_MENUNAMES)
869 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
870#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +0000871 )
872 {
873#ifndef BACKSLASH_IN_FILENAME
874 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
875 return 2;
876#endif
877 return 1;
878 }
879 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000880}
881
882/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 * Show wildchar matches in the status line.
884 * Show at least the "match" item.
885 * We start at item 'first_match' in the list and show all matches that fit.
886 *
887 * If inversion is possible we use it. Else '=' characters are used.
888 */
889 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100890win_redr_status_matches(
891 expand_T *xp,
892 int num_matches,
893 char_u **matches, /* list of matches */
894 int match,
895 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896{
897#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
898 int row;
899 char_u *buf;
900 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +0000901 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 int fillchar;
903 int attr;
904 int i;
905 int highlight = TRUE;
906 char_u *selstart = NULL;
907 int selstart_col = 0;
908 char_u *selend = NULL;
909 static int first_match = 0;
910 int add_left = FALSE;
911 char_u *s;
912#ifdef FEAT_MENU
913 int emenu;
914#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916
917 if (matches == NULL) /* interrupted completion? */
918 return;
919
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000920 if (has_mbyte)
Bram Moolenaar964b3742019-05-24 18:54:09 +0200921 buf = alloc(Columns * MB_MAXBYTES + 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000922 else
Bram Moolenaar964b3742019-05-24 18:54:09 +0200923 buf = alloc(Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 if (buf == NULL)
925 return;
926
927 if (match == -1) /* don't show match but original text */
928 {
929 match = 0;
930 highlight = FALSE;
931 }
932 /* count 1 for the ending ">" */
933 clen = status_match_len(xp, L_MATCH(match)) + 3;
934 if (match == 0)
935 first_match = 0;
936 else if (match < first_match)
937 {
938 /* jumping left, as far as we can go */
939 first_match = match;
940 add_left = TRUE;
941 }
942 else
943 {
944 /* check if match fits on the screen */
945 for (i = first_match; i < match; ++i)
946 clen += status_match_len(xp, L_MATCH(i)) + 2;
947 if (first_match > 0)
948 clen += 2;
949 /* jumping right, put match at the left */
950 if ((long)clen > Columns)
951 {
952 first_match = match;
953 /* if showing the last match, we can add some on the left */
954 clen = 2;
955 for (i = match; i < num_matches; ++i)
956 {
957 clen += status_match_len(xp, L_MATCH(i)) + 2;
958 if ((long)clen >= Columns)
959 break;
960 }
961 if (i == num_matches)
962 add_left = TRUE;
963 }
964 }
965 if (add_left)
966 while (first_match > 0)
967 {
968 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
969 if ((long)clen >= Columns)
970 break;
971 --first_match;
972 }
973
Bram Moolenaar3633cf52017-07-31 22:29:35 +0200974 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975
976 if (first_match == 0)
977 {
978 *buf = NUL;
979 len = 0;
980 }
981 else
982 {
983 STRCPY(buf, "< ");
984 len = 2;
985 }
986 clen = len;
987
988 i = first_match;
989 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
990 {
991 if (i == match)
992 {
993 selstart = buf + len;
994 selstart_col = clen;
995 }
996
997 s = L_MATCH(i);
998 /* Check for menu separators - replace with '|' */
999#ifdef FEAT_MENU
1000 emenu = (xp->xp_context == EXPAND_MENUS
1001 || xp->xp_context == EXPAND_MENUNAMES);
1002 if (emenu && menu_is_separator(s))
1003 {
1004 STRCPY(buf + len, transchar('|'));
1005 l = (int)STRLEN(buf + len);
1006 len += l;
1007 clen += l;
1008 }
1009 else
1010#endif
1011 for ( ; *s != NUL; ++s)
1012 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00001013 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 clen += ptr2cells(s);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001015 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 {
1017 STRNCPY(buf + len, s, l);
1018 s += l - 1;
1019 len += l;
1020 }
1021 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 {
1023 STRCPY(buf + len, transchar_byte(*s));
1024 len += (int)STRLEN(buf + len);
1025 }
1026 }
1027 if (i == match)
1028 selend = buf + len;
1029
1030 *(buf + len++) = ' ';
1031 *(buf + len++) = ' ';
1032 clen += 2;
1033 if (++i == num_matches)
1034 break;
1035 }
1036
1037 if (i != num_matches)
1038 {
1039 *(buf + len++) = '>';
1040 ++clen;
1041 }
1042
1043 buf[len] = NUL;
1044
1045 row = cmdline_row - 1;
1046 if (row >= 0)
1047 {
1048 if (wild_menu_showing == 0)
1049 {
1050 if (msg_scrolled > 0)
1051 {
1052 /* Put the wildmenu just above the command line. If there is
1053 * no room, scroll the screen one line up. */
1054 if (cmdline_row == Rows - 1)
1055 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001056 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 ++msg_scrolled;
1058 }
1059 else
1060 {
1061 ++cmdline_row;
1062 ++row;
1063 }
1064 wild_menu_showing = WM_SCROLLED;
1065 }
1066 else
1067 {
1068 /* Create status line if needed by setting 'laststatus' to 2.
1069 * Set 'winminheight' to zero to avoid that the window is
1070 * resized. */
1071 if (lastwin->w_status_height == 0)
1072 {
1073 save_p_ls = p_ls;
1074 save_p_wmh = p_wmh;
1075 p_ls = 2;
1076 p_wmh = 0;
1077 last_status(FALSE);
1078 }
1079 wild_menu_showing = WM_SHOWN;
1080 }
1081 }
1082
1083 screen_puts(buf, row, 0, attr);
1084 if (selstart != NULL && highlight)
1085 {
1086 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001087 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 }
1089
1090 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
1091 }
1092
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 vim_free(buf);
1095}
1096#endif
1097
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 * Return TRUE if the status line of window "wp" is connected to the status
1100 * line of the window right of it. If not, then it's a vertical separator.
1101 * Only call if (wp->w_vsep_width != 0).
1102 */
1103 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001104stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105{
1106 frame_T *fr;
1107
1108 fr = wp->w_frame;
1109 while (fr->fr_parent != NULL)
1110 {
1111 if (fr->fr_parent->fr_layout == FR_COL)
1112 {
1113 if (fr->fr_next != NULL)
1114 break;
1115 }
1116 else
1117 {
1118 if (fr->fr_next != NULL)
1119 return TRUE;
1120 }
1121 fr = fr->fr_parent;
1122 }
1123 return FALSE;
1124}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127/*
1128 * Get the value to show for the language mappings, active 'keymap'.
1129 */
1130 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001131get_keymap_str(
1132 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02001133 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01001134 char_u *buf, /* buffer for the result */
1135 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136{
1137 char_u *p;
1138
1139 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
1140 return FALSE;
1141
1142 {
1143#ifdef FEAT_EVAL
1144 buf_T *old_curbuf = curbuf;
1145 win_T *old_curwin = curwin;
1146 char_u *s;
1147
1148 curbuf = wp->w_buffer;
1149 curwin = wp;
1150 STRCPY(buf, "b:keymap_name"); /* must be writable */
1151 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001152 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 --emsg_skip;
1154 curbuf = old_curbuf;
1155 curwin = old_curwin;
1156 if (p == NULL || *p == NUL)
1157#endif
1158 {
1159#ifdef FEAT_KEYMAP
1160 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
1161 p = wp->w_buffer->b_p_keymap;
1162 else
1163#endif
1164 p = (char_u *)"lang";
1165 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02001166 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 buf[0] = NUL;
1168#ifdef FEAT_EVAL
1169 vim_free(s);
1170#endif
1171 }
1172 return buf[0] != NUL;
1173}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174
1175#if defined(FEAT_STL_OPT) || defined(PROTO)
1176/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001177 * Redraw the status line or ruler of window "wp".
1178 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001180 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001181win_redr_custom(
1182 win_T *wp,
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001183 int draw_ruler) // TRUE or FALSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184{
Bram Moolenaar1d633412013-12-11 15:52:01 +01001185 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 int attr;
1187 int curattr;
1188 int row;
1189 int col = 0;
1190 int maxwidth;
1191 int width;
1192 int n;
1193 int len;
1194 int fillchar;
1195 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00001196 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001198 struct stl_hlrec hltab[STL_MAX_ITEM];
1199 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001200 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01001201 win_T *ewp;
1202 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001204 // There is a tiny chance that this gets called recursively: When
1205 // redrawing a status line triggers redrawing the ruler or tabline.
1206 // Avoid trouble by not allowing recursion.
Bram Moolenaar1d633412013-12-11 15:52:01 +01001207 if (entered)
1208 return;
1209 entered = TRUE;
1210
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001211 // setup environment for the task at hand
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001212 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 {
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001214 // Use 'tabline'. Always at the first line of the screen.
Bram Moolenaar362f3562009-11-03 16:20:34 +00001215 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001216 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00001217 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01001218 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001219 maxwidth = Columns;
1220# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001221 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001222# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001224 else
1225 {
1226 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02001227 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02001228 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001229
1230 if (draw_ruler)
1231 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00001232 stl = p_ruf;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001233 // advance past any leading group spec - implicit in ru_col
Bram Moolenaar362f3562009-11-03 16:20:34 +00001234 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001235 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00001236 if (*++stl == '-')
1237 stl++;
1238 if (atoi((char *)stl))
1239 while (VIM_ISDIGIT(*stl))
1240 stl++;
1241 if (*stl++ != '(')
1242 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001243 }
Bram Moolenaar02631462017-09-22 15:20:32 +02001244 col = ru_col - (Columns - wp->w_width);
1245 if (col < (wp->w_width + 1) / 2)
1246 col = (wp->w_width + 1) / 2;
1247 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001248 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001249 {
1250 row = Rows - 1;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001251 --maxwidth; // writing in last column may cause scrolling
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001252 fillchar = ' ';
1253 attr = 0;
1254 }
1255
1256# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001257 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001258# endif
1259 }
1260 else
1261 {
1262 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00001263 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001264 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00001265 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001266# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001267 use_sandbox = was_set_insecurely((char_u *)"statusline",
1268 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001269# endif
1270 }
1271
Bram Moolenaar53f81742017-09-22 14:35:51 +02001272 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001273 }
1274
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01001276 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001278 // Temporarily reset 'cursorbind', we don't want a side effect from moving
1279 // the cursor away and back.
Bram Moolenaar61452852011-02-01 18:01:11 +01001280 ewp = wp == NULL ? curwin : wp;
1281 p_crb_save = ewp->w_p_crb;
1282 ewp->w_p_crb = FALSE;
1283
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001284 // Make a copy, because the statusline may include a function call that
1285 // might change the option value and free the memory.
Bram Moolenaar362f3562009-11-03 16:20:34 +00001286 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01001287 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00001288 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001289 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00001290 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01001291 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001293 // Make all characters printable.
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01001294 p = transstr(buf);
1295 if (p != NULL)
1296 {
1297 vim_strncpy(buf, p, sizeof(buf) - 1);
1298 vim_free(p);
1299 }
1300
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001301 // fill up with "fillchar"
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01001302 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00001303 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 len += (*mb_char2bytes)(fillchar, buf + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 ++width;
1307 }
1308 buf[len] = NUL;
1309
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001310 /*
1311 * Draw each snippet with the specified highlighting.
1312 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 curattr = attr;
1314 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001315 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001317 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 screen_puts_len(p, len, row, col, curattr);
1319 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001320 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001322 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001324 else if (hltab[n].userhl < 0)
1325 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02001326#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02001327 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
1328 && wp->w_status_height != 0)
1329 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02001330 else if (wp != NULL && bt_terminal(wp->w_buffer)
1331 && wp->w_status_height != 0)
1332 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02001333#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00001334 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001335 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001337 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 }
1339 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001340
1341 if (wp == NULL)
1342 {
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001343 // Fill the TabPageIdxs[] array for clicking in the tab pagesline.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001344 col = 0;
1345 len = 0;
1346 p = buf;
1347 fillchar = 0;
1348 for (n = 0; tabtab[n].start != NULL; n++)
1349 {
1350 len += vim_strnsize(p, (int)(tabtab[n].start - p));
1351 while (col < len)
1352 TabPageIdxs[col++] = fillchar;
1353 p = tabtab[n].start;
1354 fillchar = tabtab[n].userhl;
1355 }
1356 while (col < Columns)
1357 TabPageIdxs[col++] = fillchar;
1358 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01001359
1360theend:
1361 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362}
1363
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001364#endif // FEAT_STL_OPT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365
1366/*
1367 * Output a single character directly to the screen and update ScreenLines.
1368 */
1369 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001370screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 char_u buf[MB_MAXBYTES + 1];
1373
Bram Moolenaar9a920d82012-06-01 15:21:02 +02001374 if (has_mbyte)
1375 buf[(*mb_char2bytes)(c, buf)] = NUL;
1376 else
Bram Moolenaar9a920d82012-06-01 15:21:02 +02001377 {
1378 buf[0] = c;
1379 buf[1] = NUL;
1380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 screen_puts(buf, row, col, attr);
1382}
1383
1384/*
1385 * Get a single character directly from ScreenLines into "bytes[]".
1386 * Also return its attribute in *attrp;
1387 */
1388 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001389screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390{
1391 unsigned off;
1392
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001393 // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
1395 {
1396 off = LineOffset[row] + col;
1397 *attrp = ScreenAttrs[off];
1398 bytes[0] = ScreenLines[off];
1399 bytes[1] = NUL;
1400
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 if (enc_utf8 && ScreenLinesUC[off] != 0)
1402 bytes[utfc_char2bytes(off, bytes)] = NUL;
1403 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
1404 {
1405 bytes[0] = ScreenLines[off];
1406 bytes[1] = ScreenLines2[off];
1407 bytes[2] = NUL;
1408 }
1409 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
1410 {
1411 bytes[1] = ScreenLines[off + 1];
1412 bytes[2] = NUL;
1413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 }
1415}
1416
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001417/*
1418 * Return TRUE if composing characters for screen posn "off" differs from
1419 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01001420 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001421 */
1422 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001423screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001424{
1425 int i;
1426
1427 for (i = 0; i < Screen_mco; ++i)
1428 {
1429 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
1430 return TRUE;
1431 if (u8cc[i] == 0)
1432 break;
1433 }
1434 return FALSE;
1435}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001436
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437/*
1438 * Put string '*text' on the screen at position 'row' and 'col', with
1439 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
1440 * Note: only outputs within one row, message is truncated at screen boundary!
1441 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
1442 */
1443 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001444screen_puts(
1445 char_u *text,
1446 int row,
1447 int col,
1448 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449{
1450 screen_puts_len(text, -1, row, col, attr);
1451}
1452
1453/*
1454 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
1455 * a NUL.
1456 */
1457 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001458screen_puts_len(
1459 char_u *text,
1460 int textlen,
1461 int row,
1462 int col,
1463 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464{
1465 unsigned off;
1466 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02001467 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 int c;
Bram Moolenaar367329b2007-08-30 11:53:22 +00001469 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 int mbyte_blen = 1;
1471 int mbyte_cells = 1;
1472 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001473 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 int clear_next_cell = FALSE;
Bram Moolenaara12a1612019-01-24 16:39:02 +01001475#ifdef FEAT_ARABIC
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001476 int prev_c = 0; // previous Arabic character
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001477 int pc, nc, nc1;
1478 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001480 int force_redraw_this;
1481 int force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001482 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483
Bram Moolenaar0b4c9ed2019-06-03 22:04:23 +02001484 // Safety check. The check for negative row and column is to fix issue
1485 // #4102. TODO: find out why row/col could be negative.
1486 if (ScreenLines == NULL
1487 || row >= screen_Rows || row < 0
1488 || col >= screen_Columns || col < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001490 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001492 // When drawing over the right halve of a double-wide char clear out the
1493 // left halve. Only needed in a terminal.
Bram Moolenaar7693ec62008-07-24 18:29:37 +00001494 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaara12a1612019-01-24 16:39:02 +01001495#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00001496 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01001497#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00001498 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001499 {
1500 ScreenLines[off - 1] = ' ';
1501 ScreenAttrs[off - 1] = 0;
1502 if (enc_utf8)
1503 {
1504 ScreenLinesUC[off - 1] = 0;
1505 ScreenLinesC[0][off - 1] = 0;
1506 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001507 // redraw the previous cell, make it empty
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001508 screen_char(off - 1, row, col - 1);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001509 // force the cell at "col" to be redrawn
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001510 force_redraw_next = TRUE;
1511 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00001512
Bram Moolenaar367329b2007-08-30 11:53:22 +00001513 max_off = LineOffset[row] + screen_Columns;
Bram Moolenaara064ac82007-08-05 18:10:54 +00001514 while (col < screen_Columns
1515 && (len < 0 || (int)(ptr - text) < len)
1516 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 {
1518 c = *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 /* check if this is the first byte of a multibyte */
1520 if (has_mbyte)
1521 {
1522 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001523 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001525 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
1527 mbyte_cells = 1;
1528 else if (enc_dbcs != 0)
1529 mbyte_cells = mbyte_blen;
1530 else /* enc_utf8 */
1531 {
1532 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001533 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 (int)((text + len) - ptr));
1535 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001536 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaara12a1612019-01-24 16:39:02 +01001538#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
1540 {
1541 /* Do Arabic shaping. */
1542 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
1543 {
1544 /* Past end of string to be displayed. */
1545 nc = NUL;
1546 nc1 = NUL;
1547 }
1548 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001549 {
Bram Moolenaar54620182009-11-11 16:07:20 +00001550 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
1551 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001552 nc1 = pcc[0];
1553 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 pc = prev_c;
1555 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001556 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 }
1558 else
1559 prev_c = u8c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01001560#endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01001561 if (col + mbyte_cells > screen_Columns)
1562 {
1563 /* Only 1 cell left, but character requires 2 cells:
1564 * display a '>' in the last column to avoid wrapping. */
1565 c = '>';
1566 mbyte_cells = 1;
1567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 }
1569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001571 force_redraw_this = force_redraw_next;
1572 force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001573
1574 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 || (mbyte_cells == 2
1576 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
1577 || (enc_dbcs == DBCS_JPNU
1578 && c == 0x8e
1579 && ScreenLines2[off] != ptr[1])
1580 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01001581 && (ScreenLinesUC[off] !=
1582 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
1583 || (ScreenLinesUC[off] != 0
1584 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001586 || exmode_active;
1587
Bram Moolenaar24a5ac52019-06-08 19:01:18 +02001588 if ((need_redraw || force_redraw_this)
1589#ifdef FEAT_TEXT_PROP
Bram Moolenaarc662ec92019-06-23 00:15:57 +02001590 && !blocked_by_popup(row, col)
Bram Moolenaar24a5ac52019-06-08 19:01:18 +02001591#endif
1592 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 {
1594#if defined(FEAT_GUI) || defined(UNIX)
1595 /* The bold trick makes a single row of pixels appear in the next
1596 * character. When a bold character is removed, the next
1597 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001598 * and for some xterms. */
1599 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600# ifdef FEAT_GUI
1601 gui.in_use
1602# endif
1603# if defined(FEAT_GUI) && defined(UNIX)
1604 ||
1605# endif
1606# ifdef UNIX
1607 term_is_xterm
1608# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001609 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001611 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001613 if (n > HL_ALL)
1614 n = syn_attr2attr(n);
1615 if (n & HL_BOLD)
1616 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 }
1618#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 /* When at the end of the text and overwriting a two-cell
1620 * character with a one-cell character, need to clear the next
1621 * cell. Also when overwriting the left halve of a two-cell char
1622 * with the right halve of a two-cell char. Do this only once
1623 * (mb_off2cells() may return 2 on the right halve). */
1624 if (clear_next_cell)
1625 clear_next_cell = FALSE;
1626 else if (has_mbyte
1627 && (len < 0 ? ptr[mbyte_blen] == NUL
1628 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00001629 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00001631 && (*mb_off2cells)(off, max_off) == 1
1632 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 clear_next_cell = TRUE;
1634
1635 /* Make sure we never leave a second byte of a double-byte behind,
1636 * it confuses mb_off2cells(). */
1637 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00001638 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00001640 && (*mb_off2cells)(off, max_off) == 1
1641 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 ScreenLines[off + mbyte_blen] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 ScreenLines[off] = c;
1644 ScreenAttrs[off] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 if (enc_utf8)
1646 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001647 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 ScreenLinesUC[off] = 0;
1649 else
1650 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001651 int i;
1652
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001654 for (i = 0; i < Screen_mco; ++i)
1655 {
1656 ScreenLinesC[i][off] = u8cc[i];
1657 if (u8cc[i] == 0)
1658 break;
1659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 }
1661 if (mbyte_cells == 2)
1662 {
1663 ScreenLines[off + 1] = 0;
1664 ScreenAttrs[off + 1] = attr;
1665 }
1666 screen_char(off, row, col);
1667 }
1668 else if (mbyte_cells == 2)
1669 {
1670 ScreenLines[off + 1] = ptr[1];
1671 ScreenAttrs[off + 1] = attr;
1672 screen_char_2(off, row, col);
1673 }
1674 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
1675 {
1676 ScreenLines2[off] = ptr[1];
1677 screen_char(off, row, col);
1678 }
1679 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 screen_char(off, row, col);
1681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 if (has_mbyte)
1683 {
1684 off += mbyte_cells;
1685 col += mbyte_cells;
1686 ptr += mbyte_blen;
1687 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02001688 {
1689 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02001691 len = -1;
1692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 }
1694 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 {
1696 ++off;
1697 ++col;
1698 ++ptr;
1699 }
1700 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001701
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001702 /* If we detected the next character needs to be redrawn, but the text
1703 * doesn't extend up to there, update the character here. */
1704 if (force_redraw_next && col < screen_Columns)
1705 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001706 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
1707 screen_char_2(off, row, col);
1708 else
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001709 screen_char(off, row, col);
1710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711}
1712
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001713#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001715 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001717 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001718start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719{
1720 if (p_hls && !no_hlsearch)
1721 {
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
1725 /* 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 Moolenaard1f56e62006-02-22 21:25:37 +00001762 /* The GUI handles this internally. */
1763 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 {
1769 if (attr > HL_ALL) /* special HL attr. */
1770 {
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);
1775 if (aep == NULL) /* did ":syntax clear" */
1776 attr = 0;
1777 else
1778 attr = aep->ae_attr;
1779 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01001780 if ((attr & HL_BOLD) && *T_MD != NUL) /* bold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001782 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001783#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001784 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
1785 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
1786 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001787#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001788 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001789 /* If the Normal FG color has BOLD attribute and the new HL
1790 * has a FG color defined, clear BOLD. */
1791 out_str(T_ME);
Bram Moolenaar45a00002017-12-22 21:12:34 +01001792 if ((attr & HL_STANDOUT) && *T_SO != NUL) /* standout */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 out_str(T_SO);
Bram Moolenaar45a00002017-12-22 21:12:34 +01001794 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) /* undercurl */
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01001795 out_str(T_UCS);
1796 if (((attr & HL_UNDERLINE) /* underline or undercurl */
Bram Moolenaar45a00002017-12-22 21:12:34 +01001797 || ((attr & HL_UNDERCURL) && *T_UCS == NUL))
1798 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 out_str(T_US);
Bram Moolenaar45a00002017-12-22 21:12:34 +01001800 if ((attr & HL_ITALIC) && *T_CZH != NUL) /* italic */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 out_str(T_CZH);
Bram Moolenaar45a00002017-12-22 21:12:34 +01001802 if ((attr & HL_INVERSE) && *T_MR != NUL) /* inverse (reverse) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 out_str(T_MR);
Bram Moolenaar45a00002017-12-22 21:12:34 +01001804 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) /* strike */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02001805 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806
1807 /*
1808 * Output the color or start string after bold etc., in case the
1809 * bold etc. override the color setting.
1810 */
1811 if (aep != NULL)
1812 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001813#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001814 /* When 'termguicolors' is set but fg or bg is unset,
1815 * fall back to the cterm colors. This helps for SpellBad,
1816 * where the GUI uses a red undercurl. */
1817 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001819 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001820 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001821 }
1822 else
1823#endif
1824 if (t_colors > 1)
1825 {
1826 if (aep->ae_u.cterm.fg_color)
1827 term_fg_color(aep->ae_u.cterm.fg_color - 1);
1828 }
1829#ifdef FEAT_TERMGUICOLORS
1830 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
1831 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001832 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001833 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 }
1835 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001836#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001837 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001839 if (aep->ae_u.cterm.bg_color)
1840 term_bg_color(aep->ae_u.cterm.bg_color - 1);
1841 }
1842
Bram Moolenaarf708ac52018-03-12 21:48:32 +01001843 if (!IS_CTERM)
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001844 {
1845 if (aep->ae_u.term.start != NULL)
1846 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 }
1848 }
1849 }
1850 }
1851}
1852
1853 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001854screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855{
1856 int do_ME = FALSE; /* output T_ME code */
1857
1858 if (screen_attr != 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01001859#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 && termcap_active
1861#endif
1862 )
1863 {
1864#ifdef FEAT_GUI
1865 if (gui.in_use)
1866 {
1867 char buf[20];
1868
1869 /* use internal GUI code */
1870 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
1871 OUT_STR(buf);
1872 }
1873 else
1874#endif
1875 {
1876 if (screen_attr > HL_ALL) /* special HL attr. */
1877 {
1878 attrentry_T *aep;
1879
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001880 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 {
1882 /*
1883 * Assume that t_me restores the original colors!
1884 */
1885 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001886 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001887#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001888 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
1889 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
1890 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001891#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001892 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001893#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001894 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
1895 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
1896 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001897#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001898 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 do_ME = TRUE;
1900 }
1901 else
1902 {
1903 aep = syn_term_attr2entry(screen_attr);
1904 if (aep != NULL && aep->ae_u.term.stop != NULL)
1905 {
1906 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
1907 do_ME = TRUE;
1908 else
1909 out_str(aep->ae_u.term.stop);
1910 }
1911 }
1912 if (aep == NULL) /* did ":syntax clear" */
1913 screen_attr = 0;
1914 else
1915 screen_attr = aep->ae_attr;
1916 }
1917
1918 /*
1919 * Often all ending-codes are equal to T_ME. Avoid outputting the
1920 * same sequence several times.
1921 */
1922 if (screen_attr & HL_STANDOUT)
1923 {
1924 if (STRCMP(T_SE, T_ME) == 0)
1925 do_ME = TRUE;
1926 else
1927 out_str(T_SE);
1928 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01001929 if ((screen_attr & HL_UNDERCURL) && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01001930 {
1931 if (STRCMP(T_UCE, T_ME) == 0)
1932 do_ME = TRUE;
1933 else
1934 out_str(T_UCE);
1935 }
1936 if ((screen_attr & HL_UNDERLINE)
Bram Moolenaar45a00002017-12-22 21:12:34 +01001937 || ((screen_attr & HL_UNDERCURL) && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 {
1939 if (STRCMP(T_UE, T_ME) == 0)
1940 do_ME = TRUE;
1941 else
1942 out_str(T_UE);
1943 }
1944 if (screen_attr & HL_ITALIC)
1945 {
1946 if (STRCMP(T_CZR, T_ME) == 0)
1947 do_ME = TRUE;
1948 else
1949 out_str(T_CZR);
1950 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02001951 if (screen_attr & HL_STRIKETHROUGH)
1952 {
1953 if (STRCMP(T_STE, T_ME) == 0)
1954 do_ME = TRUE;
1955 else
1956 out_str(T_STE);
1957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
1959 out_str(T_ME);
1960
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001961#ifdef FEAT_TERMGUICOLORS
1962 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001964 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001965 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001966 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001967 term_bg_rgb_color(cterm_normal_bg_gui_color);
1968 }
1969 else
1970#endif
1971 {
1972 if (t_colors > 1)
1973 {
1974 /* set Normal cterm colors */
1975 if (cterm_normal_fg_color != 0)
1976 term_fg_color(cterm_normal_fg_color - 1);
1977 if (cterm_normal_bg_color != 0)
1978 term_bg_color(cterm_normal_bg_color - 1);
1979 if (cterm_normal_fg_bold)
1980 out_str(T_MD);
1981 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 }
1983 }
1984 }
1985 screen_attr = 0;
1986}
1987
1988/*
1989 * Reset the colors for a cterm. Used when leaving Vim.
1990 * The machine specific code may override this again.
1991 */
1992 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001993reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001995 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 {
1997 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001998#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001999 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
2000 || cterm_normal_bg_gui_color != INVALCOLOR)
2001 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002002#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002004#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 {
2006 out_str(T_OP);
2007 screen_attr = -1;
2008 }
2009 if (cterm_normal_fg_bold)
2010 {
2011 out_str(T_ME);
2012 screen_attr = -1;
2013 }
2014 }
2015}
2016
2017/*
2018 * Put character ScreenLines["off"] on the screen at position "row" and "col",
2019 * using the attributes from ScreenAttrs["off"].
2020 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002021 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002022screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023{
2024 int attr;
2025
2026 /* Check for illegal values, just in case (could happen just after
2027 * resizing). */
2028 if (row >= screen_Rows || col >= screen_Columns)
2029 return;
2030
Bram Moolenaar33796b32019-06-08 16:01:13 +02002031 // Skip if under the popup menu.
2032 // Popup windows with zindex higher than POPUPMENU_ZINDEX go on top.
2033 if (pum_under_menu(row, col)
Bram Moolenaare2c453d2019-08-21 14:37:09 +02002034#ifdef FEAT_TEXT_PROP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002035 && screen_zindex <= POPUPMENU_ZINDEX
Bram Moolenaare2c453d2019-08-21 14:37:09 +02002036#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02002037 )
Bram Moolenaarae654382019-01-17 21:09:05 +01002038 return;
Bram Moolenaar33796b32019-06-08 16:01:13 +02002039#ifdef FEAT_TEXT_PROP
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002040 if (blocked_by_popup(row, col))
Bram Moolenaar33796b32019-06-08 16:01:13 +02002041 return;
2042#endif
2043
Bram Moolenaar494838a2015-02-10 19:20:37 +01002044 /* Outputting a character in the last cell on the screen may scroll the
2045 * screen up. Only do it when the "xn" termcap property is set, otherwise
2046 * mark the character invalid (update it when scrolled up). */
2047 if (*T_XN == NUL
2048 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049#ifdef FEAT_RIGHTLEFT
2050 /* account for first command-line character in rightleft mode */
2051 && !cmdmsg_rl
2052#endif
2053 )
2054 {
2055 ScreenAttrs[off] = (sattr_T)-1;
2056 return;
2057 }
2058
2059 /*
2060 * Stop highlighting first, so it's easier to move the cursor.
2061 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 if (screen_char_attr != 0)
2063 attr = screen_char_attr;
2064 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 attr = ScreenAttrs[off];
2066 if (screen_attr != attr)
2067 screen_stop_highlight();
2068
2069 windgoto(row, col);
2070
2071 if (screen_attr != attr)
2072 screen_start_highlight(attr);
2073
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 if (enc_utf8 && ScreenLinesUC[off] != 0)
2075 {
2076 char_u buf[MB_MAXBYTES + 1];
2077
Bram Moolenaarcb070082016-04-02 22:14:51 +02002078 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002079 {
2080 if (*p_ambw == 'd'
Bram Moolenaara12a1612019-01-24 16:39:02 +01002081#ifdef FEAT_GUI
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002082 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01002083#endif
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002084 )
2085 {
2086 /* Clear the two screen cells. If the character is actually
2087 * single width it won't change the second cell. */
2088 out_str((char_u *)" ");
2089 term_windgoto(row, col);
2090 }
2091 /* not sure where the cursor is after drawing the ambiguous width
2092 * character */
Bram Moolenaarcb070082016-04-02 22:14:51 +02002093 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002094 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02002095 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002097
2098 /* Convert the UTF-8 character to bytes and write it. */
2099 buf[utfc_char2bytes(off, buf)] = NUL;
2100 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 }
2102 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 /* double-byte character in single-width cell */
2107 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2108 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 }
2110
2111 screen_cur_col++;
2112}
2113
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114/*
2115 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
2116 * on the screen at position 'row' and 'col'.
2117 * The attributes of the first byte is used for all. This is required to
2118 * output the two bytes of a double-byte character with nothing in between.
2119 */
2120 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002121screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122{
2123 /* Check for illegal values (could be wrong when screen was resized). */
2124 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
2125 return;
2126
2127 /* Outputting the last character on the screen may scrollup the screen.
2128 * Don't to it! Mark the character invalid (update it when scrolled up) */
2129 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
2130 {
2131 ScreenAttrs[off] = (sattr_T)-1;
2132 return;
2133 }
2134
2135 /* Output the first byte normally (positions the cursor), then write the
2136 * second byte directly. */
2137 screen_char(off, row, col);
2138 out_char(ScreenLines[off + 1]);
2139 ++screen_cur_col;
2140}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142/*
2143 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
2144 * This uses the contents of ScreenLines[] and doesn't change it.
2145 */
2146 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002147screen_draw_rectangle(
2148 int row,
2149 int col,
2150 int height,
2151 int width,
2152 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153{
2154 int r, c;
2155 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00002156 int max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002158 /* Can't use ScreenLines unless initialized */
2159 if (ScreenLines == NULL)
2160 return;
2161
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 if (invert)
2163 screen_char_attr = HL_INVERSE;
2164 for (r = row; r < row + height; ++r)
2165 {
2166 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00002167 max_off = off + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 for (c = col; c < col + width; ++c)
2169 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00002170 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 {
2172 screen_char_2(off + c, r, c);
2173 ++c;
2174 }
2175 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 {
2177 screen_char(off + c, r, c);
Bram Moolenaar367329b2007-08-30 11:53:22 +00002178 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 ++c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 }
2181 }
2182 }
2183 screen_char_attr = 0;
2184}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186/*
2187 * Redraw the characters for a vertically split window.
2188 */
2189 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002190redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191{
2192 int col;
2193 int width;
2194
2195# ifdef FEAT_CLIPBOARD
2196 clip_may_clear_selection(row, end - 1);
2197# endif
2198
2199 if (wp == NULL)
2200 {
2201 col = 0;
2202 width = Columns;
2203 }
2204 else
2205 {
2206 col = wp->w_wincol;
2207 width = wp->w_width;
2208 }
2209 screen_draw_rectangle(row, col, end - row, width, FALSE);
2210}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002212 void
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002213space_to_screenline(int off, int attr)
2214{
2215 ScreenLines[off] = ' ';
2216 ScreenAttrs[off] = attr;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002217 if (enc_utf8)
2218 ScreenLinesUC[off] = 0;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002219}
2220
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221/*
2222 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
2223 * with character 'c1' in first column followed by 'c2' in the other columns.
2224 * Use attributes 'attr'.
2225 */
2226 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002227screen_fill(
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002228 int start_row,
2229 int end_row,
2230 int start_col,
2231 int end_col,
2232 int c1,
2233 int c2,
2234 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002236 int row;
2237 int col;
2238 int off;
2239 int end_off;
2240 int did_delete;
2241 int c;
2242 int norm_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002244 int force_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245#endif
2246
2247 if (end_row > screen_Rows) /* safety check */
2248 end_row = screen_Rows;
2249 if (end_col > screen_Columns) /* safety check */
2250 end_col = screen_Columns;
2251 if (ScreenLines == NULL
2252 || start_row >= end_row
2253 || start_col >= end_col) /* nothing to do */
2254 return;
2255
2256 /* it's a "normal" terminal when not in a GUI or cterm */
2257 norm_term = (
2258#ifdef FEAT_GUI
2259 !gui.in_use &&
2260#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002261 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 for (row = start_row; row < end_row; ++row)
2263 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00002264 if (has_mbyte
Bram Moolenaara12a1612019-01-24 16:39:02 +01002265#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00002266 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01002267#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00002268 )
2269 {
2270 /* When drawing over the right halve of a double-wide char clear
2271 * out the left halve. When drawing over the left halve of a
2272 * double wide-char clear out the right halve. Only needed in a
2273 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00002274 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00002275 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00002276 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00002277 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00002278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 /*
2280 * Try to use delete-line termcap code, when no attributes or in a
2281 * "normal" terminal, where a bold/italic space is just a
2282 * space.
2283 */
2284 did_delete = FALSE;
2285 if (c2 == ' '
2286 && end_col == Columns
2287 && can_clear(T_CE)
2288 && (attr == 0
2289 || (norm_term
2290 && attr <= HL_ALL
2291 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
2292 {
2293 /*
2294 * check if we really need to clear something
2295 */
2296 col = start_col;
2297 if (c1 != ' ') /* don't clear first char */
2298 ++col;
2299
2300 off = LineOffset[row] + col;
2301 end_off = LineOffset[row] + end_col;
2302
2303 /* skip blanks (used often, keep it fast!) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 if (enc_utf8)
2305 while (off < end_off && ScreenLines[off] == ' '
2306 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
2307 ++off;
2308 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 while (off < end_off && ScreenLines[off] == ' '
2310 && ScreenAttrs[off] == 0)
2311 ++off;
2312 if (off < end_off) /* something to be cleared */
2313 {
2314 col = off - LineOffset[row];
2315 screen_stop_highlight();
2316 term_windgoto(row, col);/* clear rest of this screen line */
2317 out_str(T_CE);
2318 screen_start(); /* don't know where cursor is now */
2319 col = end_col - col;
2320 while (col--) /* clear chars in ScreenLines */
2321 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002322 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 ++off;
2324 }
2325 }
2326 did_delete = TRUE; /* the chars are cleared now */
2327 }
2328
2329 off = LineOffset[row] + start_col;
2330 c = c1;
2331 for (col = start_col; col < end_col; ++col)
2332 {
Bram Moolenaar33796b32019-06-08 16:01:13 +02002333 if ((ScreenLines[off] != c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002334 || (enc_utf8 && (int)ScreenLinesUC[off]
2335 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 || ScreenAttrs[off] != attr
2337#if defined(FEAT_GUI) || defined(UNIX)
2338 || force_next
2339#endif
2340 )
Bram Moolenaar33796b32019-06-08 16:01:13 +02002341#ifdef FEAT_TEXT_PROP
2342 // Skip if under a(nother) popup.
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002343 && !blocked_by_popup(row, col)
Bram Moolenaar33796b32019-06-08 16:01:13 +02002344#endif
2345 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 {
2347#if defined(FEAT_GUI) || defined(UNIX)
2348 /* The bold trick may make a single row of pixels appear in
2349 * the next character. When a bold character is removed, the
2350 * next character should be redrawn too. This happens for our
2351 * own GUI and for some xterms. */
2352 if (
2353# ifdef FEAT_GUI
2354 gui.in_use
2355# endif
2356# if defined(FEAT_GUI) && defined(UNIX)
2357 ||
2358# endif
2359# ifdef UNIX
2360 term_is_xterm
2361# endif
2362 )
2363 {
2364 if (ScreenLines[off] != ' '
2365 && (ScreenAttrs[off] > HL_ALL
2366 || ScreenAttrs[off] & HL_BOLD))
2367 force_next = TRUE;
2368 else
2369 force_next = FALSE;
2370 }
2371#endif
2372 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 if (enc_utf8)
2374 {
2375 if (c >= 0x80)
2376 {
2377 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002378 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 }
2380 else
2381 ScreenLinesUC[off] = 0;
2382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 ScreenAttrs[off] = attr;
2384 if (!did_delete || c != ' ')
2385 screen_char(off, row, col);
2386 }
2387 ++off;
2388 if (col == start_col)
2389 {
2390 if (did_delete)
2391 break;
2392 c = c2;
2393 }
2394 }
2395 if (end_col == Columns)
2396 LineWraps[row] = FALSE;
2397 if (row == Rows - 1) /* overwritten the command line */
2398 {
2399 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02002400 if (start_col == 0 && end_col == Columns
2401 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002403 if (start_col == 0)
2404 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 }
2406 }
2407}
2408
2409/*
2410 * Check if there should be a delay. Used before clearing or redrawing the
2411 * screen or the command line.
2412 */
2413 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002414check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415{
2416 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
2417 && !did_wait_return
2418 && emsg_silent == 0)
2419 {
2420 out_flush();
Bram Moolenaareda1da02019-11-17 17:06:33 +01002421 ui_delay(1006L, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 emsg_on_display = FALSE;
2423 if (check_msg_scroll)
2424 msg_scroll = FALSE;
2425 }
2426}
2427
2428/*
Bram Moolenaarca57ab52019-04-13 14:53:16 +02002429 * Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect.
2430 */
2431 static void
2432clear_TabPageIdxs(void)
2433{
2434 int scol;
2435
2436 for (scol = 0; scol < Columns; ++scol)
2437 TabPageIdxs[scol] = 0;
2438}
2439
2440/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002442 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 * Returns TRUE if there is a valid screen to write to.
2444 * Returns FALSE when starting up and screen not initialized yet.
2445 */
2446 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002447screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002449 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 return (ScreenLines != NULL);
2451}
2452
2453/*
2454 * Resize the shell to Rows and Columns.
2455 * Allocate ScreenLines[] and associated items.
2456 *
2457 * There may be some time between setting Rows and Columns and (re)allocating
2458 * ScreenLines[]. This happens when starting up and when (manually) changing
2459 * the shell size. Always use screen_Rows and screen_Columns to access items
2460 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
2461 * final size of the shell is needed.
2462 */
2463 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002464screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465{
2466 int new_row, old_row;
2467#ifdef FEAT_GUI
2468 int old_Rows;
2469#endif
2470 win_T *wp;
2471 int outofmem = FALSE;
2472 int len;
2473 schar_T *new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002475 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002477 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 sattr_T *new_ScreenAttrs;
2479 unsigned *new_LineOffset;
2480 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002481 short *new_TabPageIdxs;
Bram Moolenaar33796b32019-06-08 16:01:13 +02002482#ifdef FEAT_TEXT_PROP
2483 short *new_popup_mask;
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002484 short *new_popup_mask_next;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002485 char *new_popup_transparent;
Bram Moolenaar33796b32019-06-08 16:01:13 +02002486#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00002487 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002489 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00002490 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491
Bram Moolenaar87e817c2009-02-22 20:13:39 +00002492retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 /*
2494 * Allocation of the screen buffers is done only when the size changes and
2495 * when Rows and Columns have been set and we have started doing full
2496 * screen stuff.
2497 */
2498 if ((ScreenLines != NULL
2499 && Rows == screen_Rows
2500 && Columns == screen_Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 && enc_utf8 == (ScreenLinesUC != NULL)
2502 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaara12a1612019-01-24 16:39:02 +01002503 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 || Rows == 0
2505 || Columns == 0
2506 || (!full_screen && ScreenLines == NULL))
2507 return;
2508
2509 /*
2510 * It's possible that we produce an out-of-memory message below, which
2511 * will cause this function to be called again. To break the loop, just
2512 * return here.
2513 */
2514 if (entered)
2515 return;
2516 entered = TRUE;
2517
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00002518 /*
2519 * Note that the window sizes are updated before reallocating the arrays,
2520 * thus we must not redraw here!
2521 */
2522 ++RedrawingDisabled;
2523
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 win_new_shellsize(); /* fit the windows in the new sized shell */
2525
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 comp_col(); /* recompute columns for shown command and ruler */
2527
2528 /*
2529 * We're changing the size of the screen.
2530 * - Allocate new arrays for ScreenLines and ScreenAttrs.
2531 * - Move lines from the old arrays into the new arrays, clear extra
2532 * lines (unless the screen is going to be cleared).
2533 * - Free the old arrays.
2534 *
2535 * If anything fails, make ScreenLines NULL, so we don't do anything!
2536 * Continuing with the old ScreenLines may result in a crash, because the
2537 * size is wrong.
2538 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002539 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00002541 if (aucmd_win != NULL)
2542 win_free_lsize(aucmd_win);
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002543#ifdef FEAT_TEXT_PROP
2544 // global popup windows
2545 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2546 win_free_lsize(wp);
2547 // tab-local popup windows
2548 FOR_ALL_TABPAGES(tp)
2549 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2550 win_free_lsize(wp);
2551#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002553 new_ScreenLines = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
Bram Moolenaar216b7102010-03-23 13:56:59 +01002554 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 if (enc_utf8)
2556 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002557 new_ScreenLinesUC = LALLOC_MULT(u8char_T, (Rows + 1) * Columns);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002558 for (i = 0; i < p_mco; ++i)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002559 new_ScreenLinesC[i] = LALLOC_CLEAR_MULT(u8char_T,
2560 (Rows + 1) * Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 }
2562 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002563 new_ScreenLines2 = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
2564 new_ScreenAttrs = LALLOC_MULT(sattr_T, (Rows + 1) * Columns);
2565 new_LineOffset = LALLOC_MULT(unsigned, Rows);
2566 new_LineWraps = LALLOC_MULT(char_u, Rows);
2567 new_TabPageIdxs = LALLOC_MULT(short, Columns);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002568#ifdef FEAT_TEXT_PROP
2569 new_popup_mask = LALLOC_MULT(short, Rows * Columns);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002570 new_popup_mask_next = LALLOC_MULT(short, Rows * Columns);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002571 new_popup_transparent = LALLOC_MULT(char, Rows * Columns);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002572#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00002574 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 {
2576 if (win_alloc_lines(wp) == FAIL)
2577 {
2578 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00002579 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 }
2581 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00002582 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
2583 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002584 outofmem = TRUE;
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002585#ifdef FEAT_TEXT_PROP
2586 // global popup windows
2587 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2588 if (win_alloc_lines(wp) == FAIL)
2589 {
2590 outofmem = TRUE;
2591 goto give_up;
2592 }
2593 // tab-local popup windows
2594 FOR_ALL_TABPAGES(tp)
2595 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2596 if (win_alloc_lines(wp) == FAIL)
2597 {
2598 outofmem = TRUE;
2599 goto give_up;
2600 }
2601#endif
2602
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00002603give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002605 for (i = 0; i < p_mco; ++i)
2606 if (new_ScreenLinesC[i] == NULL)
2607 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 if (new_ScreenLines == NULL
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002609 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 || new_ScreenAttrs == NULL
2612 || new_LineOffset == NULL
2613 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00002614 || new_TabPageIdxs == NULL
Bram Moolenaar33796b32019-06-08 16:01:13 +02002615#ifdef FEAT_TEXT_PROP
2616 || new_popup_mask == NULL
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002617 || new_popup_mask_next == NULL
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002618 || new_popup_transparent == NULL
Bram Moolenaar33796b32019-06-08 16:01:13 +02002619#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 || outofmem)
2621 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002622 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002623 {
2624 /* guess the size */
2625 do_outofmem_msg((long_u)((Rows + 1) * Columns));
2626
2627 /* Remember we did this to avoid getting outofmem messages over
2628 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002629 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002630 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01002631 VIM_CLEAR(new_ScreenLines);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002632 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002633 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01002634 VIM_CLEAR(new_ScreenLinesC[i]);
2635 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002636 VIM_CLEAR(new_ScreenAttrs);
2637 VIM_CLEAR(new_LineOffset);
2638 VIM_CLEAR(new_LineWraps);
2639 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002640#ifdef FEAT_TEXT_PROP
2641 VIM_CLEAR(new_popup_mask);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002642 VIM_CLEAR(new_popup_mask_next);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002643 VIM_CLEAR(new_popup_transparent);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002644#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 }
2646 else
2647 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002648 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002649
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 for (new_row = 0; new_row < Rows; ++new_row)
2651 {
2652 new_LineOffset[new_row] = new_row * Columns;
2653 new_LineWraps[new_row] = FALSE;
2654
2655 /*
2656 * If the screen is not going to be cleared, copy as much as
2657 * possible from the old screen to the new one and clear the rest
2658 * (used when resizing the window at the "--more--" prompt or when
2659 * executing an external command, for the GUI).
2660 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002661 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 {
2663 (void)vim_memset(new_ScreenLines + new_row * Columns,
2664 ' ', (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 if (enc_utf8)
2666 {
2667 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
2668 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002669 for (i = 0; i < p_mco; ++i)
2670 (void)vim_memset(new_ScreenLinesC[i]
2671 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 0, (size_t)Columns * sizeof(u8char_T));
2673 }
2674 if (enc_dbcs == DBCS_JPNU)
2675 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
2676 0, (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
2678 0, (size_t)Columns * sizeof(sattr_T));
2679 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002680 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 {
2682 if (screen_Columns < Columns)
2683 len = screen_Columns;
2684 else
2685 len = Columns;
Bram Moolenaarf4d11452005-12-02 00:46:37 +00002686 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002687 * may be invalid now. Also when p_mco changes. */
2688 if (!(enc_utf8 && ScreenLinesUC == NULL)
2689 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002690 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
2691 ScreenLines + LineOffset[old_row],
2692 (size_t)len * sizeof(schar_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002693 if (enc_utf8 && ScreenLinesUC != NULL
2694 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 {
2696 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
2697 ScreenLinesUC + LineOffset[old_row],
2698 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002699 for (i = 0; i < p_mco; ++i)
2700 mch_memmove(new_ScreenLinesC[i]
2701 + new_LineOffset[new_row],
2702 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 (size_t)len * sizeof(u8char_T));
2704 }
2705 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
2706 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
2707 ScreenLines2 + LineOffset[old_row],
2708 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
2710 ScreenAttrs + LineOffset[old_row],
2711 (size_t)len * sizeof(sattr_T));
2712 }
2713 }
2714 }
2715 /* Use the last line of the screen for the current line. */
2716 current_ScreenLine = new_ScreenLines + Rows * Columns;
Bram Moolenaar6ace95e2019-08-13 23:09:49 +02002717
2718#ifdef FEAT_TEXT_PROP
2719 vim_memset(new_popup_mask, 0, Rows * Columns * sizeof(short));
2720 vim_memset(new_popup_transparent, 0, Rows * Columns * sizeof(char));
2721#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 }
2723
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002724 free_screenlines();
2725
Bram Moolenaar6ace95e2019-08-13 23:09:49 +02002726 // NOTE: this may result in all pointers to become NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 ScreenLines = new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002729 for (i = 0; i < p_mco; ++i)
2730 ScreenLinesC[i] = new_ScreenLinesC[i];
2731 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 ScreenLines2 = new_ScreenLines2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 ScreenAttrs = new_ScreenAttrs;
2734 LineOffset = new_LineOffset;
2735 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002736 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar33796b32019-06-08 16:01:13 +02002737#ifdef FEAT_TEXT_PROP
2738 popup_mask = new_popup_mask;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002739 popup_mask_next = new_popup_mask_next;
2740 popup_transparent = new_popup_transparent;
Bram Moolenaar33796b32019-06-08 16:01:13 +02002741 popup_mask_refresh = TRUE;
2742#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743
2744 /* It's important that screen_Rows and screen_Columns reflect the actual
2745 * size of ScreenLines[]. Set them before calling anything. */
2746#ifdef FEAT_GUI
2747 old_Rows = screen_Rows;
2748#endif
2749 screen_Rows = Rows;
2750 screen_Columns = Columns;
2751
2752 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002753 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 screenclear2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755#ifdef FEAT_GUI
2756 else if (gui.in_use
2757 && !gui.starting
2758 && ScreenLines != NULL
2759 && old_Rows != Rows)
2760 {
2761 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
2762 /*
2763 * Adjust the position of the cursor, for when executing an external
2764 * command.
2765 */
2766 if (msg_row >= Rows) /* Rows got smaller */
2767 msg_row = Rows - 1; /* put cursor at last row */
2768 else if (Rows > old_Rows) /* Rows got bigger */
2769 msg_row += Rows - old_Rows; /* put cursor in same place */
2770 if (msg_col >= Columns) /* Columns got smaller */
2771 msg_col = Columns - 1; /* put cursor at last column */
2772 }
2773#endif
Bram Moolenaarca57ab52019-04-13 14:53:16 +02002774 clear_TabPageIdxs();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00002777 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00002778
Bram Moolenaar87e817c2009-02-22 20:13:39 +00002779 /*
2780 * Do not apply autocommands more than 3 times to avoid an endless loop
2781 * in case applying autocommands always changes Rows or Columns.
2782 */
2783 if (starting == 0 && ++retry_count <= 3)
2784 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00002785 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00002786 /* In rare cases, autocommands may have altered Rows or Columns,
2787 * jump back to check if we need to allocate the screen again. */
2788 goto retry;
2789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790}
2791
2792 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002793free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002794{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002795 int i;
2796
Bram Moolenaar33796b32019-06-08 16:01:13 +02002797 VIM_CLEAR(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002798 for (i = 0; i < Screen_mco; ++i)
Bram Moolenaar33796b32019-06-08 16:01:13 +02002799 VIM_CLEAR(ScreenLinesC[i]);
2800 VIM_CLEAR(ScreenLines2);
2801 VIM_CLEAR(ScreenLines);
2802 VIM_CLEAR(ScreenAttrs);
2803 VIM_CLEAR(LineOffset);
2804 VIM_CLEAR(LineWraps);
2805 VIM_CLEAR(TabPageIdxs);
2806#ifdef FEAT_TEXT_PROP
2807 VIM_CLEAR(popup_mask);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002808 VIM_CLEAR(popup_mask_next);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002809 VIM_CLEAR(popup_transparent);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002810#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002811}
2812
2813 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002814screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815{
2816 check_for_delay(FALSE);
2817 screenalloc(FALSE); /* allocate screen buffers if size changed */
2818 screenclear2(); /* clear the screen */
2819}
2820
2821 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002822screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823{
2824 int i;
2825
2826 if (starting == NO_SCREEN || ScreenLines == NULL
2827#ifdef FEAT_GUI
2828 || (gui.in_use && gui.starting)
2829#endif
2830 )
2831 return;
2832
2833#ifdef FEAT_GUI
2834 if (!gui.in_use)
2835#endif
2836 screen_attr = -1; /* force setting the Normal colors */
2837 screen_stop_highlight(); /* don't want highlighting here */
2838
2839#ifdef FEAT_CLIPBOARD
2840 /* disable selection without redrawing it */
2841 clip_scroll_selection(9999);
2842#endif
2843
2844 /* blank out ScreenLines */
2845 for (i = 0; i < Rows; ++i)
2846 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002847 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 LineWraps[i] = FALSE;
2849 }
2850
2851 if (can_clear(T_CL))
2852 {
2853 out_str(T_CL); /* clear the display */
2854 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002855 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 }
2857 else
2858 {
2859 /* can't clear the screen, mark all chars with invalid attributes */
2860 for (i = 0; i < Rows; ++i)
2861 lineinvalid(LineOffset[i], (int)Columns);
2862 clear_cmdline = TRUE;
2863 }
2864
2865 screen_cleared = TRUE; /* can use contents of ScreenLines now */
2866
2867 win_rest_invalid(firstwin);
2868 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002869 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 if (must_redraw == CLEAR) /* no need to clear again */
2871 must_redraw = NOT_VALID;
2872 compute_cmdrow();
2873 msg_row = cmdline_row; /* put cursor on last line for messages */
2874 msg_col = 0;
2875 screen_start(); /* don't know where cursor is now */
2876 msg_scrolled = 0; /* can't scroll back */
2877 msg_didany = FALSE;
2878 msg_didout = FALSE;
2879}
2880
2881/*
2882 * Clear one line in ScreenLines.
2883 */
2884 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002885lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886{
2887 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 if (enc_utf8)
2889 (void)vim_memset(ScreenLinesUC + off, 0,
2890 (size_t)width * sizeof(u8char_T));
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002891 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892}
2893
2894/*
2895 * Mark one line in ScreenLines invalid by setting the attributes to an
2896 * invalid value.
2897 */
2898 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002899lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900{
2901 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
2902}
2903
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904/*
2905 * Copy part of a Screenline for vertically split window "wp".
2906 */
2907 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002908linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909{
2910 unsigned off_to = LineOffset[to] + wp->w_wincol;
2911 unsigned off_from = LineOffset[from] + wp->w_wincol;
2912
2913 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
2914 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 if (enc_utf8)
2916 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002917 int i;
2918
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
2920 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002921 for (i = 0; i < p_mco; ++i)
2922 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
2923 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 }
2925 if (enc_dbcs == DBCS_JPNU)
2926 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
2927 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
2929 wp->w_width * sizeof(sattr_T));
2930}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931
2932/*
2933 * Return TRUE if clearing with term string "p" would work.
2934 * It can't work when the string is empty or it won't set the right background.
Bram Moolenaar33796b32019-06-08 16:01:13 +02002935 * Don't clear to end-of-line when there are popups, it may cause flicker.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 */
2937 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002938can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939{
2940 return (*p != NUL && (t_colors <= 1
2941#ifdef FEAT_GUI
2942 || gui.in_use
2943#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02002944#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002945 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02002946 || (!p_tgc && cterm_normal_bg_color == 0)
2947#else
2948 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002949#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02002950 || *T_UT != NUL)
2951#ifdef FEAT_TEXT_PROP
2952 && !(p == T_CE && popup_visible)
2953#endif
2954 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955}
2956
2957/*
2958 * Reset cursor position. Use whenever cursor was moved because of outputting
2959 * something directly to the screen (shell commands) or a terminal control
2960 * code.
2961 */
2962 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002963screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964{
2965 screen_cur_row = screen_cur_col = 9999;
2966}
2967
2968/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 * Move the cursor to position "row","col" in the screen.
2970 * This tries to find the most efficient way to move, minimizing the number of
2971 * characters sent to the terminal.
2972 */
2973 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002974windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00002976 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 int i;
2978 int plan;
2979 int cost;
2980 int wouldbe_col;
2981 int noinvcurs;
2982 char_u *bs;
2983 int goto_cost;
2984 int attr;
2985
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00002986#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
2988
2989#define PLAN_LE 1
2990#define PLAN_CR 2
2991#define PLAN_NL 3
2992#define PLAN_WRITE 4
2993 /* Can't use ScreenLines unless initialized */
2994 if (ScreenLines == NULL)
2995 return;
2996
2997 if (col != screen_cur_col || row != screen_cur_row)
2998 {
2999 /* Check for valid position. */
3000 if (row < 0) /* window without text lines? */
3001 row = 0;
3002 if (row >= screen_Rows)
3003 row = screen_Rows - 1;
3004 if (col >= screen_Columns)
3005 col = screen_Columns - 1;
3006
3007 /* check if no cursor movement is allowed in highlight mode */
3008 if (screen_attr && *T_MS == NUL)
3009 noinvcurs = HIGHL_COST;
3010 else
3011 noinvcurs = 0;
3012 goto_cost = GOTO_COST + noinvcurs;
3013
3014 /*
3015 * Plan how to do the positioning:
3016 * 1. Use CR to move it to column 0, same row.
3017 * 2. Use T_LE to move it a few columns to the left.
3018 * 3. Use NL to move a few lines down, column 0.
3019 * 4. Move a few columns to the right with T_ND or by writing chars.
3020 *
3021 * Don't do this if the cursor went beyond the last column, the cursor
3022 * position is unknown then (some terminals wrap, some don't )
3023 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00003024 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 * characters to move the cursor to the right.
3026 */
3027 if (row >= screen_cur_row && screen_cur_col < Columns)
3028 {
3029 /*
3030 * If the cursor is in the same row, bigger col, we can use CR
3031 * or T_LE.
3032 */
3033 bs = NULL; /* init for GCC */
3034 attr = screen_attr;
3035 if (row == screen_cur_row && col < screen_cur_col)
3036 {
3037 /* "le" is preferred over "bc", because "bc" is obsolete */
3038 if (*T_LE)
3039 bs = T_LE; /* "cursor left" */
3040 else
3041 bs = T_BC; /* "backspace character (old) */
3042 if (*bs)
3043 cost = (screen_cur_col - col) * (int)STRLEN(bs);
3044 else
3045 cost = 999;
3046 if (col + 1 < cost) /* using CR is less characters */
3047 {
3048 plan = PLAN_CR;
3049 wouldbe_col = 0;
3050 cost = 1; /* CR is just one character */
3051 }
3052 else
3053 {
3054 plan = PLAN_LE;
3055 wouldbe_col = col;
3056 }
3057 if (noinvcurs) /* will stop highlighting */
3058 {
3059 cost += noinvcurs;
3060 attr = 0;
3061 }
3062 }
3063
3064 /*
3065 * If the cursor is above where we want to be, we can use CR LF.
3066 */
3067 else if (row > screen_cur_row)
3068 {
3069 plan = PLAN_NL;
3070 wouldbe_col = 0;
3071 cost = (row - screen_cur_row) * 2; /* CR LF */
3072 if (noinvcurs) /* will stop highlighting */
3073 {
3074 cost += noinvcurs;
3075 attr = 0;
3076 }
3077 }
3078
3079 /*
3080 * If the cursor is in the same row, smaller col, just use write.
3081 */
3082 else
3083 {
3084 plan = PLAN_WRITE;
3085 wouldbe_col = screen_cur_col;
3086 cost = 0;
3087 }
3088
3089 /*
3090 * Check if any characters that need to be written have the
3091 * correct attributes. Also avoid UTF-8 characters.
3092 */
3093 i = col - wouldbe_col;
3094 if (i > 0)
3095 cost += i;
3096 if (cost < goto_cost && i > 0)
3097 {
3098 /*
3099 * Check if the attributes are correct without additionally
3100 * stopping highlighting.
3101 */
3102 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
3103 while (i && *p++ == attr)
3104 --i;
3105 if (i != 0)
3106 {
3107 /*
3108 * Try if it works when highlighting is stopped here.
3109 */
3110 if (*--p == 0)
3111 {
3112 cost += noinvcurs;
3113 while (i && *p++ == 0)
3114 --i;
3115 }
3116 if (i != 0)
3117 cost = 999; /* different attributes, don't do it */
3118 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 if (enc_utf8)
3120 {
3121 /* Don't use an UTF-8 char for positioning, it's slow. */
3122 for (i = wouldbe_col; i < col; ++i)
3123 if (ScreenLinesUC[LineOffset[row] + i] != 0)
3124 {
3125 cost = 999;
3126 break;
3127 }
3128 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 }
3130
3131 /*
3132 * We can do it without term_windgoto()!
3133 */
3134 if (cost < goto_cost)
3135 {
3136 if (plan == PLAN_LE)
3137 {
3138 if (noinvcurs)
3139 screen_stop_highlight();
3140 while (screen_cur_col > col)
3141 {
3142 out_str(bs);
3143 --screen_cur_col;
3144 }
3145 }
3146 else if (plan == PLAN_CR)
3147 {
3148 if (noinvcurs)
3149 screen_stop_highlight();
3150 out_char('\r');
3151 screen_cur_col = 0;
3152 }
3153 else if (plan == PLAN_NL)
3154 {
3155 if (noinvcurs)
3156 screen_stop_highlight();
3157 while (screen_cur_row < row)
3158 {
3159 out_char('\n');
3160 ++screen_cur_row;
3161 }
3162 screen_cur_col = 0;
3163 }
3164
3165 i = col - screen_cur_col;
3166 if (i > 0)
3167 {
3168 /*
3169 * Use cursor-right if it's one character only. Avoids
3170 * removing a line of pixels from the last bold char, when
3171 * using the bold trick in the GUI.
3172 */
3173 if (T_ND[0] != NUL && T_ND[1] == NUL)
3174 {
3175 while (i-- > 0)
3176 out_char(*T_ND);
3177 }
3178 else
3179 {
3180 int off;
3181
3182 off = LineOffset[row] + screen_cur_col;
3183 while (i-- > 0)
3184 {
3185 if (ScreenAttrs[off] != screen_attr)
3186 screen_stop_highlight();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 if (enc_dbcs == DBCS_JPNU
3190 && ScreenLines[off] == 0x8e)
3191 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 ++off;
3193 }
3194 }
3195 }
3196 }
3197 }
3198 else
3199 cost = 999;
3200
3201 if (cost >= goto_cost)
3202 {
3203 if (noinvcurs)
3204 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02003205 if (row == screen_cur_row && (col > screen_cur_col)
3206 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 term_cursor_right(col - screen_cur_col);
3208 else
3209 term_windgoto(row, col);
3210 }
3211 screen_cur_row = row;
3212 screen_cur_col = col;
3213 }
3214}
3215
3216/*
3217 * Set cursor to its position in the current window.
3218 */
3219 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003220setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221{
Bram Moolenaar987723e2018-03-06 11:43:04 +01003222 setcursor_mayforce(FALSE);
3223}
3224
3225/*
3226 * Set cursor to its position in the current window.
3227 * When "force" is TRUE also when not redrawing.
3228 */
3229 void
3230setcursor_mayforce(int force)
3231{
3232 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 {
3234 validate_cursor();
3235 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02003236 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00003238 /* With 'rightleft' set and the cursor on a double-wide
3239 * character, position it on the leftmost column. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01003240 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol
3241 - ((has_mbyte
Bram Moolenaar561f9db2008-02-20 13:16:29 +00003242 && (*mb_ptr2cells)(ml_get_cursor()) == 2
Bram Moolenaara12a1612019-01-24 16:39:02 +01003243 && vim_isprintc(gchar_cursor())) ? 2 : 1)) :
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244#endif
3245 curwin->w_wcol));
3246 }
3247}
3248
3249
3250/*
Bram Moolenaar86033562017-07-12 20:24:41 +02003251 * Insert 'line_count' lines at 'row' in window 'wp'.
3252 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
3253 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 * scrolling.
3255 * Returns FAIL if the lines are not inserted, OK for success.
3256 */
3257 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003258win_ins_lines(
3259 win_T *wp,
3260 int row,
3261 int line_count,
3262 int invalid,
3263 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264{
3265 int did_delete;
3266 int nextrow;
3267 int lastrow;
3268 int retval;
3269
3270 if (invalid)
3271 wp->w_lines_valid = 0;
3272
3273 if (wp->w_height < 5)
3274 return FAIL;
3275
3276 if (line_count > wp->w_height - row)
3277 line_count = wp->w_height - row;
3278
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003279 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 if (retval != MAYBE)
3281 return retval;
3282
3283 /*
3284 * If there is a next window or a status line, we first try to delete the
3285 * lines at the bottom to avoid messing what is after the window.
Bram Moolenaarc363fe12019-08-04 18:13:46 +02003286 * If this fails and there are following windows, don't do anything to
3287 * avoid messing up those windows, better just redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 */
3289 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 if (wp->w_next != NULL || wp->w_status_height)
3291 {
3292 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003293 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 did_delete = TRUE;
3295 else if (wp->w_next)
3296 return FAIL;
3297 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 /*
3299 * if no lines deleted, blank the lines that will end up below the window
3300 */
3301 if (!did_delete)
3302 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003305 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 lastrow = nextrow + line_count;
3307 if (lastrow > Rows)
3308 lastrow = Rows;
3309 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02003310 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 ' ', ' ', 0);
3312 }
3313
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003314 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 == FAIL)
3316 {
Bram Moolenaarc363fe12019-08-04 18:13:46 +02003317 // deletion will have messed up other windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 if (did_delete)
3319 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 win_rest_invalid(W_NEXT(wp));
3322 }
3323 return FAIL;
3324 }
3325
3326 return OK;
3327}
3328
3329/*
Bram Moolenaar86033562017-07-12 20:24:41 +02003330 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
3332 * If "mayclear" is TRUE the screen will be cleared if it is faster than
3333 * scrolling
3334 * Return OK for success, FAIL if the lines are not deleted.
3335 */
3336 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003337win_del_lines(
3338 win_T *wp,
3339 int row,
3340 int line_count,
3341 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003342 int mayclear,
3343 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344{
3345 int retval;
3346
3347 if (invalid)
3348 wp->w_lines_valid = 0;
3349
3350 if (line_count > wp->w_height - row)
3351 line_count = wp->w_height - row;
3352
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003353 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 if (retval != MAYBE)
3355 return retval;
3356
3357 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003358 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 return FAIL;
3360
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 /*
3362 * If there are windows or status lines below, try to put them at the
3363 * correct place. If we can't do that, they have to be redrawn.
3364 */
3365 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
3366 {
3367 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003368 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 {
3370 wp->w_redr_status = TRUE;
3371 win_rest_invalid(wp->w_next);
3372 }
3373 }
3374 /*
3375 * If this is the last window and there is no status line, redraw the
3376 * command line later.
3377 */
3378 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 redraw_cmdline = TRUE;
3380 return OK;
3381}
3382
3383/*
3384 * Common code for win_ins_lines() and win_del_lines().
3385 * Returns OK or FAIL when the work has been done.
3386 * Returns MAYBE when not finished yet.
3387 */
3388 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003389win_do_lines(
3390 win_T *wp,
3391 int row,
3392 int line_count,
3393 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003394 int del,
3395 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396{
3397 int retval;
3398
3399 if (!redrawing() || line_count <= 0)
3400 return FAIL;
3401
Bram Moolenaar33796b32019-06-08 16:01:13 +02003402 // When inserting lines would result in loss of command output, just redraw
3403 // the lines.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003404 if (no_win_do_lines_ins && !del)
3405 return FAIL;
3406
Bram Moolenaar33796b32019-06-08 16:01:13 +02003407 // only a few lines left: redraw is faster
Bram Moolenaar4033c552017-09-16 20:54:51 +02003408 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003410 if (!no_win_do_lines_ins)
Bram Moolenaar33796b32019-06-08 16:01:13 +02003411 screenclear(); // will set wp->w_lines_valid to 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 return FAIL;
3413 }
3414
Bram Moolenaar33796b32019-06-08 16:01:13 +02003415#ifdef FEAT_TEXT_PROP
Bram Moolenaar4c063a02019-06-10 21:24:12 +02003416 // this doesn't work when there are popups visible
Bram Moolenaar33796b32019-06-08 16:01:13 +02003417 if (popup_visible)
3418 return FAIL;
3419#endif
3420
3421 // Delete all remaining lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 if (row + line_count >= wp->w_height)
3423 {
3424 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02003425 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 ' ', ' ', 0);
3427 return OK;
3428 }
3429
3430 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003431 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003433 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003435 if (!no_win_do_lines_ins)
3436 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437
3438 /*
3439 * If the terminal can set a scroll region, use that.
3440 * Always do this in a vertically split window. This will redraw from
3441 * ScreenLines[] when t_CV isn't defined. That's faster than using
3442 * win_line().
3443 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003444 * a character in the lower right corner of the scroll region may cause a
3445 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 */
Bram Moolenaar02631462017-09-22 15:20:32 +02003447 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 scroll_region_set(wp, row);
3451 if (del)
3452 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003453 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 else
3455 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003456 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 scroll_region_reset();
3459 return retval;
3460 }
3461
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
3463 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464
3465 return MAYBE;
3466}
3467
3468/*
3469 * window 'wp' and everything after it is messed up, mark it for redraw
3470 */
3471 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003472win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 {
3476 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 wp->w_redr_status = TRUE;
3478 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 }
3480 redraw_cmdline = TRUE;
3481}
3482
3483/*
3484 * The rest of the routines in this file perform screen manipulations. The
3485 * given operation is performed physically on the screen. The corresponding
3486 * change is also made to the internal screen image. In this way, the editor
3487 * anticipates the effect of editing changes on the appearance of the screen.
3488 * That way, when we call screenupdate a complete redraw isn't usually
3489 * necessary. Another advantage is that we can keep adding code to anticipate
3490 * screen changes, and in the meantime, everything still works.
3491 */
3492
3493/*
3494 * types for inserting or deleting lines
3495 */
3496#define USE_T_CAL 1
3497#define USE_T_CDL 2
3498#define USE_T_AL 3
3499#define USE_T_CE 4
3500#define USE_T_DL 5
3501#define USE_T_SR 6
3502#define USE_NL 7
3503#define USE_T_CD 8
3504#define USE_REDRAW 9
3505
3506/*
3507 * insert lines on the screen and update ScreenLines[]
3508 * 'end' is the line after the scrolled part. Normally it is Rows.
3509 * When scrolling region used 'off' is the offset from the top for the region.
3510 * 'row' and 'end' are relative to the start of the region.
3511 *
3512 * return FAIL for failure, OK for success.
3513 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003514 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003515screen_ins_lines(
3516 int off,
3517 int row,
3518 int line_count,
3519 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003520 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01003521 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522{
3523 int i;
3524 int j;
3525 unsigned temp;
3526 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003527 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 int type;
3529 int result_empty;
3530 int can_ce = can_clear(T_CE);
3531
3532 /*
3533 * FAIL if
3534 * - there is no valid screen
3535 * - the screen has to be redrawn completely
3536 * - the line count is less than one
3537 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003538 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar33796b32019-06-08 16:01:13 +02003539 * - there is a popup window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 */
Bram Moolenaar33796b32019-06-08 16:01:13 +02003541 if (!screen_valid(TRUE)
3542 || line_count <= 0 || line_count > p_ttyscroll
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003543#ifdef FEAT_CLIPBOARD
3544 || (clip_star.state != SELECT_CLEARED
3545 && redrawing_for_callback > 0)
3546#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02003547#ifdef FEAT_TEXT_PROP
3548 || popup_visible
3549#endif
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003550 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 return FAIL;
3552
3553 /*
3554 * There are seven ways to insert lines:
3555 * 0. When in a vertically split window and t_CV isn't set, redraw the
3556 * characters from ScreenLines[].
3557 * 1. Use T_CD (clear to end of display) if it exists and the result of
3558 * the insert is just empty lines
3559 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
3560 * present or line_count > 1. It looks better if we do all the inserts
3561 * at once.
3562 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
3563 * insert is just empty lines and T_CE is not present or line_count >
3564 * 1.
3565 * 4. Use T_AL (insert line) if it exists.
3566 * 5. Use T_CE (erase line) if it exists and the result of the insert is
3567 * just empty lines.
3568 * 6. Use T_DL (delete line) if it exists and the result of the insert is
3569 * just empty lines.
3570 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
3571 * the 'da' flag is not set or we have clear line capability.
3572 * 8. redraw the characters from ScreenLines[].
3573 *
3574 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
3575 * the scrollbar for the window. It does have insert line, use that if it
3576 * exists.
3577 */
3578 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
3580 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02003581 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 type = USE_T_CD;
3583 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
3584 type = USE_T_CAL;
3585 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
3586 type = USE_T_CDL;
3587 else if (*T_AL != NUL)
3588 type = USE_T_AL;
3589 else if (can_ce && result_empty)
3590 type = USE_T_CE;
3591 else if (*T_DL != NUL && result_empty)
3592 type = USE_T_DL;
3593 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
3594 type = USE_T_SR;
3595 else
3596 return FAIL;
3597
3598 /*
3599 * For clearing the lines screen_del_lines() is used. This will also take
3600 * care of t_db if necessary.
3601 */
3602 if (type == USE_T_CD || type == USE_T_CDL ||
3603 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003604 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605
3606 /*
3607 * If text is retained below the screen, first clear or delete as many
3608 * lines at the bottom of the window as are about to be inserted so that
3609 * the deleted lines won't later surface during a screen_del_lines.
3610 */
3611 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003612 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613
3614#ifdef FEAT_CLIPBOARD
3615 /* Remove a modeless selection when inserting lines halfway the screen
3616 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02003617 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003618 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 else
3620 clip_scroll_selection(-line_count);
3621#endif
3622
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623#ifdef FEAT_GUI
3624 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
3625 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02003626 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627#endif
3628
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003629 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
3630 cursor_col = wp->w_wincol;
3631
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 if (*T_CCS != NUL) /* cursor relative to region */
3633 cursor_row = row;
3634 else
3635 cursor_row = row + off;
3636
3637 /*
3638 * Shift LineOffset[] line_count down to reflect the inserted lines.
3639 * Clear the inserted lines in ScreenLines[].
3640 */
3641 row += off;
3642 end += off;
3643 for (i = 0; i < line_count; ++i)
3644 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 if (wp != NULL && wp->w_width != Columns)
3646 {
3647 /* need to copy part of a line */
3648 j = end - 1 - i;
3649 while ((j -= line_count) >= row)
3650 linecopy(j + line_count, j, wp);
3651 j += line_count;
3652 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003653 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
3654 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 else
3656 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
3657 LineWraps[j] = FALSE;
3658 }
3659 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 {
3661 j = end - 1 - i;
3662 temp = LineOffset[j];
3663 while ((j -= line_count) >= row)
3664 {
3665 LineOffset[j + line_count] = LineOffset[j];
3666 LineWraps[j + line_count] = LineWraps[j];
3667 }
3668 LineOffset[j + line_count] = temp;
3669 LineWraps[j + line_count] = FALSE;
3670 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003671 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 else
3673 lineinvalid(temp, (int)Columns);
3674 }
3675 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676
3677 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003678 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003679 if (clear_attr != 0)
3680 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 /* redraw the characters */
3683 if (type == USE_REDRAW)
3684 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02003685 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 {
3687 term_append_lines(line_count);
3688 screen_start(); /* don't know where cursor is now */
3689 }
3690 else
3691 {
3692 for (i = 0; i < line_count; i++)
3693 {
3694 if (type == USE_T_AL)
3695 {
3696 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003697 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 out_str(T_AL);
3699 }
3700 else /* type == USE_T_SR */
3701 out_str(T_SR);
3702 screen_start(); /* don't know where cursor is now */
3703 }
3704 }
3705
3706 /*
3707 * With scroll-reverse and 'da' flag set we need to clear the lines that
3708 * have been scrolled down into the region.
3709 */
3710 if (type == USE_T_SR && *T_DA)
3711 {
3712 for (i = 0; i < line_count; ++i)
3713 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003714 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 out_str(T_CE);
3716 screen_start(); /* don't know where cursor is now */
3717 }
3718 }
3719
3720#ifdef FEAT_GUI
3721 gui_can_update_cursor();
3722 if (gui.in_use)
3723 out_flush(); /* always flush after a scroll */
3724#endif
3725 return OK;
3726}
3727
3728/*
Bram Moolenaar107abd22016-08-12 14:08:25 +02003729 * Delete lines on the screen and update ScreenLines[].
3730 * "end" is the line after the scrolled part. Normally it is Rows.
3731 * When scrolling region used "off" is the offset from the top for the region.
3732 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 *
3734 * Return OK for success, FAIL if the lines are not deleted.
3735 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003737screen_del_lines(
3738 int off,
3739 int row,
3740 int line_count,
3741 int end,
3742 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003743 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +01003744 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745{
3746 int j;
3747 int i;
3748 unsigned temp;
3749 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003750 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 int cursor_end;
3752 int result_empty; /* result is empty until end of region */
3753 int can_delete; /* deleting line codes can be used */
3754 int type;
3755
3756 /*
3757 * FAIL if
3758 * - there is no valid screen
3759 * - the screen has to be redrawn completely
3760 * - the line count is less than one
3761 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003762 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003764 if (!screen_valid(TRUE) || line_count <= 0
3765 || (!force && line_count > p_ttyscroll)
3766#ifdef FEAT_CLIPBOARD
3767 || (clip_star.state != SELECT_CLEARED
3768 && redrawing_for_callback > 0)
3769#endif
3770 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 return FAIL;
3772
3773 /*
3774 * Check if the rest of the current region will become empty.
3775 */
3776 result_empty = row + line_count >= end;
3777
3778 /*
3779 * We can delete lines only when 'db' flag not set or when 'ce' option
3780 * available.
3781 */
3782 can_delete = (*T_DB == NUL || can_clear(T_CE));
3783
3784 /*
3785 * There are six ways to delete lines:
3786 * 0. When in a vertically split window and t_CV isn't set, redraw the
3787 * characters from ScreenLines[].
3788 * 1. Use T_CD if it exists and the result is empty.
3789 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
3790 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
3791 * none of the other ways work.
3792 * 4. Use T_CE (erase line) if the result is empty.
3793 * 5. Use T_DL (delete line) if it exists.
3794 * 6. redraw the characters from ScreenLines[].
3795 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
3797 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02003798 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 type = USE_T_CD;
3800#if defined(__BEOS__) && defined(BEOS_DR8)
3801 /*
3802 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
3803 * its internal termcap... this works okay for tests which test *T_DB !=
3804 * NUL. It has the disadvantage that the user cannot use any :set t_*
3805 * command to get T_DB (back) to empty_option, only :set term=... will do
3806 * the trick...
3807 * Anyway, this hack will hopefully go away with the next OS release.
3808 * (Olaf Seibert)
3809 */
3810 else if (row == 0 && T_DB == empty_option
3811 && (line_count == 1 || *T_CDL == NUL))
3812#else
3813 else if (row == 0 && (
3814#ifndef AMIGA
3815 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
3816 * up, so use delete-line command */
3817 line_count == 1 ||
3818#endif
3819 *T_CDL == NUL))
3820#endif
3821 type = USE_NL;
3822 else if (*T_CDL != NUL && line_count > 1 && can_delete)
3823 type = USE_T_CDL;
3824 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +02003825 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 type = USE_T_CE;
3827 else if (*T_DL != NUL && can_delete)
3828 type = USE_T_DL;
3829 else if (*T_CDL != NUL && can_delete)
3830 type = USE_T_CDL;
3831 else
3832 return FAIL;
3833
3834#ifdef FEAT_CLIPBOARD
3835 /* Remove a modeless selection when deleting lines halfway the screen or
3836 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02003837 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003838 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 else
3840 clip_scroll_selection(line_count);
3841#endif
3842
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843#ifdef FEAT_GUI
3844 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
3845 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02003846 gui_dont_update_cursor(gui.cursor_row >= row + off
3847 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848#endif
3849
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003850 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
3851 cursor_col = wp->w_wincol;
3852
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 if (*T_CCS != NUL) /* cursor relative to region */
3854 {
3855 cursor_row = row;
3856 cursor_end = end;
3857 }
3858 else
3859 {
3860 cursor_row = row + off;
3861 cursor_end = end + off;
3862 }
3863
3864 /*
3865 * Now shift LineOffset[] line_count up to reflect the deleted lines.
3866 * Clear the inserted lines in ScreenLines[].
3867 */
3868 row += off;
3869 end += off;
3870 for (i = 0; i < line_count; ++i)
3871 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 if (wp != NULL && wp->w_width != Columns)
3873 {
3874 /* need to copy part of a line */
3875 j = row + i;
3876 while ((j += line_count) <= end - 1)
3877 linecopy(j - line_count, j, wp);
3878 j -= line_count;
3879 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003880 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
3881 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 else
3883 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
3884 LineWraps[j] = FALSE;
3885 }
3886 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 {
3888 /* whole width, moving the line pointers is faster */
3889 j = row + i;
3890 temp = LineOffset[j];
3891 while ((j += line_count) <= end - 1)
3892 {
3893 LineOffset[j - line_count] = LineOffset[j];
3894 LineWraps[j - line_count] = LineWraps[j];
3895 }
3896 LineOffset[j - line_count] = temp;
3897 LineWraps[j - line_count] = FALSE;
3898 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003899 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 else
3901 lineinvalid(temp, (int)Columns);
3902 }
3903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003905 if (screen_attr != clear_attr)
3906 screen_stop_highlight();
3907 if (clear_attr != 0)
3908 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 /* redraw the characters */
3911 if (type == USE_REDRAW)
3912 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02003913 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003915 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 out_str(T_CD);
3917 screen_start(); /* don't know where cursor is now */
3918 }
3919 else if (type == USE_T_CDL)
3920 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003921 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 term_delete_lines(line_count);
3923 screen_start(); /* don't know where cursor is now */
3924 }
3925 /*
3926 * Deleting lines at top of the screen or scroll region: Just scroll
3927 * the whole screen (scroll region) up by outputting newlines on the
3928 * last line.
3929 */
3930 else if (type == USE_NL)
3931 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003932 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 for (i = line_count; --i >= 0; )
3934 out_char('\n'); /* cursor will remain on same line */
3935 }
3936 else
3937 {
3938 for (i = line_count; --i >= 0; )
3939 {
3940 if (type == USE_T_DL)
3941 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003942 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 out_str(T_DL); /* delete a line */
3944 }
3945 else /* type == USE_T_CE */
3946 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003947 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 out_str(T_CE); /* erase a line */
3949 }
3950 screen_start(); /* don't know where cursor is now */
3951 }
3952 }
3953
3954 /*
3955 * If the 'db' flag is set, we need to clear the lines that have been
3956 * scrolled up at the bottom of the region.
3957 */
3958 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
3959 {
3960 for (i = line_count; i > 0; --i)
3961 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003962 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 out_str(T_CE); /* erase a line */
3964 screen_start(); /* don't know where cursor is now */
3965 }
3966 }
3967
3968#ifdef FEAT_GUI
3969 gui_can_update_cursor();
3970 if (gui.in_use)
3971 out_flush(); /* always flush after a scroll */
3972#endif
3973
3974 return OK;
3975}
3976
3977/*
Bram Moolenaarcb574f42019-01-25 22:29:57 +01003978 * Return TRUE when postponing displaying the mode message: when not redrawing
3979 * or inside a mapping.
3980 */
3981 int
3982skip_showmode()
3983{
3984 // Call char_avail() only when we are going to show something, because it
3985 // takes a bit of time. redrawing() may also call char_avail_avail().
3986 if (global_busy
3987 || msg_silent != 0
3988 || !redrawing()
3989 || (char_avail() && !KeyTyped))
3990 {
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02003991 redraw_mode = TRUE; // show mode later
Bram Moolenaarcb574f42019-01-25 22:29:57 +01003992 return TRUE;
3993 }
3994 return FALSE;
3995}
3996
3997/*
Bram Moolenaar81226e02018-02-20 21:44:45 +01003998 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 *
4000 * If clear_cmdline is TRUE, clear the rest of the cmdline.
4001 * If clear_cmdline is FALSE there may be a message there that needs to be
4002 * cleared only if a mode is shown.
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004003 * If redraw_mode is TRUE show or clear the mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 * Return the length of the message (0 if no message).
4005 */
4006 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004007showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008{
4009 int need_clear;
4010 int length = 0;
4011 int do_mode;
4012 int attr;
4013 int nwr_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 int sub_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015
Bram Moolenaar7df351e2006-01-23 22:30:28 +00004016 do_mode = ((p_smd && msg_silent == 0)
4017 && ((State & INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +02004018 || restart_edit != NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004019 || VIsual_active));
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02004020 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 {
Bram Moolenaarcb574f42019-01-25 22:29:57 +01004022 if (skip_showmode())
4023 return 0; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024
4025 nwr_save = need_wait_return;
4026
4027 /* wait a bit before overwriting an important message */
4028 check_for_delay(FALSE);
4029
4030 /* if the cmdline is more than one line high, erase top lines */
4031 need_clear = clear_cmdline;
4032 if (clear_cmdline && cmdline_row < Rows - 1)
4033 msg_clr_cmdline(); /* will reset clear_cmdline */
4034
4035 /* Position on the last line in the window, column 0 */
4036 msg_pos_mode();
4037 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +01004038 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 if (do_mode)
4040 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004041 msg_puts_attr("--", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00004043 if (
Bram Moolenaar09092152010-08-08 16:38:42 +02004044# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +00004045 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +02004046# else
Bram Moolenaarc236c162008-07-13 17:41:49 +00004047 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +00004048# endif
Bram Moolenaar09092152010-08-08 16:38:42 +02004049 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02004050# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar32526b32019-01-19 17:43:09 +01004051 msg_puts_attr(" IM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052# else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004053 msg_puts_attr(" XIM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054# endif
4055#endif
Bram Moolenaarea389e92014-05-28 21:40:52 +02004056 /* CTRL-X in Insert mode */
4057 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 {
4059 /* These messages can get long, avoid a wrap in a narrow
4060 * window. Prefer showing edit_submode_extra. */
4061 length = (Rows - msg_row) * Columns - 3;
4062 if (edit_submode_extra != NULL)
4063 length -= vim_strsize(edit_submode_extra);
4064 if (length > 0)
4065 {
4066 if (edit_submode_pre != NULL)
4067 length -= vim_strsize(edit_submode_pre);
4068 if (length - vim_strsize(edit_submode) > 0)
4069 {
4070 if (edit_submode_pre != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004071 msg_puts_attr((char *)edit_submode_pre, attr);
4072 msg_puts_attr((char *)edit_submode, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 }
4074 if (edit_submode_extra != NULL)
4075 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004076 msg_puts_attr(" ", attr); /* add a space in between */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004078 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 else
4080 sub_attr = attr;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004081 msg_puts_attr((char *)edit_submode_extra, sub_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 }
4083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 }
4085 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 if (State & VREPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004088 msg_puts_attr(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02004089 else if (State & REPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004090 msg_puts_attr(_(" REPLACE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 else if (State & INSERT)
4092 {
4093#ifdef FEAT_RIGHTLEFT
4094 if (p_ri)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004095 msg_puts_attr(_(" REVERSE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01004097 msg_puts_attr(_(" INSERT"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 }
Bram Moolenaar942b4542018-06-17 16:23:34 +02004099 else if (restart_edit == 'I' || restart_edit == 'A')
Bram Moolenaar32526b32019-01-19 17:43:09 +01004100 msg_puts_attr(_(" (insert)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 else if (restart_edit == 'R')
Bram Moolenaar32526b32019-01-19 17:43:09 +01004102 msg_puts_attr(_(" (replace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 else if (restart_edit == 'V')
Bram Moolenaar32526b32019-01-19 17:43:09 +01004104 msg_puts_attr(_(" (vreplace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105#ifdef FEAT_RIGHTLEFT
4106 if (p_hkmap)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004107 msg_puts_attr(_(" Hebrew"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108#endif
4109#ifdef FEAT_KEYMAP
4110 if (State & LANGMAP)
4111 {
4112# ifdef FEAT_ARABIC
4113 if (curwin->w_p_arab)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004114 msg_puts_attr(_(" Arabic"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 else
4116# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02004117 if (get_keymap_str(curwin, (char_u *)" (%s)",
4118 NameBuff, MAXPATHL))
Bram Moolenaar32526b32019-01-19 17:43:09 +01004119 msg_puts_attr((char *)NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 }
4121#endif
4122 if ((State & INSERT) && p_paste)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004123 msg_puts_attr(_(" (paste)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 if (VIsual_active)
4126 {
4127 char *p;
4128
4129 /* Don't concatenate separate words to avoid translation
4130 * problems. */
4131 switch ((VIsual_select ? 4 : 0)
4132 + (VIsual_mode == Ctrl_V) * 2
4133 + (VIsual_mode == 'V'))
4134 {
4135 case 0: p = N_(" VISUAL"); break;
4136 case 1: p = N_(" VISUAL LINE"); break;
4137 case 2: p = N_(" VISUAL BLOCK"); break;
4138 case 4: p = N_(" SELECT"); break;
4139 case 5: p = N_(" SELECT LINE"); break;
4140 default: p = N_(" SELECT BLOCK"); break;
4141 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01004142 msg_puts_attr(_(p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01004144 msg_puts_attr(" --", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004146
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 need_clear = TRUE;
4148 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02004149 if (reg_recording != 0
Bram Moolenaare2c453d2019-08-21 14:37:09 +02004150 && edit_submode == NULL) // otherwise it gets too long
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004152 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 need_clear = TRUE;
4154 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004155
4156 mode_displayed = TRUE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004157 if (need_clear || clear_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 msg_clr_eos();
4159 msg_didout = FALSE; /* overwrite this message */
4160 length = msg_col;
4161 msg_col = 0;
4162 need_wait_return = nwr_save; /* never ask for hit-return for this */
4163 }
4164 else if (clear_cmdline && msg_silent == 0)
4165 /* Clear the whole command line. Will reset "clear_cmdline". */
4166 msg_clr_cmdline();
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004167 else if (redraw_mode)
4168 {
4169 msg_pos_mode();
4170 msg_clr_eos();
4171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172
4173#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 /* In Visual mode the size of the selected area must be redrawn. */
4175 if (VIsual_active)
4176 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177
4178 /* If the last window has no status line, the ruler is after the mode
4179 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +02004180 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +02004181 win_redr_ruler(lastwin, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182#endif
4183 redraw_cmdline = FALSE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004184 redraw_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 clear_cmdline = FALSE;
4186
4187 return length;
4188}
4189
4190/*
4191 * Position for a mode message.
4192 */
4193 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004194msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195{
4196 msg_col = 0;
4197 msg_row = Rows - 1;
4198}
4199
4200/*
4201 * Delete mode message. Used when ESC is typed which is expected to end
4202 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004203 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 */
4205 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004206unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207{
4208 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +01004209 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 */
4211 if (!redrawing() || (!force && char_avail() && !KeyTyped))
4212 redraw_cmdline = TRUE; /* delete mode later */
4213 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +02004214 clearmode();
4215}
4216
4217/*
4218 * Clear the mode message.
4219 */
4220 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02004221clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +02004222{
Bram Moolenaar2abad542018-05-19 14:43:45 +02004223 int save_msg_row = msg_row;
4224 int save_msg_col = msg_col;
4225
Bram Moolenaarfd773e92016-04-02 19:39:16 +02004226 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02004227 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004228 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +02004229 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +02004230
4231 msg_col = save_msg_col;
4232 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233}
4234
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004235 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004236recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004237{
Bram Moolenaar32526b32019-01-19 17:43:09 +01004238 msg_puts_attr(_("recording"), attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004239 if (!shortmess(SHM_RECORDING))
4240 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004241 char s[4];
4242
4243 sprintf(s, " @%c", reg_recording);
4244 msg_puts_attr(s, attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004245 }
4246}
4247
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004248/*
4249 * Draw the tab pages line at the top of the Vim window.
4250 */
Bram Moolenaare12bab32019-01-08 22:02:56 +01004251 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004252draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004253{
4254 int tabcount = 0;
4255 tabpage_T *tp;
4256 int tabwidth;
4257 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00004258 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004259 int attr;
4260 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004261 win_T *cwp;
4262 int wincount;
4263 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004264 int c;
4265 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004266 int attr_sel = HL_ATTR(HLF_TPS);
4267 int attr_nosel = HL_ATTR(HLF_TP);
4268 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00004269 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004270 int room;
4271 int use_sep_chars = (t_colors < 8
4272#ifdef FEAT_GUI
4273 && !gui.in_use
4274#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02004275#ifdef FEAT_TERMGUICOLORS
4276 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +02004277#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004278 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004279
Bram Moolenaarc695cec2017-01-08 20:00:04 +01004280 if (ScreenLines == NULL)
4281 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00004282 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004283
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004284#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +00004285 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004286 if (gui_use_tabline())
4287 {
4288 gui_update_tabline();
4289 return;
4290 }
4291#endif
4292
4293 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004294 return;
4295
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004296#if defined(FEAT_STL_OPT)
Bram Moolenaarca57ab52019-04-13 14:53:16 +02004297 clear_TabPageIdxs();
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004298
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004299 /* Use the 'tabline' option if it's set. */
4300 if (*p_tal != NUL)
4301 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +02004302 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +00004303
4304 /* Check for an error. If there is one we would loop in redrawing the
4305 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +02004306 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004307 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +02004308 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +00004309 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004310 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +02004311 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004312 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00004313 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004314#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004315 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004316 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00004317 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004318
Bram Moolenaar238a5642006-02-21 22:12:05 +00004319 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
4320 if (tabwidth < 6)
4321 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004322
Bram Moolenaar238a5642006-02-21 22:12:05 +00004323 attr = attr_nosel;
4324 tabcount = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004325 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
4326 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00004327 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00004328 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004329
Bram Moolenaar238a5642006-02-21 22:12:05 +00004330 if (tp->tp_topframe == topframe)
4331 attr = attr_sel;
4332 if (use_sep_chars && col > 0)
4333 screen_putchar('|', 0, col++, attr);
4334
4335 if (tp->tp_topframe != topframe)
4336 attr = attr_nosel;
4337
4338 screen_putchar(' ', 0, col++, attr);
4339
4340 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00004341 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00004342 cwp = curwin;
4343 wp = firstwin;
4344 }
4345 else
4346 {
4347 cwp = tp->tp_curwin;
4348 wp = tp->tp_firstwin;
4349 }
4350
4351 modified = FALSE;
4352 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
4353 if (bufIsChanged(wp->w_buffer))
4354 modified = TRUE;
4355 if (modified || wincount > 1)
4356 {
4357 if (wincount > 1)
4358 {
4359 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004360 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004361 if (col + len >= Columns - 3)
4362 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +00004363 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004364#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004365 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004366#else
Bram Moolenaare0f14822014-08-06 13:20:56 +02004367 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004368#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00004369 );
4370 col += len;
4371 }
4372 if (modified)
4373 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
4374 screen_putchar(' ', 0, col++, attr);
4375 }
4376
4377 room = scol - col + tabwidth - 1;
4378 if (room > 0)
4379 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004380 /* Get buffer name in NameBuff[] */
4381 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004382 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +00004383 len = vim_strsize(NameBuff);
4384 p = NameBuff;
Bram Moolenaar238a5642006-02-21 22:12:05 +00004385 if (has_mbyte)
4386 while (len > room)
4387 {
4388 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004389 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +00004390 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01004391 else if (len > room)
Bram Moolenaar238a5642006-02-21 22:12:05 +00004392 {
4393 p += len - room;
4394 len = room;
4395 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004396 if (len > Columns - col - 1)
4397 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +00004398
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004399 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +00004400 col += len;
4401 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00004402 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +00004403
4404 /* Store the tab page number in TabPageIdxs[], so that
4405 * jump_to_mouse() knows where each one is. */
4406 ++tabcount;
4407 while (scol < col)
4408 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004409 }
4410
Bram Moolenaar238a5642006-02-21 22:12:05 +00004411 if (use_sep_chars)
4412 c = '_';
4413 else
4414 c = ' ';
4415 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004416
4417 /* Put an "X" for closing the current tab if there are several. */
4418 if (first_tabpage->tp_next != NULL)
4419 {
4420 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
4421 TabPageIdxs[Columns - 1] = -999;
4422 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004423 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +00004424
4425 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
4426 * set. */
4427 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004428}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004429
4430/*
4431 * Get buffer name for "buf" into NameBuff[].
4432 * Takes care of special buffer names and translates special characters.
4433 */
4434 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004435get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004436{
4437 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02004438 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004439 else
4440 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
4441 trans_characters(NameBuff, MAXPATHL);
4442}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004443
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444/*
4445 * Get the character to use in a status line. Get its attributes in "*attr".
4446 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02004447 int
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004448fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449{
4450 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004451
4452#ifdef FEAT_TERMINAL
4453 if (bt_terminal(wp->w_buffer))
4454 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004455 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02004456 {
4457 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004458 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02004459 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004460 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02004461 {
4462 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004463 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02004464 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004465 }
4466 else
4467#endif
4468 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004470 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 fill = fill_stl;
4472 }
4473 else
4474 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004475 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 fill = fill_stlnc;
4477 }
4478 /* Use fill when there is highlighting, and highlighting of current
4479 * window differs, or the fillchars differ, or this is not the
4480 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004481 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004482 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 || (fill_stl != fill_stlnc)))
4484 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004485 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 return '^';
4487 return '=';
4488}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490/*
4491 * Get the character to use in a separator between vertically split windows.
4492 * Get its attributes in "*attr".
4493 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02004494 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004495fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496{
Bram Moolenaar8820b482017-03-16 17:23:31 +01004497 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 if (*attr == 0 && fill_vert == ' ')
4499 return '|';
4500 else
4501 return fill_vert;
4502}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503
4504/*
4505 * Return TRUE if redrawing should currently be done.
4506 */
4507 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004508redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509{
Bram Moolenaareb992cb2017-03-09 18:20:16 +01004510#ifdef FEAT_EVAL
4511 if (disable_redraw_for_testing)
4512 return 0;
4513 else
4514#endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +02004515 return ((!RedrawingDisabled
4516#ifdef FEAT_EVAL
4517 || ignore_redraw_flag_for_testing
4518#endif
4519 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520}
4521
4522/*
4523 * Return TRUE if printing messages should currently be done.
4524 */
4525 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004526messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527{
4528 return (!(p_lz && char_avail() && !KeyTyped));
4529}
4530
Bram Moolenaare677df82019-09-02 22:31:11 +02004531/*
4532 * Compute columns for ruler and shown command. 'sc_col' is also used to
4533 * decide what the maximum length of a message on the status line can be.
4534 * If there is a status line for the last window, 'sc_col' is independent
4535 * of 'ru_col'.
4536 */
4537
4538#define COL_RULER 17 // columns needed by standard ruler
4539
4540 void
4541comp_col(void)
4542{
4543#if defined(FEAT_CMDL_INFO)
4544 int last_has_status = (p_ls == 2 || (p_ls == 1 && !ONE_WINDOW));
4545
4546 sc_col = 0;
4547 ru_col = 0;
4548 if (p_ru)
4549 {
4550# ifdef FEAT_STL_OPT
4551 ru_col = (ru_wid ? ru_wid : COL_RULER) + 1;
4552# else
4553 ru_col = COL_RULER + 1;
4554# endif
4555 // no last status line, adjust sc_col
4556 if (!last_has_status)
4557 sc_col = ru_col;
4558 }
4559 if (p_sc)
4560 {
4561 sc_col += SHOWCMD_COLS;
4562 if (!p_ru || last_has_status) // no need for separating space
4563 ++sc_col;
4564 }
4565 sc_col = Columns - sc_col;
4566 ru_col = Columns - ru_col;
4567 if (sc_col <= 0) // screen too narrow, will become a mess
4568 sc_col = 1;
4569 if (ru_col <= 0)
4570 ru_col = 1;
4571#else
4572 sc_col = Columns;
4573 ru_col = Columns;
4574#endif
4575#ifdef FEAT_EVAL
4576 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
4577#endif
4578}
4579
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004580#if defined(FEAT_LINEBREAK) || defined(PROTO)
4581/*
Bram Moolenaar64486672010-05-16 15:46:46 +02004582 * Return the width of the 'number' and 'relativenumber' column.
4583 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004584 * Otherwise it depends on 'numberwidth' and the line count.
4585 */
4586 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004587number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004588{
4589 int n;
4590 linenr_T lnum;
4591
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004592 if (wp->w_p_rnu && !wp->w_p_nu)
4593 /* cursor line shows "0" */
4594 lnum = wp->w_height;
4595 else
4596 /* cursor line shows absolute line number */
4597 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +02004598
Bram Moolenaar6b314672015-03-20 15:42:10 +01004599 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004600 return wp->w_nrwidth_width;
4601 wp->w_nrwidth_line_count = lnum;
4602
4603 n = 0;
4604 do
4605 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004606 lnum /= 10;
4607 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004608 } while (lnum > 0);
4609
4610 /* 'numberwidth' gives the minimal width plus one */
4611 if (n < wp->w_p_nuw - 1)
4612 n = wp->w_p_nuw - 1;
4613
Bram Moolenaare4b407f2019-07-04 11:59:28 +02004614# ifdef FEAT_SIGNS
4615 // If 'signcolumn' is set to 'number' and there is a sign to display, then
4616 // the minimal width for the number column is 2.
Bram Moolenaar4eb7dae2019-11-12 22:33:45 +01004617 if (n < 2 && get_first_valid_sign(wp) != NULL
Bram Moolenaare4b407f2019-07-04 11:59:28 +02004618 && (*wp->w_p_scl == 'n' && *(wp->w_p_scl + 1) == 'u'))
4619 n = 2;
4620# endif
4621
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004622 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +01004623 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004624 return n;
4625}
4626#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +01004627
Bram Moolenaar113e1072019-01-20 15:30:40 +01004628#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9750bb12012-12-05 16:10:42 +01004629/*
4630 * Return the current cursor column. This is the actual position on the
4631 * screen. First column is 0.
4632 */
4633 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004634screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +01004635{
4636 return screen_cur_col;
4637}
4638
4639/*
4640 * Return the current cursor row. This is the actual position on the screen.
4641 * First row is 0.
4642 */
4643 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004644screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +01004645{
4646 return screen_cur_row;
4647}
Bram Moolenaar113e1072019-01-20 15:30:40 +01004648#endif
Bram Moolenaare677df82019-09-02 22:31:11 +02004649
4650/*
4651 * Handle setting 'listchars' or 'fillchars'.
4652 * Returns error message, NULL if it's OK.
4653 */
4654 char *
4655set_chars_option(char_u **varp)
4656{
4657 int round, i, len, entries;
4658 char_u *p, *s;
4659 int c1 = 0, c2 = 0, c3 = 0;
4660 struct charstab
4661 {
4662 int *cp;
4663 char *name;
4664 };
4665 static struct charstab filltab[] =
4666 {
4667 {&fill_stl, "stl"},
4668 {&fill_stlnc, "stlnc"},
4669 {&fill_vert, "vert"},
4670 {&fill_fold, "fold"},
4671 {&fill_diff, "diff"},
4672 };
4673 static struct charstab lcstab[] =
4674 {
4675 {&lcs_eol, "eol"},
4676 {&lcs_ext, "extends"},
4677 {&lcs_nbsp, "nbsp"},
4678 {&lcs_prec, "precedes"},
4679 {&lcs_space, "space"},
4680 {&lcs_tab2, "tab"},
4681 {&lcs_trail, "trail"},
4682#ifdef FEAT_CONCEAL
4683 {&lcs_conceal, "conceal"},
4684#else
4685 {NULL, "conceal"},
4686#endif
4687 };
4688 struct charstab *tab;
4689
4690 if (varp == &p_lcs)
4691 {
4692 tab = lcstab;
4693 entries = sizeof(lcstab) / sizeof(struct charstab);
4694 }
4695 else
4696 {
4697 tab = filltab;
4698 entries = sizeof(filltab) / sizeof(struct charstab);
4699 }
4700
4701 // first round: check for valid value, second round: assign values
4702 for (round = 0; round <= 1; ++round)
4703 {
4704 if (round > 0)
4705 {
4706 // After checking that the value is valid: set defaults: space for
4707 // 'fillchars', NUL for 'listchars'
4708 for (i = 0; i < entries; ++i)
4709 if (tab[i].cp != NULL)
4710 *(tab[i].cp) = (varp == &p_lcs ? NUL : ' ');
4711
4712 if (varp == &p_lcs)
4713 {
4714 lcs_tab1 = NUL;
4715 lcs_tab3 = NUL;
4716 }
4717 else
4718 fill_diff = '-';
4719 }
4720 p = *varp;
4721 while (*p)
4722 {
4723 for (i = 0; i < entries; ++i)
4724 {
4725 len = (int)STRLEN(tab[i].name);
4726 if (STRNCMP(p, tab[i].name, len) == 0
4727 && p[len] == ':'
4728 && p[len + 1] != NUL)
4729 {
4730 c2 = c3 = 0;
4731 s = p + len + 1;
4732 c1 = mb_ptr2char_adv(&s);
4733 if (mb_char2cells(c1) > 1)
4734 continue;
4735 if (tab[i].cp == &lcs_tab2)
4736 {
4737 if (*s == NUL)
4738 continue;
4739 c2 = mb_ptr2char_adv(&s);
4740 if (mb_char2cells(c2) > 1)
4741 continue;
4742 if (!(*s == ',' || *s == NUL))
4743 {
4744 c3 = mb_ptr2char_adv(&s);
4745 if (mb_char2cells(c3) > 1)
4746 continue;
4747 }
4748 }
4749
4750 if (*s == ',' || *s == NUL)
4751 {
4752 if (round)
4753 {
4754 if (tab[i].cp == &lcs_tab2)
4755 {
4756 lcs_tab1 = c1;
4757 lcs_tab2 = c2;
4758 lcs_tab3 = c3;
4759 }
4760 else if (tab[i].cp != NULL)
4761 *(tab[i].cp) = c1;
4762
4763 }
4764 p = s;
4765 break;
4766 }
4767 }
4768 }
4769
4770 if (i == entries)
4771 return e_invarg;
4772 if (*p == ',')
4773 ++p;
4774 }
4775 }
4776
4777 return NULL; // no error
4778}
Bram Moolenaar017ba072019-09-14 21:01:23 +02004779