blob: eb71d767825f65180d428d14c65ba45430363c04 [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/*
11 * screen.c: code for displaying on the screen
12 *
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[].
38 *
39 * update_screen() is the function that updates all windows and status lines.
40 * It is called form the main loop when must_redraw is non-zero. It may be
Bram Moolenaar2c7a7632007-05-10 18:19:11 +000041 * called from other places when an immediate screen update is needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +000042 *
43 * The part of the buffer that is displayed in a window is set with:
44 * - w_topline (first buffer line in window)
Bram Moolenaarea389e92014-05-28 21:40:52 +020045 * - w_topfill (filler lines above the first line)
Bram Moolenaar071d4272004-06-13 20:20:40 +000046 * - w_leftcol (leftmost window cell in window),
47 * - w_skipcol (skipped window cells of first line)
48 *
49 * Commands that only move the cursor around in a window, do not need to take
50 * action to update the display. The main loop will check if w_topline is
51 * valid and update it (scroll the window) when needed.
52 *
53 * Commands that scroll a window change w_topline and must call
54 * check_cursor() to move the cursor into the visible part of the window, and
55 * call redraw_later(VALID) to have the window displayed by update_screen()
56 * later.
57 *
58 * Commands that change text in the buffer must call changed_bytes() or
59 * changed_lines() to mark the area that changed and will require updating
60 * later. The main loop will call update_screen(), which will update each
61 * window that shows the changed buffer. This assumes text above the change
62 * can remain displayed as it is. Text after the change may need updating for
63 * scrolling, folding and syntax highlighting.
64 *
65 * Commands that change how a window is displayed (e.g., setting 'list') or
66 * invalidate the contents of a window in another way (e.g., change fold
67 * settings), must call redraw_later(NOT_VALID) to have the whole window
68 * redisplayed by update_screen() later.
69 *
70 * Commands that change how a buffer is displayed (e.g., setting 'tabstop')
71 * must call redraw_curbuf_later(NOT_VALID) to have all the windows for the
72 * buffer redisplayed by update_screen() later.
73 *
Bram Moolenaar600dddc2006-03-12 22:05:10 +000074 * Commands that change highlighting and possibly cause a scroll too must call
75 * redraw_later(SOME_VALID) to update the whole window but still use scrolling
76 * to avoid redrawing everything. But the length of displayed lines must not
77 * change, use NOT_VALID then.
78 *
Bram Moolenaar071d4272004-06-13 20:20:40 +000079 * Commands that move the window position must call redraw_later(NOT_VALID).
80 * TODO: should minimize redrawing by scrolling when possible.
81 *
82 * Commands that change everything (e.g., resizing the screen) must call
83 * redraw_all_later(NOT_VALID) or redraw_all_later(CLEAR).
84 *
85 * Things that are handled indirectly:
86 * - When messages scroll the screen up, msg_scrolled will be set and
87 * update_screen() called to redraw.
88 */
89
90#include "vim.h"
91
Bram Moolenaar5641f382012-06-13 18:06:36 +020092#define MB_FILLER_CHAR '<' /* character used when a double-width character
93 * doesn't fit. */
94
Bram Moolenaar071d4272004-06-13 20:20:40 +000095/*
96 * The attributes that are actually active for writing to the screen.
97 */
98static int screen_attr = 0;
99
100/*
101 * Positioning the cursor is reduced by remembering the last position.
102 * Mostly used by windgoto() and screen_char().
103 */
104static int screen_cur_row, screen_cur_col; /* last known cursor position */
105
106#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarbbca7732019-07-24 18:13:16 +0200107static match_T search_hl; // used for 'hlsearch' highlight matching
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108#endif
109
110#ifdef FEAT_FOLDING
111static foldinfo_T win_foldinfo; /* info for 'foldcolumn' */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100112static int compute_foldcolumn(win_T *wp, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113#endif
114
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200115/* Flag that is set when drawing for a callback, not from the main command
116 * loop. */
117static int redrawing_for_callback = 0;
118
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119/*
120 * Buffer for one screen line (characters and attributes).
121 */
122static schar_T *current_ScreenLine;
123
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100124static void win_update(win_T *wp);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200125static void win_redr_status(win_T *wp, int ignore_pum);
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100126static void win_draw_end(win_T *wp, int c1, int c2, int draw_margin, int row, int endrow, hlf_T hl);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127#ifdef FEAT_FOLDING
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100128static void fold_line(win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row);
129static void fill_foldcolumn(char_u *p, win_T *wp, int closed, linenr_T lnum);
130static void copy_text_attr(int off, char_u *buf, int len, int attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +0200132static int win_line(win_T *, linenr_T, int, int, int nochange, int number_only);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100133static void draw_vsep_win(win_T *wp, int row);
Bram Moolenaar238a5642006-02-21 22:12:05 +0000134#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100135static void redraw_custom_statusline(win_T *wp);
Bram Moolenaar238a5642006-02-21 22:12:05 +0000136#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100138static void start_search_hl(void);
139static void end_search_hl(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100141static void screen_char(unsigned off, int row, int col);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100142static void screen_char_2(unsigned off, int row, int col);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100143static void screenclear2(void);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200144static void lineclear(unsigned off, int width, int attr);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100145static void lineinvalid(unsigned off, int width);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200146static 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 +0100147static void win_rest_invalid(win_T *wp);
148static void msg_pos_mode(void);
149static void recording_mode(int attr);
Bram Moolenaar3633cf52017-07-31 22:29:35 +0200150static int fillchar_status(int *attr, win_T *wp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100151static int fillchar_vsep(int *attr);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200152#ifdef FEAT_MENU
153static void redraw_win_toolbar(win_T *wp);
154#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100156static void win_redr_custom(win_T *wp, int draw_ruler);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157#endif
158#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +0200159static void win_redr_ruler(win_T *wp, int always, int ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160#endif
161
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162/* Ugly global: overrule attribute used by screen_char() */
163static int screen_char_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164
Bram Moolenaarc0aa4822017-07-16 14:04:29 +0200165#ifdef FEAT_RIGHTLEFT
166# define HAS_RIGHTLEFT(x) x
167#else
168# define HAS_RIGHTLEFT(x) FALSE
169#endif
170
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200171// flags for screen_line()
172#define SLF_RIGHTLEFT 1
173#define SLF_POPUP 2
174
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175/*
176 * Redraw the current window later, with update_screen(type).
177 * Set must_redraw only if not already set to a higher value.
Bram Moolenaarae654382019-01-17 21:09:05 +0100178 * E.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179 */
180 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100181redraw_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182{
183 redraw_win_later(curwin, type);
184}
185
186 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100187redraw_win_later(
188 win_T *wp,
189 int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190{
Bram Moolenaar4f198282017-10-23 21:53:30 +0200191 if (!exiting && wp->w_redr_type < type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 {
193 wp->w_redr_type = type;
194 if (type >= NOT_VALID)
195 wp->w_lines_valid = 0;
196 if (must_redraw < type) /* must_redraw is the maximum of all windows */
197 must_redraw = type;
198 }
199}
200
201/*
202 * Force a complete redraw later. Also resets the highlighting. To be used
203 * after executing a shell command that messes up the screen.
204 */
205 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100206redraw_later_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207{
208 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000209#ifdef FEAT_GUI
210 if (gui.in_use)
211 /* Use a code that will reset gui.highlight_mask in
212 * gui_stop_highlight(). */
213 screen_attr = HL_ALL + 1;
214 else
215#endif
216 /* Use attributes that is very unlikely to appear in text. */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +0200217 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE | HL_STRIKETHROUGH;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218}
219
220/*
221 * Mark all windows to be redrawn later.
222 */
223 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100224redraw_all_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225{
226 win_T *wp;
227
228 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229 redraw_win_later(wp, type);
Bram Moolenaar04b4e1a2019-01-06 22:22:07 +0100230 // This may be needed when switching tabs.
231 if (must_redraw < type)
232 must_redraw = type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233}
234
235/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000236 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237 */
238 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100239redraw_curbuf_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240{
241 redraw_buf_later(curbuf, type);
242}
243
244 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100245redraw_buf_later(buf_T *buf, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246{
247 win_T *wp;
248
249 FOR_ALL_WINDOWS(wp)
250 {
251 if (wp->w_buffer == buf)
252 redraw_win_later(wp, type);
253 }
254}
255
Bram Moolenaar113e1072019-01-20 15:30:40 +0100256#if defined(FEAT_SIGNS) || defined(PROTO)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200257 void
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100258redraw_buf_line_later(buf_T *buf, linenr_T lnum)
259{
260 win_T *wp;
261
262 FOR_ALL_WINDOWS(wp)
263 if (wp->w_buffer == buf && lnum >= wp->w_topline
264 && lnum < wp->w_botline)
265 redrawWinline(wp, lnum);
266}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100267#endif
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100268
Bram Moolenaar113e1072019-01-20 15:30:40 +0100269#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100270 void
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200271redraw_buf_and_status_later(buf_T *buf, int type)
272{
273 win_T *wp;
274
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200275#ifdef FEAT_WILDMENU
Bram Moolenaar86033562017-07-12 20:24:41 +0200276 if (wild_menu_showing != 0)
277 /* Don't redraw while the command line completion is displayed, it
278 * would disappear. */
279 return;
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200280#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200281 FOR_ALL_WINDOWS(wp)
282 {
283 if (wp->w_buffer == buf)
284 {
285 redraw_win_later(wp, type);
286 wp->w_redr_status = TRUE;
287 }
288 }
289}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100290#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200291
Bram Moolenaar113e1072019-01-20 15:30:40 +0100292#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000293/*
Bram Moolenaar2951b772013-07-03 12:45:31 +0200294 * Redraw as soon as possible. When the command line is not scrolled redraw
295 * right away and restore what was on the command line.
296 * Return a code indicating what happened.
297 */
298 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100299redraw_asap(int type)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200300{
301 int rows;
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200302 int cols = screen_Columns;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200303 int r;
304 int ret = 0;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200305 schar_T *screenline; /* copy from ScreenLines[] */
306 sattr_T *screenattr; /* copy from ScreenAttrs[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200307 int i;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200308 u8char_T *screenlineUC = NULL; /* copy from ScreenLinesUC[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200309 u8char_T *screenlineC[MAX_MCO]; /* copy from ScreenLinesC[][] */
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200310 schar_T *screenline2 = NULL; /* copy from ScreenLines2[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200311
312 redraw_later(type);
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200313 if (msg_scrolled || (State != NORMAL && State != NORMAL_BUSY) || exiting)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200314 return ret;
315
316 /* Allocate space to save the text displayed in the command line area. */
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200317 rows = screen_Rows - cmdline_row;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200318 screenline = LALLOC_MULT(schar_T, rows * cols);
319 screenattr = LALLOC_MULT(sattr_T, rows * cols);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200320 if (screenline == NULL || screenattr == NULL)
321 ret = 2;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200322 if (enc_utf8)
323 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200324 screenlineUC = LALLOC_MULT(u8char_T, rows * cols);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200325 if (screenlineUC == NULL)
326 ret = 2;
327 for (i = 0; i < p_mco; ++i)
328 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200329 screenlineC[i] = LALLOC_MULT(u8char_T, rows * cols);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200330 if (screenlineC[i] == NULL)
331 ret = 2;
332 }
333 }
334 if (enc_dbcs == DBCS_JPNU)
335 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200336 screenline2 = LALLOC_MULT(schar_T, rows * cols);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200337 if (screenline2 == NULL)
338 ret = 2;
339 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200340
341 if (ret != 2)
342 {
343 /* Save the text displayed in the command line area. */
344 for (r = 0; r < rows; ++r)
345 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200346 mch_memmove(screenline + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200347 ScreenLines + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200348 (size_t)cols * sizeof(schar_T));
349 mch_memmove(screenattr + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200350 ScreenAttrs + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200351 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200352 if (enc_utf8)
353 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200354 mch_memmove(screenlineUC + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200355 ScreenLinesUC + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200356 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200357 for (i = 0; i < p_mco; ++i)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200358 mch_memmove(screenlineC[i] + r * cols,
359 ScreenLinesC[i] + LineOffset[cmdline_row + r],
360 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200361 }
362 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200363 mch_memmove(screenline2 + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200364 ScreenLines2 + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200365 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200366 }
367
368 update_screen(0);
369 ret = 3;
370
371 if (must_redraw == 0)
372 {
373 int off = (int)(current_ScreenLine - ScreenLines);
374
375 /* Restore the text displayed in the command line area. */
376 for (r = 0; r < rows; ++r)
377 {
378 mch_memmove(current_ScreenLine,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200379 screenline + r * cols,
380 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200381 mch_memmove(ScreenAttrs + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200382 screenattr + r * cols,
383 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200384 if (enc_utf8)
385 {
386 mch_memmove(ScreenLinesUC + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200387 screenlineUC + r * cols,
388 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200389 for (i = 0; i < p_mco; ++i)
390 mch_memmove(ScreenLinesC[i] + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200391 screenlineC[i] + r * cols,
392 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200393 }
394 if (enc_dbcs == DBCS_JPNU)
395 mch_memmove(ScreenLines2 + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200396 screenline2 + r * cols,
397 (size_t)cols * sizeof(schar_T));
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200398 screen_line(cmdline_row + r, 0, cols, cols, 0);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200399 }
400 ret = 4;
401 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200402 }
403
404 vim_free(screenline);
405 vim_free(screenattr);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200406 if (enc_utf8)
407 {
408 vim_free(screenlineUC);
409 for (i = 0; i < p_mco; ++i)
410 vim_free(screenlineC[i]);
411 }
412 if (enc_dbcs == DBCS_JPNU)
413 vim_free(screenline2);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200414
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200415 /* Show the intro message when appropriate. */
416 maybe_intro_message();
417
418 setcursor();
419
Bram Moolenaar2951b772013-07-03 12:45:31 +0200420 return ret;
421}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100422#endif
Bram Moolenaar2951b772013-07-03 12:45:31 +0200423
424/*
Bram Moolenaar975b5272016-03-15 23:10:59 +0100425 * Invoked after an asynchronous callback is called.
426 * If an echo command was used the cursor needs to be put back where
427 * it belongs. If highlighting was changed a redraw is needed.
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200428 * If "call_update_screen" is FALSE don't call update_screen() when at the
429 * command line.
Bram Moolenaar975b5272016-03-15 23:10:59 +0100430 */
431 void
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200432redraw_after_callback(int call_update_screen)
Bram Moolenaar975b5272016-03-15 23:10:59 +0100433{
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200434 ++redrawing_for_callback;
435
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100436 if (State == HITRETURN || State == ASKMORE)
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200437 ; // do nothing
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100438 else if (State & CMDLINE)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200439 {
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200440 // Don't redraw when in prompt_for_number().
441 if (cmdline_row > 0)
442 {
443 // Redrawing only works when the screen didn't scroll. Don't clear
444 // wildmenu entries.
445 if (msg_scrolled == 0
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200446#ifdef FEAT_WILDMENU
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200447 && wild_menu_showing == 0
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200448#endif
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200449 && call_update_screen)
450 update_screen(0);
451
452 // Redraw in the same position, so that the user can continue
453 // editing the command.
454 redrawcmdline_ex(FALSE);
455 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200456 }
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200457 else if (State & (NORMAL | INSERT | TERMINAL))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100458 {
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200459 // keep the command line if possible
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200460 update_screen(VALID_NO_UPDATE);
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100461 setcursor();
462 }
Bram Moolenaar975b5272016-03-15 23:10:59 +0100463 cursor_on();
Bram Moolenaar975b5272016-03-15 23:10:59 +0100464#ifdef FEAT_GUI
Bram Moolenaara338adc2018-01-31 20:51:47 +0100465 if (gui.in_use && !gui_mch_is_blink_off())
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200466 // Don't update the cursor when it is blinking and off to avoid
467 // flicker.
Bram Moolenaara338adc2018-01-31 20:51:47 +0100468 out_flush_cursor(FALSE, FALSE);
469 else
Bram Moolenaar975b5272016-03-15 23:10:59 +0100470#endif
Bram Moolenaaracda04f2018-02-08 09:57:28 +0100471 out_flush();
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200472
473 --redrawing_for_callback;
Bram Moolenaar975b5272016-03-15 23:10:59 +0100474}
475
476/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477 * Changed something in the current window, at buffer line "lnum", that
478 * requires that line and possibly other lines to be redrawn.
479 * Used when entering/leaving Insert mode with the cursor on a folded line.
480 * Used to remove the "$" from a change command.
481 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
482 * may become invalid and the whole window will have to be redrawn.
483 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100485redrawWinline(
Bram Moolenaar90a99792018-09-12 21:52:18 +0200486 win_T *wp,
Bram Moolenaarae12f4b2019-01-09 20:51:04 +0100487 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488{
Bram Moolenaar90a99792018-09-12 21:52:18 +0200489 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
490 wp->w_redraw_top = lnum;
491 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
492 wp->w_redraw_bot = lnum;
493 redraw_win_later(wp, VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494}
495
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200496/*
497 * To be called when "updating_screen" was set before and now the postponed
498 * side effects may take place.
499 */
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200500 void
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200501after_updating_screen(int may_resize_shell UNUSED)
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200502{
503 updating_screen = FALSE;
504#ifdef FEAT_GUI
505 if (may_resize_shell)
506 gui_may_resize_shell();
507#endif
508#ifdef FEAT_TERMINAL
509 term_check_channel_closed_recently();
510#endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +0200511
512#ifdef HAVE_DROP_FILE
513 // If handle_drop() was called while updating_screen was TRUE need to
514 // handle the drop now.
515 handle_any_postponed_drop();
516#endif
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200517}
518
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519/*
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200520 * Update all windows that are editing the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521 */
522 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100523update_curbuf(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524{
525 redraw_curbuf_later(type);
526 update_screen(type);
527}
528
529/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 * Based on the current value of curwin->w_topline, transfer a screenfull
531 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
Bram Moolenaar072412e2017-09-13 22:11:35 +0200532 * Return OK when the screen was updated, FAIL if it was not done.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533 */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200534 int
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200535update_screen(int type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536{
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200537 int type = type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538 win_T *wp;
539 static int did_intro = FALSE;
540#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
541 int did_one;
542#endif
Bram Moolenaar144445d2016-07-08 21:41:54 +0200543#ifdef FEAT_GUI
Bram Moolenaar107abd22016-08-12 14:08:25 +0200544 int did_undraw = FALSE;
Bram Moolenaar1f3601e2019-04-26 20:33:00 +0200545 int gui_cursor_col = 0;
546 int gui_cursor_row = 0;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200547#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200548 int no_update = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000550 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 if (!screen_valid(TRUE))
Bram Moolenaar072412e2017-09-13 22:11:35 +0200552 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200554 if (type == VALID_NO_UPDATE)
555 {
556 no_update = TRUE;
557 type = 0;
558 }
559
Bram Moolenaara3347722019-05-11 21:14:24 +0200560#ifdef FEAT_EVAL
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200561 {
562 buf_T *buf;
563
564 // Before updating the screen, notify any listeners of changed text.
565 FOR_ALL_BUFFERS(buf)
566 invoke_listeners(buf);
567 }
Bram Moolenaara3347722019-05-11 21:14:24 +0200568#endif
569
Bram Moolenaar4f57eef2019-08-24 20:54:19 +0200570#ifdef FEAT_DIFF
571 // May have postponed updating diffs.
572 if (need_diff_redraw)
573 diff_redraw(TRUE);
574#endif
575
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576 if (must_redraw)
577 {
578 if (type < must_redraw) /* use maximal type */
579 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000580
581 /* must_redraw is reset here, so that when we run into some weird
582 * reason to redraw while busy redrawing (e.g., asynchronous
583 * scrolling), or update_topline() in win_update() will cause a
584 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585 must_redraw = 0;
586 }
587
Bram Moolenaar6eddadf2018-05-06 16:40:16 +0200588 /* May need to update w_lines[]. */
589 if (curwin->w_lines_valid == 0 && type < NOT_VALID
590#ifdef FEAT_TERMINAL
591 && !term_do_update_window(curwin)
592#endif
593 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 type = NOT_VALID;
595
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000596 /* Postpone the redrawing when it's not needed and when being called
597 * recursively. */
598 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 {
600 redraw_later(type); /* remember type for next time */
601 must_redraw = type;
602 if (type > INVERTED_ALL)
603 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200604 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605 }
Bram Moolenaarec572ad2019-07-07 14:26:59 +0200606 updating_screen = TRUE;
Bram Moolenaar33796b32019-06-08 16:01:13 +0200607
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200608#ifdef FEAT_TEXT_PROP
Bram Moolenaar4c063a02019-06-10 21:24:12 +0200609 // Update popup_mask if needed. This may set w_redraw_top and w_redraw_bot
610 // in some windows.
611 may_update_popup_mask(type);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200612#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614#ifdef FEAT_SYN_HL
615 ++display_tick; /* let syntax code know we're in a next round of
616 * display updating */
617#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200618 if (no_update)
619 ++no_win_do_lines_ins;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620
621 /*
622 * if the screen was scrolled up when displaying a message, scroll it down
623 */
624 if (msg_scrolled)
625 {
626 clear_cmdline = TRUE;
627 if (msg_scrolled > Rows - 5) /* clearing is faster */
628 type = CLEAR;
629 else if (type != CLEAR)
630 {
631 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200632 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, 0, NULL)
633 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 type = CLEAR;
635 FOR_ALL_WINDOWS(wp)
636 {
Bram Moolenaar98fb65c2019-06-02 20:33:32 +0200637 if (wp->w_winrow < msg_scrolled)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 {
639 if (W_WINROW(wp) + wp->w_height > msg_scrolled
640 && wp->w_redr_type < REDRAW_TOP
641 && wp->w_lines_valid > 0
642 && wp->w_topline == wp->w_lines[0].wl_lnum)
643 {
644 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
645 wp->w_redr_type = REDRAW_TOP;
646 }
647 else
648 {
649 wp->w_redr_type = NOT_VALID;
Bram Moolenaare0de17d2017-09-24 16:24:34 +0200650 if (W_WINROW(wp) + wp->w_height + wp->w_status_height
651 <= msg_scrolled)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 }
654 }
655 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200656 if (!no_update)
657 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000658 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 }
660 msg_scrolled = 0;
661 need_wait_return = FALSE;
662 }
663
664 /* reset cmdline_row now (may have been changed temporarily) */
665 compute_cmdrow();
666
667 /* Check for changed highlighting */
668 if (need_highlight_changed)
669 highlight_changed();
670
671 if (type == CLEAR) /* first clear screen */
672 {
673 screenclear(); /* will reset clear_cmdline */
674 type = NOT_VALID;
Bram Moolenaar9f5f7bf2017-06-28 20:45:26 +0200675 /* must_redraw may be set indirectly, avoid another redraw later */
676 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 }
678
679 if (clear_cmdline) /* going to clear cmdline (done below) */
680 check_for_delay(FALSE);
681
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000682#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200683 /* Force redraw when width of 'number' or 'relativenumber' column
684 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000685 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200686 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
687 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000688 curwin->w_redr_type = NOT_VALID;
689#endif
690
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 /*
692 * Only start redrawing if there is really something to do.
693 */
694 if (type == INVERTED)
695 update_curswant();
696 if (curwin->w_redr_type < type
697 && !((type == VALID
698 && curwin->w_lines[0].wl_valid
699#ifdef FEAT_DIFF
700 && curwin->w_topfill == curwin->w_old_topfill
701 && curwin->w_botfill == curwin->w_old_botfill
702#endif
703 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000705 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
707 && curwin->w_old_visual_mode == VIsual_mode
708 && (curwin->w_valid & VALID_VIRTCOL)
709 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 ))
711 curwin->w_redr_type = type;
712
Bram Moolenaar5a305422006-04-28 22:38:25 +0000713 /* Redraw the tab pages line if needed. */
714 if (redraw_tabline || type >= NOT_VALID)
715 draw_tabline();
Bram Moolenaar5a305422006-04-28 22:38:25 +0000716
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717#ifdef FEAT_SYN_HL
718 /*
719 * Correct stored syntax highlighting info for changes in each displayed
720 * buffer. Each buffer must only be done once.
721 */
722 FOR_ALL_WINDOWS(wp)
723 {
724 if (wp->w_buffer->b_mod_set)
725 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 win_T *wwp;
727
728 /* Check if we already did this buffer. */
729 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
730 if (wwp->w_buffer == wp->w_buffer)
731 break;
Bram Moolenaar4033c552017-09-16 20:54:51 +0200732 if (wwp == wp && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 syn_stack_apply_changes(wp->w_buffer);
734 }
735 }
736#endif
737
738 /*
739 * Go from top to bottom through the windows, redrawing the ones that need
740 * it.
741 */
742#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
743 did_one = FALSE;
744#endif
745#ifdef FEAT_SEARCH_EXTRA
746 search_hl.rm.regprog = NULL;
747#endif
748 FOR_ALL_WINDOWS(wp)
749 {
750 if (wp->w_redr_type != 0)
751 {
752 cursor_off();
753#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
754 if (!did_one)
755 {
756 did_one = TRUE;
757# ifdef FEAT_SEARCH_EXTRA
758 start_search_hl();
759# endif
760# ifdef FEAT_CLIPBOARD
761 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200762 if (clip_star.available && clip_isautosel_star())
763 clip_update_selection(&clip_star);
764 if (clip_plus.available && clip_isautosel_plus())
765 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766# endif
767#ifdef FEAT_GUI
768 /* Remove the cursor before starting to do anything, because
769 * scrolling may make it difficult to redraw the text under
770 * it. */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200771 if (gui.in_use && wp == curwin)
Bram Moolenaar144445d2016-07-08 21:41:54 +0200772 {
773 gui_cursor_col = gui.cursor_col;
774 gui_cursor_row = gui.cursor_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 gui_undraw_cursor();
Bram Moolenaar107abd22016-08-12 14:08:25 +0200776 did_undraw = TRUE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200777 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778#endif
779 }
780#endif
781 win_update(wp);
782 }
783
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 /* redraw status line after the window to minimize cursor movement */
785 if (wp->w_redr_status)
786 {
787 cursor_off();
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200788 win_redr_status(wp, TRUE); // any popup menu will be redrawn below
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 }
791#if defined(FEAT_SEARCH_EXTRA)
792 end_search_hl();
793#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100794 /* May need to redraw the popup menu. */
Bram Moolenaar491ac282018-06-17 14:47:55 +0200795 pum_may_redraw();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 /* Reset b_mod_set flags. Going through all windows is probably faster
798 * than going through all buffers (there could be many buffers). */
Bram Moolenaar29323592016-07-24 22:04:11 +0200799 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 wp->w_buffer->b_mod_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +0200802#ifdef FEAT_TEXT_PROP
803 // Display popup windows on top of the windows and command line.
804 update_popups(win_update);
805#endif
806
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200807 after_updating_screen(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808
809 /* Clear or redraw the command line. Done last, because scrolling may
810 * mess up the command line. */
Bram Moolenaar4c25bd72019-04-20 23:38:07 +0200811 if (clear_cmdline || redraw_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 showmode();
813
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200814 if (no_update)
815 --no_win_do_lines_ins;
816
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200818 if (!did_intro)
819 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 did_intro = TRUE;
821
822#ifdef FEAT_GUI
823 /* Redraw the cursor and update the scrollbars when all screen updating is
824 * done. */
825 if (gui.in_use)
826 {
Bram Moolenaar107abd22016-08-12 14:08:25 +0200827 if (did_undraw && !gui_mch_is_blink_off())
Bram Moolenaar144445d2016-07-08 21:41:54 +0200828 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100829 mch_disable_flush();
830 out_flush(); /* required before updating the cursor */
831 mch_enable_flush();
832
Bram Moolenaar144445d2016-07-08 21:41:54 +0200833 /* Put the GUI position where the cursor was, gui_update_cursor()
834 * uses that. */
835 gui.col = gui_cursor_col;
836 gui.row = gui_cursor_row;
Bram Moolenaar84dbd492016-10-02 23:09:31 +0200837 gui.col = mb_fix_col(gui.col, gui.row);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara338adc2018-01-31 20:51:47 +0100839 gui_may_flush();
Bram Moolenaar65549bd2016-07-08 22:52:37 +0200840 screen_cur_col = gui.col;
841 screen_cur_row = gui.row;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200842 }
Bram Moolenaara338adc2018-01-31 20:51:47 +0100843 else
844 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 gui_update_scrollbars(FALSE);
846 }
847#endif
Bram Moolenaar072412e2017-09-13 22:11:35 +0200848 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849}
850
Bram Moolenaar1fa8fdd2019-02-25 05:41:15 +0100851#if defined(FEAT_NETBEANS_INTG) || defined(FEAT_GUI)
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100852/*
853 * Prepare for updating one or more windows.
854 * Caller must check for "updating_screen" already set to avoid recursiveness.
855 */
856 static void
857update_prepare(void)
858{
859 cursor_off();
860 updating_screen = TRUE;
861#ifdef FEAT_GUI
862 /* Remove the cursor before starting to do anything, because scrolling may
863 * make it difficult to redraw the text under it. */
864 if (gui.in_use)
865 gui_undraw_cursor();
866#endif
867#ifdef FEAT_SEARCH_EXTRA
868 start_search_hl();
869#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +0200870#ifdef FEAT_TEXT_PROP
871 // Update popup_mask if needed.
Bram Moolenaar68acb412019-06-26 03:40:36 +0200872 may_update_popup_mask(must_redraw);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200873#endif
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100874}
875
876/*
877 * Finish updating one or more windows.
878 */
879 static void
880update_finish(void)
881{
Bram Moolenaar4c25bd72019-04-20 23:38:07 +0200882 if (redraw_cmdline || redraw_mode)
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100883 showmode();
884
885# ifdef FEAT_SEARCH_EXTRA
886 end_search_hl();
887# endif
888
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200889 after_updating_screen(TRUE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100890
891# ifdef FEAT_GUI
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100892 /* Redraw the cursor and update the scrollbars when all screen updating is
893 * done. */
894 if (gui.in_use)
895 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100896 out_flush_cursor(FALSE, FALSE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100897 gui_update_scrollbars(FALSE);
898 }
899# endif
900}
901#endif
902
Bram Moolenaar860cae12010-06-05 23:22:07 +0200903#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200904/*
905 * Return TRUE if the cursor line in window "wp" may be concealed, according
906 * to the 'concealcursor' option.
907 */
908 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100909conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200910{
911 int c;
912
913 if (*wp->w_p_cocu == NUL)
914 return FALSE;
915 if (get_real_state() & VISUAL)
916 c = 'v';
917 else if (State & INSERT)
918 c = 'i';
919 else if (State & NORMAL)
920 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200921 else if (State & CMDLINE)
922 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200923 else
924 return FALSE;
925 return vim_strchr(wp->w_p_cocu, c) != NULL;
926}
927
928/*
929 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
930 */
931 void
Bram Moolenaarb9464822018-05-10 15:09:49 +0200932conceal_check_cursor_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200933{
934 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
935 {
936 need_cursor_line_redraw = TRUE;
937 /* Need to recompute cursor column, e.g., when starting Visual mode
938 * without concealing. */
939 curs_columns(TRUE);
940 }
941}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942#endif
943
Bram Moolenaar113e1072019-01-20 15:30:40 +0100944#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100946update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947{
948 win_T *wp;
949 int doit = FALSE;
950
951# ifdef FEAT_FOLDING
952 win_foldinfo.fi_level = 0;
953# endif
954
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100955 // update/delete a specific sign
956 redraw_buf_line_later(buf, lnum);
957
958 // check if it resulted in the need to redraw a window
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 if (wp->w_redr_type != 0)
961 doit = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100963 /* Return when there is nothing to do, screen updating is already
Bram Moolenaar07920482017-08-01 20:53:30 +0200964 * happening (recursive call), messages on the screen or still starting up.
965 */
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100966 if (!doit || updating_screen
Bram Moolenaar07920482017-08-01 20:53:30 +0200967 || State == ASKMORE || State == HITRETURN
968 || msg_scrolled
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100969#ifdef FEAT_GUI
970 || gui.starting
971#endif
972 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 return;
974
975 /* update all windows that need updating */
976 update_prepare();
977
Bram Moolenaar29323592016-07-24 22:04:11 +0200978 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 {
980 if (wp->w_redr_type != 0)
981 win_update(wp);
982 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200983 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985
986 update_finish();
987}
988#endif
989
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200990/*
991 * Get 'wincolor' attribute for window "wp". If not set and "wp" is a popup
992 * window then get the "Pmenu" highlight attribute.
993 */
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200994 int
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200995get_wcr_attr(win_T *wp)
996{
997 int wcr_attr = 0;
998
999 if (*wp->w_p_wcr != NUL)
1000 wcr_attr = syn_name2attr(wp->w_p_wcr);
1001#ifdef FEAT_TEXT_PROP
Bram Moolenaarc363fe12019-08-04 18:13:46 +02001002 else if (WIN_IS_POPUP(wp))
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001003 {
1004 if (wp->w_popup_flags & POPF_INFO)
1005 wcr_attr = HL_ATTR(HLF_PSI); // PmenuSel
1006 else
1007 wcr_attr = HL_ATTR(HLF_PNI); // Pmenu
1008 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001009#endif
1010 return wcr_attr;
1011}
1012
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013#if defined(FEAT_GUI) || defined(PROTO)
1014/*
1015 * Update a single window, its status line and maybe the command line msg.
1016 * Used for the GUI scrollbar.
1017 */
1018 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001019updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020{
Bram Moolenaar19f990e2009-11-25 12:08:03 +00001021 /* return if already busy updating */
1022 if (updating_screen)
1023 return;
1024
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 update_prepare();
1026
1027#ifdef FEAT_CLIPBOARD
1028 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001029 if (clip_star.available && clip_isautosel_star())
1030 clip_update_selection(&clip_star);
1031 if (clip_plus.available && clip_isautosel_plus())
1032 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001034
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001036
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001037 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001038 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001039 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001040
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 if (wp->w_redr_status
1042# ifdef FEAT_CMDL_INFO
1043 || p_ru
1044# endif
1045# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001046 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047# endif
1048 )
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001049 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050
Bram Moolenaar988c4332019-06-02 14:12:11 +02001051#ifdef FEAT_TEXT_PROP
1052 // Display popup windows on top of everything.
Bram Moolenaara540f8a2019-06-14 19:23:57 +02001053 update_popups(win_update);
Bram Moolenaar988c4332019-06-02 14:12:11 +02001054#endif
1055
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 update_finish();
1057}
1058#endif
1059
1060/*
1061 * Update a single window.
1062 *
1063 * This may cause the windows below it also to be redrawn (when clearing the
1064 * screen or scrolling lines).
1065 *
1066 * How the window is redrawn depends on wp->w_redr_type. Each type also
1067 * implies the one below it.
1068 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001069 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1071 * INVERTED redraw the changed part of the Visual area
1072 * INVERTED_ALL redraw the whole Visual area
1073 * VALID 1. scroll up/down to adjust for a changed w_topline
1074 * 2. update lines at the top when scrolled down
1075 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001076 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 * b_mod_top and b_mod_bot.
1078 * - if wp->w_redraw_top non-zero, redraw lines between
1079 * wp->w_redraw_top and wp->w_redr_bot.
1080 * - continue redrawing when syntax status is invalid.
1081 * 4. if scrolled up, update lines at the bottom.
1082 * This results in three areas that may need updating:
1083 * top: from first row to top_end (when scrolled down)
1084 * mid: from mid_start to mid_end (update inversion or changed text)
1085 * bot: from bot_start to last row (when scrolled up)
1086 */
1087 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001088win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089{
1090 buf_T *buf = wp->w_buffer;
1091 int type;
1092 int top_end = 0; /* Below last row of the top area that needs
1093 updating. 0 when no top area updating. */
1094 int mid_start = 999;/* first row of the mid area that needs
1095 updating. 999 when no mid area updating. */
1096 int mid_end = 0; /* Below last row of the mid area that needs
1097 updating. 0 when no mid area updating. */
1098 int bot_start = 999;/* first row of the bot area that needs
1099 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 int scrolled_down = FALSE; /* TRUE when scrolled down when
1101 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarfbfb7572019-07-25 20:53:03 +02001103 int top_to_mod = FALSE; // redraw above mod_top
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104#endif
1105
1106 int row; /* current window row to display */
1107 linenr_T lnum; /* current buffer lnum to display */
1108 int idx; /* current index in w_lines[] */
1109 int srow; /* starting row of the current line */
1110
1111 int eof = FALSE; /* if TRUE, we hit the end of the file */
1112 int didline = FALSE; /* if TRUE, we finished the last line */
1113 int i;
1114 long j;
1115 static int recursive = FALSE; /* being called recursively */
1116 int old_botline = wp->w_botline;
1117#ifdef FEAT_FOLDING
1118 long fold_count;
1119#endif
1120#ifdef FEAT_SYN_HL
1121 /* remember what happened to the previous line, to know if
1122 * check_visual_highlight() can be used */
1123#define DID_NONE 1 /* didn't update a line */
1124#define DID_LINE 2 /* updated a normal line */
1125#define DID_FOLD 3 /* updated a folded line */
1126 int did_update = DID_NONE;
1127 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1128#endif
1129 linenr_T mod_top = 0;
1130 linenr_T mod_bot = 0;
1131#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1132 int save_got_int;
1133#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001134#ifdef SYN_TIME_LIMIT
1135 proftime_T syntax_tm;
1136#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137
1138 type = wp->w_redr_type;
1139
1140 if (type == NOT_VALID)
1141 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 wp->w_lines_valid = 0;
1144 }
1145
1146 /* Window is zero-height: nothing to draw. */
Bram Moolenaar415a6932017-12-05 20:31:07 +01001147 if (wp->w_height + WINBAR_HEIGHT(wp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 {
1149 wp->w_redr_type = 0;
1150 return;
1151 }
1152
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 /* Window is zero-width: Only need to draw the separator. */
1154 if (wp->w_width == 0)
1155 {
1156 /* draw the vertical separator right of this window */
1157 draw_vsep_win(wp, 0);
1158 wp->w_redr_type = 0;
1159 return;
1160 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001162#ifdef FEAT_TERMINAL
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001163 // If this window contains a terminal, redraw works completely differently.
1164 if (term_do_update_window(wp))
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001165 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001166 term_update_window(wp);
Bram Moolenaar181ca992018-02-13 21:19:21 +01001167# ifdef FEAT_MENU
1168 /* Draw the window toolbar, if there is one. */
1169 if (winbar_height(wp) > 0)
1170 redraw_win_toolbar(wp);
1171# endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001172 wp->w_redr_type = 0;
1173 return;
1174 }
1175#endif
1176
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarbbca7732019-07-24 18:13:16 +02001178 init_search_hl(wp, &search_hl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179#endif
1180
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001181#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001182 /* Force redraw when width of 'number' or 'relativenumber' column
1183 * changes. */
1184 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001185 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001186 {
1187 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001188 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001189 }
1190 else
1191#endif
1192
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1194 {
1195 /*
1196 * When there are both inserted/deleted lines and specific lines to be
1197 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1198 * everything (only happens when redrawing is off for while).
1199 */
1200 type = NOT_VALID;
1201 }
1202 else
1203 {
1204 /*
1205 * Set mod_top to the first line that needs displaying because of
1206 * changes. Set mod_bot to the first line after the changes.
1207 */
1208 mod_top = wp->w_redraw_top;
1209 if (wp->w_redraw_bot != 0)
1210 mod_bot = wp->w_redraw_bot + 1;
1211 else
1212 mod_bot = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 if (buf->b_mod_set)
1214 {
1215 if (mod_top == 0 || mod_top > buf->b_mod_top)
1216 {
1217 mod_top = buf->b_mod_top;
1218#ifdef FEAT_SYN_HL
1219 /* Need to redraw lines above the change that may be included
1220 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001221 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001223 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 if (mod_top < 1)
1225 mod_top = 1;
1226 }
1227#endif
1228 }
1229 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1230 mod_bot = buf->b_mod_bot;
1231
1232#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarfbfb7572019-07-25 20:53:03 +02001233 // When 'hlsearch' is on and using a multi-line search pattern, a
1234 // change in one line may make the Search highlighting in a
1235 // previous line invalid. Simple solution: redraw all visible
1236 // lines above the change.
1237 // Same for a match pattern.
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001238 if (search_hl.rm.regprog != NULL
1239 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001241 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001242 {
Bram Moolenaarfbfb7572019-07-25 20:53:03 +02001243 matchitem_T *cur = wp->w_match_head;
1244
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001245 while (cur != NULL)
1246 {
1247 if (cur->match.regprog != NULL
1248 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001249 {
1250 top_to_mod = TRUE;
1251 break;
1252 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001253 cur = cur->next;
1254 }
1255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256#endif
1257 }
1258#ifdef FEAT_FOLDING
1259 if (mod_top != 0 && hasAnyFolding(wp))
1260 {
1261 linenr_T lnumt, lnumb;
1262
1263 /*
1264 * A change in a line can cause lines above it to become folded or
1265 * unfolded. Find the top most buffer line that may be affected.
1266 * If the line was previously folded and displayed, get the first
1267 * line of that fold. If the line is folded now, get the first
1268 * folded line. Use the minimum of these two.
1269 */
1270
1271 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1272 * the line below it. If there is no valid entry, use w_topline.
1273 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1274 * to this line. If there is no valid entry, use MAXLNUM. */
1275 lnumt = wp->w_topline;
1276 lnumb = MAXLNUM;
1277 for (i = 0; i < wp->w_lines_valid; ++i)
1278 if (wp->w_lines[i].wl_valid)
1279 {
1280 if (wp->w_lines[i].wl_lastlnum < mod_top)
1281 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1282 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1283 {
1284 lnumb = wp->w_lines[i].wl_lnum;
1285 /* When there is a fold column it might need updating
1286 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001287 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 ++lnumb;
1289 }
1290 }
1291
1292 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1293 if (mod_top > lnumt)
1294 mod_top = lnumt;
1295
1296 /* Now do the same for the bottom line (one above mod_bot). */
1297 --mod_bot;
1298 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1299 ++mod_bot;
1300 if (mod_bot < lnumb)
1301 mod_bot = lnumb;
1302 }
1303#endif
1304
1305 /* When a change starts above w_topline and the end is below
1306 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001307 * If the end of the change is above w_topline: do like no change was
1308 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 if (mod_top != 0 && mod_top < wp->w_topline)
1310 {
1311 if (mod_bot > wp->w_topline)
1312 mod_top = wp->w_topline;
1313#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001314 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 top_end = 1;
1316#endif
1317 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001318
1319 /* When line numbers are displayed need to redraw all lines below
1320 * inserted/deleted lines. */
1321 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1322 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 }
Bram Moolenaar895d9662019-01-31 21:57:21 +01001324 wp->w_redraw_top = 0; // reset for next time
1325 wp->w_redraw_bot = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326
1327 /*
1328 * When only displaying the lines at the top, set top_end. Used when
1329 * window has scrolled down for msg_scrolled.
1330 */
1331 if (type == REDRAW_TOP)
1332 {
1333 j = 0;
1334 for (i = 0; i < wp->w_lines_valid; ++i)
1335 {
1336 j += wp->w_lines[i].wl_size;
1337 if (j >= wp->w_upd_rows)
1338 {
1339 top_end = j;
1340 break;
1341 }
1342 }
1343 if (top_end == 0)
1344 /* not found (cannot happen?): redraw everything */
1345 type = NOT_VALID;
1346 else
1347 /* top area defined, the rest is VALID */
1348 type = VALID;
1349 }
1350
Bram Moolenaar367329b2007-08-30 11:53:22 +00001351 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001352 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1353 * non-zero and thus not FALSE) will indicate that screenclear() was not
1354 * called. */
1355 if (screen_cleared)
1356 screen_cleared = MAYBE;
1357
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 /*
1359 * If there are no changes on the screen that require a complete redraw,
1360 * handle three cases:
1361 * 1: we are off the top of the screen by a few lines: scroll down
1362 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1363 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1364 * w_lines[] that needs updating.
1365 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001366 if ((type == VALID || type == SOME_VALID
1367 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368#ifdef FEAT_DIFF
1369 && !wp->w_botfill && !wp->w_old_botfill
1370#endif
1371 )
1372 {
1373 if (mod_top != 0 && wp->w_topline == mod_top)
1374 {
1375 /*
1376 * w_topline is the first changed line, the scrolling will be done
1377 * further down.
1378 */
1379 }
1380 else if (wp->w_lines[0].wl_valid
1381 && (wp->w_topline < wp->w_lines[0].wl_lnum
1382#ifdef FEAT_DIFF
1383 || (wp->w_topline == wp->w_lines[0].wl_lnum
1384 && wp->w_topfill > wp->w_old_topfill)
1385#endif
1386 ))
1387 {
1388 /*
1389 * New topline is above old topline: May scroll down.
1390 */
1391#ifdef FEAT_FOLDING
1392 if (hasAnyFolding(wp))
1393 {
1394 linenr_T ln;
1395
1396 /* count the number of lines we are off, counting a sequence
1397 * of folded lines as one */
1398 j = 0;
1399 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1400 {
1401 ++j;
1402 if (j >= wp->w_height - 2)
1403 break;
1404 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1405 }
1406 }
1407 else
1408#endif
1409 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1410 if (j < wp->w_height - 2) /* not too far off */
1411 {
1412 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1413#ifdef FEAT_DIFF
1414 /* insert extra lines for previously invisible filler lines */
1415 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1416 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1417 - wp->w_old_topfill;
1418#endif
1419 if (i < wp->w_height - 2) /* less than a screen off */
1420 {
1421 /*
1422 * Try to insert the correct number of lines.
1423 * If not the last window, delete the lines at the bottom.
1424 * win_ins_lines may fail when the terminal can't do it.
1425 */
1426 if (i > 0)
1427 check_for_delay(FALSE);
1428 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1429 {
1430 if (wp->w_lines_valid != 0)
1431 {
1432 /* Need to update rows that are new, stop at the
1433 * first one that scrolled down. */
1434 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436
1437 /* Move the entries that were scrolled, disable
1438 * the entries for the lines to be redrawn. */
1439 if ((wp->w_lines_valid += j) > wp->w_height)
1440 wp->w_lines_valid = wp->w_height;
1441 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1442 wp->w_lines[idx] = wp->w_lines[idx - j];
1443 while (idx >= 0)
1444 wp->w_lines[idx--].wl_valid = FALSE;
1445 }
1446 }
1447 else
1448 mid_start = 0; /* redraw all lines */
1449 }
1450 else
1451 mid_start = 0; /* redraw all lines */
1452 }
1453 else
1454 mid_start = 0; /* redraw all lines */
1455 }
1456 else
1457 {
1458 /*
1459 * New topline is at or below old topline: May scroll up.
1460 * When topline didn't change, find first entry in w_lines[] that
1461 * needs updating.
1462 */
1463
1464 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1465 j = -1;
1466 row = 0;
1467 for (i = 0; i < wp->w_lines_valid; i++)
1468 {
1469 if (wp->w_lines[i].wl_valid
1470 && wp->w_lines[i].wl_lnum == wp->w_topline)
1471 {
1472 j = i;
1473 break;
1474 }
1475 row += wp->w_lines[i].wl_size;
1476 }
1477 if (j == -1)
1478 {
1479 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1480 * lines */
1481 mid_start = 0;
1482 }
1483 else
1484 {
1485 /*
1486 * Try to delete the correct number of lines.
1487 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1488 */
1489#ifdef FEAT_DIFF
1490 /* If the topline didn't change, delete old filler lines,
1491 * otherwise delete filler lines of the new topline... */
1492 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1493 row += wp->w_old_topfill;
1494 else
1495 row += diff_check_fill(wp, wp->w_topline);
1496 /* ... but don't delete new filler lines. */
1497 row -= wp->w_topfill;
1498#endif
1499 if (row > 0)
1500 {
1501 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001502 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1503 == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 bot_start = wp->w_height - row;
1505 else
1506 mid_start = 0; /* redraw all lines */
1507 }
1508 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1509 {
1510 /*
1511 * Skip the lines (below the deleted lines) that are still
1512 * valid and don't need redrawing. Copy their info
1513 * upwards, to compensate for the deleted lines. Set
1514 * bot_start to the first row that needs redrawing.
1515 */
1516 bot_start = 0;
1517 idx = 0;
1518 for (;;)
1519 {
1520 wp->w_lines[idx] = wp->w_lines[j];
1521 /* stop at line that didn't fit, unless it is still
1522 * valid (no lines deleted) */
1523 if (row > 0 && bot_start + row
1524 + (int)wp->w_lines[j].wl_size > wp->w_height)
1525 {
1526 wp->w_lines_valid = idx + 1;
1527 break;
1528 }
1529 bot_start += wp->w_lines[idx++].wl_size;
1530
1531 /* stop at the last valid entry in w_lines[].wl_size */
1532 if (++j >= wp->w_lines_valid)
1533 {
1534 wp->w_lines_valid = idx;
1535 break;
1536 }
1537 }
1538#ifdef FEAT_DIFF
1539 /* Correct the first entry for filler lines at the top
1540 * when it won't get updated below. */
1541 if (wp->w_p_diff && bot_start > 0)
1542 wp->w_lines[0].wl_size =
1543 plines_win_nofill(wp, wp->w_topline, TRUE)
1544 + wp->w_topfill;
1545#endif
1546 }
1547 }
1548 }
1549
1550 /* When starting redraw in the first line, redraw all lines. When
1551 * there is only one window it's probably faster to clear the screen
1552 * first. */
1553 if (mid_start == 0)
1554 {
1555 mid_end = wp->w_height;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001556 if (ONE_WINDOW && !WIN_IS_POPUP(wp))
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001557 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001558 /* Clear the screen when it was not done by win_del_lines() or
1559 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1560 * then. */
1561 if (screen_cleared != TRUE)
1562 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001563 /* The screen was cleared, redraw the tab pages line. */
1564 if (redraw_tabline)
1565 draw_tabline();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001568
1569 /* When win_del_lines() or win_ins_lines() caused the screen to be
1570 * cleared (only happens for the first window) or when screenclear()
1571 * was called directly above, "must_redraw" will have been set to
1572 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1573 if (screen_cleared == TRUE)
1574 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 }
1576 else
1577 {
1578 /* Not VALID or INVERTED: redraw all lines. */
1579 mid_start = 0;
1580 mid_end = wp->w_height;
1581 }
1582
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001583 if (type == SOME_VALID)
1584 {
1585 /* SOME_VALID: redraw all lines. */
1586 mid_start = 0;
1587 mid_end = wp->w_height;
1588 type = NOT_VALID;
1589 }
1590
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 /* check if we are updating or removing the inverted part */
1592 if ((VIsual_active && buf == curwin->w_buffer)
1593 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1594 {
1595 linenr_T from, to;
1596
1597 if (VIsual_active)
1598 {
1599 if (VIsual_active
1600 && (VIsual_mode != wp->w_old_visual_mode
1601 || type == INVERTED_ALL))
1602 {
1603 /*
1604 * If the type of Visual selection changed, redraw the whole
1605 * selection. Also when the ownership of the X selection is
1606 * gained or lost.
1607 */
1608 if (curwin->w_cursor.lnum < VIsual.lnum)
1609 {
1610 from = curwin->w_cursor.lnum;
1611 to = VIsual.lnum;
1612 }
1613 else
1614 {
1615 from = VIsual.lnum;
1616 to = curwin->w_cursor.lnum;
1617 }
1618 /* redraw more when the cursor moved as well */
1619 if (wp->w_old_cursor_lnum < from)
1620 from = wp->w_old_cursor_lnum;
1621 if (wp->w_old_cursor_lnum > to)
1622 to = wp->w_old_cursor_lnum;
1623 if (wp->w_old_visual_lnum < from)
1624 from = wp->w_old_visual_lnum;
1625 if (wp->w_old_visual_lnum > to)
1626 to = wp->w_old_visual_lnum;
1627 }
1628 else
1629 {
1630 /*
1631 * Find the line numbers that need to be updated: The lines
1632 * between the old cursor position and the current cursor
1633 * position. Also check if the Visual position changed.
1634 */
1635 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1636 {
1637 from = curwin->w_cursor.lnum;
1638 to = wp->w_old_cursor_lnum;
1639 }
1640 else
1641 {
1642 from = wp->w_old_cursor_lnum;
1643 to = curwin->w_cursor.lnum;
1644 if (from == 0) /* Visual mode just started */
1645 from = to;
1646 }
1647
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001648 if (VIsual.lnum != wp->w_old_visual_lnum
1649 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 {
1651 if (wp->w_old_visual_lnum < from
1652 && wp->w_old_visual_lnum != 0)
1653 from = wp->w_old_visual_lnum;
1654 if (wp->w_old_visual_lnum > to)
1655 to = wp->w_old_visual_lnum;
1656 if (VIsual.lnum < from)
1657 from = VIsual.lnum;
1658 if (VIsual.lnum > to)
1659 to = VIsual.lnum;
1660 }
1661 }
1662
1663 /*
1664 * If in block mode and changed column or curwin->w_curswant:
1665 * update all lines.
1666 * First compute the actual start and end column.
1667 */
1668 if (VIsual_mode == Ctrl_V)
1669 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001670 colnr_T fromc, toc;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001671#if defined(FEAT_LINEBREAK)
Bram Moolenaar404406a2014-10-09 13:24:43 +02001672 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673
Bram Moolenaar404406a2014-10-09 13:24:43 +02001674 if (curwin->w_p_lbr)
1675 ve_flags = VE_ALL;
1676#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001678#if defined(FEAT_LINEBREAK)
Bram Moolenaar404406a2014-10-09 13:24:43 +02001679 ve_flags = save_ve_flags;
1680#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 ++toc;
1682 if (curwin->w_curswant == MAXCOL)
1683 toc = MAXCOL;
1684
1685 if (fromc != wp->w_old_cursor_fcol
1686 || toc != wp->w_old_cursor_lcol)
1687 {
1688 if (from > VIsual.lnum)
1689 from = VIsual.lnum;
1690 if (to < VIsual.lnum)
1691 to = VIsual.lnum;
1692 }
1693 wp->w_old_cursor_fcol = fromc;
1694 wp->w_old_cursor_lcol = toc;
1695 }
1696 }
1697 else
1698 {
1699 /* Use the line numbers of the old Visual area. */
1700 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1701 {
1702 from = wp->w_old_cursor_lnum;
1703 to = wp->w_old_visual_lnum;
1704 }
1705 else
1706 {
1707 from = wp->w_old_visual_lnum;
1708 to = wp->w_old_cursor_lnum;
1709 }
1710 }
1711
1712 /*
1713 * There is no need to update lines above the top of the window.
1714 */
1715 if (from < wp->w_topline)
1716 from = wp->w_topline;
1717
1718 /*
1719 * If we know the value of w_botline, use it to restrict the update to
1720 * the lines that are visible in the window.
1721 */
1722 if (wp->w_valid & VALID_BOTLINE)
1723 {
1724 if (from >= wp->w_botline)
1725 from = wp->w_botline - 1;
1726 if (to >= wp->w_botline)
1727 to = wp->w_botline - 1;
1728 }
1729
1730 /*
1731 * Find the minimal part to be updated.
1732 * Watch out for scrolling that made entries in w_lines[] invalid.
1733 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1734 * top_end; need to redraw from top_end to the "to" line.
1735 * A middle mouse click with a Visual selection may change the text
1736 * above the Visual area and reset wl_valid, do count these for
1737 * mid_end (in srow).
1738 */
1739 if (mid_start > 0)
1740 {
1741 lnum = wp->w_topline;
1742 idx = 0;
1743 srow = 0;
1744 if (scrolled_down)
1745 mid_start = top_end;
1746 else
1747 mid_start = 0;
1748 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1749 {
1750 if (wp->w_lines[idx].wl_valid)
1751 mid_start += wp->w_lines[idx].wl_size;
1752 else if (!scrolled_down)
1753 srow += wp->w_lines[idx].wl_size;
1754 ++idx;
1755# ifdef FEAT_FOLDING
1756 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1757 lnum = wp->w_lines[idx].wl_lnum;
1758 else
1759# endif
1760 ++lnum;
1761 }
1762 srow += mid_start;
1763 mid_end = wp->w_height;
1764 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1765 {
1766 if (wp->w_lines[idx].wl_valid
1767 && wp->w_lines[idx].wl_lnum >= to + 1)
1768 {
1769 /* Only update until first row of this line */
1770 mid_end = srow;
1771 break;
1772 }
1773 srow += wp->w_lines[idx].wl_size;
1774 }
1775 }
1776 }
1777
1778 if (VIsual_active && buf == curwin->w_buffer)
1779 {
1780 wp->w_old_visual_mode = VIsual_mode;
1781 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1782 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001783 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 wp->w_old_curswant = curwin->w_curswant;
1785 }
1786 else
1787 {
1788 wp->w_old_visual_mode = 0;
1789 wp->w_old_cursor_lnum = 0;
1790 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001791 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793
1794#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1795 /* reset got_int, otherwise regexp won't work */
1796 save_got_int = got_int;
1797 got_int = 0;
1798#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001799#ifdef SYN_TIME_LIMIT
1800 /* Set the time limit to 'redrawtime'. */
1801 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02001802 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001803#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804#ifdef FEAT_FOLDING
1805 win_foldinfo.fi_level = 0;
1806#endif
1807
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001808#ifdef FEAT_MENU
1809 /*
1810 * Draw the window toolbar, if there is one.
1811 * TODO: only when needed.
1812 */
1813 if (winbar_height(wp) > 0)
1814 redraw_win_toolbar(wp);
1815#endif
1816
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 /*
1818 * Update all the window rows.
1819 */
1820 idx = 0; /* first entry in w_lines[].wl_size */
1821 row = 0;
1822 srow = 0;
1823 lnum = wp->w_topline; /* first line shown in window */
1824 for (;;)
1825 {
1826 /* stop updating when reached the end of the window (check for _past_
1827 * the end of the window is at the end of the loop) */
1828 if (row == wp->w_height)
1829 {
1830 didline = TRUE;
1831 break;
1832 }
1833
1834 /* stop updating when hit the end of the file */
1835 if (lnum > buf->b_ml.ml_line_count)
1836 {
1837 eof = TRUE;
1838 break;
1839 }
1840
1841 /* Remember the starting row of the line that is going to be dealt
1842 * with. It is used further down when the line doesn't fit. */
1843 srow = row;
1844
1845 /*
1846 * Update a line when it is in an area that needs updating, when it
1847 * has changes or w_lines[idx] is invalid.
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02001848 * "bot_start" may be halfway a wrapped line after using
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 * win_del_lines(), check if the current line includes it.
1850 * When syntax folding is being used, the saved syntax states will
1851 * already have been updated, we can't see where the syntax state is
1852 * the same again, just update until the end of the window.
1853 */
1854 if (row < top_end
1855 || (row >= mid_start && row < mid_end)
1856#ifdef FEAT_SEARCH_EXTRA
1857 || top_to_mod
1858#endif
1859 || idx >= wp->w_lines_valid
1860 || (row + wp->w_lines[idx].wl_size > bot_start)
1861 || (mod_top != 0
1862 && (lnum == mod_top
1863 || (lnum >= mod_top
1864 && (lnum < mod_bot
1865#ifdef FEAT_SYN_HL
1866 || did_update == DID_FOLD
1867 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001868 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 && (
1870# ifdef FEAT_FOLDING
1871 (foldmethodIsSyntax(wp)
1872 && hasAnyFolding(wp)) ||
1873# endif
1874 syntax_check_changed(lnum)))
1875#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001876#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001877 /* match in fixed position might need redraw
1878 * if lines were inserted or deleted */
1879 || (wp->w_match_head != NULL
1880 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001881#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 )))))
1883 {
1884#ifdef FEAT_SEARCH_EXTRA
1885 if (lnum == mod_top)
1886 top_to_mod = FALSE;
1887#endif
1888
1889 /*
1890 * When at start of changed lines: May scroll following lines
1891 * up or down to minimize redrawing.
1892 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001893 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 */
1895 if (lnum == mod_top
1896 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001897 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 {
1899 int old_rows = 0;
1900 int new_rows = 0;
1901 int xtra_rows;
1902 linenr_T l;
1903
1904 /* Count the old number of window rows, using w_lines[], which
1905 * should still contain the sizes for the lines as they are
1906 * currently displayed. */
1907 for (i = idx; i < wp->w_lines_valid; ++i)
1908 {
1909 /* Only valid lines have a meaningful wl_lnum. Invalid
1910 * lines are part of the changed area. */
1911 if (wp->w_lines[i].wl_valid
1912 && wp->w_lines[i].wl_lnum == mod_bot)
1913 break;
1914 old_rows += wp->w_lines[i].wl_size;
1915#ifdef FEAT_FOLDING
1916 if (wp->w_lines[i].wl_valid
1917 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1918 {
1919 /* Must have found the last valid entry above mod_bot.
1920 * Add following invalid entries. */
1921 ++i;
1922 while (i < wp->w_lines_valid
1923 && !wp->w_lines[i].wl_valid)
1924 old_rows += wp->w_lines[i++].wl_size;
1925 break;
1926 }
1927#endif
1928 }
1929
1930 if (i >= wp->w_lines_valid)
1931 {
1932 /* We can't find a valid line below the changed lines,
1933 * need to redraw until the end of the window.
1934 * Inserting/deleting lines has no use. */
1935 bot_start = 0;
1936 }
1937 else
1938 {
1939 /* Able to count old number of rows: Count new window
1940 * rows, and may insert/delete lines */
1941 j = idx;
1942 for (l = lnum; l < mod_bot; ++l)
1943 {
1944#ifdef FEAT_FOLDING
1945 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1946 ++new_rows;
1947 else
1948#endif
1949#ifdef FEAT_DIFF
1950 if (l == wp->w_topline)
1951 new_rows += plines_win_nofill(wp, l, TRUE)
1952 + wp->w_topfill;
1953 else
1954#endif
1955 new_rows += plines_win(wp, l, TRUE);
1956 ++j;
1957 if (new_rows > wp->w_height - row - 2)
1958 {
1959 /* it's getting too much, must redraw the rest */
1960 new_rows = 9999;
1961 break;
1962 }
1963 }
1964 xtra_rows = new_rows - old_rows;
1965 if (xtra_rows < 0)
1966 {
1967 /* May scroll text up. If there is not enough
1968 * remaining text or scrolling fails, must redraw the
1969 * rest. If scrolling works, must redraw the text
1970 * below the scrolled text. */
1971 if (row - xtra_rows >= wp->w_height - 2)
1972 mod_bot = MAXLNUM;
1973 else
1974 {
1975 check_for_delay(FALSE);
1976 if (win_del_lines(wp, row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001977 -xtra_rows, FALSE, FALSE, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 mod_bot = MAXLNUM;
1979 else
1980 bot_start = wp->w_height + xtra_rows;
1981 }
1982 }
1983 else if (xtra_rows > 0)
1984 {
1985 /* May scroll text down. If there is not enough
1986 * remaining text of scrolling fails, must redraw the
1987 * rest. */
1988 if (row + xtra_rows >= wp->w_height - 2)
1989 mod_bot = MAXLNUM;
1990 else
1991 {
1992 check_for_delay(FALSE);
1993 if (win_ins_lines(wp, row + old_rows,
1994 xtra_rows, FALSE, FALSE) == FAIL)
1995 mod_bot = MAXLNUM;
1996 else if (top_end > row + old_rows)
1997 /* Scrolled the part at the top that requires
1998 * updating down. */
1999 top_end += xtra_rows;
2000 }
2001 }
2002
2003 /* When not updating the rest, may need to move w_lines[]
2004 * entries. */
2005 if (mod_bot != MAXLNUM && i != j)
2006 {
2007 if (j < i)
2008 {
2009 int x = row + new_rows;
2010
2011 /* move entries in w_lines[] upwards */
2012 for (;;)
2013 {
2014 /* stop at last valid entry in w_lines[] */
2015 if (i >= wp->w_lines_valid)
2016 {
2017 wp->w_lines_valid = j;
2018 break;
2019 }
2020 wp->w_lines[j] = wp->w_lines[i];
2021 /* stop at a line that won't fit */
2022 if (x + (int)wp->w_lines[j].wl_size
2023 > wp->w_height)
2024 {
2025 wp->w_lines_valid = j + 1;
2026 break;
2027 }
2028 x += wp->w_lines[j++].wl_size;
2029 ++i;
2030 }
2031 if (bot_start > x)
2032 bot_start = x;
2033 }
2034 else /* j > i */
2035 {
2036 /* move entries in w_lines[] downwards */
2037 j -= i;
2038 wp->w_lines_valid += j;
2039 if (wp->w_lines_valid > wp->w_height)
2040 wp->w_lines_valid = wp->w_height;
2041 for (i = wp->w_lines_valid; i - j >= idx; --i)
2042 wp->w_lines[i] = wp->w_lines[i - j];
2043
2044 /* The w_lines[] entries for inserted lines are
2045 * now invalid, but wl_size may be used above.
2046 * Reset to zero. */
2047 while (i >= idx)
2048 {
2049 wp->w_lines[i].wl_size = 0;
2050 wp->w_lines[i--].wl_valid = FALSE;
2051 }
2052 }
2053 }
2054 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 }
2056
2057#ifdef FEAT_FOLDING
2058 /*
2059 * When lines are folded, display one line for all of them.
2060 * Otherwise, display normally (can be several display lines when
2061 * 'wrap' is on).
2062 */
2063 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2064 if (fold_count != 0)
2065 {
2066 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2067 ++row;
2068 --fold_count;
2069 wp->w_lines[idx].wl_folded = TRUE;
2070 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2071# ifdef FEAT_SYN_HL
2072 did_update = DID_FOLD;
2073# endif
2074 }
2075 else
2076#endif
2077 if (idx < wp->w_lines_valid
2078 && wp->w_lines[idx].wl_valid
2079 && wp->w_lines[idx].wl_lnum == lnum
2080 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002081 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002082 && !WIN_IS_POPUP(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 && srow + wp->w_lines[idx].wl_size > wp->w_height
2084#ifdef FEAT_DIFF
2085 && diff_check_fill(wp, lnum) == 0
2086#endif
2087 )
2088 {
2089 /* This line is not going to fit. Don't draw anything here,
2090 * will draw "@ " lines below. */
2091 row = wp->w_height + 1;
2092 }
2093 else
2094 {
2095#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarbbca7732019-07-24 18:13:16 +02002096 prepare_search_hl(wp, &search_hl, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097#endif
2098#ifdef FEAT_SYN_HL
2099 /* Let the syntax stuff know we skipped a few lines. */
2100 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002101 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 syntax_end_parsing(syntax_last_parsed + 1);
2103#endif
2104
2105 /*
2106 * Display one line.
2107 */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002108 row = win_line(wp, lnum, srow, wp->w_height,
2109 mod_top == 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110
2111#ifdef FEAT_FOLDING
2112 wp->w_lines[idx].wl_folded = FALSE;
2113 wp->w_lines[idx].wl_lastlnum = lnum;
2114#endif
2115#ifdef FEAT_SYN_HL
2116 did_update = DID_LINE;
2117 syntax_last_parsed = lnum;
2118#endif
2119 }
2120
2121 wp->w_lines[idx].wl_lnum = lnum;
2122 wp->w_lines[idx].wl_valid = TRUE;
Bram Moolenaar0e19fc02017-10-28 14:45:16 +02002123
2124 /* Past end of the window or end of the screen. Note that after
2125 * resizing wp->w_height may be end up too big. That's a problem
2126 * elsewhere, but prevent a crash here. */
2127 if (row > wp->w_height || row + wp->w_winrow >= Rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 {
2129 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002130 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2132 ++idx;
2133 break;
2134 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002135 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 wp->w_lines[idx].wl_size = row - srow;
2137 ++idx;
2138#ifdef FEAT_FOLDING
2139 lnum += fold_count + 1;
2140#else
2141 ++lnum;
2142#endif
2143 }
2144 else
2145 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002146 if (wp->w_p_rnu)
2147 {
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002148#ifdef FEAT_FOLDING
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002149 // 'relativenumber' set: The text doesn't need to be drawn, but
2150 // the number column nearly always does.
Bram Moolenaar7701f302018-10-02 21:20:32 +02002151 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2152 if (fold_count != 0)
Bram Moolenaar7701f302018-10-02 21:20:32 +02002153 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
Bram Moolenaar7701f302018-10-02 21:20:32 +02002154 else
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002155#endif
Bram Moolenaar7701f302018-10-02 21:20:32 +02002156 (void)win_line(wp, lnum, srow, wp->w_height, TRUE, TRUE);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002157 }
2158
2159 // This line does not need to be drawn, advance to the next one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 row += wp->w_lines[idx++].wl_size;
2161 if (row > wp->w_height) /* past end of screen */
2162 break;
2163#ifdef FEAT_FOLDING
2164 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2165#else
2166 ++lnum;
2167#endif
2168#ifdef FEAT_SYN_HL
2169 did_update = DID_NONE;
2170#endif
2171 }
2172
2173 if (lnum > buf->b_ml.ml_line_count)
2174 {
2175 eof = TRUE;
2176 break;
2177 }
2178 }
2179 /*
2180 * End of loop over all window lines.
2181 */
2182
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002183#ifdef FEAT_VTP
2184 /* Rewrite the character at the end of the screen line. */
2185 if (use_vtp())
2186 {
2187 int i;
2188
2189 for (i = 0; i < Rows; ++i)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002190 if (enc_utf8)
2191 if ((*mb_off2cells)(LineOffset[i] + Columns - 2,
2192 LineOffset[i] + screen_Columns) > 1)
2193 screen_draw_rectangle(i, Columns - 2, 1, 2, FALSE);
2194 else
2195 screen_draw_rectangle(i, Columns - 1, 1, 1, FALSE);
2196 else
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002197 screen_char(LineOffset[i] + Columns - 1, i, Columns - 1);
2198 }
2199#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200
2201 if (idx > wp->w_lines_valid)
2202 wp->w_lines_valid = idx;
2203
2204#ifdef FEAT_SYN_HL
2205 /*
2206 * Let the syntax stuff know we stop parsing here.
2207 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002208 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 syntax_end_parsing(syntax_last_parsed + 1);
2210#endif
2211
2212 /*
2213 * If we didn't hit the end of the file, and we didn't finish the last
2214 * line we were working on, then the line didn't fit.
2215 */
2216 wp->w_empty_rows = 0;
2217#ifdef FEAT_DIFF
2218 wp->w_filler_rows = 0;
2219#endif
2220 if (!eof && !didline)
2221 {
2222 if (lnum == wp->w_topline)
2223 {
2224 /*
2225 * Single line that does not fit!
2226 * Don't overwrite it, it can be edited.
2227 */
2228 wp->w_botline = lnum + 1;
2229 }
2230#ifdef FEAT_DIFF
2231 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2232 {
2233 /* Window ends in filler lines. */
2234 wp->w_botline = lnum;
2235 wp->w_filler_rows = wp->w_height - srow;
2236 }
2237#endif
Bram Moolenaar17146962019-05-30 00:12:11 +02002238#ifdef FEAT_TEXT_PROP
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002239 else if (WIN_IS_POPUP(wp))
Bram Moolenaar17146962019-05-30 00:12:11 +02002240 {
2241 // popup line that doesn't fit is left as-is
2242 wp->w_botline = lnum;
2243 }
2244#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002245 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2246 {
2247 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2248
2249 /*
2250 * Last line isn't finished: Display "@@@" in the last screen line.
2251 */
Bram Moolenaar53f81742017-09-22 14:35:51 +02002252 screen_puts_len((char_u *)"@@", 2, scr_row, wp->w_wincol,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002253 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002254 screen_fill(scr_row, scr_row + 1,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002255 (int)wp->w_wincol + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002256 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002257 set_empty_rows(wp, srow);
2258 wp->w_botline = lnum;
2259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2261 {
2262 /*
2263 * Last line isn't finished: Display "@@@" at the end.
2264 */
2265 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2266 W_WINROW(wp) + wp->w_height,
2267 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002268 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 set_empty_rows(wp, srow);
2270 wp->w_botline = lnum;
2271 }
2272 else
2273 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002274 win_draw_end(wp, '@', ' ', TRUE, srow, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 wp->w_botline = lnum;
2276 }
2277 }
2278 else
2279 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 if (eof) /* we hit the end of the file */
2282 {
2283 wp->w_botline = buf->b_ml.ml_line_count + 1;
2284#ifdef FEAT_DIFF
2285 j = diff_check_fill(wp, wp->w_botline);
2286 if (j > 0 && !wp->w_botfill)
2287 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002288 // Display filler lines at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 if (char2cells(fill_diff) > 1)
2290 i = '-';
2291 else
2292 i = fill_diff;
2293 if (row + j > wp->w_height)
2294 j = wp->w_height - row;
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002295 win_draw_end(wp, i, i, TRUE, row, row + (int)j, HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 row += j;
2297 }
2298#endif
2299 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002300 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 wp->w_botline = lnum;
2302
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002303 // Make sure the rest of the screen is blank
2304 // put '~'s on rows that aren't part of the file.
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002305 win_draw_end(wp, WIN_IS_POPUP(wp) ? ' ' : '~',
2306 ' ', FALSE, row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 }
2308
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002309#ifdef SYN_TIME_LIMIT
2310 syn_set_timeout(NULL);
2311#endif
2312
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 /* Reset the type of redrawing required, the window has been updated. */
2314 wp->w_redr_type = 0;
2315#ifdef FEAT_DIFF
2316 wp->w_old_topfill = wp->w_topfill;
2317 wp->w_old_botfill = wp->w_botfill;
2318#endif
2319
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002320 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 {
2322 /*
2323 * There is a trick with w_botline. If we invalidate it on each
2324 * change that might modify it, this will cause a lot of expensive
2325 * calls to plines() in update_topline() each time. Therefore the
2326 * value of w_botline is often approximated, and this value is used to
2327 * compute the value of w_topline. If the value of w_botline was
2328 * wrong, check that the value of w_topline is correct (cursor is on
2329 * the visible part of the text). If it's not, we need to redraw
2330 * again. Mostly this just means scrolling up a few lines, so it
2331 * doesn't look too bad. Only do this for the current window (where
2332 * changes are relevant).
2333 */
2334 wp->w_valid |= VALID_BOTLINE;
2335 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2336 {
2337 recursive = TRUE;
2338 curwin->w_valid &= ~VALID_TOPLINE;
2339 update_topline(); /* may invalidate w_botline again */
2340 if (must_redraw != 0)
2341 {
2342 /* Don't update for changes in buffer again. */
2343 i = curbuf->b_mod_set;
2344 curbuf->b_mod_set = FALSE;
2345 win_update(curwin);
2346 must_redraw = 0;
2347 curbuf->b_mod_set = i;
2348 }
2349 recursive = FALSE;
2350 }
2351 }
2352
2353#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2354 /* restore got_int, unless CTRL-C was hit while redrawing */
2355 if (!got_int)
2356 got_int = save_got_int;
2357#endif
2358}
2359
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360/*
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002361 * Call screen_fill() with the columns adjusted for 'rightleft' if needed.
2362 * Return the new offset.
2363 */
2364 static int
2365screen_fill_end(
2366 win_T *wp,
2367 int c1,
2368 int c2,
2369 int off,
2370 int width,
2371 int row,
2372 int endrow,
2373 int attr)
2374{
2375 int nn = off + width;
2376
2377 if (nn > wp->w_width)
2378 nn = wp->w_width;
2379#ifdef FEAT_RIGHTLEFT
2380 if (wp->w_p_rl)
2381 {
2382 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2383 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - off,
2384 c1, c2, attr);
2385 }
2386 else
2387#endif
2388 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2389 wp->w_wincol + off, (int)wp->w_wincol + nn,
2390 c1, c2, attr);
2391 return nn;
2392}
2393
2394/*
2395 * Clear lines near the end the window and mark the unused lines with "c1".
2396 * use "c2" as the filler character.
2397 * When "draw_margin" is TRUE then draw the sign, fold and number columns.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 */
2399 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002400win_draw_end(
2401 win_T *wp,
2402 int c1,
2403 int c2,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002404 int draw_margin,
Bram Moolenaar05540972016-01-30 20:31:25 +01002405 int row,
2406 int endrow,
2407 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 int n = 0;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002410 int attr = HL_ATTR(hl);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002411 int wcr_attr = get_wcr_attr(wp);
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002412
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002413 attr = hl_combine_attr(wcr_attr, attr);
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002414
2415 if (draw_margin)
2416 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002417#ifdef FEAT_FOLDING
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002418 int fdc = compute_foldcolumn(wp, 0);
2419
2420 if (fdc > 0)
2421 // draw the fold column
2422 n = screen_fill_end(wp, ' ', ' ', n, fdc,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002423 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_FC)));
Bram Moolenaar1c934292015-01-27 16:39:29 +01002424#endif
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002425#ifdef FEAT_SIGNS
2426 if (signcolumn_on(wp))
2427 // draw the sign column
2428 n = screen_fill_end(wp, ' ', ' ', n, 2,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002429 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_SC)));
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002430#endif
2431 if ((wp->w_p_nu || wp->w_p_rnu)
2432 && vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
2433 // draw the number column
2434 n = screen_fill_end(wp, ' ', ' ', n, number_width(wp) + 1,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002435 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_N)));
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437
2438#ifdef FEAT_RIGHTLEFT
2439 if (wp->w_p_rl)
2440 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002442 wp->w_wincol, W_ENDCOL(wp) - 1 - n,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002443 c2, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002445 W_ENDCOL(wp) - 1 - n, W_ENDCOL(wp) - n,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002446 c1, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 }
2448 else
2449#endif
2450 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002452 wp->w_wincol + n, (int)W_ENDCOL(wp),
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002453 c1, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 }
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002455
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 set_empty_rows(wp, row);
2457}
2458
Bram Moolenaar1a384422010-07-14 19:53:30 +02002459#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02002460/*
2461 * Advance **color_cols and return TRUE when there are columns to draw.
2462 */
2463 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002464advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002465{
2466 while (**color_cols >= 0 && vcol > **color_cols)
2467 ++*color_cols;
2468 return (**color_cols >= 0);
2469}
2470#endif
2471
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002472#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
2473/*
2474 * Copy "text" to ScreenLines using "attr".
2475 * Returns the next screen column.
2476 */
2477 static int
2478text_to_screenline(win_T *wp, char_u *text, int col)
2479{
2480 int off = (int)(current_ScreenLine - ScreenLines);
2481
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002482 if (has_mbyte)
2483 {
2484 int cells;
2485 int u8c, u8cc[MAX_MCO];
2486 int i;
2487 int idx;
2488 int c_len;
2489 char_u *p;
2490# ifdef FEAT_ARABIC
2491 int prev_c = 0; /* previous Arabic character */
2492 int prev_c1 = 0; /* first composing char for prev_c */
2493# endif
2494
2495# ifdef FEAT_RIGHTLEFT
2496 if (wp->w_p_rl)
2497 idx = off;
2498 else
2499# endif
2500 idx = off + col;
2501
2502 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2503 for (p = text; *p != NUL; )
2504 {
2505 cells = (*mb_ptr2cells)(p);
2506 c_len = (*mb_ptr2len)(p);
Bram Moolenaar02631462017-09-22 15:20:32 +02002507 if (col + cells > wp->w_width
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002508# ifdef FEAT_RIGHTLEFT
2509 - (wp->w_p_rl ? col : 0)
2510# endif
2511 )
2512 break;
2513 ScreenLines[idx] = *p;
2514 if (enc_utf8)
2515 {
2516 u8c = utfc_ptr2char(p, u8cc);
2517 if (*p < 0x80 && u8cc[0] == 0)
2518 {
2519 ScreenLinesUC[idx] = 0;
2520#ifdef FEAT_ARABIC
2521 prev_c = u8c;
2522#endif
2523 }
2524 else
2525 {
2526#ifdef FEAT_ARABIC
2527 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2528 {
2529 /* Do Arabic shaping. */
2530 int pc, pc1, nc;
2531 int pcc[MAX_MCO];
2532 int firstbyte = *p;
2533
2534 /* The idea of what is the previous and next
2535 * character depends on 'rightleft'. */
2536 if (wp->w_p_rl)
2537 {
2538 pc = prev_c;
2539 pc1 = prev_c1;
2540 nc = utf_ptr2char(p + c_len);
2541 prev_c1 = u8cc[0];
2542 }
2543 else
2544 {
2545 pc = utfc_ptr2char(p + c_len, pcc);
2546 nc = prev_c;
2547 pc1 = pcc[0];
2548 }
2549 prev_c = u8c;
2550
2551 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
2552 pc, pc1, nc);
2553 ScreenLines[idx] = firstbyte;
2554 }
2555 else
2556 prev_c = u8c;
2557#endif
2558 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar9ba61172019-01-24 18:20:17 +01002559 ScreenLinesUC[idx] = u8c;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002560 for (i = 0; i < Screen_mco; ++i)
2561 {
2562 ScreenLinesC[i][idx] = u8cc[i];
2563 if (u8cc[i] == 0)
2564 break;
2565 }
2566 }
2567 if (cells > 1)
2568 ScreenLines[idx + 1] = 0;
2569 }
2570 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2571 /* double-byte single width character */
2572 ScreenLines2[idx] = p[1];
2573 else if (cells > 1)
2574 /* double-width character */
2575 ScreenLines[idx + 1] = p[1];
2576 col += cells;
2577 idx += cells;
2578 p += c_len;
2579 }
2580 }
2581 else
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002582 {
2583 int len = (int)STRLEN(text);
2584
Bram Moolenaar02631462017-09-22 15:20:32 +02002585 if (len > wp->w_width - col)
2586 len = wp->w_width - col;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002587 if (len > 0)
2588 {
2589#ifdef FEAT_RIGHTLEFT
2590 if (wp->w_p_rl)
Bram Moolenaarc6663882019-02-22 19:14:54 +01002591 mch_memmove(current_ScreenLine, text, len);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002592 else
2593#endif
Bram Moolenaarc6663882019-02-22 19:14:54 +01002594 mch_memmove(current_ScreenLine + col, text, len);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002595 col += len;
2596 }
2597 }
2598 return col;
2599}
2600#endif
2601
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602#ifdef FEAT_FOLDING
2603/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002604 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2605 * space is available for window "wp", minus "col".
2606 */
2607 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002608compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002609{
2610 int fdc = wp->w_p_fdc;
2611 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
Bram Moolenaar02631462017-09-22 15:20:32 +02002612 int wwidth = wp->w_width;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002613
2614 if (fdc > wwidth - (col + wmw))
2615 fdc = wwidth - (col + wmw);
2616 return fdc;
2617}
2618
2619/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 * Display one folded line.
2621 */
2622 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002623fold_line(
2624 win_T *wp,
2625 long fold_count,
2626 foldinfo_T *foldinfo,
2627 linenr_T lnum,
2628 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002630 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 pos_T *top, *bot;
2632 linenr_T lnume = lnum + fold_count - 1;
2633 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002634 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 int col;
2637 int txtcol;
2638 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002639 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640
2641 /* Build the fold line:
2642 * 1. Add the cmdwin_type for the command-line window
2643 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002644 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 * 4. Compose the text
2646 * 5. Add the text
2647 * 6. set highlighting for the Visual area an other text
2648 */
2649 col = 0;
2650
2651 /*
2652 * 1. Add the cmdwin_type for the command-line window
2653 * Ignores 'rightleft', this window is never right-left.
2654 */
2655#ifdef FEAT_CMDWIN
2656 if (cmdwin_type != 0 && wp == curwin)
2657 {
2658 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002659 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 if (enc_utf8)
2661 ScreenLinesUC[off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 ++col;
2663 }
2664#endif
2665
2666 /*
2667 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002668 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002670 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 if (fdc > 0)
2672 {
2673 fill_foldcolumn(buf, wp, TRUE, lnum);
2674#ifdef FEAT_RIGHTLEFT
2675 if (wp->w_p_rl)
2676 {
2677 int i;
2678
Bram Moolenaar02631462017-09-22 15:20:32 +02002679 copy_text_attr(off + wp->w_width - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002680 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 /* reverse the fold column */
2682 for (i = 0; i < fdc; ++i)
Bram Moolenaar02631462017-09-22 15:20:32 +02002683 ScreenLines[off + wp->w_width - i - 1 - col] = buf[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 }
2685 else
2686#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002687 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 col += fdc;
2689 }
2690
2691#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6f470022018-04-10 18:47:20 +02002692# define RL_MEMSET(p, v, l) \
2693 do { \
2694 if (wp->w_p_rl) \
2695 for (ri = 0; ri < l; ++ri) \
2696 ScreenAttrs[off + (wp->w_width - (p) - (l)) + ri] = v; \
2697 else \
2698 for (ri = 0; ri < l; ++ri) \
2699 ScreenAttrs[off + (p) + ri] = v; \
2700 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701#else
Bram Moolenaar6f470022018-04-10 18:47:20 +02002702# define RL_MEMSET(p, v, l) \
2703 do { \
2704 for (ri = 0; ri < l; ++ri) \
2705 ScreenAttrs[off + (p) + ri] = v; \
2706 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707#endif
2708
Bram Moolenaar64486672010-05-16 15:46:46 +02002709 /* Set all attributes of the 'number' or 'relativenumber' column and the
2710 * text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002711 RL_MEMSET(col, HL_ATTR(HLF_FL), wp->w_width - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712
2713#ifdef FEAT_SIGNS
2714 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002715 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002717 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 if (len > 0)
2719 {
2720 if (len > 2)
2721 len = 2;
2722# ifdef FEAT_RIGHTLEFT
2723 if (wp->w_p_rl)
2724 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002725 copy_text_attr(off + wp->w_width - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002726 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 else
2728# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002729 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 col += len;
2731 }
2732 }
2733#endif
2734
2735 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002736 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002738 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002740 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 if (len > 0)
2742 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002743 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002744 long num;
2745 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002746
2747 if (len > w + 1)
2748 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002749
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002750 if (wp->w_p_nu && !wp->w_p_rnu)
2751 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002752 num = (long)lnum;
2753 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002754 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002755 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002756 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002757 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002758 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002759 /* 'number' + 'relativenumber': cursor line shows absolute
2760 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002761 num = lnum;
2762 fmt = "%-*ld ";
2763 }
2764 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002765
Bram Moolenaar700e7342013-01-30 12:31:36 +01002766 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767#ifdef FEAT_RIGHTLEFT
2768 if (wp->w_p_rl)
2769 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002770 copy_text_attr(off + wp->w_width - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002771 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 else
2773#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002774 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 col += len;
2776 }
2777 }
2778
2779 /*
2780 * 4. Compose the folded-line string with 'foldtext', if set.
2781 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002782 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783
2784 txtcol = col; /* remember where text starts */
2785
2786 /*
2787 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2788 * Right-left text is put in columns 0 - number-col, normal text is put
2789 * in columns number-col - window-width.
2790 */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002791 col = text_to_screenline(wp, text, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792
2793 /* Fill the rest of the line with the fold filler */
2794#ifdef FEAT_RIGHTLEFT
2795 if (wp->w_p_rl)
2796 col -= txtcol;
2797#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02002798 while (col < wp->w_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799#ifdef FEAT_RIGHTLEFT
2800 - (wp->w_p_rl ? txtcol : 0)
2801#endif
2802 )
2803 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 if (enc_utf8)
2805 {
2806 if (fill_fold >= 0x80)
2807 {
2808 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002809 ScreenLinesC[0][off + col] = 0;
Bram Moolenaaracda04f2018-02-08 09:57:28 +01002810 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 }
2812 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002813 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002815 ScreenLines[off + col] = fill_fold;
2816 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002817 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002819 else
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002820 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 }
2822
2823 if (text != buf)
2824 vim_free(text);
2825
2826 /*
2827 * 6. set highlighting for the Visual area an other text.
2828 * If all folded lines are in the Visual area, highlight the line.
2829 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2831 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002832 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 {
2834 /* Visual is after curwin->w_cursor */
2835 top = &curwin->w_cursor;
2836 bot = &VIsual;
2837 }
2838 else
2839 {
2840 /* Visual is before curwin->w_cursor */
2841 top = &VIsual;
2842 bot = &curwin->w_cursor;
2843 }
2844 if (lnum >= top->lnum
2845 && lnume <= bot->lnum
2846 && (VIsual_mode != 'v'
2847 || ((lnum > top->lnum
2848 || (lnum == top->lnum
2849 && top->col == 0))
2850 && (lnume < bot->lnum
2851 || (lnume == bot->lnum
2852 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002853 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 {
2855 if (VIsual_mode == Ctrl_V)
2856 {
2857 /* Visual block mode: highlight the chars part of the block */
Bram Moolenaar02631462017-09-22 15:20:32 +02002858 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002860 if (wp->w_old_cursor_lcol != MAXCOL
2861 && wp->w_old_cursor_lcol + txtcol
Bram Moolenaar02631462017-09-22 15:20:32 +02002862 < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 len = wp->w_old_cursor_lcol;
2864 else
Bram Moolenaar02631462017-09-22 15:20:32 +02002865 len = wp->w_width - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002866 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002867 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 }
2869 }
2870 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002871 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 /* Set all attributes of the text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002873 RL_MEMSET(txtcol, HL_ATTR(HLF_V), wp->w_width - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 }
2876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002878#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002879 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2880 if (wp->w_p_cc_cols)
2881 {
2882 int i = 0;
2883 int j = wp->w_p_cc_cols[i];
2884 int old_txtcol = txtcol;
2885
2886 while (j > -1)
2887 {
2888 txtcol += j;
2889 if (wp->w_p_wrap)
2890 txtcol -= wp->w_skipcol;
2891 else
2892 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002893 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002894 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002895 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002896 txtcol = old_txtcol;
2897 j = wp->w_p_cc_cols[++i];
2898 }
2899 }
2900
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002901 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002902 if (wp->w_p_cuc)
2903 {
2904 txtcol += wp->w_virtcol;
2905 if (wp->w_p_wrap)
2906 txtcol -= wp->w_skipcol;
2907 else
2908 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002909 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaar85595c52008-10-02 16:04:05 +00002910 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002911 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00002912 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002913#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914
Bram Moolenaar02631462017-09-22 15:20:32 +02002915 screen_line(row + W_WINROW(wp), wp->w_wincol, (int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002916 (int)wp->w_width, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917
2918 /*
2919 * Update w_cline_height and w_cline_folded if the cursor line was
2920 * updated (saves a call to plines() later).
2921 */
2922 if (wp == curwin
2923 && lnum <= curwin->w_cursor.lnum
2924 && lnume >= curwin->w_cursor.lnum)
2925 {
2926 curwin->w_cline_row = row;
2927 curwin->w_cline_height = 1;
2928 curwin->w_cline_folded = TRUE;
2929 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2930 }
2931}
2932
2933/*
2934 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2935 */
2936 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002937copy_text_attr(
2938 int off,
2939 char_u *buf,
2940 int len,
2941 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002943 int i;
2944
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 mch_memmove(ScreenLines + off, buf, (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 if (enc_utf8)
2947 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002948 for (i = 0; i < len; ++i)
2949 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950}
2951
2952/*
2953 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002954 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 */
2956 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002957fill_foldcolumn(
2958 char_u *p,
2959 win_T *wp,
2960 int closed, /* TRUE of FALSE */
2961 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962{
2963 int i = 0;
2964 int level;
2965 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002966 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002967 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968
2969 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002970 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971
2972 level = win_foldinfo.fi_level;
2973 if (level > 0)
2974 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002975 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002976 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002977
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 /* If the column is too narrow, we start at the lowest level that
2979 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002980 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 if (first_level < 1)
2982 first_level = 1;
2983
Bram Moolenaar1c934292015-01-27 16:39:29 +01002984 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 {
2986 if (win_foldinfo.fi_lnum == lnum
2987 && first_level + i >= win_foldinfo.fi_low_level)
2988 p[i] = '-';
2989 else if (first_level == 1)
2990 p[i] = '|';
2991 else if (first_level + i <= 9)
2992 p[i] = '0' + first_level + i;
2993 else
2994 p[i] = '>';
2995 if (first_level + i == level)
2996 break;
2997 }
2998 }
2999 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01003000 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001}
3002#endif /* FEAT_FOLDING */
3003
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01003004#ifdef FEAT_TEXT_PROP
3005static textprop_T *current_text_props = NULL;
3006static buf_T *current_buf = NULL;
3007
3008 static int
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01003009text_prop_compare(const void *s1, const void *s2)
3010{
3011 int idx1, idx2;
3012 proptype_T *pt1, *pt2;
3013 colnr_T col1, col2;
3014
3015 idx1 = *(int *)s1;
3016 idx2 = *(int *)s2;
3017 pt1 = text_prop_type_by_id(current_buf, current_text_props[idx1].tp_type);
3018 pt2 = text_prop_type_by_id(current_buf, current_text_props[idx2].tp_type);
3019 if (pt1 == pt2)
3020 return 0;
3021 if (pt1 == NULL)
3022 return -1;
3023 if (pt2 == NULL)
3024 return 1;
3025 if (pt1->pt_priority != pt2->pt_priority)
3026 return pt1->pt_priority > pt2->pt_priority ? 1 : -1;
3027 col1 = current_text_props[idx1].tp_col;
3028 col2 = current_text_props[idx2].tp_col;
3029 return col1 == col2 ? 0 : col1 > col2 ? 1 : -1;
3030}
3031#endif
3032
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003033#ifdef FEAT_SIGNS
3034/*
3035 * Get information needed to display the sign in line 'lnum' in window 'wp'.
3036 * If 'nrcol' is TRUE, the sign is going to be displayed in the number column.
3037 * Otherwise the sign is going to be displayed in the sign column.
3038 */
3039 static void
3040get_sign_display_info(
3041 int nrcol,
3042 win_T *wp,
Bram Moolenaar4e038572019-07-04 18:28:35 +02003043 linenr_T lnum UNUSED,
3044 sign_attrs_T *sattr,
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003045 int wcr_attr,
3046 int row,
3047 int startrow,
Bram Moolenaarbf8c3ad2019-06-19 14:28:43 +02003048 int filler_lines UNUSED,
3049 int filler_todo UNUSED,
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003050 int *c_extrap,
3051 int *c_finalp,
3052 char_u *extra,
3053 char_u **pp_extra,
3054 int *n_extrap,
3055 int *char_attrp)
3056{
3057 int text_sign;
3058# ifdef FEAT_SIGN_ICONS
3059 int icon_sign;
3060# endif
3061
3062 // Draw two cells with the sign value or blank.
3063 *c_extrap = ' ';
3064 *c_finalp = NUL;
3065 if (nrcol)
3066 *n_extrap = number_width(wp) + 1;
3067 else
3068 {
3069 *char_attrp = hl_combine_attr(wcr_attr, HL_ATTR(HLF_SC));
3070 *n_extrap = 2;
3071 }
3072
3073 if (row == startrow
3074#ifdef FEAT_DIFF
3075 + filler_lines && filler_todo <= 0
3076#endif
3077 )
3078 {
Bram Moolenaar4e038572019-07-04 18:28:35 +02003079 text_sign = (sattr->text != NULL) ? sattr->typenr : 0;
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003080# ifdef FEAT_SIGN_ICONS
Bram Moolenaar4e038572019-07-04 18:28:35 +02003081 icon_sign = (sattr->icon != NULL) ? sattr->typenr : 0;
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003082 if (gui.in_use && icon_sign != 0)
3083 {
3084 // Use the image in this position.
Bram Moolenaar4dff4ae2019-06-19 16:31:28 +02003085 if (nrcol)
3086 {
3087 *c_extrap = NUL;
3088 sprintf((char *)extra, "%-*c ", number_width(wp), SIGN_BYTE);
3089 *pp_extra = extra;
3090 *n_extrap = (int)STRLEN(*pp_extra);
3091 }
3092 else
3093 *c_extrap = SIGN_BYTE;
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003094# ifdef FEAT_NETBEANS_INTG
Bram Moolenaar4e038572019-07-04 18:28:35 +02003095 if (netbeans_active() && (buf_signcount(wp->w_buffer, lnum) > 1))
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003096 {
Bram Moolenaar4dff4ae2019-06-19 16:31:28 +02003097 if (nrcol)
3098 {
3099 *c_extrap = NUL;
3100 sprintf((char *)extra, "%-*c ", number_width(wp),
3101 MULTISIGN_BYTE);
3102 *pp_extra = extra;
3103 *n_extrap = (int)STRLEN(*pp_extra);
3104 }
3105 else
3106 *c_extrap = MULTISIGN_BYTE;
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003107 }
3108# endif
Bram Moolenaar4dff4ae2019-06-19 16:31:28 +02003109 *c_finalp = NUL;
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003110 *char_attrp = icon_sign;
3111 }
3112 else
3113# endif
3114 if (text_sign != 0)
3115 {
Bram Moolenaar4e038572019-07-04 18:28:35 +02003116 *pp_extra = sattr->text;
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003117 if (*pp_extra != NULL)
3118 {
3119 if (nrcol)
3120 {
Bram Moolenaard6bcff42019-07-18 12:48:16 +02003121 int n, width = number_width(wp) - 2;
3122
3123 for (n = 0; n < width; n++)
3124 extra[n] = ' ';
3125 extra[n] = 0;
3126 STRCAT(extra, *pp_extra);
3127 STRCAT(extra, " ");
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003128 *pp_extra = extra;
3129 }
3130 *c_extrap = NUL;
3131 *c_finalp = NUL;
3132 *n_extrap = (int)STRLEN(*pp_extra);
3133 }
Bram Moolenaar4e038572019-07-04 18:28:35 +02003134 *char_attrp = sattr->texthl;
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003135 }
3136 }
3137}
3138#endif
3139
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140/*
3141 * Display line "lnum" of window 'wp' on the screen.
3142 * Start at row "startrow", stop when "endrow" is reached.
3143 * wp->w_virtcol needs to be valid.
3144 *
3145 * Return the number of last row the line occupies.
3146 */
3147 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003148win_line(
3149 win_T *wp,
3150 linenr_T lnum,
3151 int startrow,
3152 int endrow,
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003153 int nochange UNUSED, // not updating for changed text
3154 int number_only) // only update the number column
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155{
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003156 int col = 0; // visual column on screen
3157 unsigned off; // offset in ScreenLines/ScreenAttrs
3158 int c = 0; // init for GCC
3159 long vcol = 0; // virtual column (for tabs)
Bram Moolenaard574ea22015-01-14 19:35:14 +01003160#ifdef FEAT_LINEBREAK
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003161 long vcol_sbr = -1; // virtual column after showbreak
Bram Moolenaard574ea22015-01-14 19:35:14 +01003162#endif
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003163 long vcol_prev = -1; // "vcol" of previous character
3164 char_u *line; // current line
3165 char_u *ptr; // current position in "line"
3166 int row; // row in the window, excl w_winrow
3167 int screen_row; // row on the screen, incl w_winrow
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003169 char_u extra[21]; // "%ld " and 'fdc' must fit in here
3170 int n_extra = 0; // number of extra chars
3171 char_u *p_extra = NULL; // string of extra chars, plus NUL
3172 char_u *p_extra_free = NULL; // p_extra needs to be freed
3173 int c_extra = NUL; // extra chars, all the same
3174 int c_final = NUL; // final char, mandatory if set
3175 int extra_attr = 0; // attributes when n_extra != 0
3176 static char_u *at_end_str = (char_u *)""; // used for p_extra when
3177 // displaying lcs_eol at end-of-line
3178 int lcs_eol_one = lcs_eol; // lcs_eol until it's been used
3179 int lcs_prec_todo = lcs_prec; // lcs_prec until it's been used
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003181 // saved "extra" items for when draw_state becomes WL_LINE (again)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 int saved_n_extra = 0;
3183 char_u *saved_p_extra = NULL;
3184 int saved_c_extra = 0;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003185 int saved_c_final = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 int saved_char_attr = 0;
3187
3188 int n_attr = 0; /* chars with special attr */
3189 int saved_attr2 = 0; /* char_attr saved for n_attr */
3190 int n_attr3 = 0; /* chars with overruling special attr */
3191 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
3192
3193 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
3194
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003195 int fromcol = -10; // start of inverting
3196 int tocol = MAXCOL; // end of inverting
3197 int fromcol_prev = -2; // start of inverting after cursor
3198 int noinvcur = FALSE; // don't invert the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003200 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 pos_T pos;
3202 long v;
3203
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003204 int char_attr = 0; // attributes for next character
3205 int attr_pri = FALSE; // char_attr has priority
3206 int area_highlighting = FALSE; // Visual or incsearch highlighting
3207 // in this line
3208 int vi_attr = 0; // attributes for Visual and incsearch
3209 // highlighting
3210 int wcr_attr = 0; // attributes from 'wincolor'
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003211 int win_attr = 0; // background for whole window, except
3212 // margins and "~" lines.
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003213 int area_attr = 0; // attributes desired by highlighting
3214 int search_attr = 0; // attributes desired by 'hlsearch'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003216 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 int syntax_attr = 0; /* attributes desired by syntax */
3218 int has_syntax = FALSE; /* this buffer has syntax highl. */
3219 int save_did_emsg;
Bram Moolenaar1a384422010-07-14 19:53:30 +02003220 int draw_color_col = FALSE; /* highlight colorcolumn */
3221 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003222#endif
Bram Moolenaarf914a332019-07-20 15:09:56 +02003223 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003224#ifdef FEAT_TEXT_PROP
3225 int text_prop_count;
3226 int text_prop_next = 0; // next text property to use
3227 textprop_T *text_props = NULL;
3228 int *text_prop_idxs = NULL;
3229 int text_props_active = 0;
3230 proptype_T *text_prop_type = NULL;
3231 int text_prop_attr = 0;
Bram Moolenaarde24a872019-05-05 15:48:00 +02003232 int text_prop_combine = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003233#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003234#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003235 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003236# define SPWORDLEN 150
3237 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003238 int nextlinecol = 0; /* column where nextline[] starts */
3239 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003240 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003241 int spell_attr = 0; /* attributes desired by spelling */
3242 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003243 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3244 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003245 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003246 static int cap_col = -1; /* column to check for Cap word */
3247 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003248 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003250 int extra_check = 0; // has extra highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 int multi_attr = 0; /* attributes desired by multibyte */
3252 int mb_l = 1; /* multi-byte byte length */
3253 int mb_c = 0; /* decoded multi-byte character */
3254 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003255 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaare9726e32019-06-19 18:01:21 +02003256#if defined(FEAT_DIFF) || defined(FEAT_SIGNS)
Bram Moolenaarbf8c3ad2019-06-19 14:28:43 +02003257 int filler_lines = 0; /* nr of filler lines to be drawn */
3258 int filler_todo = 0; /* nr of filler lines still to do + 1 */
Bram Moolenaare9726e32019-06-19 18:01:21 +02003259#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260#ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003261 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 int change_start = MAXCOL; /* first col of changed area */
3263 int change_end = -1; /* last col of changed area */
3264#endif
3265 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3266#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003267 int need_showbreak = FALSE; /* overlong line, skipping first x
3268 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003270#if defined(FEAT_SIGNS) || defined(FEAT_QUICKFIX) \
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003271 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003273 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274#endif
Bram Moolenaarb4d9b892019-07-04 22:59:06 +02003275#ifdef FEAT_SIGNS
3276 int sign_present = FALSE;
3277 sign_attrs_T sattr;
3278#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279#ifdef FEAT_ARABIC
3280 int prev_c = 0; /* previous Arabic character */
3281 int prev_c1 = 0; /* first composing char for prev_c */
3282#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003283#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003284 int did_line_attr = 0;
3285#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003286#ifdef FEAT_TERMINAL
3287 int get_term_attr = FALSE;
3288#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289
3290 /* draw_state: items that are drawn in sequence: */
3291#define WL_START 0 /* nothing done yet */
3292#ifdef FEAT_CMDWIN
3293# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3294#else
3295# define WL_CMDLINE WL_START
3296#endif
3297#ifdef FEAT_FOLDING
3298# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3299#else
3300# define WL_FOLD WL_CMDLINE
3301#endif
3302#ifdef FEAT_SIGNS
3303# define WL_SIGN WL_FOLD + 1 /* column for signs */
3304#else
3305# define WL_SIGN WL_FOLD /* column for signs */
3306#endif
3307#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003308#ifdef FEAT_LINEBREAK
3309# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003311# define WL_BRI WL_NR
3312#endif
3313#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3314# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3315#else
3316# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317#endif
3318#define WL_LINE WL_SBR + 1 /* text in the line */
3319 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003320#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 int feedback_col = 0;
3322 int feedback_old_attr = -1;
3323#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003324 int screen_line_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325
Bram Moolenaarfbfb7572019-07-25 20:53:03 +02003326#if defined(FEAT_CONCEAL) || defined(FEAT_SEARCH_EXTRA)
Bram Moolenaar07d13562019-07-24 18:43:08 +02003327 int match_conc = 0; // cchar for match functions
Bram Moolenaarfbfb7572019-07-25 20:53:03 +02003328#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02003329#ifdef FEAT_CONCEAL
3330 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003331 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003332 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003333 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003334 int is_concealing = FALSE;
Bram Moolenaar07d13562019-07-24 18:43:08 +02003335 int boguscols = 0; // nonexistent columns added to force
3336 // wrapping
3337 int vcol_off = 0; // offset for concealed characters
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003338 int did_wcol = FALSE;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003339 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003340# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003341# define FIX_FOR_BOGUSCOLS \
3342 { \
3343 n_extra += vcol_off; \
3344 vcol -= vcol_off; \
3345 vcol_off = 0; \
3346 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003347 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003348 boguscols = 0; \
3349 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003350#else
3351# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003352#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353
3354 if (startrow > endrow) /* past the end already! */
3355 return startrow;
3356
3357 row = startrow;
3358 screen_row = row + W_WINROW(wp);
3359
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003360 if (!number_only)
3361 {
3362 /*
3363 * To speed up the loop below, set extra_check when there is linebreak,
3364 * trailing white space and/or syntax processing to be done.
3365 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366#ifdef FEAT_LINEBREAK
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003367 extra_check = wp->w_p_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368#endif
3369#ifdef FEAT_SYN_HL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003370 if (syntax_present(wp) && !wp->w_s->b_syn_error
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003371# ifdef SYN_TIME_LIMIT
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003372 && !wp->w_s->b_syn_slow
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003373# endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003374 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003376 /* Prepare for syntax highlighting in this line. When there is an
3377 * error, stop syntax highlighting. */
3378 save_did_emsg = did_emsg;
3379 did_emsg = FALSE;
3380 syntax_start(wp, lnum);
3381 if (did_emsg)
3382 wp->w_s->b_syn_error = TRUE;
3383 else
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003384 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003385 did_emsg = save_did_emsg;
3386#ifdef SYN_TIME_LIMIT
3387 if (!wp->w_s->b_syn_slow)
3388#endif
3389 {
3390 has_syntax = TRUE;
3391 extra_check = TRUE;
3392 }
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003395
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003396 /* Check for columns to display for 'colorcolumn'. */
3397 color_cols = wp->w_p_cc_cols;
3398 if (color_cols != NULL)
3399 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003400#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003401
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003402#ifdef FEAT_TERMINAL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003403 if (term_show_buffer(wp->w_buffer))
3404 {
3405 extra_check = TRUE;
3406 get_term_attr = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003407 win_attr = term_get_attr(wp->w_buffer, lnum, -1);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003408 }
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003409#endif
3410
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003411#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003412 if (wp->w_p_spell
3413 && *wp->w_s->b_p_spl != NUL
3414 && wp->w_s->b_langp.ga_len > 0
3415 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003416 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003417 /* Prepare for spell checking. */
3418 has_spell = TRUE;
3419 extra_check = TRUE;
3420
Bram Moolenaar7701f302018-10-02 21:20:32 +02003421 /* Get the start of the next line, so that words that wrap to the
3422 * next line are found too: "et<line-break>al.".
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003423 * Trick: skip a few chars for C/shell/Vim comments */
3424 nextline[SPWORDLEN] = NUL;
3425 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3426 {
3427 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3428 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3429 }
3430
Bram Moolenaar7701f302018-10-02 21:20:32 +02003431 /* When a word wrapped from the previous line the start of the
3432 * current line is valid. */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003433 if (lnum == checked_lnum)
3434 cur_checked_col = checked_col;
3435 checked_lnum = 0;
3436
3437 /* When there was a sentence end in the previous line may require a
3438 * word starting with capital in this line. In line 1 always check
3439 * the first word. */
3440 if (lnum != capcol_lnum)
3441 cap_col = -1;
3442 if (lnum == 1)
3443 cap_col = 0;
3444 capcol_lnum = 0;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446#endif
3447
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003448 /*
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003449 * handle Visual active in this window
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003450 */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003451 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003453 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 {
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003455 // Visual is after curwin->w_cursor
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003456 top = &curwin->w_cursor;
3457 bot = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 }
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003459 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 {
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003461 // Visual is before curwin->w_cursor
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003462 top = &VIsual;
3463 bot = &curwin->w_cursor;
3464 }
3465 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003466 if (VIsual_mode == Ctrl_V)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003467 {
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003468 // block mode
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003469 if (lnum_in_visual_area)
3470 {
3471 fromcol = wp->w_old_cursor_fcol;
3472 tocol = wp->w_old_cursor_lcol;
3473 }
3474 }
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003475 else
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003476 {
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003477 // non-block mode
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003478 if (lnum > top->lnum && lnum <= bot->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 fromcol = 0;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003480 else if (lnum == top->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 {
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003482 if (VIsual_mode == 'V') // linewise
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003483 fromcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 else
3485 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003486 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3487 if (gchar_pos(top) == NUL)
3488 tocol = fromcol + 1;
3489 }
3490 }
3491 if (VIsual_mode != 'V' && lnum == bot->lnum)
3492 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003493 if (*p_sel == 'e' && bot->col == 0 && bot->coladd == 0)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003494 {
3495 fromcol = -10;
3496 tocol = MAXCOL;
3497 }
3498 else if (bot->col == MAXCOL)
3499 tocol = MAXCOL;
3500 else
3501 {
3502 pos = *bot;
3503 if (*p_sel == 'e')
3504 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3505 else
3506 {
3507 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3508 ++tocol;
3509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 }
3511 }
3512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003514 /* Check if the character under the cursor should not be inverted */
3515 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003516#ifdef FEAT_GUI
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003517 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003518#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003519 )
3520 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003522 /* if inverting in this line set area_highlighting */
3523 if (fromcol >= 0)
3524 {
3525 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003526 vi_attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003528 if ((clip_star.available && !clip_star.owned
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003529 && clip_isautosel_star())
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003530 || (clip_plus.available && !clip_plus.owned
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003531 && clip_isautosel_plus()))
3532 vi_attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003534 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003537 /*
3538 * handle 'incsearch' and ":s///c" highlighting
3539 */
3540 else if (highlight_match
3541 && wp == curwin
3542 && lnum >= curwin->w_cursor.lnum
3543 && lnum <= curwin->w_cursor.lnum + search_match_lines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003545 if (lnum == curwin->w_cursor.lnum)
3546 getvcol(curwin, &(curwin->w_cursor),
Bram Moolenaarc6663882019-02-22 19:14:54 +01003547 (colnr_T *)&fromcol, NULL, NULL);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003548 else
3549 fromcol = 0;
3550 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3551 {
3552 pos.lnum = lnum;
3553 pos.col = search_match_endcol;
3554 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3555 }
3556 else
3557 tocol = MAXCOL;
3558 /* do at least one character; happens when past end of line */
3559 if (fromcol == tocol)
3560 tocol = fromcol + 1;
3561 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003562 vi_attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 }
3565
3566#ifdef FEAT_DIFF
3567 filler_lines = diff_check(wp, lnum);
3568 if (filler_lines < 0)
3569 {
3570 if (filler_lines == -1)
3571 {
3572 if (diff_find_change(wp, lnum, &change_start, &change_end))
3573 diff_hlf = HLF_ADD; /* added line */
3574 else if (change_start == 0)
3575 diff_hlf = HLF_TXD; /* changed text */
3576 else
3577 diff_hlf = HLF_CHD; /* changed line */
3578 }
3579 else
3580 diff_hlf = HLF_ADD; /* added line */
3581 filler_lines = 0;
3582 area_highlighting = TRUE;
3583 }
3584 if (lnum == wp->w_topline)
3585 filler_lines = wp->w_topfill;
3586 filler_todo = filler_lines;
3587#endif
3588
Bram Moolenaar4e038572019-07-04 18:28:35 +02003589#ifdef FEAT_SIGNS
3590 sign_present = buf_get_signattrs(wp->w_buffer, lnum, &sattr);
3591#endif
3592
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593#ifdef LINE_ATTR
3594# ifdef FEAT_SIGNS
3595 /* If this line has a sign with line highlighting set line_attr. */
Bram Moolenaar4e038572019-07-04 18:28:35 +02003596 if (sign_present)
3597 line_attr = sattr.linehl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003599# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003601 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003602 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603# endif
3604 if (line_attr != 0)
3605 area_highlighting = TRUE;
3606#endif
3607
3608 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3609 ptr = line;
3610
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003611#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003612 if (has_spell && !number_only)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003613 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003614 /* For checking first word with a capital skip white space. */
3615 if (cap_col == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003616 cap_col = getwhitecols(line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003617
Bram Moolenaar30abd282005-06-22 22:35:10 +00003618 /* To be able to spell-check over line boundaries copy the end of the
3619 * current line into nextline[]. Above the start of the next line was
3620 * copied to nextline[SPWORDLEN]. */
3621 if (nextline[SPWORDLEN] == NUL)
3622 {
3623 /* No next line or it is empty. */
3624 nextlinecol = MAXCOL;
3625 nextline_idx = 0;
3626 }
3627 else
3628 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003629 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003630 if (v < SPWORDLEN)
3631 {
3632 /* Short line, use it completely and append the start of the
3633 * next line. */
3634 nextlinecol = 0;
3635 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003636 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003637 nextline_idx = v + 1;
3638 }
3639 else
3640 {
3641 /* Long line, use only the last SPWORDLEN bytes. */
3642 nextlinecol = v - SPWORDLEN;
3643 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3644 nextline_idx = SPWORDLEN + 1;
3645 }
3646 }
3647 }
3648#endif
3649
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003650 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 {
Bram Moolenaar895d9662019-01-31 21:57:21 +01003652 if (lcs_space || lcs_trail || lcs_nbsp)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003653 extra_check = TRUE;
3654 /* find start of trailing whitespace */
3655 if (lcs_trail)
3656 {
3657 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003658 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003659 --trailcol;
3660 trailcol += (colnr_T) (ptr - line);
3661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 }
3663
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003664 wcr_attr = get_wcr_attr(wp);
3665 if (wcr_attr != 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003666 {
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003667 win_attr = wcr_attr;
3668 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003669 }
3670#ifdef FEAT_TEXT_PROP
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02003671 if (WIN_IS_POPUP(wp))
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003672 screen_line_flags |= SLF_POPUP;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003673#endif
3674
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 /*
3676 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3677 * first character to be displayed.
3678 */
3679 if (wp->w_p_wrap)
3680 v = wp->w_skipcol;
3681 else
3682 v = wp->w_leftcol;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003683 if (v > 0 && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 char_u *prev_ptr = ptr;
Bram Moolenaara12a1612019-01-24 16:39:02 +01003686
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 while (vcol < v && *ptr != NUL)
3688 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003689 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 vcol += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 prev_ptr = ptr;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003692 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 }
3694
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003695 /* When:
3696 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003697 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003698 * - 'virtualedit' is set, or
3699 * - the visual mode is active,
3700 * the end of the line may be before the start of the displayed part.
3701 */
3702 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003703#ifdef FEAT_SYN_HL
3704 wp->w_p_cuc || draw_color_col ||
3705#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003706 virtual_active() ||
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003707 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 vcol = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709
3710 /* Handle a character that's not completely on the screen: Put ptr at
3711 * that character but skip the first few screen characters. */
3712 if (vcol > v)
3713 {
3714 vcol -= c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 ptr = prev_ptr;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003716 /* If the character fits on the screen, don't need to skip it.
3717 * Except for a TAB. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01003718 if (( (*mb_ptr2cells)(ptr) >= c || *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003719 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 }
3721
3722 /*
3723 * Adjust for when the inverted text is before the screen,
3724 * and when the start of the inverted text is before the screen.
3725 */
3726 if (tocol <= vcol)
3727 fromcol = 0;
3728 else if (fromcol >= 0 && fromcol < vcol)
3729 fromcol = vcol;
3730
3731#ifdef FEAT_LINEBREAK
3732 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3733 if (wp->w_p_wrap)
3734 need_showbreak = TRUE;
3735#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003736#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003737 /* When spell checking a word we need to figure out the start of the
3738 * word and if it's badly spelled or not. */
3739 if (has_spell)
3740 {
3741 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003742 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003743 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003744
3745 pos = wp->w_cursor;
3746 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003747 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003748 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003749
3750 /* spell_move_to() may call ml_get() and make "line" invalid */
3751 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3752 ptr = line + linecol;
3753
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003754 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003755 {
3756 /* no bad word found at line start, don't check until end of a
3757 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003758 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003759 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003760 }
3761 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003762 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003763 /* bad word found, use attributes until end of word */
3764 word_end = wp->w_cursor.col + len + 1;
3765
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003766 /* Turn index into actual attributes. */
3767 if (spell_hlf != HLF_COUNT)
3768 spell_attr = highlight_attr[spell_hlf];
3769 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003770 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003771
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003772# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003773 /* Need to restart syntax highlighting for this line. */
3774 if (has_syntax)
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003775 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003776# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003777 }
3778#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 }
3780
3781 /*
3782 * Correct highlighting for cursor that can't be disabled.
3783 * Avoids having to check this for each character.
3784 */
3785 if (fromcol >= 0)
3786 {
3787 if (noinvcur)
3788 {
3789 if ((colnr_T)fromcol == wp->w_virtcol)
3790 {
3791 /* highlighting starts at cursor, let it start just after the
3792 * cursor */
3793 fromcol_prev = fromcol;
3794 fromcol = -1;
3795 }
3796 else if ((colnr_T)fromcol < wp->w_virtcol)
3797 /* restart highlighting after the cursor */
3798 fromcol_prev = wp->w_virtcol;
3799 }
3800 if (fromcol >= tocol)
3801 fromcol = -1;
3802 }
3803
3804#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarbbca7732019-07-24 18:13:16 +02003805 if (!number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003807 v = (long)(ptr - line);
Bram Moolenaarbbca7732019-07-24 18:13:16 +02003808 area_highlighting |= prepare_search_hl_line(wp, lnum, (colnr_T)v,
3809 &line, &search_hl, &search_attr);
3810 ptr = line + v; // "line" may have been updated
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 }
3812#endif
3813
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003814#ifdef FEAT_SYN_HL
Bram Moolenaar8156ed32019-03-09 11:46:15 +01003815 // Cursor line highlighting for 'cursorline' in the current window.
3816 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003817 {
Bram Moolenaar8156ed32019-03-09 11:46:15 +01003818 // Do not show the cursor line when Visual mode is active, because it's
3819 // not clear what is selected then. Do update w_last_cursorline.
Bram Moolenaar410e98a2019-09-09 22:05:49 +02003820 if (!(wp == curwin && VIsual_active) && *wp->w_p_culopt != 'n')
Bram Moolenaar8156ed32019-03-09 11:46:15 +01003821 {
3822 line_attr = HL_ATTR(HLF_CUL);
3823 area_highlighting = TRUE;
3824 }
Bram Moolenaarc07ff5c2019-01-30 21:41:14 +01003825 wp->w_last_cursorline = wp->w_cursor.lnum;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003826 }
3827#endif
3828
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003829#ifdef FEAT_TEXT_PROP
3830 {
3831 char_u *prop_start;
3832
3833 text_prop_count = get_text_props(wp->w_buffer, lnum,
3834 &prop_start, FALSE);
3835 if (text_prop_count > 0)
3836 {
3837 // Make a copy of the properties, so that they are properly
3838 // aligned.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003839 text_props = ALLOC_MULT(textprop_T, text_prop_count);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003840 if (text_props != NULL)
3841 mch_memmove(text_props, prop_start,
3842 text_prop_count * sizeof(textprop_T));
3843
3844 // Allocate an array for the indexes.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003845 text_prop_idxs = ALLOC_MULT(int, text_prop_count);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003846 area_highlighting = TRUE;
3847 extra_check = TRUE;
3848 }
3849 }
3850#endif
3851
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003852 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 col = 0;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003854
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855#ifdef FEAT_RIGHTLEFT
3856 if (wp->w_p_rl)
3857 {
3858 /* Rightleft window: process the text in the normal direction, but put
3859 * it in current_ScreenLine[] from right to left. Start at the
3860 * rightmost column of the window. */
Bram Moolenaar02631462017-09-22 15:20:32 +02003861 col = wp->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 off += col;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003863 screen_line_flags |= SLF_RIGHTLEFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 }
3865#endif
3866
3867 /*
3868 * Repeat for the whole displayed line.
3869 */
3870 for (;;)
3871 {
Bram Moolenaarfbfb7572019-07-25 20:53:03 +02003872#if defined(FEAT_CONCEAL) || defined(FEAT_SEARCH_EXTRA)
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02003873 int has_match_conc = 0; // match wants to conceal
Bram Moolenaarfbfb7572019-07-25 20:53:03 +02003874#endif
Bram Moolenaar07d13562019-07-24 18:43:08 +02003875#ifdef FEAT_CONCEAL
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02003876 int did_decrement_ptr = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003877#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 /* Skip this quickly when working on the text. */
3879 if (draw_state != WL_LINE)
3880 {
3881#ifdef FEAT_CMDWIN
3882 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3883 {
3884 draw_state = WL_CMDLINE;
3885 if (cmdwin_type != 0 && wp == curwin)
3886 {
3887 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003889 c_extra = cmdwin_type;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003890 c_final = NUL;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003891 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 }
3893 }
3894#endif
3895
3896#ifdef FEAT_FOLDING
3897 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3898 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003899 int fdc = compute_foldcolumn(wp, 0);
3900
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003902 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003904 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003905 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003906 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003907 p_extra_free = alloc(12 + 1);
3908
3909 if (p_extra_free != NULL)
3910 {
3911 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3912 n_extra = fdc;
3913 p_extra_free[n_extra] = NUL;
3914 p_extra = p_extra_free;
3915 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003916 c_final = NUL;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003917 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_FC));
Bram Moolenaar62706602016-12-09 19:28:48 +01003918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 }
3920 }
3921#endif
3922
3923#ifdef FEAT_SIGNS
3924 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3925 {
3926 draw_state = WL_SIGN;
3927 /* Show the sign column when there are any signs in this
3928 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003929 if (signcolumn_on(wp))
Bram Moolenaar4e038572019-07-04 18:28:35 +02003930 get_sign_display_info(FALSE, wp, lnum, &sattr, wcr_attr,
3931 row, startrow, filler_lines, filler_todo, &c_extra,
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003932 &c_final, extra, &p_extra, &n_extra, &char_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 }
3934#endif
3935
3936 if (draw_state == WL_NR - 1 && n_extra == 0)
3937 {
3938 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003939 /* Display the absolute or relative line number. After the
3940 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3941 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 && (row == startrow
3943#ifdef FEAT_DIFF
3944 + filler_lines
3945#endif
3946 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3947 {
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003948#ifdef FEAT_SIGNS
3949 // If 'signcolumn' is set to 'number' and a sign is present
3950 // in 'lnum', then display the sign instead of the line
3951 // number.
3952 if ((*wp->w_p_scl == 'n' && *(wp->w_p_scl + 1) == 'u')
Bram Moolenaar4e038572019-07-04 18:28:35 +02003953 && sign_present)
3954 get_sign_display_info(TRUE, wp, lnum, &sattr, wcr_attr,
3955 row, startrow, filler_lines, filler_todo,
3956 &c_extra, &c_final, extra, &p_extra, &n_extra,
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003957 &char_attr);
3958 else
3959#endif
3960 {
3961 /* Draw the line number (empty space after wrapping). */
3962 if (row == startrow
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963#ifdef FEAT_DIFF
3964 + filler_lines
3965#endif
3966 )
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003967 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003968 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003969 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003970
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003971 if (wp->w_p_nu && !wp->w_p_rnu)
3972 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003973 num = (long)lnum;
3974 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003975 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003976 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003977 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003978 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003979 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003980 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003981 num = lnum;
3982 fmt = "%-*ld ";
3983 }
3984 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003985
Bram Moolenaar700e7342013-01-30 12:31:36 +01003986 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003987 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 if (wp->w_skipcol > 0)
3989 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3990 *p_extra = '-';
3991#ifdef FEAT_RIGHTLEFT
3992 if (wp->w_p_rl) /* reverse line numbers */
Bram Moolenaare73f9112019-03-29 18:29:54 +01003993 {
3994 char_u *p1, *p2;
3995 int t;
3996
3997 // like rl_mirror(), but keep the space at the end
3998 p2 = skiptowhite(extra) - 1;
3999 for (p1 = extra; p1 < p2; ++p1, --p2)
4000 {
4001 t = *p1;
4002 *p1 = *p2;
4003 *p2 = t;
4004 }
4005 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006#endif
4007 p_extra = extra;
4008 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004009 c_final = NUL;
Bram Moolenaar394c5d82019-06-17 21:48:05 +02004010 }
4011 else
4012 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004014 c_final = NUL;
Bram Moolenaar394c5d82019-06-17 21:48:05 +02004015 }
4016 n_extra = number_width(wp) + 1;
4017 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_N));
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004018#ifdef FEAT_SYN_HL
Bram Moolenaar394c5d82019-06-17 21:48:05 +02004019 /* When 'cursorline' is set highlight the line number of
4020 * the current line differently.
4021 * TODO: Can we use CursorLine instead of CursorLineNr
4022 * when CursorLineNr isn't set? */
4023 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar410e98a2019-09-09 22:05:49 +02004024 && *wp->w_p_culopt != 'l'
Bram Moolenaar700e7342013-01-30 12:31:36 +01004025 && lnum == wp->w_cursor.lnum)
Bram Moolenaar193ffd12019-05-25 22:57:30 +02004026 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_CLN));
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004027#endif
Bram Moolenaar394c5d82019-06-17 21:48:05 +02004028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 }
4030 }
4031
Bram Moolenaar597a4222014-06-25 14:39:50 +02004032#ifdef FEAT_LINEBREAK
4033 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
4034 && n_extra == 0 && *p_sbr != NUL)
4035 /* draw indent after showbreak value */
4036 draw_state = WL_BRI;
4037 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
4038 /* After the showbreak, draw the breakindent */
4039 draw_state = WL_BRI - 1;
4040
4041 /* draw 'breakindent': indent wrapped text accordingly */
4042 if (draw_state == WL_BRI - 1 && n_extra == 0)
4043 {
4044 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01004045 /* if need_showbreak is set, breakindent also applies */
4046 if (wp->w_p_bri && n_extra == 0
4047 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004048# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004049 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004050# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004051 )
4052 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01004053 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004054# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004055 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004056 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004057 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004058# ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02004059 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
4060 && *wp->w_p_culopt != 'n')
Bram Moolenaare0f14822014-08-06 13:20:56 +02004061 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004062 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004063# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02004064 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004065# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02004066 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02004067 c_extra = ' ';
4068 n_extra = get_breakindent_win(wp,
4069 ml_get_buf(wp->w_buffer, lnum, FALSE));
4070 /* Correct end of highlighted area for 'breakindent',
4071 * required when 'linebreak' is also set. */
4072 if (tocol == vcol)
4073 tocol += n_extra;
4074 }
4075 }
4076#endif
4077
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
4079 if (draw_state == WL_SBR - 1 && n_extra == 0)
4080 {
4081 draw_state = WL_SBR;
4082# ifdef FEAT_DIFF
4083 if (filler_todo > 0)
4084 {
4085 /* Draw "deleted" diff line(s). */
4086 if (char2cells(fill_diff) > 1)
Bram Moolenaar83a52172019-01-16 22:41:54 +01004087 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 c_extra = '-';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004089 c_final = NUL;
4090 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 else
Bram Moolenaar83a52172019-01-16 22:41:54 +01004092 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 c_extra = fill_diff;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004094 c_final = NUL;
4095 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096# ifdef FEAT_RIGHTLEFT
4097 if (wp->w_p_rl)
4098 n_extra = col + 1;
4099 else
4100# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004101 n_extra = wp->w_width - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004102 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 }
4104# endif
4105# ifdef FEAT_LINEBREAK
4106 if (*p_sbr != NUL && need_showbreak)
4107 {
4108 /* Draw 'showbreak' at the start of each broken line. */
4109 p_extra = p_sbr;
4110 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004111 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004113 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004115 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 /* Correct end of highlighted area for 'showbreak',
4117 * required when 'linebreak' is also set. */
4118 if (tocol == vcol)
4119 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004120#ifdef FEAT_SYN_HL
4121 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaar410e98a2019-09-09 22:05:49 +02004122 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
4123 && *wp->w_p_culopt != 'n')
Bram Moolenaare0f14822014-08-06 13:20:56 +02004124 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004125 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004126#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 }
4128# endif
4129 }
4130#endif
4131
4132 if (draw_state == WL_LINE - 1 && n_extra == 0)
4133 {
4134 draw_state = WL_LINE;
4135 if (saved_n_extra)
4136 {
4137 /* Continue item from end of wrapped line. */
4138 n_extra = saved_n_extra;
4139 c_extra = saved_c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004140 c_final = saved_c_final;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 p_extra = saved_p_extra;
4142 char_attr = saved_char_attr;
4143 }
4144 else
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004145 char_attr = win_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 }
4147 }
4148
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004149 // When still displaying '$' of change command, stop at cursor.
4150 // When only displaying the (relative) line number and that's done,
4151 // stop here.
4152 if ((dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004153 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154#ifdef FEAT_DIFF
4155 && filler_todo <= 0
4156#endif
4157 )
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004158 || (number_only && draw_state > WL_NR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 {
Bram Moolenaar02631462017-09-22 15:20:32 +02004160 screen_line(screen_row, wp->w_wincol, col, -(int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004161 screen_line_flags);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004162 /* Pretend we have finished updating the window. Except when
4163 * 'cursorcolumn' is set. */
4164#ifdef FEAT_SYN_HL
4165 if (wp->w_p_cuc)
4166 row = wp->w_cline_row + wp->w_cline_height;
4167 else
4168#endif
4169 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 break;
4171 }
4172
Bram Moolenaar637532b2019-01-03 21:44:40 +01004173 if (draw_state == WL_LINE && (area_highlighting
4174#ifdef FEAT_SPELL
4175 || has_spell
4176#endif
4177 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 {
4179 /* handle Visual or match highlighting in this line */
4180 if (vcol == fromcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4182 && (*mb_ptr2cells)(ptr) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004184 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 && vcol < tocol))
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004186 area_attr = vi_attr; /* start highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 else if (area_attr != 0
4188 && (vcol == tocol
4189 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191
4192#ifdef FEAT_SEARCH_EXTRA
4193 if (!n_extra)
4194 {
4195 /*
Bram Moolenaarbbca7732019-07-24 18:13:16 +02004196 * Check for start/end of 'hlsearch' and other matches.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 * After end, check for start/end of next match.
4198 * When another match, have to check for start again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004200 v = (long)(ptr - line);
Bram Moolenaarbbca7732019-07-24 18:13:16 +02004201 search_attr = update_search_hl(wp, lnum, (colnr_T)v, &line,
4202 &search_hl, &has_match_conc, &match_conc,
4203 did_line_attr, lcs_eol_one);
4204 ptr = line + v; // "line" may have been changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 }
4206#endif
4207
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004209 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004211 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4212 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004214 if (diff_hlf == HLF_TXD && ptr - line > change_end
4215 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004217 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaar410e98a2019-09-09 22:05:49 +02004218 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
4219 && *wp->w_p_culopt != 'n')
Bram Moolenaar8820b482017-03-16 17:23:31 +01004220 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 }
4222#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004223
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004224#ifdef FEAT_TEXT_PROP
4225 if (text_props != NULL)
4226 {
4227 int pi;
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004228 int bcol = (int)(ptr - line);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004229
Bram Moolenaara956bf62019-06-19 17:34:24 +02004230 if (n_extra > 0)
4231 --bcol; // still working on the previous char, e.g. Tab
4232
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004233 // Check if any active property ends.
4234 for (pi = 0; pi < text_props_active; ++pi)
4235 {
4236 int tpi = text_prop_idxs[pi];
4237
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004238 if (bcol >= text_props[tpi].tp_col - 1
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004239 + text_props[tpi].tp_len)
4240 {
4241 if (pi + 1 < text_props_active)
4242 mch_memmove(text_prop_idxs + pi,
4243 text_prop_idxs + pi + 1,
4244 sizeof(int)
4245 * (text_props_active - (pi + 1)));
4246 --text_props_active;
4247 --pi;
4248 }
4249 }
4250
4251 // Add any text property that starts in this column.
4252 while (text_prop_next < text_prop_count
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004253 && bcol >= text_props[text_prop_next].tp_col - 1)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004254 text_prop_idxs[text_props_active++] = text_prop_next++;
4255
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004256 text_prop_attr = 0;
Bram Moolenaarde24a872019-05-05 15:48:00 +02004257 text_prop_combine = FALSE;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004258 if (text_props_active > 0)
4259 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004260 // Sort the properties on priority and/or starting last.
4261 // Then combine the attributes, highest priority last.
4262 current_text_props = text_props;
4263 current_buf = wp->w_buffer;
4264 qsort((void *)text_prop_idxs, (size_t)text_props_active,
4265 sizeof(int), text_prop_compare);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004266
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004267 for (pi = 0; pi < text_props_active; ++pi)
4268 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004269 int tpi = text_prop_idxs[pi];
Bram Moolenaarde24a872019-05-05 15:48:00 +02004270 proptype_T *pt = text_prop_type_by_id(
4271 wp->w_buffer, text_props[tpi].tp_type);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004272
Bram Moolenaard74af422019-06-28 21:38:00 +02004273 if (pt != NULL && pt->pt_hl_id > 0)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004274 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004275 int pt_attr = syn_id2attr(pt->pt_hl_id);
4276
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004277 text_prop_type = pt;
Bram Moolenaarde24a872019-05-05 15:48:00 +02004278 text_prop_attr =
4279 hl_combine_attr(text_prop_attr, pt_attr);
4280 text_prop_combine = pt->pt_flags & PT_FLAG_COMBINE;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004281 }
4282 }
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004283 }
4284 }
4285#endif
4286
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004287 /* Decide which of the highlight attributes to use. */
4288 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004289#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004290 if (area_attr != 0)
4291 char_attr = hl_combine_attr(line_attr, area_attr);
4292 else if (search_attr != 0)
4293 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004294# ifdef FEAT_TEXT_PROP
4295 else if (text_prop_type != NULL)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02004296 {
4297 char_attr = hl_combine_attr(
4298 line_attr != 0 ? line_attr : win_attr, text_prop_attr);
4299 }
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004300# endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004301 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004302 || vcol < fromcol || vcol_prev < fromcol_prev
4303 || vcol >= tocol))
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004304 // Use line_attr when not in the Visual or 'incsearch' area
4305 // (area_attr may be 0 when "noinvcur" is set).
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004306 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004307#else
4308 if (area_attr != 0)
4309 char_attr = area_attr;
4310 else if (search_attr != 0)
4311 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004312#endif
4313 else
4314 {
4315 attr_pri = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004316#ifdef FEAT_TEXT_PROP
4317 if (text_prop_type != NULL)
Bram Moolenaarde24a872019-05-05 15:48:00 +02004318 {
4319 if (text_prop_combine)
4320 char_attr = hl_combine_attr(
4321 syntax_attr, text_prop_attr);
4322 else
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02004323 char_attr = hl_combine_attr(
4324 win_attr, text_prop_attr);
Bram Moolenaarde24a872019-05-05 15:48:00 +02004325 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004326 else
4327#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004328#ifdef FEAT_SYN_HL
4329 if (has_syntax)
4330 char_attr = syntax_attr;
4331 else
4332#endif
4333 char_attr = 0;
4334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004336 if (char_attr == 0)
4337 char_attr = win_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338
4339 /*
4340 * Get the next character to put on the screen.
4341 */
4342 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004343 * The "p_extra" points to the extra stuff that is inserted to
4344 * represent special characters (non-printable stuff) and other
4345 * things. When all characters are the same, c_extra is used.
Bram Moolenaar83a52172019-01-16 22:41:54 +01004346 * If c_final is set, it will compulsorily be used at the end.
Bram Moolenaara064ac82007-08-05 18:10:54 +00004347 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4348 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4350 */
4351 if (n_extra > 0)
4352 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004353 if (c_extra != NUL || (n_extra == 1 && c_final != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004355 c = (n_extra == 1 && c_final != NUL) ? c_final : c_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004357 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 {
4359 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004360 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004361 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 }
4363 else
4364 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 }
4366 else
4367 {
4368 c = *p_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 if (has_mbyte)
4370 {
4371 mb_c = c;
4372 if (enc_utf8)
4373 {
4374 /* If the UTF-8 character is more than one byte:
4375 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004376 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 mb_utf8 = FALSE;
4378 if (mb_l > n_extra)
4379 mb_l = 1;
4380 else if (mb_l > 1)
4381 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004382 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004384 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 }
4386 }
4387 else
4388 {
4389 /* if this is a DBCS character, put it in "mb_c" */
4390 mb_l = MB_BYTE2LEN(c);
4391 if (mb_l >= n_extra)
4392 mb_l = 1;
4393 else if (mb_l > 1)
4394 mb_c = (c << 8) + p_extra[1];
4395 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004396 if (mb_l == 0) /* at the NUL at end-of-line */
4397 mb_l = 1;
4398
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 /* If a double-width char doesn't fit display a '>' in the
4400 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004401 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402# ifdef FEAT_RIGHTLEFT
4403 wp->w_p_rl ? (col <= 0) :
4404# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004405 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 && (*mb_char2cells)(mb_c) == 2)
4407 {
4408 c = '>';
4409 mb_c = c;
4410 mb_l = 1;
4411 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004412 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 /* put the pointer back to output the double-width
4414 * character at the start of the next line. */
4415 ++n_extra;
4416 --p_extra;
4417 }
4418 else
4419 {
4420 n_extra -= mb_l - 1;
4421 p_extra += mb_l - 1;
4422 }
4423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 ++p_extra;
4425 }
4426 --n_extra;
4427 }
4428 else
4429 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004430#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004431 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004432#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004433
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004434 if (p_extra_free != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01004435 VIM_CLEAR(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 /*
4437 * Get a character from the line itself.
4438 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004439 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004440#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004441 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004442#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 if (has_mbyte)
4444 {
4445 mb_c = c;
4446 if (enc_utf8)
4447 {
4448 /* If the UTF-8 character is more than one byte: Decode it
4449 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004450 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 mb_utf8 = FALSE;
4452 if (mb_l > 1)
4453 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004454 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 /* Overlong encoded ASCII or ASCII with composing char
4456 * is displayed normally, except a NUL. */
4457 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004458 {
4459 c = mb_c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004460#ifdef FEAT_LINEBREAK
Bram Moolenaar88e76882017-02-27 20:33:46 +01004461 c0 = mb_c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004462#endif
Bram Moolenaar88e76882017-02-27 20:33:46 +01004463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004465
4466 /* At start of the line we can have a composing char.
4467 * Draw it as a space with a composing char. */
4468 if (utf_iscomposing(mb_c))
4469 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004470 int i;
4471
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004472 for (i = Screen_mco - 1; i > 0; --i)
4473 u8cc[i] = u8cc[i - 1];
4474 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004475 mb_c = ' ';
4476 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 }
4478
4479 if ((mb_l == 1 && c >= 0x80)
4480 || (mb_l >= 1 && mb_c == 0)
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004481 || (mb_l > 1 && (!vim_isprintc(mb_c))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 {
4483 /*
4484 * Illegal UTF-8 byte: display as <xx>.
4485 * Non-BMP character : display as ? or fullwidth ?.
4486 */
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004487 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004488# ifdef FEAT_RIGHTLEFT
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004489 if (wp->w_p_rl) /* reverse */
4490 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004491# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 p_extra = extra;
4493 c = *p_extra;
4494 mb_c = mb_ptr2char_adv(&p_extra);
4495 mb_utf8 = (c >= 0x80);
4496 n_extra = (int)STRLEN(p_extra);
4497 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004498 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 if (area_attr == 0 && search_attr == 0)
4500 {
4501 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004502 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 saved_attr2 = char_attr; /* save current attr */
4504 }
4505 }
4506 else if (mb_l == 0) /* at the NUL at end-of-line */
4507 mb_l = 1;
4508#ifdef FEAT_ARABIC
4509 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4510 {
4511 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004512 int pc, pc1, nc;
4513 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514
4515 /* The idea of what is the previous and next
4516 * character depends on 'rightleft'. */
4517 if (wp->w_p_rl)
4518 {
4519 pc = prev_c;
4520 pc1 = prev_c1;
4521 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004522 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 }
4524 else
4525 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004526 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004528 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 }
4530 prev_c = mb_c;
4531
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004532 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 }
4534 else
4535 prev_c = mb_c;
4536#endif
4537 }
4538 else /* enc_dbcs */
4539 {
4540 mb_l = MB_BYTE2LEN(c);
4541 if (mb_l == 0) /* at the NUL at end-of-line */
4542 mb_l = 1;
4543 else if (mb_l > 1)
4544 {
4545 /* We assume a second byte below 32 is illegal.
4546 * Hopefully this is OK for all double-byte encodings!
4547 */
4548 if (ptr[1] >= 32)
4549 mb_c = (c << 8) + ptr[1];
4550 else
4551 {
4552 if (ptr[1] == NUL)
4553 {
4554 /* head byte at end of line */
4555 mb_l = 1;
4556 transchar_nonprint(extra, c);
4557 }
4558 else
4559 {
4560 /* illegal tail byte */
4561 mb_l = 2;
4562 STRCPY(extra, "XX");
4563 }
4564 p_extra = extra;
4565 n_extra = (int)STRLEN(extra) - 1;
4566 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004567 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 c = *p_extra++;
4569 if (area_attr == 0 && search_attr == 0)
4570 {
4571 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004572 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 saved_attr2 = char_attr; /* save current attr */
4574 }
4575 mb_c = c;
4576 }
4577 }
4578 }
4579 /* If a double-width char doesn't fit display a '>' in the
4580 * last column; the character is displayed at the start of the
4581 * next line. */
4582 if ((
4583# ifdef FEAT_RIGHTLEFT
4584 wp->w_p_rl ? (col <= 0) :
4585# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004586 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 && (*mb_char2cells)(mb_c) == 2)
4588 {
4589 c = '>';
4590 mb_c = c;
4591 mb_utf8 = FALSE;
4592 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004593 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02004594 // Put pointer back so that the character will be
4595 // displayed at the start of the next line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 --ptr;
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02004597#ifdef FEAT_CONCEAL
4598 did_decrement_ptr = TRUE;
4599#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 }
4601 else if (*ptr != NUL)
4602 ptr += mb_l - 1;
4603
4604 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004605 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004606 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004607 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004610 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004611 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 c = ' ';
4613 if (area_attr == 0 && search_attr == 0)
4614 {
4615 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004616 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 saved_attr2 = char_attr; /* save current attr */
4618 }
4619 mb_c = c;
4620 mb_utf8 = FALSE;
4621 mb_l = 1;
4622 }
4623
4624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 ++ptr;
4626
4627 if (extra_check)
4628 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004629#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004630 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004631#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004632
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004633#ifdef FEAT_TERMINAL
4634 if (get_term_attr)
4635 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02004636 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004637
4638 if (!attr_pri)
4639 char_attr = syntax_attr;
4640 else
4641 char_attr = hl_combine_attr(syntax_attr, char_attr);
4642 }
4643#endif
4644
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004645#ifdef FEAT_SYN_HL
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004646 // Get syntax attribute, unless still at the start of the line
4647 // (double-wide char that doesn't fit).
Bram Moolenaar217ad922005-03-20 22:37:15 +00004648 v = (long)(ptr - line);
4649 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 {
4651 /* Get the syntax attribute for the character. If there
4652 * is an error, disable syntax highlighting. */
4653 save_did_emsg = did_emsg;
4654 did_emsg = FALSE;
4655
Bram Moolenaar217ad922005-03-20 22:37:15 +00004656 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004657# ifdef FEAT_SPELL
4658 has_spell ? &can_spell :
4659# endif
4660 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661
4662 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004663 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004664 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004665 has_syntax = FALSE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004666 syntax_attr = 0;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 else
4669 did_emsg = save_did_emsg;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004670
4671 // combine syntax attribute with 'wincolor'
4672 if (win_attr != 0)
4673 syntax_attr = hl_combine_attr(win_attr, syntax_attr);
4674
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004675#ifdef SYN_TIME_LIMIT
4676 if (wp->w_s->b_syn_slow)
4677 has_syntax = FALSE;
4678#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679
4680 /* Need to get the line again, a multi-line regexp may
4681 * have made it invalid. */
4682 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4683 ptr = line + v;
4684
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004685# ifdef FEAT_TEXT_PROP
Bram Moolenaarde24a872019-05-05 15:48:00 +02004686 // Text properties overrule syntax highlighting or combine.
4687 if (text_prop_attr == 0 || text_prop_combine)
4688# endif
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004689 {
Bram Moolenaarde24a872019-05-05 15:48:00 +02004690 int comb_attr = syntax_attr;
4691# ifdef FEAT_TEXT_PROP
4692 comb_attr = hl_combine_attr(text_prop_attr, comb_attr);
4693# endif
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004694 if (!attr_pri)
Bram Moolenaarde24a872019-05-05 15:48:00 +02004695 char_attr = comb_attr;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004696 else
Bram Moolenaarde24a872019-05-05 15:48:00 +02004697 char_attr = hl_combine_attr(comb_attr, char_attr);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004698 }
Bram Moolenaarc095b282010-07-20 22:33:34 +02004699# ifdef FEAT_CONCEAL
4700 /* no concealing past the end of the line, it interferes
4701 * with line highlighting */
4702 if (c == NUL)
4703 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004704 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004705 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004706# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004708#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004709
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004710#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004711 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004712 * Only do this when there is no syntax highlighting, the
4713 * @Spell cluster is not used or the current syntax item
4714 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004715 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004716 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004717 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004718 if (c != 0 && (
4719# ifdef FEAT_SYN_HL
4720 !has_syntax ||
4721# endif
4722 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004723 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004724 char_u *prev_ptr, *p;
4725 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004726 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaare7566042005-06-17 22:00:15 +00004727 if (has_mbyte)
4728 {
4729 prev_ptr = ptr - mb_l;
4730 v -= mb_l - 1;
4731 }
4732 else
Bram Moolenaare7566042005-06-17 22:00:15 +00004733 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004734
4735 /* Use nextline[] if possible, it has the start of the
4736 * next line concatenated. */
4737 if ((prev_ptr - line) - nextlinecol >= 0)
4738 p = nextline + (prev_ptr - line) - nextlinecol;
4739 else
4740 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004741 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004742 len = spell_check(wp, p, &spell_hlf, &cap_col,
4743 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004744 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004745
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004746 /* In Insert mode only highlight a word that
4747 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004748 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004749 && (State & INSERT) != 0
4750 && wp->w_cursor.lnum == lnum
4751 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004752 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004753 && wp->w_cursor.col < (colnr_T)word_end)
4754 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004755 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004756 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004757 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004758
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004759 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004760 && (p - nextline) + len > nextline_idx)
4761 {
4762 /* Remember that the good word continues at the
4763 * start of the next line. */
4764 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004765 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004766 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004767
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004768 /* Turn index into actual attributes. */
4769 if (spell_hlf != HLF_COUNT)
4770 spell_attr = highlight_attr[spell_hlf];
4771
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004772 if (cap_col > 0)
4773 {
4774 if (p != prev_ptr
4775 && (p - nextline) + cap_col >= nextline_idx)
4776 {
4777 /* Remember that the word in the next line
4778 * must start with a capital. */
4779 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004780 cap_col = (int)((p - nextline) + cap_col
4781 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004782 }
4783 else
4784 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004785 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004786 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004787 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004788 }
4789 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004790 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004791 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004792 char_attr = hl_combine_attr(char_attr, spell_attr);
4793 else
4794 char_attr = hl_combine_attr(spell_attr, char_attr);
4795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796#endif
4797#ifdef FEAT_LINEBREAK
4798 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004799 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004801 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004802 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 {
Bram Moolenaar4df70292015-03-21 14:20:16 +01004804 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004805 char_u *p = ptr - (mb_off + 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004806
Bram Moolenaar597a4222014-06-25 14:39:50 +02004807 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004808 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004809 NULL) - 1;
Bram Moolenaar02631462017-09-22 15:20:32 +02004810 if (c == TAB && n_extra + col > wp->w_width)
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004811# ifdef FEAT_VARTABS
Bram Moolenaara87b72c2018-06-25 21:24:51 +02004812 n_extra = tabstop_padding(vcol, wp->w_buffer->b_p_ts,
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004813 wp->w_buffer->b_p_vts_array) - 1;
4814# else
Bram Moolenaara3650912014-11-19 13:21:57 +01004815 n_extra = (int)wp->w_buffer->b_p_ts
4816 - vcol % (int)wp->w_buffer->b_p_ts - 1;
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004817# endif
Bram Moolenaara3650912014-11-19 13:21:57 +01004818
Bram Moolenaar4df70292015-03-21 14:20:16 +01004819 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004820 c_final = NUL;
Bram Moolenaar1c465442017-03-12 20:10:05 +01004821 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004822 {
4823#ifdef FEAT_CONCEAL
4824 if (c == TAB)
4825 /* See "Tab alignment" below. */
4826 FIX_FOR_BOGUSCOLS;
4827#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004828 if (!wp->w_p_list)
4829 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 }
4832#endif
4833
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004834 // 'list': Change char 160 to lcs_nbsp and space to lcs_space.
4835 // But not when the character is followed by a composing
4836 // character (use mb_l to check that).
4837 if (wp->w_p_list
4838 && ((((c == 160 && mb_l == 1)
4839 || (mb_utf8
4840 && ((mb_c == 160 && mb_l == 2)
4841 || (mb_c == 0x202f && mb_l == 3))))
4842 && lcs_nbsp)
4843 || (c == ' '
4844 && mb_l == 1
4845 && lcs_space
4846 && ptr - line <= trailcol)))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004847 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004848 c = (c == ' ') ? lcs_space : lcs_nbsp;
4849 if (area_attr == 0 && search_attr == 0)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004850 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004851 n_attr = 1;
4852 extra_attr = HL_ATTR(HLF_8);
4853 saved_attr2 = char_attr; /* save current attr */
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004854 }
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004855 mb_c = c;
4856 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004857 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004858 mb_utf8 = TRUE;
4859 u8cc[0] = 0;
4860 c = 0xc0;
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004861 }
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004862 else
4863 mb_utf8 = FALSE;
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004864 }
4865
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4867 {
4868 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004869 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 {
4871 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004872 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 saved_attr2 = char_attr; /* save current attr */
4874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004876 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 {
4878 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004879 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004880 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 }
4882 else
4883 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 }
4885 }
4886
4887 /*
4888 * Handling of non-printable characters.
4889 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004890 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 {
4892 /*
4893 * when getting a character from the file, we may have to
4894 * turn it into something else on the way to putting it
4895 * into "ScreenLines".
4896 */
4897 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4898 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004899 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004900 long vcol_adjusted = vcol; /* removed showbreak length */
4901#ifdef FEAT_LINEBREAK
4902 /* only adjust the tab_len, when at the first column
4903 * after the showbreak value was drawn */
4904 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4905 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4906#endif
Bram Moolenaar69cbbec2019-08-17 14:10:56 +02004907 // tab amount depends on current column
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004908#ifdef FEAT_VARTABS
4909 tab_len = tabstop_padding(vcol_adjusted,
4910 wp->w_buffer->b_p_ts,
4911 wp->w_buffer->b_p_vts_array) - 1;
4912#else
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004913 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004914 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4915#endif
Bram Moolenaard574ea22015-01-14 19:35:14 +01004916
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004917#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004918 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004919#endif
Bram Moolenaar69cbbec2019-08-17 14:10:56 +02004920 // tab amount depends on current column
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004921 n_extra = tab_len;
4922#ifdef FEAT_LINEBREAK
4923 else
4924 {
Bram Moolenaar69cbbec2019-08-17 14:10:56 +02004925 char_u *p;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01004926 int len;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004927 int i;
4928 int saved_nextra = n_extra;
4929
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004930#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004931 if (vcol_off > 0)
Bram Moolenaar69cbbec2019-08-17 14:10:56 +02004932 // there are characters to conceal
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004933 tab_len += vcol_off;
Bram Moolenaar69cbbec2019-08-17 14:10:56 +02004934 // boguscols before FIX_FOR_BOGUSCOLS macro from above
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004935 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
4936 && n_extra > tab_len)
4937 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004938#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004939
Bram Moolenaar69cbbec2019-08-17 14:10:56 +02004940 // if n_extra > 0, it gives the number of chars, to
4941 // use for a tab, else we need to calculate the width
4942 // for a tab
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004943 len = (tab_len * mb_char2len(lcs_tab2));
4944 if (n_extra > 0)
4945 len += n_extra - tab_len;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004946 c = lcs_tab1;
Bram Moolenaar964b3742019-05-24 18:54:09 +02004947 p = alloc(len + 1);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004948 vim_memset(p, ' ', len);
4949 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004950 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004951 p_extra_free = p;
4952 for (i = 0; i < tab_len; i++)
4953 {
Bram Moolenaar69cbbec2019-08-17 14:10:56 +02004954 int lcs = lcs_tab2;
4955
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004956 if (*p == NUL)
4957 {
4958 tab_len = i;
4959 break;
4960 }
Bram Moolenaar69cbbec2019-08-17 14:10:56 +02004961
4962 // if lcs_tab3 is given, need to change the char
4963 // for tab
4964 if (lcs_tab3 && i == tab_len - 1)
4965 lcs = lcs_tab3;
4966 mb_char2bytes(lcs, p);
4967 p += mb_char2len(lcs);
4968 n_extra += mb_char2len(lcs)
4969 - (saved_nextra > 0 ? 1 : 0);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004970 }
4971 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004972#ifdef FEAT_CONCEAL
Bram Moolenaar69cbbec2019-08-17 14:10:56 +02004973 // n_extra will be increased by FIX_FOX_BOGUSCOLS
4974 // macro below, so need to adjust for that here
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004975 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004976 n_extra -= vcol_off;
4977#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004978 }
4979#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004980#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004981 {
4982 int vc_saved = vcol_off;
4983
4984 /* Tab alignment should be identical regardless of
4985 * 'conceallevel' value. So tab compensates of all
4986 * previous concealed characters, and thus resets
4987 * vcol_off and boguscols accumulated so far in the
4988 * line. Note that the tab can be longer than
4989 * 'tabstop' when there are concealed characters. */
4990 FIX_FOR_BOGUSCOLS;
4991
4992 /* Make sure, the highlighting for the tab char will be
4993 * correctly set further below (effectively reverts the
4994 * FIX_FOR_BOGSUCOLS macro */
4995 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004996 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004997 tab_len += vc_saved;
4998 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004999#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 if (wp->w_p_list)
5002 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01005003 c = (n_extra == 0 && lcs_tab3) ? lcs_tab3 : lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005004#ifdef FEAT_LINEBREAK
5005 if (wp->w_p_lbr)
5006 c_extra = NUL; /* using p_extra from above */
5007 else
5008#endif
5009 c_extra = lcs_tab2;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005010 c_final = lcs_tab3;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005011 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005012 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 saved_attr2 = char_attr; /* save current attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005015 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 {
5017 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005018 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005019 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 }
5022 else
5023 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01005024 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 c_extra = ' ';
5026 c = ' ';
5027 }
5028 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005029 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02005030 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005031 || ((fromcol >= 0 || fromcol_prev >= 0)
5032 && tocol > vcol
5033 && VIsual_mode != Ctrl_V
5034 && (
5035# ifdef FEAT_RIGHTLEFT
5036 wp->w_p_rl ? (col >= 0) :
5037# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005038 (col < wp->w_width))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005039 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005040 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005041 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02005042 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005044 /* Display a '$' after the line or highlight an extra
5045 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046#if defined(FEAT_DIFF) || defined(LINE_ATTR)
5047 /* For a diff line the highlighting continues after the
5048 * "$". */
5049 if (
5050# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005051 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052# ifdef LINE_ATTR
5053 &&
5054# endif
5055# endif
5056# ifdef LINE_ATTR
5057 line_attr == 0
5058# endif
5059 )
5060#endif
5061 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 /* In virtualedit, visual selections may extend
5063 * beyond end of line. */
5064 if (area_highlighting && virtual_active()
5065 && tocol != MAXCOL && vcol < tocol)
5066 n_extra = 0;
5067 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 {
5069 p_extra = at_end_str;
5070 n_extra = 1;
5071 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005072 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 }
5074 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02005075 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005076 c = lcs_eol;
5077 else
5078 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 lcs_eol_one = -1;
5080 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005081 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005083 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 n_attr = 1;
5085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005087 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 {
5089 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005090 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005091 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 }
5093 else
5094 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 }
5096 else if (c != NUL)
5097 {
5098 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02005099 if (n_extra == 0)
5100 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101#ifdef FEAT_RIGHTLEFT
5102 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
5103 rl_mirror(p_extra); /* reverse "<12>" */
5104#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005106 c_final = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005107#ifdef FEAT_LINEBREAK
5108 if (wp->w_p_lbr)
5109 {
5110 char_u *p;
5111
5112 c = *p_extra;
Bram Moolenaar964b3742019-05-24 18:54:09 +02005113 p = alloc(n_extra + 1);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005114 vim_memset(p, ' ', n_extra);
5115 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5116 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005117 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005118 p_extra_free = p_extra = p;
5119 }
5120 else
5121#endif
5122 {
5123 n_extra = byte2cells(c) - 1;
5124 c = *p_extra++;
5125 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005126 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 {
5128 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005129 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 saved_attr2 = char_attr; /* save current attr */
5131 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 else if (VIsual_active
5135 && (VIsual_mode == Ctrl_V
5136 || VIsual_mode == 'v')
5137 && virtual_active()
5138 && tocol != MAXCOL
5139 && vcol < tocol
5140 && (
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005141#ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 wp->w_p_rl ? (col >= 0) :
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005143#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005144 (col < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 {
5146 c = ' ';
5147 --ptr; /* put it back at the NUL */
5148 }
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005149#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 else if ((
5151# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005152 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005154# ifdef FEAT_TERMINAL
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005155 win_attr != 0 ||
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005156# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 ) && (
5159# ifdef FEAT_RIGHTLEFT
5160 wp->w_p_rl ? (col >= 0) :
5161# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005162 (col
5163# ifdef FEAT_CONCEAL
5164 - boguscols
5165# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005166 < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 {
5168 /* Highlight until the right side of the window */
5169 c = ' ';
5170 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005171
5172 /* Remember we do the char for line highlighting. */
5173 ++did_line_attr;
5174
5175 /* don't do search HL for the rest of the line */
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005176 if (line_attr != 0 && char_attr == search_attr
5177 && (did_line_attr > 1
5178 || (wp->w_p_list && lcs_eol > 0)))
Bram Moolenaar91170f82006-05-05 21:15:17 +00005179 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180# ifdef FEAT_DIFF
5181 if (diff_hlf == HLF_TXD)
5182 {
5183 diff_hlf = HLF_CHD;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005184 if (vi_attr == 0 || char_attr != vi_attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005185 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005186 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005187 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
5188 && *wp->w_p_culopt != 'n')
Bram Moolenaare0f14822014-08-06 13:20:56 +02005189 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005190 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005191 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 }
5193# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005194# ifdef FEAT_TERMINAL
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005195 if (win_attr != 0)
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005196 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005197 char_attr = win_attr;
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005198 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5199 char_attr = hl_combine_attr(char_attr,
5200 HL_ATTR(HLF_CUL));
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02005201 else if (line_attr)
5202 char_attr = hl_combine_attr(char_attr, line_attr);
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005203 }
5204# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 }
5206#endif
5207 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005208
5209#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005210 if ( wp->w_p_cole > 0
5211 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaarab62c192019-03-30 16:39:05 +01005212 conceal_cursor_line(wp))
5213 && ((syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005214 && !(lnum_in_visual_area
5215 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005216 {
5217 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005218 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005219 && (syn_get_sub_char() != NUL || match_conc
5220 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005221 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005222 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005223 /* First time at this concealed item: display one
5224 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005225 if (match_conc)
5226 c = match_conc;
5227 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005228 c = syn_get_sub_char();
5229 else if (lcs_conceal != NUL)
5230 c = lcs_conceal;
5231 else
5232 c = ' ';
5233
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005234 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005235
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005236 if (n_extra > 0)
5237 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005238 vcol += n_extra;
5239 if (wp->w_p_wrap && n_extra > 0)
5240 {
5241# ifdef FEAT_RIGHTLEFT
5242 if (wp->w_p_rl)
5243 {
5244 col -= n_extra;
5245 boguscols -= n_extra;
5246 }
5247 else
5248# endif
5249 {
5250 boguscols += n_extra;
5251 col += n_extra;
5252 }
5253 }
5254 n_extra = 0;
5255 n_attr = 0;
5256 }
5257 else if (n_skip == 0)
5258 {
5259 is_concealing = TRUE;
5260 n_skip = 1;
5261 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005262 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005263 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005264 {
5265 mb_utf8 = TRUE;
5266 u8cc[0] = 0;
5267 c = 0xc0;
5268 }
5269 else
5270 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005271 }
5272 else
5273 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005274 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005275 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005276 }
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02005277
5278 if (n_skip > 0 && did_decrement_ptr)
5279 // not showing the '>', put pointer back to avoid getting stuck
5280 ++ptr;
5281
5282#endif // FEAT_CONCEAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 }
5284
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005285#ifdef FEAT_CONCEAL
5286 /* In the cursor line and we may be concealing characters: correct
5287 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005288 if (!did_wcol && draw_state == WL_LINE
5289 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005290 && conceal_cursor_line(wp)
5291 && (int)wp->w_virtcol <= vcol + n_skip)
5292 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005293# ifdef FEAT_RIGHTLEFT
5294 if (wp->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02005295 wp->w_wcol = wp->w_width - col + boguscols - 1;
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005296 else
5297# endif
5298 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005299 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005300 did_wcol = TRUE;
5301 }
5302#endif
5303
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 /* Don't override visual selection highlighting. */
5305 if (n_attr > 0
5306 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005307 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 char_attr = extra_attr;
5309
Bram Moolenaar81695252004-12-29 20:58:21 +00005310#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 /* XIM don't send preedit_start and preedit_end, but they send
5312 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5313 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005314 if (p_imst == IM_ON_THE_SPOT
5315 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005316 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 && (State & INSERT)
5318 && !p_imdisable
5319 && im_is_preediting()
5320 && draw_state == WL_LINE)
5321 {
5322 colnr_T tcol;
5323
5324 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005325 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 else
5327 tcol = preedit_end_col;
5328 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5329 {
5330 if (feedback_old_attr < 0)
5331 {
5332 feedback_col = 0;
5333 feedback_old_attr = char_attr;
5334 }
5335 char_attr = im_get_feedback_attr(feedback_col);
5336 if (char_attr < 0)
5337 char_attr = feedback_old_attr;
5338 feedback_col++;
5339 }
5340 else if (feedback_old_attr >= 0)
5341 {
5342 char_attr = feedback_old_attr;
5343 feedback_old_attr = -1;
5344 feedback_col = 0;
5345 }
5346 }
5347#endif
5348 /*
5349 * Handle the case where we are in column 0 but not on the first
5350 * character of the line and the user wants us to show us a
5351 * special character (via 'listchars' option "precedes:<char>".
5352 */
5353 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005354 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5356#ifdef FEAT_DIFF
5357 && filler_todo <= 0
5358#endif
5359 && draw_state > WL_NR
5360 && c != NUL)
5361 {
5362 c = lcs_prec;
5363 lcs_prec_todo = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005364 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5365 {
5366 /* Double-width character being overwritten by the "precedes"
5367 * character, need to fill up half the character. */
5368 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005369 c_final = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005370 n_extra = 1;
5371 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005372 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005375 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 {
5377 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005378 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005379 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 }
5381 else
5382 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005383 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 {
5385 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005386 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 n_attr3 = 1;
5388 }
5389 }
5390
5391 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005392 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 */
Bram Moolenaarf914a332019-07-20 15:09:56 +02005394 if ((c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005395#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005396 || did_line_attr == 1
5397#endif
Bram Moolenaarf914a332019-07-20 15:09:56 +02005398 ) && eol_hl_off == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005400#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarbbca7732019-07-24 18:13:16 +02005401 // flag to indicate whether prevcol equals startcol of search_hl or
5402 // one of the matches
5403 int prevcol_hl_flag = get_prevcol_hl_flag(wp, &search_hl,
5404 (long)(ptr - line) - (c == NUL));
Bram Moolenaar91170f82006-05-05 21:15:17 +00005405#endif
Bram Moolenaarbbca7732019-07-24 18:13:16 +02005406 // Invert at least one char, used for Visual and empty line or
5407 // highlight match at end of line. If it's beyond the last
5408 // char on the screen, just overwrite that one (tricky!) Not
5409 // needed when a '$' was displayed for 'list'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005411 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005412 && (VIsual_mode != Ctrl_V
5413 || lnum == VIsual.lnum
5414 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005415 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarbbca7732019-07-24 18:13:16 +02005417 // highlight 'hlsearch' match at end of line
5418 || (prevcol_hl_flag
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005419# ifdef FEAT_SYN_HL
5420 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
5421 && !(wp == curwin && VIsual_active))
5422# endif
5423# ifdef FEAT_DIFF
5424 && diff_hlf == (hlf_T)0
5425# endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005426# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005427 && did_line_attr <= 1
5428# endif
5429 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430#endif
5431 ))
5432 {
5433 int n = 0;
5434
5435#ifdef FEAT_RIGHTLEFT
5436 if (wp->w_p_rl)
5437 {
5438 if (col < 0)
5439 n = 1;
5440 }
5441 else
5442#endif
5443 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005444 if (col >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 n = -1;
5446 }
5447 if (n != 0)
5448 {
5449 /* At the window boundary, highlight the last character
5450 * instead (better than nothing). */
5451 off += n;
5452 col += n;
5453 }
5454 else
5455 {
5456 /* Add a blank character to highlight. */
5457 ScreenLines[off] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 if (enc_utf8)
5459 ScreenLinesUC[off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 }
5461#ifdef FEAT_SEARCH_EXTRA
5462 if (area_attr == 0)
5463 {
Bram Moolenaarbbca7732019-07-24 18:13:16 +02005464 // Use attributes from match with highest priority among
5465 // 'search_hl' and the match list.
5466 get_search_match_hl(wp, &search_hl,
5467 (long)(ptr - line), &char_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 }
5469#endif
5470 ScreenAttrs[off] = char_attr;
5471#ifdef FEAT_RIGHTLEFT
5472 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005473 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005475 --off;
5476 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 else
5478#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005479 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005481 ++off;
5482 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005483 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005484 eol_hl_off = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005486 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487
Bram Moolenaar91170f82006-05-05 21:15:17 +00005488 /*
5489 * At end of the text line.
5490 */
5491 if (c == NUL)
5492 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005493#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005494 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005495 if (wp->w_p_wrap)
5496 v = wp->w_skipcol;
5497 else
5498 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005499
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005500 /* check if line ends before left margin */
5501 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005502 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005503#ifdef FEAT_CONCEAL
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01005504 // Get rid of the boguscols now, we want to draw until the right
5505 // edge for 'cursorcolumn'.
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005506 col -= boguscols;
5507 boguscols = 0;
5508#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005509
5510 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005511 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005512
5513 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005514 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5515 && (int)wp->w_virtcol <
Bram Moolenaar02631462017-09-22 15:20:32 +02005516 wp->w_width * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005517 && lnum != wp->w_cursor.lnum)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005518 || draw_color_col
5519 || win_attr != 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005520# ifdef FEAT_RIGHTLEFT
5521 && !wp->w_p_rl
5522# endif
5523 )
5524 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005525 int rightmost_vcol = 0;
5526 int i;
5527
5528 if (wp->w_p_cuc)
5529 rightmost_vcol = wp->w_virtcol;
5530 if (draw_color_col)
5531 /* determine rightmost colorcolumn to possibly draw */
5532 for (i = 0; color_cols[i] >= 0; ++i)
5533 if (rightmost_vcol < color_cols[i])
5534 rightmost_vcol = color_cols[i];
5535
Bram Moolenaar02631462017-09-22 15:20:32 +02005536 while (col < wp->w_width)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005537 {
5538 ScreenLines[off] = ' ';
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005539 if (enc_utf8)
5540 ScreenLinesUC[off] = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005541 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005542 if (draw_color_col)
5543 draw_color_col = advance_color_col(VCOL_HLC,
5544 &color_cols);
5545
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005546 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005547 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005548 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005549 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005550 else
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005551 ScreenAttrs[off++] = win_attr;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005552
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005553 if (VCOL_HLC >= rightmost_vcol && win_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005554 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005555
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005556 ++vcol;
5557 }
5558 }
5559#endif
5560
Bram Moolenaar53f81742017-09-22 14:35:51 +02005561 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005562 (int)wp->w_width, screen_line_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 row++;
5564
5565 /*
5566 * Update w_cline_height and w_cline_folded if the cursor line was
5567 * updated (saves a call to plines() later).
5568 */
5569 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5570 {
5571 curwin->w_cline_row = startrow;
5572 curwin->w_cline_height = row - startrow;
5573#ifdef FEAT_FOLDING
5574 curwin->w_cline_folded = FALSE;
5575#endif
5576 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5577 }
5578
5579 break;
5580 }
5581
Bram Moolenaara5c6a0b2019-05-08 20:20:46 +02005582 // Show "extends" character from 'listchars' if beyond the line end and
5583 // 'list' is set.
5584 if (lcs_ext != NUL
5585 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 && !wp->w_p_wrap
5587#ifdef FEAT_DIFF
5588 && filler_todo <= 0
5589#endif
5590 && (
5591#ifdef FEAT_RIGHTLEFT
5592 wp->w_p_rl ? col == 0 :
5593#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005594 col == wp->w_width - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005596 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5598 {
5599 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005600 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005602 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 {
5604 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005605 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005606 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 }
5608 else
5609 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610 }
5611
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005612#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005613 /* advance to the next 'colorcolumn' */
5614 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005615 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005616
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005617 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005618 * highlight the cursor position itself.
5619 * Also highlight the 'colorcolumn' if it is different than
5620 * 'cursorcolumn' */
5621 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005622 if (draw_state == WL_LINE && !lnum_in_visual_area
5623 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005624 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005625 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005626 && lnum != wp->w_cursor.lnum)
5627 {
5628 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005629 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005630 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005631 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005632 {
5633 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005634 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005635 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005636 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005637#endif
5638
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 /*
5640 * Store character to be displayed.
5641 * Skip characters that are left of the screen for 'nowrap'.
5642 */
5643 vcol_prev = vcol;
5644 if (draw_state < WL_LINE || n_skip <= 0)
5645 {
5646 /*
5647 * Store the character.
5648 */
Bram Moolenaara12a1612019-01-24 16:39:02 +01005649#if defined(FEAT_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5651 {
5652 /* A double-wide character is: put first halve in left cell. */
5653 --off;
5654 --col;
5655 }
5656#endif
5657 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005659 {
5660 if ((mb_c & 0xff00) == 0x8e00)
5661 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 else if (enc_utf8)
5665 {
5666 if (mb_utf8)
5667 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005668 int i;
5669
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005671 if ((c & 0xff) == 0)
5672 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005673 for (i = 0; i < Screen_mco; ++i)
5674 {
5675 ScreenLinesC[i][off] = u8cc[i];
5676 if (u8cc[i] == 0)
5677 break;
5678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 }
5680 else
5681 ScreenLinesUC[off] = 0;
5682 }
5683 if (multi_attr)
5684 {
5685 ScreenAttrs[off] = multi_attr;
5686 multi_attr = 0;
5687 }
5688 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 ScreenAttrs[off] = char_attr;
5690
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5692 {
5693 /* Need to fill two screen columns. */
5694 ++off;
5695 ++col;
5696 if (enc_utf8)
5697 /* UTF-8: Put a 0 in the second screen char. */
5698 ScreenLines[off] = 0;
5699 else
5700 /* DBCS: Put second byte in the second screen char. */
5701 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005702 if (draw_state > WL_NR
5703#ifdef FEAT_DIFF
5704 && filler_todo <= 0
5705#endif
5706 )
5707 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 /* When "tocol" is halfway a character, set it to the end of
5709 * the character, otherwise highlighting won't stop. */
5710 if (tocol == vcol)
5711 ++tocol;
5712#ifdef FEAT_RIGHTLEFT
5713 if (wp->w_p_rl)
5714 {
5715 /* now it's time to backup one cell */
5716 --off;
5717 --col;
5718 }
5719#endif
5720 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721#ifdef FEAT_RIGHTLEFT
5722 if (wp->w_p_rl)
5723 {
5724 --off;
5725 --col;
5726 }
5727 else
5728#endif
5729 {
5730 ++off;
5731 ++col;
5732 }
5733 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005734#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005735 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005736 {
5737 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005738 ++vcol_off;
5739 if (n_extra > 0)
5740 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005741 if (wp->w_p_wrap)
5742 {
5743 /*
5744 * Special voodoo required if 'wrap' is on.
5745 *
5746 * Advance the column indicator to force the line
5747 * drawing to wrap early. This will make the line
5748 * take up the same screen space when parts are concealed,
5749 * so that cursor line computations aren't messed up.
5750 *
5751 * To avoid the fictitious advance of 'col' causing
5752 * trailing junk to be written out of the screen line
5753 * we are building, 'boguscols' keeps track of the number
5754 * of bad columns we have advanced.
5755 */
5756 if (n_extra > 0)
5757 {
5758 vcol += n_extra;
5759# ifdef FEAT_RIGHTLEFT
5760 if (wp->w_p_rl)
5761 {
5762 col -= n_extra;
5763 boguscols -= n_extra;
5764 }
5765 else
5766# endif
5767 {
5768 col += n_extra;
5769 boguscols += n_extra;
5770 }
5771 n_extra = 0;
5772 n_attr = 0;
5773 }
5774
5775
Bram Moolenaar860cae12010-06-05 23:22:07 +02005776 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5777 {
5778 /* Need to fill two screen columns. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01005779# ifdef FEAT_RIGHTLEFT
Bram Moolenaar860cae12010-06-05 23:22:07 +02005780 if (wp->w_p_rl)
5781 {
5782 --boguscols;
5783 --col;
5784 }
5785 else
Bram Moolenaara12a1612019-01-24 16:39:02 +01005786# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02005787 {
5788 ++boguscols;
5789 ++col;
5790 }
5791 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005792
5793# ifdef FEAT_RIGHTLEFT
5794 if (wp->w_p_rl)
5795 {
5796 --boguscols;
5797 --col;
5798 }
5799 else
5800# endif
5801 {
5802 ++boguscols;
5803 ++col;
5804 }
5805 }
5806 else
5807 {
5808 if (n_extra > 0)
5809 {
5810 vcol += n_extra;
5811 n_extra = 0;
5812 n_attr = 0;
5813 }
5814 }
5815
5816 }
5817#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 else
5819 --n_skip;
5820
Bram Moolenaar64486672010-05-16 15:46:46 +02005821 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5822 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005823 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824#ifdef FEAT_DIFF
5825 && filler_todo <= 0
5826#endif
5827 )
5828 ++vcol;
5829
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005830#ifdef FEAT_SYN_HL
5831 if (vcol_save_attr >= 0)
5832 char_attr = vcol_save_attr;
5833#endif
5834
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 /* restore attributes after "predeces" in 'listchars' */
5836 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5837 char_attr = saved_attr3;
5838
5839 /* restore attributes after last 'listchars' or 'number' char */
5840 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5841 char_attr = saved_attr2;
5842
5843 /*
5844 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005845 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 */
5847 if ((
5848#ifdef FEAT_RIGHTLEFT
5849 wp->w_p_rl ? (col < 0) :
5850#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005851 (col >= wp->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 && (*ptr != NUL
5853#ifdef FEAT_DIFF
5854 || filler_todo > 0
5855#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005856 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5858 )
5859 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005860#ifdef FEAT_CONCEAL
Bram Moolenaar53f81742017-09-22 14:35:51 +02005861 screen_line(screen_row, wp->w_wincol, col - boguscols,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005862 (int)wp->w_width, screen_line_flags);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005863 boguscols = 0;
5864#else
Bram Moolenaar53f81742017-09-22 14:35:51 +02005865 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005866 (int)wp->w_width, screen_line_flags);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005867#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868 ++row;
5869 ++screen_row;
5870
5871 /* When not wrapping and finished diff lines, or when displayed
5872 * '$' and highlighting until last column, break here. */
5873 if ((!wp->w_p_wrap
5874#ifdef FEAT_DIFF
5875 && filler_todo <= 0
5876#endif
5877 ) || lcs_eol_one == -1)
5878 break;
5879
5880 /* When the window is too narrow draw all "@" lines. */
5881 if (draw_state != WL_LINE
5882#ifdef FEAT_DIFF
5883 && filler_todo <= 0
5884#endif
5885 )
5886 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01005887 win_draw_end(wp, '@', ' ', TRUE, row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889 row = endrow;
5890 }
5891
5892 /* When line got too long for screen break here. */
5893 if (row == endrow)
5894 {
5895 ++row;
5896 break;
5897 }
5898
5899 if (screen_cur_row == screen_row - 1
5900#ifdef FEAT_DIFF
5901 && filler_todo <= 0
5902#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005903 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904 {
5905 /* Remember that the line wraps, used for modeless copy. */
5906 LineWraps[screen_row - 1] = TRUE;
5907
5908 /*
5909 * Special trick to make copy/paste of wrapped lines work with
5910 * xterm/screen: write an extra character beyond the end of
5911 * the line. This will work with all terminal types
5912 * (regardless of the xn,am settings).
5913 * Only do this on a fast tty.
5914 * Only do this if the cursor is on the current line
5915 * (something has been written in it).
5916 * Don't do this for the GUI.
5917 * Don't do this for double-width characters.
5918 * Don't do this for a window not at the right screen border.
5919 */
5920 if (p_tf
5921#ifdef FEAT_GUI
5922 && !gui.in_use
5923#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005925 && ((*mb_off2cells)(LineOffset[screen_row],
5926 LineOffset[screen_row] + screen_Columns)
5927 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005929 + (int)Columns - 2,
5930 LineOffset[screen_row] + screen_Columns)
Bram Moolenaara12a1612019-01-24 16:39:02 +01005931 == 2)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932 {
5933 /* First make sure we are at the end of the screen line,
5934 * then output the same character again to let the
5935 * terminal know about the wrap. If the terminal doesn't
5936 * auto-wrap, we overwrite the character. */
Bram Moolenaar02631462017-09-22 15:20:32 +02005937 if (screen_cur_col != wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 screen_char(LineOffset[screen_row - 1]
5939 + (unsigned)Columns - 1,
5940 screen_row - 1, (int)(Columns - 1));
5941
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 /* When there is a multi-byte character, just output a
5943 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005944 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5945 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946 out_char(' ');
5947 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948 out_char(ScreenLines[LineOffset[screen_row - 1]
5949 + (Columns - 1)]);
5950 /* force a redraw of the first char on the next line */
5951 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5952 screen_start(); /* don't know where cursor is now */
5953 }
5954 }
5955
5956 col = 0;
5957 off = (unsigned)(current_ScreenLine - ScreenLines);
5958#ifdef FEAT_RIGHTLEFT
5959 if (wp->w_p_rl)
5960 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005961 col = wp->w_width - 1; /* col is not used if breaking! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 off += col;
5963 }
5964#endif
5965
5966 /* reset the drawing state for the start of a wrapped line */
5967 draw_state = WL_START;
5968 saved_n_extra = n_extra;
5969 saved_p_extra = p_extra;
5970 saved_c_extra = c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005971 saved_c_final = c_final;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972 saved_char_attr = char_attr;
5973 n_extra = 0;
5974 lcs_prec_todo = lcs_prec;
5975#ifdef FEAT_LINEBREAK
5976# ifdef FEAT_DIFF
5977 if (filler_todo <= 0)
5978# endif
5979 need_showbreak = TRUE;
5980#endif
5981#ifdef FEAT_DIFF
5982 --filler_todo;
5983 /* When the filler lines are actually below the last line of the
5984 * file, don't draw the line itself, break here. */
5985 if (filler_todo == 0 && wp->w_botfill)
5986 break;
5987#endif
5988 }
5989
5990 } /* for every character in the line */
5991
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005992#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005993 /* After an empty line check first word for capital. */
5994 if (*skipwhite(line) == NUL)
5995 {
5996 capcol_lnum = lnum + 1;
5997 cap_col = 0;
5998 }
5999#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006000#ifdef FEAT_TEXT_PROP
6001 vim_free(text_props);
6002 vim_free(text_prop_idxs);
6003#endif
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006004
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01006005 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006 return row;
6007}
6008
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006009/*
6010 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006011 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006012 */
6013 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006014comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006015{
6016 int i;
6017
6018 for (i = 0; i < Screen_mco; ++i)
6019 {
6020 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
6021 return TRUE;
6022 if (ScreenLinesC[i][off_from] == 0)
6023 break;
6024 }
6025 return FALSE;
6026}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006027
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028/*
6029 * Check whether the given character needs redrawing:
6030 * - the (first byte of the) character is different
6031 * - the attributes are different
6032 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006033 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 */
6035 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006036char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037{
6038 if (cols > 0
6039 && ((ScreenLines[off_from] != ScreenLines[off_to]
6040 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 || (enc_dbcs != 0
6042 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
6043 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
6044 ? ScreenLines2[off_from] != ScreenLines2[off_to]
6045 : (cols > 1 && ScreenLines[off_from + 1]
6046 != ScreenLines[off_to + 1])))
6047 || (enc_utf8
6048 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6049 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006050 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006051 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6052 && ScreenLines[off_from + 1]
Bram Moolenaara12a1612019-01-24 16:39:02 +01006053 != ScreenLines[off_to + 1])))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054 return TRUE;
6055 return FALSE;
6056}
6057
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006058#if defined(FEAT_TERMINAL) || defined(PROTO)
6059/*
6060 * Return the index in ScreenLines[] for the current screen line.
6061 */
6062 int
6063screen_get_current_line_off()
6064{
6065 return (int)(current_ScreenLine - ScreenLines);
6066}
6067#endif
6068
Bram Moolenaarc662ec92019-06-23 00:15:57 +02006069#ifdef FEAT_TEXT_PROP
6070/*
6071 * Return TRUE if this position has a higher level popup or this cell is
6072 * transparent in the current popup.
6073 */
6074 static int
6075blocked_by_popup(int row, int col)
6076{
6077 int off;
6078
6079 if (!popup_visible)
6080 return FALSE;
6081 off = row * screen_Columns + col;
6082 return popup_mask[off] > screen_zindex || popup_transparent[off];
6083}
6084#endif
6085
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086/*
6087 * Move one "cooked" screen line to the screen, but only the characters that
6088 * have actually changed. Handle insert/delete character.
6089 * "coloff" gives the first column on the screen for this line.
6090 * "endcol" gives the columns where valid characters are.
6091 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6092 * needs to be cleared, negative otherwise.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006093 * "flags" can have bits:
6094 * SLF_POPUP popup window
6095 * SLF_RIGHTLEFT rightleft window:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6097 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6098 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006099 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006100screen_line(
6101 int row,
6102 int coloff,
6103 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006104 int clear_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006105 int flags UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106{
6107 unsigned off_from;
6108 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006109 unsigned max_off_from;
6110 unsigned max_off_to;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 int force = FALSE; /* force update rest of the line */
6114 int redraw_this /* bool: does character need redraw? */
6115#ifdef FEAT_GUI
6116 = TRUE /* For GUI when while-loop empty */
6117#endif
6118 ;
6119 int redraw_next; /* redraw_this for next character */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120 int clear_next = FALSE;
6121 int char_cells; /* 1: normal char */
6122 /* 2: occupies two display cells */
6123# define CHAR_CELLS char_cells
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006125 /* Check for illegal row and col, just in case. */
6126 if (row >= Rows)
6127 row = Rows - 1;
6128 if (endcol > Columns)
6129 endcol = Columns;
6130
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131# ifdef FEAT_CLIPBOARD
6132 clip_may_clear_selection(row, row);
6133# endif
6134
6135 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6136 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006137 max_off_from = off_from + screen_Columns;
6138 max_off_to = LineOffset[row] + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139
6140#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006141 if (flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 {
6143 /* Clear rest first, because it's left of the text. */
6144 if (clear_width > 0)
6145 {
6146 while (col <= endcol && ScreenLines[off_to] == ' '
6147 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006148 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 {
6150 ++off_to;
6151 ++col;
6152 }
6153 if (col <= endcol)
6154 screen_fill(row, row + 1, col + coloff,
6155 endcol + coloff + 1, ' ', ' ', 0);
6156 }
6157 col = endcol + 1;
6158 off_to = LineOffset[row] + col + coloff;
6159 off_from += col;
6160 endcol = (clear_width > 0 ? clear_width : -clear_width);
6161 }
6162#endif /* FEAT_RIGHTLEFT */
6163
6164 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6165
6166 while (col < endcol)
6167 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006169 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170 else
6171 char_cells = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172
6173 redraw_this = redraw_next;
6174 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6175 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6176
6177#ifdef FEAT_GUI
6178 /* If the next character was bold, then redraw the current character to
6179 * remove any pixels that might have spilt over into us. This only
6180 * happens in the GUI.
6181 */
6182 if (redraw_next && gui.in_use)
6183 {
6184 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006185 if (hl > HL_ALL)
6186 hl = syn_attr2attr(hl);
6187 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 redraw_this = TRUE;
6189 }
6190#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02006191#ifdef FEAT_TEXT_PROP
Bram Moolenaarc662ec92019-06-23 00:15:57 +02006192 if (blocked_by_popup(row, col + coloff))
Bram Moolenaar33796b32019-06-08 16:01:13 +02006193 redraw_this = FALSE;
6194#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 if (redraw_this)
6196 {
6197 /*
6198 * Special handling when 'xs' termcap flag set (hpterm):
6199 * Attributes for characters are stored at the position where the
6200 * cursor is when writing the highlighting code. The
6201 * start-highlighting code must be written with the cursor on the
6202 * first highlighted character. The stop-highlighting code must
6203 * be written with the cursor just after the last highlighted
6204 * character.
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01006205 * Overwriting a character doesn't remove its highlighting. Need
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 * to clear the rest of the line, and force redrawing it
6207 * completely.
6208 */
6209 if ( p_wiv
6210 && !force
6211#ifdef FEAT_GUI
6212 && !gui.in_use
6213#endif
6214 && ScreenAttrs[off_to] != 0
6215 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6216 {
6217 /*
6218 * Need to remove highlighting attributes here.
6219 */
6220 windgoto(row, col + coloff);
6221 out_str(T_CE); /* clear rest of this screen line */
6222 screen_start(); /* don't know where cursor is now */
6223 force = TRUE; /* force redraw of rest of the line */
6224 redraw_next = TRUE; /* or else next char would miss out */
6225
6226 /*
6227 * If the previous character was highlighted, need to stop
6228 * highlighting at this character.
6229 */
6230 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6231 {
6232 screen_attr = ScreenAttrs[off_to - 1];
6233 term_windgoto(row, col + coloff);
6234 screen_stop_highlight();
6235 }
6236 else
6237 screen_attr = 0; /* highlighting has stopped */
6238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 if (enc_dbcs != 0)
6240 {
6241 /* Check if overwriting a double-byte with a single-byte or
6242 * the other way around requires another character to be
6243 * redrawn. For UTF-8 this isn't needed, because comparing
6244 * ScreenLinesUC[] is sufficient. */
6245 if (char_cells == 1
6246 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006247 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 {
6249 /* Writing a single-cell character over a double-cell
6250 * character: need to redraw the next cell. */
6251 ScreenLines[off_to + 1] = 0;
6252 redraw_next = TRUE;
6253 }
6254 else if (char_cells == 2
6255 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006256 && (*mb_off2cells)(off_to, max_off_to) == 1
6257 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258 {
6259 /* Writing the second half of a double-cell character over
6260 * a double-cell character: need to redraw the second
6261 * cell. */
6262 ScreenLines[off_to + 2] = 0;
6263 redraw_next = TRUE;
6264 }
6265
6266 if (enc_dbcs == DBCS_JPNU)
6267 ScreenLines2[off_to] = ScreenLines2[off_from];
6268 }
6269 /* When writing a single-width character over a double-width
6270 * character and at the end of the redrawn text, need to clear out
6271 * the right halve of the old character.
6272 * Also required when writing the right halve of a double-width
6273 * char over the left halve of an existing one. */
6274 if (has_mbyte && col + char_cells == endcol
6275 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006276 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006278 && (*mb_off2cells)(off_to, max_off_to) == 1
6279 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 clear_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281
6282 ScreenLines[off_to] = ScreenLines[off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283 if (enc_utf8)
6284 {
6285 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6286 if (ScreenLinesUC[off_from] != 0)
6287 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006288 int i;
6289
6290 for (i = 0; i < Screen_mco; ++i)
6291 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292 }
6293 }
6294 if (char_cells == 2)
6295 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296
6297#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006298 /* The bold trick makes a single column of pixels appear in the
6299 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300 * character should be redrawn too. This happens for our own GUI
6301 * and for some xterms. */
6302 if (
6303# ifdef FEAT_GUI
6304 gui.in_use
6305# endif
6306# if defined(FEAT_GUI) && defined(UNIX)
6307 ||
6308# endif
6309# ifdef UNIX
6310 term_is_xterm
6311# endif
6312 )
6313 {
6314 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006315 if (hl > HL_ALL)
6316 hl = syn_attr2attr(hl);
6317 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 redraw_next = TRUE;
6319 }
6320#endif
6321 ScreenAttrs[off_to] = ScreenAttrs[off_from];
Bram Moolenaara12a1612019-01-24 16:39:02 +01006322
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006323 /* For simplicity set the attributes of second half of a
6324 * double-wide character equal to the first half. */
6325 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006327
6328 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331 screen_char(off_to, row, col + coloff);
6332 }
6333 else if ( p_wiv
6334#ifdef FEAT_GUI
6335 && !gui.in_use
6336#endif
6337 && col + coloff > 0)
6338 {
6339 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6340 {
6341 /*
6342 * Don't output stop-highlight when moving the cursor, it will
6343 * stop the highlighting when it should continue.
6344 */
6345 screen_attr = 0;
6346 }
6347 else if (screen_attr != 0)
6348 screen_stop_highlight();
6349 }
6350
6351 off_to += CHAR_CELLS;
6352 off_from += CHAR_CELLS;
6353 col += CHAR_CELLS;
6354 }
6355
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 if (clear_next)
6357 {
6358 /* Clear the second half of a double-wide character of which the left
6359 * half was overwritten with a single-wide character. */
6360 ScreenLines[off_to] = ' ';
6361 if (enc_utf8)
6362 ScreenLinesUC[off_to] = 0;
6363 screen_char(off_to, row, col + coloff);
6364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365
6366 if (clear_width > 0
6367#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006368 && !(flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369#endif
6370 )
6371 {
6372#ifdef FEAT_GUI
6373 int startCol = col;
6374#endif
6375
6376 /* blank out the rest of the line */
6377 while (col < clear_width && ScreenLines[off_to] == ' '
6378 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006379 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380 {
6381 ++off_to;
6382 ++col;
6383 }
6384 if (col < clear_width)
6385 {
6386#ifdef FEAT_GUI
6387 /*
6388 * In the GUI, clearing the rest of the line may leave pixels
6389 * behind if the first character cleared was bold. Some bold
6390 * fonts spill over the left. In this case we redraw the previous
6391 * character too. If we didn't skip any blanks above, then we
6392 * only redraw if the character wasn't already redrawn anyway.
6393 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006394 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395 {
6396 hl = ScreenAttrs[off_to];
6397 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006398 {
6399 int prev_cells = 1;
Bram Moolenaara12a1612019-01-24 16:39:02 +01006400
Bram Moolenaar9c697322006-10-09 20:11:17 +00006401 if (enc_utf8)
6402 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6403 * that its width is 2. */
6404 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6405 else if (enc_dbcs != 0)
6406 {
6407 /* find previous character by counting from first
6408 * column and get its width. */
6409 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006410 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006411
6412 while (off < off_to)
6413 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006414 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006415 off += prev_cells;
6416 }
6417 }
6418
6419 if (enc_dbcs != 0 && prev_cells > 1)
6420 screen_char_2(off_to - prev_cells, row,
6421 col + coloff - prev_cells);
6422 else
Bram Moolenaar9c697322006-10-09 20:11:17 +00006423 screen_char(off_to - prev_cells, row,
6424 col + coloff - prev_cells);
6425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 }
6427#endif
6428 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6429 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 off_to += clear_width - col;
6431 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432 }
6433 }
6434
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006435 if (clear_width > 0
6436#ifdef FEAT_TEXT_PROP
6437 && !(flags & SLF_POPUP) // no separator for popup window
6438#endif
6439 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006441 // For a window that has a right neighbor, draw the separator char
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006442 // right of the window contents. But not on top of a popup window.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006443 if (coloff + col < Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006445#ifdef FEAT_TEXT_PROP
Bram Moolenaarc662ec92019-06-23 00:15:57 +02006446 if (!blocked_by_popup(row, col + coloff))
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006447#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006449 int c;
6450
6451 c = fillchar_vsep(&hl);
6452 if (ScreenLines[off_to] != (schar_T)c
6453 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6454 != (c >= 0x80 ? c : 0))
6455 || ScreenAttrs[off_to] != hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006457 ScreenLines[off_to] = c;
6458 ScreenAttrs[off_to] = hl;
6459 if (enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006461 if (c >= 0x80)
6462 {
6463 ScreenLinesUC[off_to] = c;
6464 ScreenLinesC[0][off_to] = 0;
6465 }
6466 else
6467 ScreenLinesUC[off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 }
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006469 screen_char(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471 }
6472 }
6473 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 LineWraps[row] = FALSE;
6475 }
6476}
6477
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006478#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006480 * Mirror text "str" for right-left displaying.
6481 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006483 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006484rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485{
6486 char_u *p1, *p2;
6487 int t;
6488
6489 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6490 {
6491 t = *p1;
6492 *p1 = *p2;
6493 *p2 = t;
6494 }
6495}
6496#endif
6497
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498/*
6499 * mark all status lines for redraw; used after first :cd
6500 */
6501 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006502status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503{
6504 win_T *wp;
6505
Bram Moolenaar29323592016-07-24 22:04:11 +02006506 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507 if (wp->w_status_height)
6508 {
6509 wp->w_redr_status = TRUE;
6510 redraw_later(VALID);
6511 }
6512}
6513
6514/*
6515 * mark all status lines of the current buffer for redraw
6516 */
6517 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006518status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519{
6520 win_T *wp;
6521
Bram Moolenaar29323592016-07-24 22:04:11 +02006522 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6524 {
6525 wp->w_redr_status = TRUE;
6526 redraw_later(VALID);
6527 }
6528}
6529
6530/*
6531 * Redraw all status lines that need to be redrawn.
6532 */
6533 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006534redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535{
6536 win_T *wp;
6537
Bram Moolenaar29323592016-07-24 22:04:11 +02006538 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006540 win_redr_status(wp, FALSE);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006541 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006542 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544
Bram Moolenaar4033c552017-09-16 20:54:51 +02006545#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546/*
6547 * Redraw all status lines at the bottom of frame "frp".
6548 */
6549 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006550win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551{
6552 if (frp->fr_layout == FR_LEAF)
6553 frp->fr_win->w_redr_status = TRUE;
6554 else if (frp->fr_layout == FR_ROW)
6555 {
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006556 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557 win_redraw_last_status(frp);
6558 }
6559 else /* frp->fr_layout == FR_COL */
6560 {
6561 frp = frp->fr_child;
6562 while (frp->fr_next != NULL)
6563 frp = frp->fr_next;
6564 win_redraw_last_status(frp);
6565 }
6566}
6567#endif
6568
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569/*
6570 * Draw the verticap separator right of window "wp" starting with line "row".
6571 */
6572 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006573draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574{
6575 int hl;
6576 int c;
6577
6578 if (wp->w_vsep_width)
6579 {
6580 /* draw the vertical separator right of this window */
6581 c = fillchar_vsep(&hl);
6582 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6583 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6584 c, ' ', hl);
6585 }
6586}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587
6588#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006589static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590
6591/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006592 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593 */
6594 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006595status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596{
6597 int len = 0;
6598
6599#ifdef FEAT_MENU
6600 int emenu = (xp->xp_context == EXPAND_MENUS
6601 || xp->xp_context == EXPAND_MENUNAMES);
6602
6603 /* Check for menu separators - replace with '|'. */
6604 if (emenu && menu_is_separator(s))
6605 return 1;
6606#endif
6607
6608 while (*s != NUL)
6609 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006610 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006611 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006612 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613 }
6614
6615 return len;
6616}
6617
6618/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006619 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006620 * These are backslashes used for escaping. Do show backslashes in help tags.
6621 */
6622 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006623skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006624{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006625 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006626#ifdef FEAT_MENU
6627 || ((xp->xp_context == EXPAND_MENUS
6628 || xp->xp_context == EXPAND_MENUNAMES)
6629 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6630#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006631 )
6632 {
6633#ifndef BACKSLASH_IN_FILENAME
6634 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6635 return 2;
6636#endif
6637 return 1;
6638 }
6639 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006640}
6641
6642/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643 * Show wildchar matches in the status line.
6644 * Show at least the "match" item.
6645 * We start at item 'first_match' in the list and show all matches that fit.
6646 *
6647 * If inversion is possible we use it. Else '=' characters are used.
6648 */
6649 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006650win_redr_status_matches(
6651 expand_T *xp,
6652 int num_matches,
6653 char_u **matches, /* list of matches */
6654 int match,
6655 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656{
6657#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6658 int row;
6659 char_u *buf;
6660 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006661 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662 int fillchar;
6663 int attr;
6664 int i;
6665 int highlight = TRUE;
6666 char_u *selstart = NULL;
6667 int selstart_col = 0;
6668 char_u *selend = NULL;
6669 static int first_match = 0;
6670 int add_left = FALSE;
6671 char_u *s;
6672#ifdef FEAT_MENU
6673 int emenu;
6674#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676
6677 if (matches == NULL) /* interrupted completion? */
6678 return;
6679
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006680 if (has_mbyte)
Bram Moolenaar964b3742019-05-24 18:54:09 +02006681 buf = alloc(Columns * MB_MAXBYTES + 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006682 else
Bram Moolenaar964b3742019-05-24 18:54:09 +02006683 buf = alloc(Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684 if (buf == NULL)
6685 return;
6686
6687 if (match == -1) /* don't show match but original text */
6688 {
6689 match = 0;
6690 highlight = FALSE;
6691 }
6692 /* count 1 for the ending ">" */
6693 clen = status_match_len(xp, L_MATCH(match)) + 3;
6694 if (match == 0)
6695 first_match = 0;
6696 else if (match < first_match)
6697 {
6698 /* jumping left, as far as we can go */
6699 first_match = match;
6700 add_left = TRUE;
6701 }
6702 else
6703 {
6704 /* check if match fits on the screen */
6705 for (i = first_match; i < match; ++i)
6706 clen += status_match_len(xp, L_MATCH(i)) + 2;
6707 if (first_match > 0)
6708 clen += 2;
6709 /* jumping right, put match at the left */
6710 if ((long)clen > Columns)
6711 {
6712 first_match = match;
6713 /* if showing the last match, we can add some on the left */
6714 clen = 2;
6715 for (i = match; i < num_matches; ++i)
6716 {
6717 clen += status_match_len(xp, L_MATCH(i)) + 2;
6718 if ((long)clen >= Columns)
6719 break;
6720 }
6721 if (i == num_matches)
6722 add_left = TRUE;
6723 }
6724 }
6725 if (add_left)
6726 while (first_match > 0)
6727 {
6728 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6729 if ((long)clen >= Columns)
6730 break;
6731 --first_match;
6732 }
6733
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006734 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735
6736 if (first_match == 0)
6737 {
6738 *buf = NUL;
6739 len = 0;
6740 }
6741 else
6742 {
6743 STRCPY(buf, "< ");
6744 len = 2;
6745 }
6746 clen = len;
6747
6748 i = first_match;
6749 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6750 {
6751 if (i == match)
6752 {
6753 selstart = buf + len;
6754 selstart_col = clen;
6755 }
6756
6757 s = L_MATCH(i);
6758 /* Check for menu separators - replace with '|' */
6759#ifdef FEAT_MENU
6760 emenu = (xp->xp_context == EXPAND_MENUS
6761 || xp->xp_context == EXPAND_MENUNAMES);
6762 if (emenu && menu_is_separator(s))
6763 {
6764 STRCPY(buf + len, transchar('|'));
6765 l = (int)STRLEN(buf + len);
6766 len += l;
6767 clen += l;
6768 }
6769 else
6770#endif
6771 for ( ; *s != NUL; ++s)
6772 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006773 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774 clen += ptr2cells(s);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006775 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776 {
6777 STRNCPY(buf + len, s, l);
6778 s += l - 1;
6779 len += l;
6780 }
6781 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782 {
6783 STRCPY(buf + len, transchar_byte(*s));
6784 len += (int)STRLEN(buf + len);
6785 }
6786 }
6787 if (i == match)
6788 selend = buf + len;
6789
6790 *(buf + len++) = ' ';
6791 *(buf + len++) = ' ';
6792 clen += 2;
6793 if (++i == num_matches)
6794 break;
6795 }
6796
6797 if (i != num_matches)
6798 {
6799 *(buf + len++) = '>';
6800 ++clen;
6801 }
6802
6803 buf[len] = NUL;
6804
6805 row = cmdline_row - 1;
6806 if (row >= 0)
6807 {
6808 if (wild_menu_showing == 0)
6809 {
6810 if (msg_scrolled > 0)
6811 {
6812 /* Put the wildmenu just above the command line. If there is
6813 * no room, scroll the screen one line up. */
6814 if (cmdline_row == Rows - 1)
6815 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02006816 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817 ++msg_scrolled;
6818 }
6819 else
6820 {
6821 ++cmdline_row;
6822 ++row;
6823 }
6824 wild_menu_showing = WM_SCROLLED;
6825 }
6826 else
6827 {
6828 /* Create status line if needed by setting 'laststatus' to 2.
6829 * Set 'winminheight' to zero to avoid that the window is
6830 * resized. */
6831 if (lastwin->w_status_height == 0)
6832 {
6833 save_p_ls = p_ls;
6834 save_p_wmh = p_wmh;
6835 p_ls = 2;
6836 p_wmh = 0;
6837 last_status(FALSE);
6838 }
6839 wild_menu_showing = WM_SHOWN;
6840 }
6841 }
6842
6843 screen_puts(buf, row, 0, attr);
6844 if (selstart != NULL && highlight)
6845 {
6846 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006847 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848 }
6849
6850 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6851 }
6852
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854 vim_free(buf);
6855}
6856#endif
6857
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858/*
6859 * Redraw the status line of window wp.
6860 *
6861 * If inversion is possible we use it. Else '=' characters are used.
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006862 * If "ignore_pum" is TRUE, also redraw statusline when the popup menu is
6863 * displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864 */
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006865 static void
Bram Moolenaar829adb72018-06-24 19:24:03 +02006866win_redr_status(win_T *wp, int ignore_pum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867{
6868 int row;
6869 char_u *p;
6870 int len;
6871 int fillchar;
6872 int attr;
6873 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006874 static int busy = FALSE;
6875
6876 /* It's possible to get here recursively when 'statusline' (indirectly)
6877 * invokes ":redrawstatus". Simply ignore the call then. */
6878 if (busy)
6879 return;
6880 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881
6882 wp->w_redr_status = FALSE;
6883 if (wp->w_status_height == 0)
6884 {
6885 /* no status line, can only be last window */
6886 redraw_cmdline = TRUE;
6887 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006888 else if (!redrawing()
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006889 // don't update status line when popup menu is visible and may be
6890 // drawn over it, unless it will be redrawn later
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006891 || (!ignore_pum && pum_visible()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892 {
6893 /* Don't redraw right now, do it later. */
6894 wp->w_redr_status = TRUE;
6895 }
6896#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006897 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898 {
6899 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006900 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 }
6902#endif
6903 else
6904 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006905 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006907 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 p = NameBuff;
6909 len = (int)STRLEN(p);
6910
Bram Moolenaard85f2712017-07-28 21:51:57 +02006911 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912#ifdef FEAT_QUICKFIX
6913 || wp->w_p_pvw
6914#endif
6915 || bufIsChanged(wp->w_buffer)
6916 || wp->w_buffer->b_p_ro)
6917 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02006918 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006920 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921 len += (int)STRLEN(p + len);
6922 }
6923#ifdef FEAT_QUICKFIX
6924 if (wp->w_p_pvw)
6925 {
6926 STRCPY(p + len, _("[Preview]"));
6927 len += (int)STRLEN(p + len);
6928 }
6929#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02006930 if (bufIsChanged(wp->w_buffer)
6931#ifdef FEAT_TERMINAL
6932 && !bt_terminal(wp->w_buffer)
6933#endif
6934 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935 {
6936 STRCPY(p + len, "[+]");
6937 len += 3;
6938 }
6939 if (wp->w_buffer->b_p_ro)
6940 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006941 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01006942 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943 }
6944
Bram Moolenaar02631462017-09-22 15:20:32 +02006945 this_ru_col = ru_col - (Columns - wp->w_width);
6946 if (this_ru_col < (wp->w_width + 1) / 2)
6947 this_ru_col = (wp->w_width + 1) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 if (this_ru_col <= 1)
6949 {
6950 p = (char_u *)"<"; /* No room for file name! */
6951 len = 1;
6952 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01006953 else if (has_mbyte)
6954 {
6955 int clen = 0, i;
6956
6957 /* Count total number of display cells. */
6958 clen = mb_string2cells(p, -1);
6959
6960 /* Find first character that will fit.
6961 * Going from start to end is much faster for DBCS. */
6962 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
6963 i += (*mb_ptr2len)(p + i))
6964 clen -= (*mb_ptr2cells)(p + i);
6965 len = clen;
6966 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967 {
Bram Moolenaara12a1612019-01-24 16:39:02 +01006968 p = p + i - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969 *p = '<';
Bram Moolenaara12a1612019-01-24 16:39:02 +01006970 ++len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 }
6972
Bram Moolenaara12a1612019-01-24 16:39:02 +01006973 }
6974 else if (len > this_ru_col - 1)
6975 {
6976 p += len - (this_ru_col - 1);
6977 *p = '<';
6978 len = this_ru_col - 1;
6979 }
6980
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar53f81742017-09-22 14:35:51 +02006982 screen_puts(p, row, wp->w_wincol, attr);
6983 screen_fill(row, row + 1, len + wp->w_wincol,
6984 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02006986 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6988 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
Bram Moolenaar53f81742017-09-22 14:35:51 +02006989 - 1 + wp->w_wincol), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990
6991#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +02006992 win_redr_ruler(wp, TRUE, ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993#endif
6994 }
6995
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 /*
6997 * May need to draw the character below the vertical separator.
6998 */
6999 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
7000 {
7001 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007002 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 else
7004 fillchar = fillchar_vsep(&attr);
7005 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
7006 attr);
7007 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007008 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009}
7010
Bram Moolenaar238a5642006-02-21 22:12:05 +00007011#ifdef FEAT_STL_OPT
7012/*
7013 * Redraw the status line according to 'statusline' and take care of any
7014 * errors encountered.
7015 */
7016 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007017redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00007018{
Bram Moolenaar362f3562009-11-03 16:20:34 +00007019 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02007020 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007021
7022 /* When called recursively return. This can happen when the statusline
7023 * contains an expression that triggers a redraw. */
7024 if (entered)
7025 return;
7026 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007027
Bram Moolenaara742e082016-04-05 21:10:38 +02007028 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007029 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02007030 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007031 {
7032 /* When there is an error disable the statusline, otherwise the
7033 * display is messed up with errors and a redraw triggers the problem
7034 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007035 set_string_option_direct((char_u *)"statusline", -1,
7036 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007037 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007038 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007039 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007040 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007041}
7042#endif
7043
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044/*
7045 * Return TRUE if the status line of window "wp" is connected to the status
7046 * line of the window right of it. If not, then it's a vertical separator.
7047 * Only call if (wp->w_vsep_width != 0).
7048 */
7049 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007050stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051{
7052 frame_T *fr;
7053
7054 fr = wp->w_frame;
7055 while (fr->fr_parent != NULL)
7056 {
7057 if (fr->fr_parent->fr_layout == FR_COL)
7058 {
7059 if (fr->fr_next != NULL)
7060 break;
7061 }
7062 else
7063 {
7064 if (fr->fr_next != NULL)
7065 return TRUE;
7066 }
7067 fr = fr->fr_parent;
7068 }
7069 return FALSE;
7070}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073/*
7074 * Get the value to show for the language mappings, active 'keymap'.
7075 */
7076 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007077get_keymap_str(
7078 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007079 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007080 char_u *buf, /* buffer for the result */
7081 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082{
7083 char_u *p;
7084
7085 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7086 return FALSE;
7087
7088 {
7089#ifdef FEAT_EVAL
7090 buf_T *old_curbuf = curbuf;
7091 win_T *old_curwin = curwin;
7092 char_u *s;
7093
7094 curbuf = wp->w_buffer;
7095 curwin = wp;
7096 STRCPY(buf, "b:keymap_name"); /* must be writable */
7097 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007098 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 --emsg_skip;
7100 curbuf = old_curbuf;
7101 curwin = old_curwin;
7102 if (p == NULL || *p == NUL)
7103#endif
7104 {
7105#ifdef FEAT_KEYMAP
7106 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7107 p = wp->w_buffer->b_p_keymap;
7108 else
7109#endif
7110 p = (char_u *)"lang";
7111 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007112 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113 buf[0] = NUL;
7114#ifdef FEAT_EVAL
7115 vim_free(s);
7116#endif
7117 }
7118 return buf[0] != NUL;
7119}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120
7121#if defined(FEAT_STL_OPT) || defined(PROTO)
7122/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007123 * Redraw the status line or ruler of window "wp".
7124 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125 */
7126 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007127win_redr_custom(
7128 win_T *wp,
7129 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007131 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132 int attr;
7133 int curattr;
7134 int row;
7135 int col = 0;
7136 int maxwidth;
7137 int width;
7138 int n;
7139 int len;
7140 int fillchar;
7141 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007142 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007144 struct stl_hlrec hltab[STL_MAX_ITEM];
7145 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007146 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007147 win_T *ewp;
7148 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149
Bram Moolenaar1d633412013-12-11 15:52:01 +01007150 /* There is a tiny chance that this gets called recursively: When
7151 * redrawing a status line triggers redrawing the ruler or tabline.
7152 * Avoid trouble by not allowing recursion. */
7153 if (entered)
7154 return;
7155 entered = TRUE;
7156
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007158 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007160 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007161 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007162 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007163 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007164 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007165 maxwidth = Columns;
7166# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007167 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007168# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007170 else
7171 {
7172 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007173 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02007174 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007175
7176 if (draw_ruler)
7177 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007178 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007179 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007180 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007181 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007182 if (*++stl == '-')
7183 stl++;
7184 if (atoi((char *)stl))
7185 while (VIM_ISDIGIT(*stl))
7186 stl++;
7187 if (*stl++ != '(')
7188 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007189 }
Bram Moolenaar02631462017-09-22 15:20:32 +02007190 col = ru_col - (Columns - wp->w_width);
7191 if (col < (wp->w_width + 1) / 2)
7192 col = (wp->w_width + 1) / 2;
7193 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007194 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007195 {
7196 row = Rows - 1;
7197 --maxwidth; /* writing in last column may cause scrolling */
7198 fillchar = ' ';
7199 attr = 0;
7200 }
7201
7202# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007203 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007204# endif
7205 }
7206 else
7207 {
7208 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007209 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007210 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007211 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007212# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007213 use_sandbox = was_set_insecurely((char_u *)"statusline",
7214 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007215# endif
7216 }
7217
Bram Moolenaar53f81742017-09-22 14:35:51 +02007218 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007219 }
7220
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007222 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223
Bram Moolenaar61452852011-02-01 18:01:11 +01007224 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7225 * the cursor away and back. */
7226 ewp = wp == NULL ? curwin : wp;
7227 p_crb_save = ewp->w_p_crb;
7228 ewp->w_p_crb = FALSE;
7229
Bram Moolenaar362f3562009-11-03 16:20:34 +00007230 /* Make a copy, because the statusline may include a function call that
7231 * might change the option value and free the memory. */
7232 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007233 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007234 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007235 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007236 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007237 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007239 /* Make all characters printable. */
7240 p = transstr(buf);
7241 if (p != NULL)
7242 {
7243 vim_strncpy(buf, p, sizeof(buf) - 1);
7244 vim_free(p);
7245 }
7246
7247 /* fill up with "fillchar" */
7248 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007249 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251 len += (*mb_char2bytes)(fillchar, buf + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252 ++width;
7253 }
7254 buf[len] = NUL;
7255
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007256 /*
7257 * Draw each snippet with the specified highlighting.
7258 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259 curattr = attr;
7260 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007261 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007263 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264 screen_puts_len(p, len, row, col, curattr);
7265 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007266 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007268 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007270 else if (hltab[n].userhl < 0)
7271 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007272#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007273 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7274 && wp->w_status_height != 0)
7275 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007276 else if (wp != NULL && bt_terminal(wp->w_buffer)
7277 && wp->w_status_height != 0)
7278 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02007279#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007280 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007281 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007283 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284 }
7285 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007286
7287 if (wp == NULL)
7288 {
7289 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7290 col = 0;
7291 len = 0;
7292 p = buf;
7293 fillchar = 0;
7294 for (n = 0; tabtab[n].start != NULL; n++)
7295 {
7296 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7297 while (col < len)
7298 TabPageIdxs[col++] = fillchar;
7299 p = tabtab[n].start;
7300 fillchar = tabtab[n].userhl;
7301 }
7302 while (col < Columns)
7303 TabPageIdxs[col++] = fillchar;
7304 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007305
7306theend:
7307 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308}
7309
7310#endif /* FEAT_STL_OPT */
7311
7312/*
7313 * Output a single character directly to the screen and update ScreenLines.
7314 */
7315 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007316screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318 char_u buf[MB_MAXBYTES + 1];
7319
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007320 if (has_mbyte)
7321 buf[(*mb_char2bytes)(c, buf)] = NUL;
7322 else
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007323 {
7324 buf[0] = c;
7325 buf[1] = NUL;
7326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327 screen_puts(buf, row, col, attr);
7328}
7329
7330/*
7331 * Get a single character directly from ScreenLines into "bytes[]".
7332 * Also return its attribute in *attrp;
7333 */
7334 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007335screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336{
7337 unsigned off;
7338
7339 /* safety check */
7340 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7341 {
7342 off = LineOffset[row] + col;
7343 *attrp = ScreenAttrs[off];
7344 bytes[0] = ScreenLines[off];
7345 bytes[1] = NUL;
7346
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347 if (enc_utf8 && ScreenLinesUC[off] != 0)
7348 bytes[utfc_char2bytes(off, bytes)] = NUL;
7349 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7350 {
7351 bytes[0] = ScreenLines[off];
7352 bytes[1] = ScreenLines2[off];
7353 bytes[2] = NUL;
7354 }
7355 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7356 {
7357 bytes[1] = ScreenLines[off + 1];
7358 bytes[2] = NUL;
7359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360 }
7361}
7362
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007363/*
7364 * Return TRUE if composing characters for screen posn "off" differs from
7365 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007366 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007367 */
7368 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007369screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007370{
7371 int i;
7372
7373 for (i = 0; i < Screen_mco; ++i)
7374 {
7375 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7376 return TRUE;
7377 if (u8cc[i] == 0)
7378 break;
7379 }
7380 return FALSE;
7381}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007382
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383/*
7384 * Put string '*text' on the screen at position 'row' and 'col', with
7385 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7386 * Note: only outputs within one row, message is truncated at screen boundary!
7387 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7388 */
7389 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007390screen_puts(
7391 char_u *text,
7392 int row,
7393 int col,
7394 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395{
7396 screen_puts_len(text, -1, row, col, attr);
7397}
7398
7399/*
7400 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7401 * a NUL.
7402 */
7403 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007404screen_puts_len(
7405 char_u *text,
7406 int textlen,
7407 int row,
7408 int col,
7409 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410{
7411 unsigned off;
7412 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007413 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414 int c;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007415 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416 int mbyte_blen = 1;
7417 int mbyte_cells = 1;
7418 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007419 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 int clear_next_cell = FALSE;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007421#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007423 int pc, nc, nc1;
7424 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007426 int force_redraw_this;
7427 int force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007428 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429
Bram Moolenaar0b4c9ed2019-06-03 22:04:23 +02007430 // Safety check. The check for negative row and column is to fix issue
7431 // #4102. TODO: find out why row/col could be negative.
7432 if (ScreenLines == NULL
7433 || row >= screen_Rows || row < 0
7434 || col >= screen_Columns || col < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007436 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437
Bram Moolenaarc236c162008-07-13 17:41:49 +00007438 /* When drawing over the right halve of a double-wide char clear out the
7439 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007440 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaara12a1612019-01-24 16:39:02 +01007441#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00007442 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01007443#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007444 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007445 {
7446 ScreenLines[off - 1] = ' ';
7447 ScreenAttrs[off - 1] = 0;
7448 if (enc_utf8)
7449 {
7450 ScreenLinesUC[off - 1] = 0;
7451 ScreenLinesC[0][off - 1] = 0;
7452 }
7453 /* redraw the previous cell, make it empty */
7454 screen_char(off - 1, row, col - 1);
7455 /* force the cell at "col" to be redrawn */
7456 force_redraw_next = TRUE;
7457 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007458
Bram Moolenaar367329b2007-08-30 11:53:22 +00007459 max_off = LineOffset[row] + screen_Columns;
Bram Moolenaara064ac82007-08-05 18:10:54 +00007460 while (col < screen_Columns
7461 && (len < 0 || (int)(ptr - text) < len)
7462 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 {
7464 c = *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465 /* check if this is the first byte of a multibyte */
7466 if (has_mbyte)
7467 {
7468 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007469 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007471 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7473 mbyte_cells = 1;
7474 else if (enc_dbcs != 0)
7475 mbyte_cells = mbyte_blen;
7476 else /* enc_utf8 */
7477 {
7478 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007479 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480 (int)((text + len) - ptr));
7481 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007482 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007484#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7486 {
7487 /* Do Arabic shaping. */
7488 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7489 {
7490 /* Past end of string to be displayed. */
7491 nc = NUL;
7492 nc1 = NUL;
7493 }
7494 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007495 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007496 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7497 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007498 nc1 = pcc[0];
7499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500 pc = prev_c;
7501 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007502 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503 }
7504 else
7505 prev_c = u8c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007506#endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007507 if (col + mbyte_cells > screen_Columns)
7508 {
7509 /* Only 1 cell left, but character requires 2 cells:
7510 * display a '>' in the last column to avoid wrapping. */
7511 c = '>';
7512 mbyte_cells = 1;
7513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514 }
7515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007517 force_redraw_this = force_redraw_next;
7518 force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007519
7520 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521 || (mbyte_cells == 2
7522 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7523 || (enc_dbcs == DBCS_JPNU
7524 && c == 0x8e
7525 && ScreenLines2[off] != ptr[1])
7526 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007527 && (ScreenLinesUC[off] !=
7528 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7529 || (ScreenLinesUC[off] != 0
7530 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007532 || exmode_active;
7533
Bram Moolenaar24a5ac52019-06-08 19:01:18 +02007534 if ((need_redraw || force_redraw_this)
7535#ifdef FEAT_TEXT_PROP
Bram Moolenaarc662ec92019-06-23 00:15:57 +02007536 && !blocked_by_popup(row, col)
Bram Moolenaar24a5ac52019-06-08 19:01:18 +02007537#endif
7538 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539 {
7540#if defined(FEAT_GUI) || defined(UNIX)
7541 /* The bold trick makes a single row of pixels appear in the next
7542 * character. When a bold character is removed, the next
7543 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007544 * and for some xterms. */
7545 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546# ifdef FEAT_GUI
7547 gui.in_use
7548# endif
7549# if defined(FEAT_GUI) && defined(UNIX)
7550 ||
7551# endif
7552# ifdef UNIX
7553 term_is_xterm
7554# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007555 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007557 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007559 if (n > HL_ALL)
7560 n = syn_attr2attr(n);
7561 if (n & HL_BOLD)
7562 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563 }
7564#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565 /* When at the end of the text and overwriting a two-cell
7566 * character with a one-cell character, need to clear the next
7567 * cell. Also when overwriting the left halve of a two-cell char
7568 * with the right halve of a two-cell char. Do this only once
7569 * (mb_off2cells() may return 2 on the right halve). */
7570 if (clear_next_cell)
7571 clear_next_cell = FALSE;
7572 else if (has_mbyte
7573 && (len < 0 ? ptr[mbyte_blen] == NUL
7574 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007575 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007577 && (*mb_off2cells)(off, max_off) == 1
7578 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579 clear_next_cell = TRUE;
7580
7581 /* Make sure we never leave a second byte of a double-byte behind,
7582 * it confuses mb_off2cells(). */
7583 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007584 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007586 && (*mb_off2cells)(off, max_off) == 1
7587 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588 ScreenLines[off + mbyte_blen] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589 ScreenLines[off] = c;
7590 ScreenAttrs[off] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 if (enc_utf8)
7592 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007593 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007594 ScreenLinesUC[off] = 0;
7595 else
7596 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007597 int i;
7598
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007600 for (i = 0; i < Screen_mco; ++i)
7601 {
7602 ScreenLinesC[i][off] = u8cc[i];
7603 if (u8cc[i] == 0)
7604 break;
7605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606 }
7607 if (mbyte_cells == 2)
7608 {
7609 ScreenLines[off + 1] = 0;
7610 ScreenAttrs[off + 1] = attr;
7611 }
7612 screen_char(off, row, col);
7613 }
7614 else if (mbyte_cells == 2)
7615 {
7616 ScreenLines[off + 1] = ptr[1];
7617 ScreenAttrs[off + 1] = attr;
7618 screen_char_2(off, row, col);
7619 }
7620 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7621 {
7622 ScreenLines2[off] = ptr[1];
7623 screen_char(off, row, col);
7624 }
7625 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626 screen_char(off, row, col);
7627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628 if (has_mbyte)
7629 {
7630 off += mbyte_cells;
7631 col += mbyte_cells;
7632 ptr += mbyte_blen;
7633 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007634 {
7635 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007637 len = -1;
7638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639 }
7640 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641 {
7642 ++off;
7643 ++col;
7644 ++ptr;
7645 }
7646 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007647
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007648 /* If we detected the next character needs to be redrawn, but the text
7649 * doesn't extend up to there, update the character here. */
7650 if (force_redraw_next && col < screen_Columns)
7651 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007652 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7653 screen_char_2(off, row, col);
7654 else
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007655 screen_char(off, row, col);
7656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657}
7658
7659#ifdef FEAT_SEARCH_EXTRA
7660/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007661 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662 */
7663 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007664start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665{
7666 if (p_hls && !no_hlsearch)
7667 {
7668 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007669 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007670# ifdef FEAT_RELTIME
7671 /* Set the time limit to 'redrawtime'. */
7672 profile_setlimit(p_rdt, &search_hl.tm);
7673# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674 }
7675}
7676
7677/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007678 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679 */
7680 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007681end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682{
7683 if (search_hl.rm.regprog != NULL)
7684 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007685 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686 search_hl.rm.regprog = NULL;
7687 }
7688}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02007689#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02007690
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007692screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693{
7694 attrentry_T *aep = NULL;
7695
7696 screen_attr = attr;
7697 if (full_screen
Bram Moolenaar4f974752019-02-17 17:44:42 +01007698#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699 && termcap_active
7700#endif
7701 )
7702 {
7703#ifdef FEAT_GUI
7704 if (gui.in_use)
7705 {
7706 char buf[20];
7707
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007708 /* The GUI handles this internally. */
7709 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710 OUT_STR(buf);
7711 }
7712 else
7713#endif
7714 {
7715 if (attr > HL_ALL) /* special HL attr. */
7716 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007717 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718 aep = syn_cterm_attr2entry(attr);
7719 else
7720 aep = syn_term_attr2entry(attr);
7721 if (aep == NULL) /* did ":syntax clear" */
7722 attr = 0;
7723 else
7724 attr = aep->ae_attr;
7725 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01007726 if ((attr & HL_BOLD) && *T_MD != NUL) /* bold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007728 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007729#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007730 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
7731 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
7732 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007733#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007734 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007735 /* If the Normal FG color has BOLD attribute and the new HL
7736 * has a FG color defined, clear BOLD. */
7737 out_str(T_ME);
Bram Moolenaar45a00002017-12-22 21:12:34 +01007738 if ((attr & HL_STANDOUT) && *T_SO != NUL) /* standout */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739 out_str(T_SO);
Bram Moolenaar45a00002017-12-22 21:12:34 +01007740 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) /* undercurl */
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01007741 out_str(T_UCS);
7742 if (((attr & HL_UNDERLINE) /* underline or undercurl */
Bram Moolenaar45a00002017-12-22 21:12:34 +01007743 || ((attr & HL_UNDERCURL) && *T_UCS == NUL))
7744 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745 out_str(T_US);
Bram Moolenaar45a00002017-12-22 21:12:34 +01007746 if ((attr & HL_ITALIC) && *T_CZH != NUL) /* italic */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747 out_str(T_CZH);
Bram Moolenaar45a00002017-12-22 21:12:34 +01007748 if ((attr & HL_INVERSE) && *T_MR != NUL) /* inverse (reverse) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749 out_str(T_MR);
Bram Moolenaar45a00002017-12-22 21:12:34 +01007750 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) /* strike */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02007751 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752
7753 /*
7754 * Output the color or start string after bold etc., in case the
7755 * bold etc. override the color setting.
7756 */
7757 if (aep != NULL)
7758 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007759#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007760 /* When 'termguicolors' is set but fg or bg is unset,
7761 * fall back to the cterm colors. This helps for SpellBad,
7762 * where the GUI uses a red undercurl. */
7763 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007764 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007765 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007766 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007767 }
7768 else
7769#endif
7770 if (t_colors > 1)
7771 {
7772 if (aep->ae_u.cterm.fg_color)
7773 term_fg_color(aep->ae_u.cterm.fg_color - 1);
7774 }
7775#ifdef FEAT_TERMGUICOLORS
7776 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
7777 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007778 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007779 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780 }
7781 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007782#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007783 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007785 if (aep->ae_u.cterm.bg_color)
7786 term_bg_color(aep->ae_u.cterm.bg_color - 1);
7787 }
7788
Bram Moolenaarf708ac52018-03-12 21:48:32 +01007789 if (!IS_CTERM)
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007790 {
7791 if (aep->ae_u.term.start != NULL)
7792 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793 }
7794 }
7795 }
7796 }
7797}
7798
7799 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007800screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801{
7802 int do_ME = FALSE; /* output T_ME code */
7803
7804 if (screen_attr != 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01007805#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806 && termcap_active
7807#endif
7808 )
7809 {
7810#ifdef FEAT_GUI
7811 if (gui.in_use)
7812 {
7813 char buf[20];
7814
7815 /* use internal GUI code */
7816 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
7817 OUT_STR(buf);
7818 }
7819 else
7820#endif
7821 {
7822 if (screen_attr > HL_ALL) /* special HL attr. */
7823 {
7824 attrentry_T *aep;
7825
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007826 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827 {
7828 /*
7829 * Assume that t_me restores the original colors!
7830 */
7831 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007832 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007833#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007834 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
7835 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
7836 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007837#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007838 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007839#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007840 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
7841 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
7842 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007843#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007844 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845 do_ME = TRUE;
7846 }
7847 else
7848 {
7849 aep = syn_term_attr2entry(screen_attr);
7850 if (aep != NULL && aep->ae_u.term.stop != NULL)
7851 {
7852 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
7853 do_ME = TRUE;
7854 else
7855 out_str(aep->ae_u.term.stop);
7856 }
7857 }
7858 if (aep == NULL) /* did ":syntax clear" */
7859 screen_attr = 0;
7860 else
7861 screen_attr = aep->ae_attr;
7862 }
7863
7864 /*
7865 * Often all ending-codes are equal to T_ME. Avoid outputting the
7866 * same sequence several times.
7867 */
7868 if (screen_attr & HL_STANDOUT)
7869 {
7870 if (STRCMP(T_SE, T_ME) == 0)
7871 do_ME = TRUE;
7872 else
7873 out_str(T_SE);
7874 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01007875 if ((screen_attr & HL_UNDERCURL) && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01007876 {
7877 if (STRCMP(T_UCE, T_ME) == 0)
7878 do_ME = TRUE;
7879 else
7880 out_str(T_UCE);
7881 }
7882 if ((screen_attr & HL_UNDERLINE)
Bram Moolenaar45a00002017-12-22 21:12:34 +01007883 || ((screen_attr & HL_UNDERCURL) && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884 {
7885 if (STRCMP(T_UE, T_ME) == 0)
7886 do_ME = TRUE;
7887 else
7888 out_str(T_UE);
7889 }
7890 if (screen_attr & HL_ITALIC)
7891 {
7892 if (STRCMP(T_CZR, T_ME) == 0)
7893 do_ME = TRUE;
7894 else
7895 out_str(T_CZR);
7896 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02007897 if (screen_attr & HL_STRIKETHROUGH)
7898 {
7899 if (STRCMP(T_STE, T_ME) == 0)
7900 do_ME = TRUE;
7901 else
7902 out_str(T_STE);
7903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
7905 out_str(T_ME);
7906
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007907#ifdef FEAT_TERMGUICOLORS
7908 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007910 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007911 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007912 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007913 term_bg_rgb_color(cterm_normal_bg_gui_color);
7914 }
7915 else
7916#endif
7917 {
7918 if (t_colors > 1)
7919 {
7920 /* set Normal cterm colors */
7921 if (cterm_normal_fg_color != 0)
7922 term_fg_color(cterm_normal_fg_color - 1);
7923 if (cterm_normal_bg_color != 0)
7924 term_bg_color(cterm_normal_bg_color - 1);
7925 if (cterm_normal_fg_bold)
7926 out_str(T_MD);
7927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928 }
7929 }
7930 }
7931 screen_attr = 0;
7932}
7933
7934/*
7935 * Reset the colors for a cterm. Used when leaving Vim.
7936 * The machine specific code may override this again.
7937 */
7938 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007939reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007941 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942 {
7943 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007944#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007945 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
7946 || cterm_normal_bg_gui_color != INVALCOLOR)
7947 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007948#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007950#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951 {
7952 out_str(T_OP);
7953 screen_attr = -1;
7954 }
7955 if (cterm_normal_fg_bold)
7956 {
7957 out_str(T_ME);
7958 screen_attr = -1;
7959 }
7960 }
7961}
7962
7963/*
7964 * Put character ScreenLines["off"] on the screen at position "row" and "col",
7965 * using the attributes from ScreenAttrs["off"].
7966 */
7967 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007968screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969{
7970 int attr;
7971
7972 /* Check for illegal values, just in case (could happen just after
7973 * resizing). */
7974 if (row >= screen_Rows || col >= screen_Columns)
7975 return;
7976
Bram Moolenaar33796b32019-06-08 16:01:13 +02007977 // Skip if under the popup menu.
7978 // Popup windows with zindex higher than POPUPMENU_ZINDEX go on top.
7979 if (pum_under_menu(row, col)
Bram Moolenaare2c453d2019-08-21 14:37:09 +02007980#ifdef FEAT_TEXT_PROP
Bram Moolenaar33796b32019-06-08 16:01:13 +02007981 && screen_zindex <= POPUPMENU_ZINDEX
Bram Moolenaare2c453d2019-08-21 14:37:09 +02007982#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02007983 )
Bram Moolenaarae654382019-01-17 21:09:05 +01007984 return;
Bram Moolenaar33796b32019-06-08 16:01:13 +02007985#ifdef FEAT_TEXT_PROP
Bram Moolenaarc662ec92019-06-23 00:15:57 +02007986 if (blocked_by_popup(row, col))
Bram Moolenaar33796b32019-06-08 16:01:13 +02007987 return;
7988#endif
7989
Bram Moolenaar494838a2015-02-10 19:20:37 +01007990 /* Outputting a character in the last cell on the screen may scroll the
7991 * screen up. Only do it when the "xn" termcap property is set, otherwise
7992 * mark the character invalid (update it when scrolled up). */
7993 if (*T_XN == NUL
7994 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995#ifdef FEAT_RIGHTLEFT
7996 /* account for first command-line character in rightleft mode */
7997 && !cmdmsg_rl
7998#endif
7999 )
8000 {
8001 ScreenAttrs[off] = (sattr_T)-1;
8002 return;
8003 }
8004
8005 /*
8006 * Stop highlighting first, so it's easier to move the cursor.
8007 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008 if (screen_char_attr != 0)
8009 attr = screen_char_attr;
8010 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011 attr = ScreenAttrs[off];
8012 if (screen_attr != attr)
8013 screen_stop_highlight();
8014
8015 windgoto(row, col);
8016
8017 if (screen_attr != attr)
8018 screen_start_highlight(attr);
8019
Bram Moolenaar071d4272004-06-13 20:20:40 +00008020 if (enc_utf8 && ScreenLinesUC[off] != 0)
8021 {
8022 char_u buf[MB_MAXBYTES + 1];
8023
Bram Moolenaarcb070082016-04-02 22:14:51 +02008024 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008025 {
8026 if (*p_ambw == 'd'
Bram Moolenaara12a1612019-01-24 16:39:02 +01008027#ifdef FEAT_GUI
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008028 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008029#endif
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008030 )
8031 {
8032 /* Clear the two screen cells. If the character is actually
8033 * single width it won't change the second cell. */
8034 out_str((char_u *)" ");
8035 term_windgoto(row, col);
8036 }
8037 /* not sure where the cursor is after drawing the ambiguous width
8038 * character */
Bram Moolenaarcb070082016-04-02 22:14:51 +02008039 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008040 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02008041 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008043
8044 /* Convert the UTF-8 character to bytes and write it. */
8045 buf[utfc_char2bytes(off, buf)] = NUL;
8046 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047 }
8048 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052 /* double-byte character in single-width cell */
8053 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8054 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055 }
8056
8057 screen_cur_col++;
8058}
8059
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060/*
8061 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8062 * on the screen at position 'row' and 'col'.
8063 * The attributes of the first byte is used for all. This is required to
8064 * output the two bytes of a double-byte character with nothing in between.
8065 */
8066 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008067screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068{
8069 /* Check for illegal values (could be wrong when screen was resized). */
8070 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8071 return;
8072
8073 /* Outputting the last character on the screen may scrollup the screen.
8074 * Don't to it! Mark the character invalid (update it when scrolled up) */
8075 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8076 {
8077 ScreenAttrs[off] = (sattr_T)-1;
8078 return;
8079 }
8080
8081 /* Output the first byte normally (positions the cursor), then write the
8082 * second byte directly. */
8083 screen_char(off, row, col);
8084 out_char(ScreenLines[off + 1]);
8085 ++screen_cur_col;
8086}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088/*
8089 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8090 * This uses the contents of ScreenLines[] and doesn't change it.
8091 */
8092 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008093screen_draw_rectangle(
8094 int row,
8095 int col,
8096 int height,
8097 int width,
8098 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099{
8100 int r, c;
8101 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008102 int max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008104 /* Can't use ScreenLines unless initialized */
8105 if (ScreenLines == NULL)
8106 return;
8107
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108 if (invert)
8109 screen_char_attr = HL_INVERSE;
8110 for (r = row; r < row + height; ++r)
8111 {
8112 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008113 max_off = off + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114 for (c = col; c < col + width; ++c)
8115 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00008116 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117 {
8118 screen_char_2(off + c, r, c);
8119 ++c;
8120 }
8121 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122 {
8123 screen_char(off + c, r, c);
Bram Moolenaar367329b2007-08-30 11:53:22 +00008124 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125 ++c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126 }
8127 }
8128 }
8129 screen_char_attr = 0;
8130}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132/*
8133 * Redraw the characters for a vertically split window.
8134 */
8135 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008136redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137{
8138 int col;
8139 int width;
8140
8141# ifdef FEAT_CLIPBOARD
8142 clip_may_clear_selection(row, end - 1);
8143# endif
8144
8145 if (wp == NULL)
8146 {
8147 col = 0;
8148 width = Columns;
8149 }
8150 else
8151 {
8152 col = wp->w_wincol;
8153 width = wp->w_width;
8154 }
8155 screen_draw_rectangle(row, col, end - row, width, FALSE);
8156}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008158 static void
8159space_to_screenline(int off, int attr)
8160{
8161 ScreenLines[off] = ' ';
8162 ScreenAttrs[off] = attr;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008163 if (enc_utf8)
8164 ScreenLinesUC[off] = 0;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008165}
8166
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167/*
8168 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8169 * with character 'c1' in first column followed by 'c2' in the other columns.
8170 * Use attributes 'attr'.
8171 */
8172 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008173screen_fill(
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008174 int start_row,
8175 int end_row,
8176 int start_col,
8177 int end_col,
8178 int c1,
8179 int c2,
8180 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008182 int row;
8183 int col;
8184 int off;
8185 int end_off;
8186 int did_delete;
8187 int c;
8188 int norm_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008190 int force_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191#endif
8192
8193 if (end_row > screen_Rows) /* safety check */
8194 end_row = screen_Rows;
8195 if (end_col > screen_Columns) /* safety check */
8196 end_col = screen_Columns;
8197 if (ScreenLines == NULL
8198 || start_row >= end_row
8199 || start_col >= end_col) /* nothing to do */
8200 return;
8201
8202 /* it's a "normal" terminal when not in a GUI or cterm */
8203 norm_term = (
8204#ifdef FEAT_GUI
8205 !gui.in_use &&
8206#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008207 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208 for (row = start_row; row < end_row; ++row)
8209 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008210 if (has_mbyte
Bram Moolenaara12a1612019-01-24 16:39:02 +01008211#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00008212 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008213#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00008214 )
8215 {
8216 /* When drawing over the right halve of a double-wide char clear
8217 * out the left halve. When drawing over the left halve of a
8218 * double wide-char clear out the right halve. Only needed in a
8219 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008220 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008221 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008222 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008223 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225 /*
8226 * Try to use delete-line termcap code, when no attributes or in a
8227 * "normal" terminal, where a bold/italic space is just a
8228 * space.
8229 */
8230 did_delete = FALSE;
8231 if (c2 == ' '
8232 && end_col == Columns
8233 && can_clear(T_CE)
8234 && (attr == 0
8235 || (norm_term
8236 && attr <= HL_ALL
8237 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8238 {
8239 /*
8240 * check if we really need to clear something
8241 */
8242 col = start_col;
8243 if (c1 != ' ') /* don't clear first char */
8244 ++col;
8245
8246 off = LineOffset[row] + col;
8247 end_off = LineOffset[row] + end_col;
8248
8249 /* skip blanks (used often, keep it fast!) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250 if (enc_utf8)
8251 while (off < end_off && ScreenLines[off] == ' '
8252 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8253 ++off;
8254 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255 while (off < end_off && ScreenLines[off] == ' '
8256 && ScreenAttrs[off] == 0)
8257 ++off;
8258 if (off < end_off) /* something to be cleared */
8259 {
8260 col = off - LineOffset[row];
8261 screen_stop_highlight();
8262 term_windgoto(row, col);/* clear rest of this screen line */
8263 out_str(T_CE);
8264 screen_start(); /* don't know where cursor is now */
8265 col = end_col - col;
8266 while (col--) /* clear chars in ScreenLines */
8267 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008268 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269 ++off;
8270 }
8271 }
8272 did_delete = TRUE; /* the chars are cleared now */
8273 }
8274
8275 off = LineOffset[row] + start_col;
8276 c = c1;
8277 for (col = start_col; col < end_col; ++col)
8278 {
Bram Moolenaar33796b32019-06-08 16:01:13 +02008279 if ((ScreenLines[off] != c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008280 || (enc_utf8 && (int)ScreenLinesUC[off]
8281 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282 || ScreenAttrs[off] != attr
8283#if defined(FEAT_GUI) || defined(UNIX)
8284 || force_next
8285#endif
8286 )
Bram Moolenaar33796b32019-06-08 16:01:13 +02008287#ifdef FEAT_TEXT_PROP
8288 // Skip if under a(nother) popup.
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008289 && !blocked_by_popup(row, col)
Bram Moolenaar33796b32019-06-08 16:01:13 +02008290#endif
8291 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292 {
8293#if defined(FEAT_GUI) || defined(UNIX)
8294 /* The bold trick may make a single row of pixels appear in
8295 * the next character. When a bold character is removed, the
8296 * next character should be redrawn too. This happens for our
8297 * own GUI and for some xterms. */
8298 if (
8299# ifdef FEAT_GUI
8300 gui.in_use
8301# endif
8302# if defined(FEAT_GUI) && defined(UNIX)
8303 ||
8304# endif
8305# ifdef UNIX
8306 term_is_xterm
8307# endif
8308 )
8309 {
8310 if (ScreenLines[off] != ' '
8311 && (ScreenAttrs[off] > HL_ALL
8312 || ScreenAttrs[off] & HL_BOLD))
8313 force_next = TRUE;
8314 else
8315 force_next = FALSE;
8316 }
8317#endif
8318 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319 if (enc_utf8)
8320 {
8321 if (c >= 0x80)
8322 {
8323 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008324 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325 }
8326 else
8327 ScreenLinesUC[off] = 0;
8328 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329 ScreenAttrs[off] = attr;
8330 if (!did_delete || c != ' ')
8331 screen_char(off, row, col);
8332 }
8333 ++off;
8334 if (col == start_col)
8335 {
8336 if (did_delete)
8337 break;
8338 c = c2;
8339 }
8340 }
8341 if (end_col == Columns)
8342 LineWraps[row] = FALSE;
8343 if (row == Rows - 1) /* overwritten the command line */
8344 {
8345 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02008346 if (start_col == 0 && end_col == Columns
8347 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008349 if (start_col == 0)
8350 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351 }
8352 }
8353}
8354
8355/*
8356 * Check if there should be a delay. Used before clearing or redrawing the
8357 * screen or the command line.
8358 */
8359 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008360check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361{
8362 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8363 && !did_wait_return
8364 && emsg_silent == 0)
8365 {
8366 out_flush();
8367 ui_delay(1000L, TRUE);
8368 emsg_on_display = FALSE;
8369 if (check_msg_scroll)
8370 msg_scroll = FALSE;
8371 }
8372}
8373
8374/*
Bram Moolenaarca57ab52019-04-13 14:53:16 +02008375 * Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect.
8376 */
8377 static void
8378clear_TabPageIdxs(void)
8379{
8380 int scol;
8381
8382 for (scol = 0; scol < Columns; ++scol)
8383 TabPageIdxs[scol] = 0;
8384}
8385
8386/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008388 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389 * Returns TRUE if there is a valid screen to write to.
8390 * Returns FALSE when starting up and screen not initialized yet.
8391 */
8392 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008393screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008395 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396 return (ScreenLines != NULL);
8397}
8398
8399/*
8400 * Resize the shell to Rows and Columns.
8401 * Allocate ScreenLines[] and associated items.
8402 *
8403 * There may be some time between setting Rows and Columns and (re)allocating
8404 * ScreenLines[]. This happens when starting up and when (manually) changing
8405 * the shell size. Always use screen_Rows and screen_Columns to access items
8406 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8407 * final size of the shell is needed.
8408 */
8409 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008410screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411{
8412 int new_row, old_row;
8413#ifdef FEAT_GUI
8414 int old_Rows;
8415#endif
8416 win_T *wp;
8417 int outofmem = FALSE;
8418 int len;
8419 schar_T *new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008421 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008423 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424 sattr_T *new_ScreenAttrs;
8425 unsigned *new_LineOffset;
8426 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008427 short *new_TabPageIdxs;
Bram Moolenaar33796b32019-06-08 16:01:13 +02008428#ifdef FEAT_TEXT_PROP
8429 short *new_popup_mask;
Bram Moolenaar4c063a02019-06-10 21:24:12 +02008430 short *new_popup_mask_next;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008431 char *new_popup_transparent;
Bram Moolenaar33796b32019-06-08 16:01:13 +02008432#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00008433 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008435 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008436 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008438retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439 /*
8440 * Allocation of the screen buffers is done only when the size changes and
8441 * when Rows and Columns have been set and we have started doing full
8442 * screen stuff.
8443 */
8444 if ((ScreenLines != NULL
8445 && Rows == screen_Rows
8446 && Columns == screen_Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447 && enc_utf8 == (ScreenLinesUC != NULL)
8448 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaara12a1612019-01-24 16:39:02 +01008449 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450 || Rows == 0
8451 || Columns == 0
8452 || (!full_screen && ScreenLines == NULL))
8453 return;
8454
8455 /*
8456 * It's possible that we produce an out-of-memory message below, which
8457 * will cause this function to be called again. To break the loop, just
8458 * return here.
8459 */
8460 if (entered)
8461 return;
8462 entered = TRUE;
8463
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008464 /*
8465 * Note that the window sizes are updated before reallocating the arrays,
8466 * thus we must not redraw here!
8467 */
8468 ++RedrawingDisabled;
8469
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470 win_new_shellsize(); /* fit the windows in the new sized shell */
8471
Bram Moolenaar071d4272004-06-13 20:20:40 +00008472 comp_col(); /* recompute columns for shown command and ruler */
8473
8474 /*
8475 * We're changing the size of the screen.
8476 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8477 * - Move lines from the old arrays into the new arrays, clear extra
8478 * lines (unless the screen is going to be cleared).
8479 * - Free the old arrays.
8480 *
8481 * If anything fails, make ScreenLines NULL, so we don't do anything!
8482 * Continuing with the old ScreenLines may result in a crash, because the
8483 * size is wrong.
8484 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008485 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008487 if (aucmd_win != NULL)
8488 win_free_lsize(aucmd_win);
Bram Moolenaar8caaf822019-06-01 18:11:22 +02008489#ifdef FEAT_TEXT_PROP
8490 // global popup windows
8491 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
8492 win_free_lsize(wp);
8493 // tab-local popup windows
8494 FOR_ALL_TABPAGES(tp)
8495 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
8496 win_free_lsize(wp);
8497#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008499 new_ScreenLines = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
Bram Moolenaar216b7102010-03-23 13:56:59 +01008500 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501 if (enc_utf8)
8502 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008503 new_ScreenLinesUC = LALLOC_MULT(u8char_T, (Rows + 1) * Columns);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008504 for (i = 0; i < p_mco; ++i)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008505 new_ScreenLinesC[i] = LALLOC_CLEAR_MULT(u8char_T,
8506 (Rows + 1) * Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008507 }
8508 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008509 new_ScreenLines2 = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
8510 new_ScreenAttrs = LALLOC_MULT(sattr_T, (Rows + 1) * Columns);
8511 new_LineOffset = LALLOC_MULT(unsigned, Rows);
8512 new_LineWraps = LALLOC_MULT(char_u, Rows);
8513 new_TabPageIdxs = LALLOC_MULT(short, Columns);
Bram Moolenaar33796b32019-06-08 16:01:13 +02008514#ifdef FEAT_TEXT_PROP
8515 new_popup_mask = LALLOC_MULT(short, Rows * Columns);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02008516 new_popup_mask_next = LALLOC_MULT(short, Rows * Columns);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008517 new_popup_transparent = LALLOC_MULT(char, Rows * Columns);
Bram Moolenaar33796b32019-06-08 16:01:13 +02008518#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008520 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521 {
8522 if (win_alloc_lines(wp) == FAIL)
8523 {
8524 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008525 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526 }
8527 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008528 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8529 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008530 outofmem = TRUE;
Bram Moolenaar8caaf822019-06-01 18:11:22 +02008531#ifdef FEAT_TEXT_PROP
8532 // global popup windows
8533 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
8534 if (win_alloc_lines(wp) == FAIL)
8535 {
8536 outofmem = TRUE;
8537 goto give_up;
8538 }
8539 // tab-local popup windows
8540 FOR_ALL_TABPAGES(tp)
8541 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
8542 if (win_alloc_lines(wp) == FAIL)
8543 {
8544 outofmem = TRUE;
8545 goto give_up;
8546 }
8547#endif
8548
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008549give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008551 for (i = 0; i < p_mco; ++i)
8552 if (new_ScreenLinesC[i] == NULL)
8553 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554 if (new_ScreenLines == NULL
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008555 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557 || new_ScreenAttrs == NULL
8558 || new_LineOffset == NULL
8559 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008560 || new_TabPageIdxs == NULL
Bram Moolenaar33796b32019-06-08 16:01:13 +02008561#ifdef FEAT_TEXT_PROP
8562 || new_popup_mask == NULL
Bram Moolenaar4c063a02019-06-10 21:24:12 +02008563 || new_popup_mask_next == NULL
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008564 || new_popup_transparent == NULL
Bram Moolenaar33796b32019-06-08 16:01:13 +02008565#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566 || outofmem)
8567 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008568 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008569 {
8570 /* guess the size */
8571 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8572
8573 /* Remember we did this to avoid getting outofmem messages over
8574 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008575 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008576 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01008577 VIM_CLEAR(new_ScreenLines);
Bram Moolenaard23a8232018-02-10 18:45:26 +01008578 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008579 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01008580 VIM_CLEAR(new_ScreenLinesC[i]);
8581 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaard23a8232018-02-10 18:45:26 +01008582 VIM_CLEAR(new_ScreenAttrs);
8583 VIM_CLEAR(new_LineOffset);
8584 VIM_CLEAR(new_LineWraps);
8585 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar33796b32019-06-08 16:01:13 +02008586#ifdef FEAT_TEXT_PROP
8587 VIM_CLEAR(new_popup_mask);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02008588 VIM_CLEAR(new_popup_mask_next);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008589 VIM_CLEAR(new_popup_transparent);
Bram Moolenaar33796b32019-06-08 16:01:13 +02008590#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591 }
8592 else
8593 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008594 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008595
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596 for (new_row = 0; new_row < Rows; ++new_row)
8597 {
8598 new_LineOffset[new_row] = new_row * Columns;
8599 new_LineWraps[new_row] = FALSE;
8600
8601 /*
8602 * If the screen is not going to be cleared, copy as much as
8603 * possible from the old screen to the new one and clear the rest
8604 * (used when resizing the window at the "--more--" prompt or when
8605 * executing an external command, for the GUI).
8606 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008607 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608 {
8609 (void)vim_memset(new_ScreenLines + new_row * Columns,
8610 ' ', (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611 if (enc_utf8)
8612 {
8613 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8614 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008615 for (i = 0; i < p_mco; ++i)
8616 (void)vim_memset(new_ScreenLinesC[i]
8617 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618 0, (size_t)Columns * sizeof(u8char_T));
8619 }
8620 if (enc_dbcs == DBCS_JPNU)
8621 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8622 0, (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8624 0, (size_t)Columns * sizeof(sattr_T));
8625 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008626 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627 {
8628 if (screen_Columns < Columns)
8629 len = screen_Columns;
8630 else
8631 len = Columns;
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008632 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008633 * may be invalid now. Also when p_mco changes. */
8634 if (!(enc_utf8 && ScreenLinesUC == NULL)
8635 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008636 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8637 ScreenLines + LineOffset[old_row],
8638 (size_t)len * sizeof(schar_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008639 if (enc_utf8 && ScreenLinesUC != NULL
8640 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641 {
8642 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8643 ScreenLinesUC + LineOffset[old_row],
8644 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008645 for (i = 0; i < p_mco; ++i)
8646 mch_memmove(new_ScreenLinesC[i]
8647 + new_LineOffset[new_row],
8648 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008649 (size_t)len * sizeof(u8char_T));
8650 }
8651 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8652 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8653 ScreenLines2 + LineOffset[old_row],
8654 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8656 ScreenAttrs + LineOffset[old_row],
8657 (size_t)len * sizeof(sattr_T));
8658 }
8659 }
8660 }
8661 /* Use the last line of the screen for the current line. */
8662 current_ScreenLine = new_ScreenLines + Rows * Columns;
Bram Moolenaar6ace95e2019-08-13 23:09:49 +02008663
8664#ifdef FEAT_TEXT_PROP
8665 vim_memset(new_popup_mask, 0, Rows * Columns * sizeof(short));
8666 vim_memset(new_popup_transparent, 0, Rows * Columns * sizeof(char));
8667#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668 }
8669
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008670 free_screenlines();
8671
Bram Moolenaar6ace95e2019-08-13 23:09:49 +02008672 // NOTE: this may result in all pointers to become NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008673 ScreenLines = new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008675 for (i = 0; i < p_mco; ++i)
8676 ScreenLinesC[i] = new_ScreenLinesC[i];
8677 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678 ScreenLines2 = new_ScreenLines2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008679 ScreenAttrs = new_ScreenAttrs;
8680 LineOffset = new_LineOffset;
8681 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008682 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar33796b32019-06-08 16:01:13 +02008683#ifdef FEAT_TEXT_PROP
8684 popup_mask = new_popup_mask;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008685 popup_mask_next = new_popup_mask_next;
8686 popup_transparent = new_popup_transparent;
Bram Moolenaar33796b32019-06-08 16:01:13 +02008687 popup_mask_refresh = TRUE;
8688#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689
8690 /* It's important that screen_Rows and screen_Columns reflect the actual
8691 * size of ScreenLines[]. Set them before calling anything. */
8692#ifdef FEAT_GUI
8693 old_Rows = screen_Rows;
8694#endif
8695 screen_Rows = Rows;
8696 screen_Columns = Columns;
8697
8698 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008699 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700 screenclear2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701#ifdef FEAT_GUI
8702 else if (gui.in_use
8703 && !gui.starting
8704 && ScreenLines != NULL
8705 && old_Rows != Rows)
8706 {
8707 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8708 /*
8709 * Adjust the position of the cursor, for when executing an external
8710 * command.
8711 */
8712 if (msg_row >= Rows) /* Rows got smaller */
8713 msg_row = Rows - 1; /* put cursor at last row */
8714 else if (Rows > old_Rows) /* Rows got bigger */
8715 msg_row += Rows - old_Rows; /* put cursor in same place */
8716 if (msg_col >= Columns) /* Columns got smaller */
8717 msg_col = Columns - 1; /* put cursor at last column */
8718 }
8719#endif
Bram Moolenaarca57ab52019-04-13 14:53:16 +02008720 clear_TabPageIdxs();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008723 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008724
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008725 /*
8726 * Do not apply autocommands more than 3 times to avoid an endless loop
8727 * in case applying autocommands always changes Rows or Columns.
8728 */
8729 if (starting == 0 && ++retry_count <= 3)
8730 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008731 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008732 /* In rare cases, autocommands may have altered Rows or Columns,
8733 * jump back to check if we need to allocate the screen again. */
8734 goto retry;
8735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736}
8737
8738 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008739free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008740{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008741 int i;
8742
Bram Moolenaar33796b32019-06-08 16:01:13 +02008743 VIM_CLEAR(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008744 for (i = 0; i < Screen_mco; ++i)
Bram Moolenaar33796b32019-06-08 16:01:13 +02008745 VIM_CLEAR(ScreenLinesC[i]);
8746 VIM_CLEAR(ScreenLines2);
8747 VIM_CLEAR(ScreenLines);
8748 VIM_CLEAR(ScreenAttrs);
8749 VIM_CLEAR(LineOffset);
8750 VIM_CLEAR(LineWraps);
8751 VIM_CLEAR(TabPageIdxs);
8752#ifdef FEAT_TEXT_PROP
8753 VIM_CLEAR(popup_mask);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02008754 VIM_CLEAR(popup_mask_next);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008755 VIM_CLEAR(popup_transparent);
Bram Moolenaar33796b32019-06-08 16:01:13 +02008756#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008757}
8758
8759 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008760screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761{
8762 check_for_delay(FALSE);
8763 screenalloc(FALSE); /* allocate screen buffers if size changed */
8764 screenclear2(); /* clear the screen */
8765}
8766
8767 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008768screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769{
8770 int i;
8771
8772 if (starting == NO_SCREEN || ScreenLines == NULL
8773#ifdef FEAT_GUI
8774 || (gui.in_use && gui.starting)
8775#endif
8776 )
8777 return;
8778
8779#ifdef FEAT_GUI
8780 if (!gui.in_use)
8781#endif
8782 screen_attr = -1; /* force setting the Normal colors */
8783 screen_stop_highlight(); /* don't want highlighting here */
8784
8785#ifdef FEAT_CLIPBOARD
8786 /* disable selection without redrawing it */
8787 clip_scroll_selection(9999);
8788#endif
8789
8790 /* blank out ScreenLines */
8791 for (i = 0; i < Rows; ++i)
8792 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02008793 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794 LineWraps[i] = FALSE;
8795 }
8796
8797 if (can_clear(T_CL))
8798 {
8799 out_str(T_CL); /* clear the display */
8800 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008801 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008802 }
8803 else
8804 {
8805 /* can't clear the screen, mark all chars with invalid attributes */
8806 for (i = 0; i < Rows; ++i)
8807 lineinvalid(LineOffset[i], (int)Columns);
8808 clear_cmdline = TRUE;
8809 }
8810
8811 screen_cleared = TRUE; /* can use contents of ScreenLines now */
8812
8813 win_rest_invalid(firstwin);
8814 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008815 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816 if (must_redraw == CLEAR) /* no need to clear again */
8817 must_redraw = NOT_VALID;
8818 compute_cmdrow();
8819 msg_row = cmdline_row; /* put cursor on last line for messages */
8820 msg_col = 0;
8821 screen_start(); /* don't know where cursor is now */
8822 msg_scrolled = 0; /* can't scroll back */
8823 msg_didany = FALSE;
8824 msg_didout = FALSE;
8825}
8826
8827/*
8828 * Clear one line in ScreenLines.
8829 */
8830 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02008831lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832{
8833 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834 if (enc_utf8)
8835 (void)vim_memset(ScreenLinesUC + off, 0,
8836 (size_t)width * sizeof(u8char_T));
Bram Moolenaarcfce7172017-08-17 20:31:48 +02008837 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838}
8839
8840/*
8841 * Mark one line in ScreenLines invalid by setting the attributes to an
8842 * invalid value.
8843 */
8844 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008845lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846{
8847 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
8848}
8849
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850/*
8851 * Copy part of a Screenline for vertically split window "wp".
8852 */
8853 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008854linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855{
8856 unsigned off_to = LineOffset[to] + wp->w_wincol;
8857 unsigned off_from = LineOffset[from] + wp->w_wincol;
8858
8859 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
8860 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861 if (enc_utf8)
8862 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008863 int i;
8864
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
8866 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008867 for (i = 0; i < p_mco; ++i)
8868 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
8869 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870 }
8871 if (enc_dbcs == DBCS_JPNU)
8872 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
8873 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
8875 wp->w_width * sizeof(sattr_T));
8876}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877
8878/*
8879 * Return TRUE if clearing with term string "p" would work.
8880 * It can't work when the string is empty or it won't set the right background.
Bram Moolenaar33796b32019-06-08 16:01:13 +02008881 * Don't clear to end-of-line when there are popups, it may cause flicker.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882 */
8883 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008884can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885{
8886 return (*p != NUL && (t_colors <= 1
8887#ifdef FEAT_GUI
8888 || gui.in_use
8889#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008890#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008891 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02008892 || (!p_tgc && cterm_normal_bg_color == 0)
8893#else
8894 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008895#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02008896 || *T_UT != NUL)
8897#ifdef FEAT_TEXT_PROP
8898 && !(p == T_CE && popup_visible)
8899#endif
8900 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901}
8902
8903/*
8904 * Reset cursor position. Use whenever cursor was moved because of outputting
8905 * something directly to the screen (shell commands) or a terminal control
8906 * code.
8907 */
8908 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008909screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008910{
8911 screen_cur_row = screen_cur_col = 9999;
8912}
8913
8914/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915 * Move the cursor to position "row","col" in the screen.
8916 * This tries to find the most efficient way to move, minimizing the number of
8917 * characters sent to the terminal.
8918 */
8919 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008920windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008922 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923 int i;
8924 int plan;
8925 int cost;
8926 int wouldbe_col;
8927 int noinvcurs;
8928 char_u *bs;
8929 int goto_cost;
8930 int attr;
8931
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008932#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008933#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
8934
8935#define PLAN_LE 1
8936#define PLAN_CR 2
8937#define PLAN_NL 3
8938#define PLAN_WRITE 4
8939 /* Can't use ScreenLines unless initialized */
8940 if (ScreenLines == NULL)
8941 return;
8942
8943 if (col != screen_cur_col || row != screen_cur_row)
8944 {
8945 /* Check for valid position. */
8946 if (row < 0) /* window without text lines? */
8947 row = 0;
8948 if (row >= screen_Rows)
8949 row = screen_Rows - 1;
8950 if (col >= screen_Columns)
8951 col = screen_Columns - 1;
8952
8953 /* check if no cursor movement is allowed in highlight mode */
8954 if (screen_attr && *T_MS == NUL)
8955 noinvcurs = HIGHL_COST;
8956 else
8957 noinvcurs = 0;
8958 goto_cost = GOTO_COST + noinvcurs;
8959
8960 /*
8961 * Plan how to do the positioning:
8962 * 1. Use CR to move it to column 0, same row.
8963 * 2. Use T_LE to move it a few columns to the left.
8964 * 3. Use NL to move a few lines down, column 0.
8965 * 4. Move a few columns to the right with T_ND or by writing chars.
8966 *
8967 * Don't do this if the cursor went beyond the last column, the cursor
8968 * position is unknown then (some terminals wrap, some don't )
8969 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008970 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00008971 * characters to move the cursor to the right.
8972 */
8973 if (row >= screen_cur_row && screen_cur_col < Columns)
8974 {
8975 /*
8976 * If the cursor is in the same row, bigger col, we can use CR
8977 * or T_LE.
8978 */
8979 bs = NULL; /* init for GCC */
8980 attr = screen_attr;
8981 if (row == screen_cur_row && col < screen_cur_col)
8982 {
8983 /* "le" is preferred over "bc", because "bc" is obsolete */
8984 if (*T_LE)
8985 bs = T_LE; /* "cursor left" */
8986 else
8987 bs = T_BC; /* "backspace character (old) */
8988 if (*bs)
8989 cost = (screen_cur_col - col) * (int)STRLEN(bs);
8990 else
8991 cost = 999;
8992 if (col + 1 < cost) /* using CR is less characters */
8993 {
8994 plan = PLAN_CR;
8995 wouldbe_col = 0;
8996 cost = 1; /* CR is just one character */
8997 }
8998 else
8999 {
9000 plan = PLAN_LE;
9001 wouldbe_col = col;
9002 }
9003 if (noinvcurs) /* will stop highlighting */
9004 {
9005 cost += noinvcurs;
9006 attr = 0;
9007 }
9008 }
9009
9010 /*
9011 * If the cursor is above where we want to be, we can use CR LF.
9012 */
9013 else if (row > screen_cur_row)
9014 {
9015 plan = PLAN_NL;
9016 wouldbe_col = 0;
9017 cost = (row - screen_cur_row) * 2; /* CR LF */
9018 if (noinvcurs) /* will stop highlighting */
9019 {
9020 cost += noinvcurs;
9021 attr = 0;
9022 }
9023 }
9024
9025 /*
9026 * If the cursor is in the same row, smaller col, just use write.
9027 */
9028 else
9029 {
9030 plan = PLAN_WRITE;
9031 wouldbe_col = screen_cur_col;
9032 cost = 0;
9033 }
9034
9035 /*
9036 * Check if any characters that need to be written have the
9037 * correct attributes. Also avoid UTF-8 characters.
9038 */
9039 i = col - wouldbe_col;
9040 if (i > 0)
9041 cost += i;
9042 if (cost < goto_cost && i > 0)
9043 {
9044 /*
9045 * Check if the attributes are correct without additionally
9046 * stopping highlighting.
9047 */
9048 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9049 while (i && *p++ == attr)
9050 --i;
9051 if (i != 0)
9052 {
9053 /*
9054 * Try if it works when highlighting is stopped here.
9055 */
9056 if (*--p == 0)
9057 {
9058 cost += noinvcurs;
9059 while (i && *p++ == 0)
9060 --i;
9061 }
9062 if (i != 0)
9063 cost = 999; /* different attributes, don't do it */
9064 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065 if (enc_utf8)
9066 {
9067 /* Don't use an UTF-8 char for positioning, it's slow. */
9068 for (i = wouldbe_col; i < col; ++i)
9069 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9070 {
9071 cost = 999;
9072 break;
9073 }
9074 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009075 }
9076
9077 /*
9078 * We can do it without term_windgoto()!
9079 */
9080 if (cost < goto_cost)
9081 {
9082 if (plan == PLAN_LE)
9083 {
9084 if (noinvcurs)
9085 screen_stop_highlight();
9086 while (screen_cur_col > col)
9087 {
9088 out_str(bs);
9089 --screen_cur_col;
9090 }
9091 }
9092 else if (plan == PLAN_CR)
9093 {
9094 if (noinvcurs)
9095 screen_stop_highlight();
9096 out_char('\r');
9097 screen_cur_col = 0;
9098 }
9099 else if (plan == PLAN_NL)
9100 {
9101 if (noinvcurs)
9102 screen_stop_highlight();
9103 while (screen_cur_row < row)
9104 {
9105 out_char('\n');
9106 ++screen_cur_row;
9107 }
9108 screen_cur_col = 0;
9109 }
9110
9111 i = col - screen_cur_col;
9112 if (i > 0)
9113 {
9114 /*
9115 * Use cursor-right if it's one character only. Avoids
9116 * removing a line of pixels from the last bold char, when
9117 * using the bold trick in the GUI.
9118 */
9119 if (T_ND[0] != NUL && T_ND[1] == NUL)
9120 {
9121 while (i-- > 0)
9122 out_char(*T_ND);
9123 }
9124 else
9125 {
9126 int off;
9127
9128 off = LineOffset[row] + screen_cur_col;
9129 while (i-- > 0)
9130 {
9131 if (ScreenAttrs[off] != screen_attr)
9132 screen_stop_highlight();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009133 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009134 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009135 if (enc_dbcs == DBCS_JPNU
9136 && ScreenLines[off] == 0x8e)
9137 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009138 ++off;
9139 }
9140 }
9141 }
9142 }
9143 }
9144 else
9145 cost = 999;
9146
9147 if (cost >= goto_cost)
9148 {
9149 if (noinvcurs)
9150 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009151 if (row == screen_cur_row && (col > screen_cur_col)
9152 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009153 term_cursor_right(col - screen_cur_col);
9154 else
9155 term_windgoto(row, col);
9156 }
9157 screen_cur_row = row;
9158 screen_cur_col = col;
9159 }
9160}
9161
9162/*
9163 * Set cursor to its position in the current window.
9164 */
9165 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009166setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009167{
Bram Moolenaar987723e2018-03-06 11:43:04 +01009168 setcursor_mayforce(FALSE);
9169}
9170
9171/*
9172 * Set cursor to its position in the current window.
9173 * When "force" is TRUE also when not redrawing.
9174 */
9175 void
9176setcursor_mayforce(int force)
9177{
9178 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179 {
9180 validate_cursor();
9181 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009182 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009183#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009184 /* With 'rightleft' set and the cursor on a double-wide
9185 * character, position it on the leftmost column. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01009186 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol
9187 - ((has_mbyte
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009188 && (*mb_ptr2cells)(ml_get_cursor()) == 2
Bram Moolenaara12a1612019-01-24 16:39:02 +01009189 && vim_isprintc(gchar_cursor())) ? 2 : 1)) :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009190#endif
9191 curwin->w_wcol));
9192 }
9193}
9194
9195
9196/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009197 * Insert 'line_count' lines at 'row' in window 'wp'.
9198 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9199 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009200 * scrolling.
9201 * Returns FAIL if the lines are not inserted, OK for success.
9202 */
9203 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009204win_ins_lines(
9205 win_T *wp,
9206 int row,
9207 int line_count,
9208 int invalid,
9209 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009210{
9211 int did_delete;
9212 int nextrow;
9213 int lastrow;
9214 int retval;
9215
9216 if (invalid)
9217 wp->w_lines_valid = 0;
9218
9219 if (wp->w_height < 5)
9220 return FAIL;
9221
9222 if (line_count > wp->w_height - row)
9223 line_count = wp->w_height - row;
9224
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009225 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009226 if (retval != MAYBE)
9227 return retval;
9228
9229 /*
9230 * If there is a next window or a status line, we first try to delete the
9231 * lines at the bottom to avoid messing what is after the window.
Bram Moolenaarc363fe12019-08-04 18:13:46 +02009232 * If this fails and there are following windows, don't do anything to
9233 * avoid messing up those windows, better just redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009234 */
9235 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009236 if (wp->w_next != NULL || wp->w_status_height)
9237 {
9238 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009239 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009240 did_delete = TRUE;
9241 else if (wp->w_next)
9242 return FAIL;
9243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009244 /*
9245 * if no lines deleted, blank the lines that will end up below the window
9246 */
9247 if (!did_delete)
9248 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009249 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009250 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02009251 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009252 lastrow = nextrow + line_count;
9253 if (lastrow > Rows)
9254 lastrow = Rows;
9255 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009256 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009257 ' ', ' ', 0);
9258 }
9259
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009260 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009261 == FAIL)
9262 {
Bram Moolenaarc363fe12019-08-04 18:13:46 +02009263 // deletion will have messed up other windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00009264 if (did_delete)
9265 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009266 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009267 win_rest_invalid(W_NEXT(wp));
9268 }
9269 return FAIL;
9270 }
9271
9272 return OK;
9273}
9274
9275/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009276 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009277 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9278 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9279 * scrolling
9280 * Return OK for success, FAIL if the lines are not deleted.
9281 */
9282 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009283win_del_lines(
9284 win_T *wp,
9285 int row,
9286 int line_count,
9287 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009288 int mayclear,
9289 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009290{
9291 int retval;
9292
9293 if (invalid)
9294 wp->w_lines_valid = 0;
9295
9296 if (line_count > wp->w_height - row)
9297 line_count = wp->w_height - row;
9298
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009299 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009300 if (retval != MAYBE)
9301 return retval;
9302
9303 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009304 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009305 return FAIL;
9306
Bram Moolenaar071d4272004-06-13 20:20:40 +00009307 /*
9308 * If there are windows or status lines below, try to put them at the
9309 * correct place. If we can't do that, they have to be redrawn.
9310 */
9311 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9312 {
9313 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009314 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009315 {
9316 wp->w_redr_status = TRUE;
9317 win_rest_invalid(wp->w_next);
9318 }
9319 }
9320 /*
9321 * If this is the last window and there is no status line, redraw the
9322 * command line later.
9323 */
9324 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009325 redraw_cmdline = TRUE;
9326 return OK;
9327}
9328
9329/*
9330 * Common code for win_ins_lines() and win_del_lines().
9331 * Returns OK or FAIL when the work has been done.
9332 * Returns MAYBE when not finished yet.
9333 */
9334 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009335win_do_lines(
9336 win_T *wp,
9337 int row,
9338 int line_count,
9339 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009340 int del,
9341 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009342{
9343 int retval;
9344
9345 if (!redrawing() || line_count <= 0)
9346 return FAIL;
9347
Bram Moolenaar33796b32019-06-08 16:01:13 +02009348 // When inserting lines would result in loss of command output, just redraw
9349 // the lines.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009350 if (no_win_do_lines_ins && !del)
9351 return FAIL;
9352
Bram Moolenaar33796b32019-06-08 16:01:13 +02009353 // only a few lines left: redraw is faster
Bram Moolenaar4033c552017-09-16 20:54:51 +02009354 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009355 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009356 if (!no_win_do_lines_ins)
Bram Moolenaar33796b32019-06-08 16:01:13 +02009357 screenclear(); // will set wp->w_lines_valid to 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00009358 return FAIL;
9359 }
9360
Bram Moolenaar33796b32019-06-08 16:01:13 +02009361#ifdef FEAT_TEXT_PROP
Bram Moolenaar4c063a02019-06-10 21:24:12 +02009362 // this doesn't work when there are popups visible
Bram Moolenaar33796b32019-06-08 16:01:13 +02009363 if (popup_visible)
9364 return FAIL;
9365#endif
9366
9367 // Delete all remaining lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00009368 if (row + line_count >= wp->w_height)
9369 {
9370 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009371 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009372 ' ', ' ', 0);
9373 return OK;
9374 }
9375
9376 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009377 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009378 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009379 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009380 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009381 if (!no_win_do_lines_ins)
9382 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009383
9384 /*
9385 * If the terminal can set a scroll region, use that.
9386 * Always do this in a vertically split window. This will redraw from
9387 * ScreenLines[] when t_CV isn't defined. That's faster than using
9388 * win_line().
9389 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009390 * a character in the lower right corner of the scroll region may cause a
9391 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009392 */
Bram Moolenaar02631462017-09-22 15:20:32 +02009393 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009394 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009395 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009396 scroll_region_set(wp, row);
9397 if (del)
9398 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009399 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009400 else
9401 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009402 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009403 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009404 scroll_region_reset();
9405 return retval;
9406 }
9407
Bram Moolenaar071d4272004-06-13 20:20:40 +00009408 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9409 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009410
9411 return MAYBE;
9412}
9413
9414/*
9415 * window 'wp' and everything after it is messed up, mark it for redraw
9416 */
9417 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009418win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009419{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009420 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009421 {
9422 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009423 wp->w_redr_status = TRUE;
9424 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009425 }
9426 redraw_cmdline = TRUE;
9427}
9428
9429/*
9430 * The rest of the routines in this file perform screen manipulations. The
9431 * given operation is performed physically on the screen. The corresponding
9432 * change is also made to the internal screen image. In this way, the editor
9433 * anticipates the effect of editing changes on the appearance of the screen.
9434 * That way, when we call screenupdate a complete redraw isn't usually
9435 * necessary. Another advantage is that we can keep adding code to anticipate
9436 * screen changes, and in the meantime, everything still works.
9437 */
9438
9439/*
9440 * types for inserting or deleting lines
9441 */
9442#define USE_T_CAL 1
9443#define USE_T_CDL 2
9444#define USE_T_AL 3
9445#define USE_T_CE 4
9446#define USE_T_DL 5
9447#define USE_T_SR 6
9448#define USE_NL 7
9449#define USE_T_CD 8
9450#define USE_REDRAW 9
9451
9452/*
9453 * insert lines on the screen and update ScreenLines[]
9454 * 'end' is the line after the scrolled part. Normally it is Rows.
9455 * When scrolling region used 'off' is the offset from the top for the region.
9456 * 'row' and 'end' are relative to the start of the region.
9457 *
9458 * return FAIL for failure, OK for success.
9459 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009460 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009461screen_ins_lines(
9462 int off,
9463 int row,
9464 int line_count,
9465 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009466 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01009467 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009468{
9469 int i;
9470 int j;
9471 unsigned temp;
9472 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009473 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009474 int type;
9475 int result_empty;
9476 int can_ce = can_clear(T_CE);
9477
9478 /*
9479 * FAIL if
9480 * - there is no valid screen
9481 * - the screen has to be redrawn completely
9482 * - the line count is less than one
9483 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009484 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar33796b32019-06-08 16:01:13 +02009485 * - there is a popup window
Bram Moolenaar071d4272004-06-13 20:20:40 +00009486 */
Bram Moolenaar33796b32019-06-08 16:01:13 +02009487 if (!screen_valid(TRUE)
9488 || line_count <= 0 || line_count > p_ttyscroll
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009489#ifdef FEAT_CLIPBOARD
9490 || (clip_star.state != SELECT_CLEARED
9491 && redrawing_for_callback > 0)
9492#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02009493#ifdef FEAT_TEXT_PROP
9494 || popup_visible
9495#endif
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009496 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497 return FAIL;
9498
9499 /*
9500 * There are seven ways to insert lines:
9501 * 0. When in a vertically split window and t_CV isn't set, redraw the
9502 * characters from ScreenLines[].
9503 * 1. Use T_CD (clear to end of display) if it exists and the result of
9504 * the insert is just empty lines
9505 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9506 * present or line_count > 1. It looks better if we do all the inserts
9507 * at once.
9508 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9509 * insert is just empty lines and T_CE is not present or line_count >
9510 * 1.
9511 * 4. Use T_AL (insert line) if it exists.
9512 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9513 * just empty lines.
9514 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9515 * just empty lines.
9516 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9517 * the 'da' flag is not set or we have clear line capability.
9518 * 8. redraw the characters from ScreenLines[].
9519 *
9520 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9521 * the scrollbar for the window. It does have insert line, use that if it
9522 * exists.
9523 */
9524 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009525 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9526 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009527 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528 type = USE_T_CD;
9529 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9530 type = USE_T_CAL;
9531 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9532 type = USE_T_CDL;
9533 else if (*T_AL != NUL)
9534 type = USE_T_AL;
9535 else if (can_ce && result_empty)
9536 type = USE_T_CE;
9537 else if (*T_DL != NUL && result_empty)
9538 type = USE_T_DL;
9539 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9540 type = USE_T_SR;
9541 else
9542 return FAIL;
9543
9544 /*
9545 * For clearing the lines screen_del_lines() is used. This will also take
9546 * care of t_db if necessary.
9547 */
9548 if (type == USE_T_CD || type == USE_T_CDL ||
9549 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009550 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009551
9552 /*
9553 * If text is retained below the screen, first clear or delete as many
9554 * lines at the bottom of the window as are about to be inserted so that
9555 * the deleted lines won't later surface during a screen_del_lines.
9556 */
9557 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009558 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009559
9560#ifdef FEAT_CLIPBOARD
9561 /* Remove a modeless selection when inserting lines halfway the screen
9562 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009563 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009564 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009565 else
9566 clip_scroll_selection(-line_count);
9567#endif
9568
Bram Moolenaar071d4272004-06-13 20:20:40 +00009569#ifdef FEAT_GUI
9570 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9571 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009572 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009573#endif
9574
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009575 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
9576 cursor_col = wp->w_wincol;
9577
Bram Moolenaar071d4272004-06-13 20:20:40 +00009578 if (*T_CCS != NUL) /* cursor relative to region */
9579 cursor_row = row;
9580 else
9581 cursor_row = row + off;
9582
9583 /*
9584 * Shift LineOffset[] line_count down to reflect the inserted lines.
9585 * Clear the inserted lines in ScreenLines[].
9586 */
9587 row += off;
9588 end += off;
9589 for (i = 0; i < line_count; ++i)
9590 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009591 if (wp != NULL && wp->w_width != Columns)
9592 {
9593 /* need to copy part of a line */
9594 j = end - 1 - i;
9595 while ((j -= line_count) >= row)
9596 linecopy(j + line_count, j, wp);
9597 j += line_count;
9598 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009599 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
9600 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601 else
9602 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9603 LineWraps[j] = FALSE;
9604 }
9605 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009606 {
9607 j = end - 1 - i;
9608 temp = LineOffset[j];
9609 while ((j -= line_count) >= row)
9610 {
9611 LineOffset[j + line_count] = LineOffset[j];
9612 LineWraps[j + line_count] = LineWraps[j];
9613 }
9614 LineOffset[j + line_count] = temp;
9615 LineWraps[j + line_count] = FALSE;
9616 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009617 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009618 else
9619 lineinvalid(temp, (int)Columns);
9620 }
9621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009622
9623 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009624 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009625 if (clear_attr != 0)
9626 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009627
Bram Moolenaar071d4272004-06-13 20:20:40 +00009628 /* redraw the characters */
9629 if (type == USE_REDRAW)
9630 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02009631 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009632 {
9633 term_append_lines(line_count);
9634 screen_start(); /* don't know where cursor is now */
9635 }
9636 else
9637 {
9638 for (i = 0; i < line_count; i++)
9639 {
9640 if (type == USE_T_AL)
9641 {
9642 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009643 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009644 out_str(T_AL);
9645 }
9646 else /* type == USE_T_SR */
9647 out_str(T_SR);
9648 screen_start(); /* don't know where cursor is now */
9649 }
9650 }
9651
9652 /*
9653 * With scroll-reverse and 'da' flag set we need to clear the lines that
9654 * have been scrolled down into the region.
9655 */
9656 if (type == USE_T_SR && *T_DA)
9657 {
9658 for (i = 0; i < line_count; ++i)
9659 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009660 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009661 out_str(T_CE);
9662 screen_start(); /* don't know where cursor is now */
9663 }
9664 }
9665
9666#ifdef FEAT_GUI
9667 gui_can_update_cursor();
9668 if (gui.in_use)
9669 out_flush(); /* always flush after a scroll */
9670#endif
9671 return OK;
9672}
9673
9674/*
Bram Moolenaar107abd22016-08-12 14:08:25 +02009675 * Delete lines on the screen and update ScreenLines[].
9676 * "end" is the line after the scrolled part. Normally it is Rows.
9677 * When scrolling region used "off" is the offset from the top for the region.
9678 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009679 *
9680 * Return OK for success, FAIL if the lines are not deleted.
9681 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009682 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009683screen_del_lines(
9684 int off,
9685 int row,
9686 int line_count,
9687 int end,
9688 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009689 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +01009690 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009691{
9692 int j;
9693 int i;
9694 unsigned temp;
9695 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009696 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009697 int cursor_end;
9698 int result_empty; /* result is empty until end of region */
9699 int can_delete; /* deleting line codes can be used */
9700 int type;
9701
9702 /*
9703 * FAIL if
9704 * - there is no valid screen
9705 * - the screen has to be redrawn completely
9706 * - the line count is less than one
9707 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009708 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009709 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009710 if (!screen_valid(TRUE) || line_count <= 0
9711 || (!force && line_count > p_ttyscroll)
9712#ifdef FEAT_CLIPBOARD
9713 || (clip_star.state != SELECT_CLEARED
9714 && redrawing_for_callback > 0)
9715#endif
9716 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009717 return FAIL;
9718
9719 /*
9720 * Check if the rest of the current region will become empty.
9721 */
9722 result_empty = row + line_count >= end;
9723
9724 /*
9725 * We can delete lines only when 'db' flag not set or when 'ce' option
9726 * available.
9727 */
9728 can_delete = (*T_DB == NUL || can_clear(T_CE));
9729
9730 /*
9731 * There are six ways to delete lines:
9732 * 0. When in a vertically split window and t_CV isn't set, redraw the
9733 * characters from ScreenLines[].
9734 * 1. Use T_CD if it exists and the result is empty.
9735 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9736 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9737 * none of the other ways work.
9738 * 4. Use T_CE (erase line) if the result is empty.
9739 * 5. Use T_DL (delete line) if it exists.
9740 * 6. redraw the characters from ScreenLines[].
9741 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009742 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9743 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009744 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009745 type = USE_T_CD;
9746#if defined(__BEOS__) && defined(BEOS_DR8)
9747 /*
9748 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9749 * its internal termcap... this works okay for tests which test *T_DB !=
9750 * NUL. It has the disadvantage that the user cannot use any :set t_*
9751 * command to get T_DB (back) to empty_option, only :set term=... will do
9752 * the trick...
9753 * Anyway, this hack will hopefully go away with the next OS release.
9754 * (Olaf Seibert)
9755 */
9756 else if (row == 0 && T_DB == empty_option
9757 && (line_count == 1 || *T_CDL == NUL))
9758#else
9759 else if (row == 0 && (
9760#ifndef AMIGA
9761 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9762 * up, so use delete-line command */
9763 line_count == 1 ||
9764#endif
9765 *T_CDL == NUL))
9766#endif
9767 type = USE_NL;
9768 else if (*T_CDL != NUL && line_count > 1 && can_delete)
9769 type = USE_T_CDL;
9770 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +02009771 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009772 type = USE_T_CE;
9773 else if (*T_DL != NUL && can_delete)
9774 type = USE_T_DL;
9775 else if (*T_CDL != NUL && can_delete)
9776 type = USE_T_CDL;
9777 else
9778 return FAIL;
9779
9780#ifdef FEAT_CLIPBOARD
9781 /* Remove a modeless selection when deleting lines halfway the screen or
9782 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009783 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009784 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009785 else
9786 clip_scroll_selection(line_count);
9787#endif
9788
Bram Moolenaar071d4272004-06-13 20:20:40 +00009789#ifdef FEAT_GUI
9790 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9791 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009792 gui_dont_update_cursor(gui.cursor_row >= row + off
9793 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009794#endif
9795
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009796 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
9797 cursor_col = wp->w_wincol;
9798
Bram Moolenaar071d4272004-06-13 20:20:40 +00009799 if (*T_CCS != NUL) /* cursor relative to region */
9800 {
9801 cursor_row = row;
9802 cursor_end = end;
9803 }
9804 else
9805 {
9806 cursor_row = row + off;
9807 cursor_end = end + off;
9808 }
9809
9810 /*
9811 * Now shift LineOffset[] line_count up to reflect the deleted lines.
9812 * Clear the inserted lines in ScreenLines[].
9813 */
9814 row += off;
9815 end += off;
9816 for (i = 0; i < line_count; ++i)
9817 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009818 if (wp != NULL && wp->w_width != Columns)
9819 {
9820 /* need to copy part of a line */
9821 j = row + i;
9822 while ((j += line_count) <= end - 1)
9823 linecopy(j - line_count, j, wp);
9824 j -= line_count;
9825 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009826 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
9827 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009828 else
9829 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9830 LineWraps[j] = FALSE;
9831 }
9832 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009833 {
9834 /* whole width, moving the line pointers is faster */
9835 j = row + i;
9836 temp = LineOffset[j];
9837 while ((j += line_count) <= end - 1)
9838 {
9839 LineOffset[j - line_count] = LineOffset[j];
9840 LineWraps[j - line_count] = LineWraps[j];
9841 }
9842 LineOffset[j - line_count] = temp;
9843 LineWraps[j - line_count] = FALSE;
9844 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009845 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009846 else
9847 lineinvalid(temp, (int)Columns);
9848 }
9849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009850
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009851 if (screen_attr != clear_attr)
9852 screen_stop_highlight();
9853 if (clear_attr != 0)
9854 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009855
Bram Moolenaar071d4272004-06-13 20:20:40 +00009856 /* redraw the characters */
9857 if (type == USE_REDRAW)
9858 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02009859 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009860 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009861 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009862 out_str(T_CD);
9863 screen_start(); /* don't know where cursor is now */
9864 }
9865 else if (type == USE_T_CDL)
9866 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009867 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009868 term_delete_lines(line_count);
9869 screen_start(); /* don't know where cursor is now */
9870 }
9871 /*
9872 * Deleting lines at top of the screen or scroll region: Just scroll
9873 * the whole screen (scroll region) up by outputting newlines on the
9874 * last line.
9875 */
9876 else if (type == USE_NL)
9877 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009878 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009879 for (i = line_count; --i >= 0; )
9880 out_char('\n'); /* cursor will remain on same line */
9881 }
9882 else
9883 {
9884 for (i = line_count; --i >= 0; )
9885 {
9886 if (type == USE_T_DL)
9887 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009888 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009889 out_str(T_DL); /* delete a line */
9890 }
9891 else /* type == USE_T_CE */
9892 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009893 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009894 out_str(T_CE); /* erase a line */
9895 }
9896 screen_start(); /* don't know where cursor is now */
9897 }
9898 }
9899
9900 /*
9901 * If the 'db' flag is set, we need to clear the lines that have been
9902 * scrolled up at the bottom of the region.
9903 */
9904 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
9905 {
9906 for (i = line_count; i > 0; --i)
9907 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009908 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009909 out_str(T_CE); /* erase a line */
9910 screen_start(); /* don't know where cursor is now */
9911 }
9912 }
9913
9914#ifdef FEAT_GUI
9915 gui_can_update_cursor();
9916 if (gui.in_use)
9917 out_flush(); /* always flush after a scroll */
9918#endif
9919
9920 return OK;
9921}
9922
9923/*
Bram Moolenaarcb574f42019-01-25 22:29:57 +01009924 * Return TRUE when postponing displaying the mode message: when not redrawing
9925 * or inside a mapping.
9926 */
9927 int
9928skip_showmode()
9929{
9930 // Call char_avail() only when we are going to show something, because it
9931 // takes a bit of time. redrawing() may also call char_avail_avail().
9932 if (global_busy
9933 || msg_silent != 0
9934 || !redrawing()
9935 || (char_avail() && !KeyTyped))
9936 {
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02009937 redraw_mode = TRUE; // show mode later
Bram Moolenaarcb574f42019-01-25 22:29:57 +01009938 return TRUE;
9939 }
9940 return FALSE;
9941}
9942
9943/*
Bram Moolenaar81226e02018-02-20 21:44:45 +01009944 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009945 *
9946 * If clear_cmdline is TRUE, clear the rest of the cmdline.
9947 * If clear_cmdline is FALSE there may be a message there that needs to be
9948 * cleared only if a mode is shown.
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02009949 * If redraw_mode is TRUE show or clear the mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009950 * Return the length of the message (0 if no message).
9951 */
9952 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009953showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009954{
9955 int need_clear;
9956 int length = 0;
9957 int do_mode;
9958 int attr;
9959 int nwr_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009960 int sub_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009961
Bram Moolenaar7df351e2006-01-23 22:30:28 +00009962 do_mode = ((p_smd && msg_silent == 0)
9963 && ((State & INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +02009964 || restart_edit != NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009965 || VIsual_active));
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009966 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009967 {
Bram Moolenaarcb574f42019-01-25 22:29:57 +01009968 if (skip_showmode())
9969 return 0; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00009970
9971 nwr_save = need_wait_return;
9972
9973 /* wait a bit before overwriting an important message */
9974 check_for_delay(FALSE);
9975
9976 /* if the cmdline is more than one line high, erase top lines */
9977 need_clear = clear_cmdline;
9978 if (clear_cmdline && cmdline_row < Rows - 1)
9979 msg_clr_cmdline(); /* will reset clear_cmdline */
9980
9981 /* Position on the last line in the window, column 0 */
9982 msg_pos_mode();
9983 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +01009984 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009985 if (do_mode)
9986 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01009987 msg_puts_attr("--", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009988#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00009989 if (
Bram Moolenaar09092152010-08-08 16:38:42 +02009990# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +00009991 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +02009992# else
Bram Moolenaarc236c162008-07-13 17:41:49 +00009993 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +00009994# endif
Bram Moolenaar09092152010-08-08 16:38:42 +02009995 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02009996# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar32526b32019-01-19 17:43:09 +01009997 msg_puts_attr(" IM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009998# else
Bram Moolenaar32526b32019-01-19 17:43:09 +01009999 msg_puts_attr(" XIM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010000# endif
10001#endif
10002#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10003 if (gui.in_use)
10004 {
10005 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010006 {
10007 /* HANGUL */
10008 if (enc_utf8)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010009 msg_puts_attr(" \355\225\234\352\270\200", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010010 else
Bram Moolenaar32526b32019-01-19 17:43:09 +010010011 msg_puts_attr(" \307\321\261\333", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010012 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010013 }
10014#endif
Bram Moolenaarea389e92014-05-28 21:40:52 +020010015 /* CTRL-X in Insert mode */
10016 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010017 {
10018 /* These messages can get long, avoid a wrap in a narrow
10019 * window. Prefer showing edit_submode_extra. */
10020 length = (Rows - msg_row) * Columns - 3;
10021 if (edit_submode_extra != NULL)
10022 length -= vim_strsize(edit_submode_extra);
10023 if (length > 0)
10024 {
10025 if (edit_submode_pre != NULL)
10026 length -= vim_strsize(edit_submode_pre);
10027 if (length - vim_strsize(edit_submode) > 0)
10028 {
10029 if (edit_submode_pre != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010030 msg_puts_attr((char *)edit_submode_pre, attr);
10031 msg_puts_attr((char *)edit_submode, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010032 }
10033 if (edit_submode_extra != NULL)
10034 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010035 msg_puts_attr(" ", attr); /* add a space in between */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010036 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010037 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010038 else
10039 sub_attr = attr;
Bram Moolenaar32526b32019-01-19 17:43:09 +010010040 msg_puts_attr((char *)edit_submode_extra, sub_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010041 }
10042 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010043 }
10044 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010045 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010046 if (State & VREPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010047 msg_puts_attr(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010048 else if (State & REPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010049 msg_puts_attr(_(" REPLACE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010050 else if (State & INSERT)
10051 {
10052#ifdef FEAT_RIGHTLEFT
10053 if (p_ri)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010054 msg_puts_attr(_(" REVERSE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010055#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +010010056 msg_puts_attr(_(" INSERT"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057 }
Bram Moolenaar942b4542018-06-17 16:23:34 +020010058 else if (restart_edit == 'I' || restart_edit == 'A')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010059 msg_puts_attr(_(" (insert)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010060 else if (restart_edit == 'R')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010061 msg_puts_attr(_(" (replace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010062 else if (restart_edit == 'V')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010063 msg_puts_attr(_(" (vreplace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010064#ifdef FEAT_RIGHTLEFT
10065 if (p_hkmap)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010066 msg_puts_attr(_(" Hebrew"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010067#endif
10068#ifdef FEAT_KEYMAP
10069 if (State & LANGMAP)
10070 {
10071# ifdef FEAT_ARABIC
10072 if (curwin->w_p_arab)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010073 msg_puts_attr(_(" Arabic"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010074 else
10075# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010076 if (get_keymap_str(curwin, (char_u *)" (%s)",
10077 NameBuff, MAXPATHL))
Bram Moolenaar32526b32019-01-19 17:43:09 +010010078 msg_puts_attr((char *)NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010079 }
10080#endif
10081 if ((State & INSERT) && p_paste)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010082 msg_puts_attr(_(" (paste)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010083
Bram Moolenaar071d4272004-06-13 20:20:40 +000010084 if (VIsual_active)
10085 {
10086 char *p;
10087
10088 /* Don't concatenate separate words to avoid translation
10089 * problems. */
10090 switch ((VIsual_select ? 4 : 0)
10091 + (VIsual_mode == Ctrl_V) * 2
10092 + (VIsual_mode == 'V'))
10093 {
10094 case 0: p = N_(" VISUAL"); break;
10095 case 1: p = N_(" VISUAL LINE"); break;
10096 case 2: p = N_(" VISUAL BLOCK"); break;
10097 case 4: p = N_(" SELECT"); break;
10098 case 5: p = N_(" SELECT LINE"); break;
10099 default: p = N_(" SELECT BLOCK"); break;
10100 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010101 msg_puts_attr(_(p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010102 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010103 msg_puts_attr(" --", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010104 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010105
Bram Moolenaar071d4272004-06-13 20:20:40 +000010106 need_clear = TRUE;
10107 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010108 if (reg_recording != 0
Bram Moolenaare2c453d2019-08-21 14:37:09 +020010109 && edit_submode == NULL) // otherwise it gets too long
Bram Moolenaar071d4272004-06-13 20:20:40 +000010110 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010111 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010112 need_clear = TRUE;
10113 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010114
10115 mode_displayed = TRUE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010116 if (need_clear || clear_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010117 msg_clr_eos();
10118 msg_didout = FALSE; /* overwrite this message */
10119 length = msg_col;
10120 msg_col = 0;
10121 need_wait_return = nwr_save; /* never ask for hit-return for this */
10122 }
10123 else if (clear_cmdline && msg_silent == 0)
10124 /* Clear the whole command line. Will reset "clear_cmdline". */
10125 msg_clr_cmdline();
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010126 else if (redraw_mode)
10127 {
10128 msg_pos_mode();
10129 msg_clr_eos();
10130 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010131
10132#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010133 /* In Visual mode the size of the selected area must be redrawn. */
10134 if (VIsual_active)
10135 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010136
10137 /* If the last window has no status line, the ruler is after the mode
10138 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010139 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +020010140 win_redr_ruler(lastwin, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010141#endif
10142 redraw_cmdline = FALSE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010143 redraw_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010144 clear_cmdline = FALSE;
10145
10146 return length;
10147}
10148
10149/*
10150 * Position for a mode message.
10151 */
10152 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010153msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010154{
10155 msg_col = 0;
10156 msg_row = Rows - 1;
10157}
10158
10159/*
10160 * Delete mode message. Used when ESC is typed which is expected to end
10161 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010162 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010163 */
10164 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010165unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010166{
10167 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010168 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010169 */
10170 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10171 redraw_cmdline = TRUE; /* delete mode later */
10172 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010173 clearmode();
10174}
10175
10176/*
10177 * Clear the mode message.
10178 */
10179 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010180clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010181{
Bram Moolenaar2abad542018-05-19 14:43:45 +020010182 int save_msg_row = msg_row;
10183 int save_msg_col = msg_col;
10184
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010185 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010186 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010187 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010188 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +020010189
10190 msg_col = save_msg_col;
10191 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010192}
10193
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010194 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010195recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010196{
Bram Moolenaar32526b32019-01-19 17:43:09 +010010197 msg_puts_attr(_("recording"), attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010198 if (!shortmess(SHM_RECORDING))
10199 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010200 char s[4];
10201
10202 sprintf(s, " @%c", reg_recording);
10203 msg_puts_attr(s, attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010204 }
10205}
10206
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010207/*
10208 * Draw the tab pages line at the top of the Vim window.
10209 */
Bram Moolenaare12bab32019-01-08 22:02:56 +010010210 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010211draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010212{
10213 int tabcount = 0;
10214 tabpage_T *tp;
10215 int tabwidth;
10216 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010217 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010218 int attr;
10219 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010220 win_T *cwp;
10221 int wincount;
10222 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010223 int c;
10224 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010225 int attr_sel = HL_ATTR(HLF_TPS);
10226 int attr_nosel = HL_ATTR(HLF_TP);
10227 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010228 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010229 int room;
10230 int use_sep_chars = (t_colors < 8
10231#ifdef FEAT_GUI
10232 && !gui.in_use
10233#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010234#ifdef FEAT_TERMGUICOLORS
10235 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010236#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010237 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010238
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010239 if (ScreenLines == NULL)
10240 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010241 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010242
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010243#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010244 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010245 if (gui_use_tabline())
10246 {
10247 gui_update_tabline();
10248 return;
10249 }
10250#endif
10251
10252 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010253 return;
10254
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010255#if defined(FEAT_STL_OPT)
Bram Moolenaarca57ab52019-04-13 14:53:16 +020010256 clear_TabPageIdxs();
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010257
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010258 /* Use the 'tabline' option if it's set. */
10259 if (*p_tal != NUL)
10260 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010261 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010262
10263 /* Check for an error. If there is one we would loop in redrawing the
10264 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010265 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010266 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010267 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010268 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010269 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010270 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010271 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010272 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010273#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010274 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010275 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010276 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010277
Bram Moolenaar238a5642006-02-21 22:12:05 +000010278 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10279 if (tabwidth < 6)
10280 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010281
Bram Moolenaar238a5642006-02-21 22:12:05 +000010282 attr = attr_nosel;
10283 tabcount = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010284 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10285 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010286 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010287 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010288
Bram Moolenaar238a5642006-02-21 22:12:05 +000010289 if (tp->tp_topframe == topframe)
10290 attr = attr_sel;
10291 if (use_sep_chars && col > 0)
10292 screen_putchar('|', 0, col++, attr);
10293
10294 if (tp->tp_topframe != topframe)
10295 attr = attr_nosel;
10296
10297 screen_putchar(' ', 0, col++, attr);
10298
10299 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010300 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010301 cwp = curwin;
10302 wp = firstwin;
10303 }
10304 else
10305 {
10306 cwp = tp->tp_curwin;
10307 wp = tp->tp_firstwin;
10308 }
10309
10310 modified = FALSE;
10311 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10312 if (bufIsChanged(wp->w_buffer))
10313 modified = TRUE;
10314 if (modified || wincount > 1)
10315 {
10316 if (wincount > 1)
10317 {
10318 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010319 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010320 if (col + len >= Columns - 3)
10321 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010322 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010323#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010324 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010325#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010326 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010327#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010328 );
10329 col += len;
10330 }
10331 if (modified)
10332 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10333 screen_putchar(' ', 0, col++, attr);
10334 }
10335
10336 room = scol - col + tabwidth - 1;
10337 if (room > 0)
10338 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010339 /* Get buffer name in NameBuff[] */
10340 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010341 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010342 len = vim_strsize(NameBuff);
10343 p = NameBuff;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010344 if (has_mbyte)
10345 while (len > room)
10346 {
10347 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010348 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010349 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010010350 else if (len > room)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010351 {
10352 p += len - room;
10353 len = room;
10354 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010355 if (len > Columns - col - 1)
10356 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010357
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010358 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010359 col += len;
10360 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010361 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010362
10363 /* Store the tab page number in TabPageIdxs[], so that
10364 * jump_to_mouse() knows where each one is. */
10365 ++tabcount;
10366 while (scol < col)
10367 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010368 }
10369
Bram Moolenaar238a5642006-02-21 22:12:05 +000010370 if (use_sep_chars)
10371 c = '_';
10372 else
10373 c = ' ';
10374 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010375
10376 /* Put an "X" for closing the current tab if there are several. */
10377 if (first_tabpage->tp_next != NULL)
10378 {
10379 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10380 TabPageIdxs[Columns - 1] = -999;
10381 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010382 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010383
10384 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10385 * set. */
10386 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010387}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010388
10389/*
10390 * Get buffer name for "buf" into NameBuff[].
10391 * Takes care of special buffer names and translates special characters.
10392 */
10393 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010394get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010395{
10396 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010397 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010398 else
10399 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10400 trans_characters(NameBuff, MAXPATHL);
10401}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010402
Bram Moolenaar071d4272004-06-13 20:20:40 +000010403/*
10404 * Get the character to use in a status line. Get its attributes in "*attr".
10405 */
10406 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010407fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010408{
10409 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010410
10411#ifdef FEAT_TERMINAL
10412 if (bt_terminal(wp->w_buffer))
10413 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010414 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010415 {
10416 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010417 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010418 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010419 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010420 {
10421 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010422 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010423 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010424 }
10425 else
10426#endif
10427 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010428 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010429 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010430 fill = fill_stl;
10431 }
10432 else
10433 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010434 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010435 fill = fill_stlnc;
10436 }
10437 /* Use fill when there is highlighting, and highlighting of current
10438 * window differs, or the fillchars differ, or this is not the
10439 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010440 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010441 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010442 || (fill_stl != fill_stlnc)))
10443 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010444 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010445 return '^';
10446 return '=';
10447}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010448
Bram Moolenaar071d4272004-06-13 20:20:40 +000010449/*
10450 * Get the character to use in a separator between vertically split windows.
10451 * Get its attributes in "*attr".
10452 */
10453 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010454fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010455{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010456 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010457 if (*attr == 0 && fill_vert == ' ')
10458 return '|';
10459 else
10460 return fill_vert;
10461}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010462
10463/*
10464 * Return TRUE if redrawing should currently be done.
10465 */
10466 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010467redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010468{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010469#ifdef FEAT_EVAL
10470 if (disable_redraw_for_testing)
10471 return 0;
10472 else
10473#endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +020010474 return ((!RedrawingDisabled
10475#ifdef FEAT_EVAL
10476 || ignore_redraw_flag_for_testing
10477#endif
10478 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010479}
10480
10481/*
10482 * Return TRUE if printing messages should currently be done.
10483 */
10484 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010485messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010486{
10487 return (!(p_lz && char_avail() && !KeyTyped));
10488}
10489
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010490#ifdef FEAT_MENU
10491/*
10492 * Draw the window toolbar.
10493 */
10494 static void
10495redraw_win_toolbar(win_T *wp)
10496{
10497 vimmenu_T *menu;
10498 int item_idx = 0;
10499 int item_count = 0;
10500 int col = 0;
10501 int next_col;
10502 int off = (int)(current_ScreenLine - ScreenLines);
10503 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
10504 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
10505
10506 vim_free(wp->w_winbar_items);
10507 for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
10508 ++item_count;
Bram Moolenaarc799fe22019-05-28 23:08:19 +020010509 wp->w_winbar_items = ALLOC_CLEAR_MULT(winbar_item_T, item_count + 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010510
10511 /* TODO: use fewer spaces if there is not enough room */
10512 for (menu = wp->w_winbar->children;
Bram Moolenaar02631462017-09-22 15:20:32 +020010513 menu != NULL && col < wp->w_width; menu = menu->next)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010514 {
10515 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010516 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010517 break;
10518 if (col > 1)
10519 {
10520 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010521 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010522 break;
10523 }
10524
10525 wp->w_winbar_items[item_idx].wb_startcol = col;
10526 space_to_screenline(off + col, button_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010527 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010528 break;
10529
10530 next_col = text_to_screenline(wp, menu->name, col);
10531 while (col < next_col)
10532 {
10533 ScreenAttrs[off + col] = button_attr;
10534 ++col;
10535 }
10536 wp->w_winbar_items[item_idx].wb_endcol = col;
10537 wp->w_winbar_items[item_idx].wb_menu = menu;
10538 ++item_idx;
10539
Bram Moolenaar02631462017-09-22 15:20:32 +020010540 if (col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010541 break;
10542 space_to_screenline(off + col, button_attr);
10543 ++col;
10544 }
Bram Moolenaar02631462017-09-22 15:20:32 +020010545 while (col < wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010546 {
10547 space_to_screenline(off + col, fill_attr);
10548 ++col;
10549 }
10550 wp->w_winbar_items[item_idx].wb_menu = NULL; /* end marker */
10551
Bram Moolenaar02631462017-09-22 15:20:32 +020010552 screen_line(wp->w_winrow, wp->w_wincol, (int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +020010553 (int)wp->w_width, 0);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010554}
10555#endif
Bram Moolenaar491ac282018-06-17 14:47:55 +020010556
Bram Moolenaar071d4272004-06-13 20:20:40 +000010557/*
10558 * Show current status info in ruler and various other places
10559 * If always is FALSE, only show ruler if position has changed.
10560 */
10561 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010562showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010563{
10564 if (!always && !redrawing())
10565 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010566 if (pum_visible())
10567 {
10568 /* Don't redraw right now, do it later. */
10569 curwin->w_redr_status = TRUE;
10570 return;
10571 }
Bram Moolenaar4033c552017-09-16 20:54:51 +020010572#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010573 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar362f3562009-11-03 16:20:34 +000010574 redraw_custom_statusline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010575 else
10576#endif
10577#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +020010578 win_redr_ruler(curwin, always, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010579#endif
10580
10581#ifdef FEAT_TITLE
10582 if (need_maketitle
10583# ifdef FEAT_STL_OPT
10584 || (p_icon && (stl_syntax & STL_IN_ICON))
10585 || (p_title && (stl_syntax & STL_IN_TITLE))
10586# endif
10587 )
10588 maketitle();
10589#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010590 /* Redraw the tab pages line if needed. */
10591 if (redraw_tabline)
10592 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010593}
10594
10595#ifdef FEAT_CMDL_INFO
10596 static void
Bram Moolenaar491ac282018-06-17 14:47:55 +020010597win_redr_ruler(win_T *wp, int always, int ignore_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010598{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010599#define RULER_BUF_LEN 70
10600 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010601 int row;
10602 int fillchar;
10603 int attr;
10604 int empty_line = FALSE;
10605 colnr_T virtcol;
10606 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010607 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010608 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010609 int this_ru_col;
10610 int off = 0;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +010010611 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010612
10613 /* If 'ruler' off or redrawing disabled, don't do anything */
10614 if (!p_ru)
10615 return;
10616
10617 /*
10618 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10619 * after deleting lines, before cursor.lnum is corrected.
10620 */
10621 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10622 return;
10623
Bram Moolenaare2c453d2019-08-21 14:37:09 +020010624 // Don't draw the ruler while doing insert-completion, it might overwrite
10625 // the (long) mode message.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010626 if (wp == lastwin && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010627 if (edit_submode != NULL)
10628 return;
Bram Moolenaar491ac282018-06-17 14:47:55 +020010629 // Don't draw the ruler when the popup menu is visible, it may overlap.
10630 // Except when the popup menu will be redrawn anyway.
10631 if (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010632 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010633
10634#ifdef FEAT_STL_OPT
10635 if (*p_ruf)
10636 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010637 int save_called_emsg = called_emsg;
10638
10639 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010640 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010641 if (called_emsg)
10642 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010643 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010644 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010645 return;
10646 }
10647#endif
10648
10649 /*
10650 * Check if not in Insert mode and the line is empty (will show "0-1").
10651 */
10652 if (!(State & INSERT)
10653 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10654 empty_line = TRUE;
10655
10656 /*
10657 * Only draw the ruler when something changed.
10658 */
10659 validate_virtcol_win(wp);
10660 if ( redraw_cmdline
10661 || always
10662 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10663 || wp->w_cursor.col != wp->w_ru_cursor.col
10664 || wp->w_virtcol != wp->w_ru_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +000010665 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
Bram Moolenaar071d4272004-06-13 20:20:40 +000010666 || wp->w_topline != wp->w_ru_topline
10667 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10668#ifdef FEAT_DIFF
10669 || wp->w_topfill != wp->w_ru_topfill
10670#endif
10671 || empty_line != wp->w_ru_empty)
10672 {
10673 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010674 if (wp->w_status_height)
10675 {
10676 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010677 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar53f81742017-09-22 14:35:51 +020010678 off = wp->w_wincol;
Bram Moolenaar02631462017-09-22 15:20:32 +020010679 width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010680 }
10681 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010682 {
10683 row = Rows - 1;
10684 fillchar = ' ';
10685 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010686 width = Columns;
10687 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010688 }
10689
10690 /* In list mode virtcol needs to be recomputed */
10691 virtcol = wp->w_virtcol;
10692 if (wp->w_p_list && lcs_tab1 == NUL)
10693 {
10694 wp->w_p_list = FALSE;
10695 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10696 wp->w_p_list = TRUE;
10697 }
10698
10699 /*
10700 * Some sprintfs return the length, some return a pointer.
10701 * To avoid portability problems we use strlen() here.
10702 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010703 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010704 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10705 ? 0L
10706 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010707 len = STRLEN(buffer);
10708 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010709 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10710 (int)virtcol + 1);
10711
10712 /*
10713 * Add a "50%" if there is room for it.
10714 * On the last line, don't print in the last column (scrolls the
10715 * screen up on some terminals).
10716 */
10717 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010718 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010719 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010720 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010721 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010722 this_ru_col = ru_col - (Columns - width);
10723 if (this_ru_col < 0)
10724 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010725 /* Never use more than half the window/screen width, leave the other
10726 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010727 if (this_ru_col < (width + 1) / 2)
10728 this_ru_col = (width + 1) / 2;
10729 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010730 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010010731 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010732 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010733 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010734 if (has_mbyte)
10735 i += (*mb_char2bytes)(fillchar, buffer + i);
10736 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010737 buffer[i++] = fillchar;
10738 ++o;
10739 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010740 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010741 }
10742 /* Truncate at window boundary. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010743 if (has_mbyte)
10744 {
10745 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010746 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010747 {
10748 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010749 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010750 {
10751 buffer[i] = NUL;
10752 break;
10753 }
10754 }
10755 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010010756 else if (this_ru_col + (int)STRLEN(buffer) > width)
Bram Moolenaar4033c552017-09-16 20:54:51 +020010757 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010758
Bram Moolenaar4033c552017-09-16 20:54:51 +020010759 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010760 i = redraw_cmdline;
10761 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020010762 this_ru_col + off + (int)STRLEN(buffer),
10763 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010764 fillchar, fillchar, attr);
10765 /* don't redraw the cmdline because of showing the ruler */
10766 redraw_cmdline = i;
10767 wp->w_ru_cursor = wp->w_cursor;
10768 wp->w_ru_virtcol = wp->w_virtcol;
10769 wp->w_ru_empty = empty_line;
10770 wp->w_ru_topline = wp->w_topline;
10771 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10772#ifdef FEAT_DIFF
10773 wp->w_ru_topfill = wp->w_topfill;
10774#endif
10775 }
10776}
10777#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010778
Bram Moolenaare677df82019-09-02 22:31:11 +020010779/*
10780 * Compute columns for ruler and shown command. 'sc_col' is also used to
10781 * decide what the maximum length of a message on the status line can be.
10782 * If there is a status line for the last window, 'sc_col' is independent
10783 * of 'ru_col'.
10784 */
10785
10786#define COL_RULER 17 // columns needed by standard ruler
10787
10788 void
10789comp_col(void)
10790{
10791#if defined(FEAT_CMDL_INFO)
10792 int last_has_status = (p_ls == 2 || (p_ls == 1 && !ONE_WINDOW));
10793
10794 sc_col = 0;
10795 ru_col = 0;
10796 if (p_ru)
10797 {
10798# ifdef FEAT_STL_OPT
10799 ru_col = (ru_wid ? ru_wid : COL_RULER) + 1;
10800# else
10801 ru_col = COL_RULER + 1;
10802# endif
10803 // no last status line, adjust sc_col
10804 if (!last_has_status)
10805 sc_col = ru_col;
10806 }
10807 if (p_sc)
10808 {
10809 sc_col += SHOWCMD_COLS;
10810 if (!p_ru || last_has_status) // no need for separating space
10811 ++sc_col;
10812 }
10813 sc_col = Columns - sc_col;
10814 ru_col = Columns - ru_col;
10815 if (sc_col <= 0) // screen too narrow, will become a mess
10816 sc_col = 1;
10817 if (ru_col <= 0)
10818 ru_col = 1;
10819#else
10820 sc_col = Columns;
10821 ru_col = Columns;
10822#endif
10823#ifdef FEAT_EVAL
10824 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
10825#endif
10826}
10827
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010828#if defined(FEAT_LINEBREAK) || defined(PROTO)
10829/*
Bram Moolenaar64486672010-05-16 15:46:46 +020010830 * Return the width of the 'number' and 'relativenumber' column.
10831 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010832 * Otherwise it depends on 'numberwidth' and the line count.
10833 */
10834 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010835number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010836{
10837 int n;
10838 linenr_T lnum;
10839
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020010840 if (wp->w_p_rnu && !wp->w_p_nu)
10841 /* cursor line shows "0" */
10842 lnum = wp->w_height;
10843 else
10844 /* cursor line shows absolute line number */
10845 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020010846
Bram Moolenaar6b314672015-03-20 15:42:10 +010010847 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010848 return wp->w_nrwidth_width;
10849 wp->w_nrwidth_line_count = lnum;
10850
10851 n = 0;
10852 do
10853 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010854 lnum /= 10;
10855 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010856 } while (lnum > 0);
10857
10858 /* 'numberwidth' gives the minimal width plus one */
10859 if (n < wp->w_p_nuw - 1)
10860 n = wp->w_p_nuw - 1;
10861
Bram Moolenaare4b407f2019-07-04 11:59:28 +020010862# ifdef FEAT_SIGNS
10863 // If 'signcolumn' is set to 'number' and there is a sign to display, then
10864 // the minimal width for the number column is 2.
10865 if (n < 2 && (wp->w_buffer->b_signlist != NULL)
10866 && (*wp->w_p_scl == 'n' && *(wp->w_p_scl + 1) == 'u'))
10867 n = 2;
10868# endif
10869
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010870 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010010871 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010872 return n;
10873}
10874#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010875
Bram Moolenaar113e1072019-01-20 15:30:40 +010010876#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010877/*
10878 * Return the current cursor column. This is the actual position on the
10879 * screen. First column is 0.
10880 */
10881 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010882screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010883{
10884 return screen_cur_col;
10885}
10886
10887/*
10888 * Return the current cursor row. This is the actual position on the screen.
10889 * First row is 0.
10890 */
10891 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010892screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010893{
10894 return screen_cur_row;
10895}
Bram Moolenaar113e1072019-01-20 15:30:40 +010010896#endif
Bram Moolenaare677df82019-09-02 22:31:11 +020010897
10898/*
10899 * Handle setting 'listchars' or 'fillchars'.
10900 * Returns error message, NULL if it's OK.
10901 */
10902 char *
10903set_chars_option(char_u **varp)
10904{
10905 int round, i, len, entries;
10906 char_u *p, *s;
10907 int c1 = 0, c2 = 0, c3 = 0;
10908 struct charstab
10909 {
10910 int *cp;
10911 char *name;
10912 };
10913 static struct charstab filltab[] =
10914 {
10915 {&fill_stl, "stl"},
10916 {&fill_stlnc, "stlnc"},
10917 {&fill_vert, "vert"},
10918 {&fill_fold, "fold"},
10919 {&fill_diff, "diff"},
10920 };
10921 static struct charstab lcstab[] =
10922 {
10923 {&lcs_eol, "eol"},
10924 {&lcs_ext, "extends"},
10925 {&lcs_nbsp, "nbsp"},
10926 {&lcs_prec, "precedes"},
10927 {&lcs_space, "space"},
10928 {&lcs_tab2, "tab"},
10929 {&lcs_trail, "trail"},
10930#ifdef FEAT_CONCEAL
10931 {&lcs_conceal, "conceal"},
10932#else
10933 {NULL, "conceal"},
10934#endif
10935 };
10936 struct charstab *tab;
10937
10938 if (varp == &p_lcs)
10939 {
10940 tab = lcstab;
10941 entries = sizeof(lcstab) / sizeof(struct charstab);
10942 }
10943 else
10944 {
10945 tab = filltab;
10946 entries = sizeof(filltab) / sizeof(struct charstab);
10947 }
10948
10949 // first round: check for valid value, second round: assign values
10950 for (round = 0; round <= 1; ++round)
10951 {
10952 if (round > 0)
10953 {
10954 // After checking that the value is valid: set defaults: space for
10955 // 'fillchars', NUL for 'listchars'
10956 for (i = 0; i < entries; ++i)
10957 if (tab[i].cp != NULL)
10958 *(tab[i].cp) = (varp == &p_lcs ? NUL : ' ');
10959
10960 if (varp == &p_lcs)
10961 {
10962 lcs_tab1 = NUL;
10963 lcs_tab3 = NUL;
10964 }
10965 else
10966 fill_diff = '-';
10967 }
10968 p = *varp;
10969 while (*p)
10970 {
10971 for (i = 0; i < entries; ++i)
10972 {
10973 len = (int)STRLEN(tab[i].name);
10974 if (STRNCMP(p, tab[i].name, len) == 0
10975 && p[len] == ':'
10976 && p[len + 1] != NUL)
10977 {
10978 c2 = c3 = 0;
10979 s = p + len + 1;
10980 c1 = mb_ptr2char_adv(&s);
10981 if (mb_char2cells(c1) > 1)
10982 continue;
10983 if (tab[i].cp == &lcs_tab2)
10984 {
10985 if (*s == NUL)
10986 continue;
10987 c2 = mb_ptr2char_adv(&s);
10988 if (mb_char2cells(c2) > 1)
10989 continue;
10990 if (!(*s == ',' || *s == NUL))
10991 {
10992 c3 = mb_ptr2char_adv(&s);
10993 if (mb_char2cells(c3) > 1)
10994 continue;
10995 }
10996 }
10997
10998 if (*s == ',' || *s == NUL)
10999 {
11000 if (round)
11001 {
11002 if (tab[i].cp == &lcs_tab2)
11003 {
11004 lcs_tab1 = c1;
11005 lcs_tab2 = c2;
11006 lcs_tab3 = c3;
11007 }
11008 else if (tab[i].cp != NULL)
11009 *(tab[i].cp) = c1;
11010
11011 }
11012 p = s;
11013 break;
11014 }
11015 }
11016 }
11017
11018 if (i == entries)
11019 return e_invarg;
11020 if (*p == ',')
11021 ++p;
11022 }
11023 }
11024
11025 return NULL; // no error
11026}