blob: 5043c0d7ffab5234884caaf700e24501cea4e79e [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 Moolenaar071d4272004-06-13 20:20:40 +0000570 if (must_redraw)
571 {
572 if (type < must_redraw) /* use maximal type */
573 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000574
575 /* must_redraw is reset here, so that when we run into some weird
576 * reason to redraw while busy redrawing (e.g., asynchronous
577 * scrolling), or update_topline() in win_update() will cause a
578 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 must_redraw = 0;
580 }
581
Bram Moolenaar6eddadf2018-05-06 16:40:16 +0200582 /* May need to update w_lines[]. */
583 if (curwin->w_lines_valid == 0 && type < NOT_VALID
584#ifdef FEAT_TERMINAL
585 && !term_do_update_window(curwin)
586#endif
587 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588 type = NOT_VALID;
589
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000590 /* Postpone the redrawing when it's not needed and when being called
591 * recursively. */
592 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 {
594 redraw_later(type); /* remember type for next time */
595 must_redraw = type;
596 if (type > INVERTED_ALL)
597 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200598 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 }
Bram Moolenaarec572ad2019-07-07 14:26:59 +0200600 updating_screen = TRUE;
Bram Moolenaar33796b32019-06-08 16:01:13 +0200601
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200602#ifdef FEAT_TEXT_PROP
Bram Moolenaar4c063a02019-06-10 21:24:12 +0200603 // Update popup_mask if needed. This may set w_redraw_top and w_redraw_bot
604 // in some windows.
605 may_update_popup_mask(type);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200606#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608#ifdef FEAT_SYN_HL
609 ++display_tick; /* let syntax code know we're in a next round of
610 * display updating */
611#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200612 if (no_update)
613 ++no_win_do_lines_ins;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614
615 /*
616 * if the screen was scrolled up when displaying a message, scroll it down
617 */
618 if (msg_scrolled)
619 {
620 clear_cmdline = TRUE;
621 if (msg_scrolled > Rows - 5) /* clearing is faster */
622 type = CLEAR;
623 else if (type != CLEAR)
624 {
625 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200626 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, 0, NULL)
627 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 type = CLEAR;
629 FOR_ALL_WINDOWS(wp)
630 {
Bram Moolenaar98fb65c2019-06-02 20:33:32 +0200631 if (wp->w_winrow < msg_scrolled)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 {
633 if (W_WINROW(wp) + wp->w_height > msg_scrolled
634 && wp->w_redr_type < REDRAW_TOP
635 && wp->w_lines_valid > 0
636 && wp->w_topline == wp->w_lines[0].wl_lnum)
637 {
638 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
639 wp->w_redr_type = REDRAW_TOP;
640 }
641 else
642 {
643 wp->w_redr_type = NOT_VALID;
Bram Moolenaare0de17d2017-09-24 16:24:34 +0200644 if (W_WINROW(wp) + wp->w_height + wp->w_status_height
645 <= msg_scrolled)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 }
648 }
649 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200650 if (!no_update)
651 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000652 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 }
654 msg_scrolled = 0;
655 need_wait_return = FALSE;
656 }
657
658 /* reset cmdline_row now (may have been changed temporarily) */
659 compute_cmdrow();
660
661 /* Check for changed highlighting */
662 if (need_highlight_changed)
663 highlight_changed();
664
665 if (type == CLEAR) /* first clear screen */
666 {
667 screenclear(); /* will reset clear_cmdline */
668 type = NOT_VALID;
Bram Moolenaar9f5f7bf2017-06-28 20:45:26 +0200669 /* must_redraw may be set indirectly, avoid another redraw later */
670 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 }
672
673 if (clear_cmdline) /* going to clear cmdline (done below) */
674 check_for_delay(FALSE);
675
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000676#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200677 /* Force redraw when width of 'number' or 'relativenumber' column
678 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000679 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200680 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
681 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000682 curwin->w_redr_type = NOT_VALID;
683#endif
684
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 /*
686 * Only start redrawing if there is really something to do.
687 */
688 if (type == INVERTED)
689 update_curswant();
690 if (curwin->w_redr_type < type
691 && !((type == VALID
692 && curwin->w_lines[0].wl_valid
693#ifdef FEAT_DIFF
694 && curwin->w_topfill == curwin->w_old_topfill
695 && curwin->w_botfill == curwin->w_old_botfill
696#endif
697 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000699 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
701 && curwin->w_old_visual_mode == VIsual_mode
702 && (curwin->w_valid & VALID_VIRTCOL)
703 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 ))
705 curwin->w_redr_type = type;
706
Bram Moolenaar5a305422006-04-28 22:38:25 +0000707 /* Redraw the tab pages line if needed. */
708 if (redraw_tabline || type >= NOT_VALID)
709 draw_tabline();
Bram Moolenaar5a305422006-04-28 22:38:25 +0000710
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711#ifdef FEAT_SYN_HL
712 /*
713 * Correct stored syntax highlighting info for changes in each displayed
714 * buffer. Each buffer must only be done once.
715 */
716 FOR_ALL_WINDOWS(wp)
717 {
718 if (wp->w_buffer->b_mod_set)
719 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 win_T *wwp;
721
722 /* Check if we already did this buffer. */
723 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
724 if (wwp->w_buffer == wp->w_buffer)
725 break;
Bram Moolenaar4033c552017-09-16 20:54:51 +0200726 if (wwp == wp && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 syn_stack_apply_changes(wp->w_buffer);
728 }
729 }
730#endif
731
732 /*
733 * Go from top to bottom through the windows, redrawing the ones that need
734 * it.
735 */
736#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
737 did_one = FALSE;
738#endif
739#ifdef FEAT_SEARCH_EXTRA
740 search_hl.rm.regprog = NULL;
741#endif
742 FOR_ALL_WINDOWS(wp)
743 {
744 if (wp->w_redr_type != 0)
745 {
746 cursor_off();
747#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
748 if (!did_one)
749 {
750 did_one = TRUE;
751# ifdef FEAT_SEARCH_EXTRA
752 start_search_hl();
753# endif
754# ifdef FEAT_CLIPBOARD
755 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200756 if (clip_star.available && clip_isautosel_star())
757 clip_update_selection(&clip_star);
758 if (clip_plus.available && clip_isautosel_plus())
759 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760# endif
761#ifdef FEAT_GUI
762 /* Remove the cursor before starting to do anything, because
763 * scrolling may make it difficult to redraw the text under
764 * it. */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200765 if (gui.in_use && wp == curwin)
Bram Moolenaar144445d2016-07-08 21:41:54 +0200766 {
767 gui_cursor_col = gui.cursor_col;
768 gui_cursor_row = gui.cursor_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 gui_undraw_cursor();
Bram Moolenaar107abd22016-08-12 14:08:25 +0200770 did_undraw = TRUE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200771 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772#endif
773 }
774#endif
775 win_update(wp);
776 }
777
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 /* redraw status line after the window to minimize cursor movement */
779 if (wp->w_redr_status)
780 {
781 cursor_off();
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200782 win_redr_status(wp, TRUE); // any popup menu will be redrawn below
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 }
785#if defined(FEAT_SEARCH_EXTRA)
786 end_search_hl();
787#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100788 /* May need to redraw the popup menu. */
Bram Moolenaar491ac282018-06-17 14:47:55 +0200789 pum_may_redraw();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 /* Reset b_mod_set flags. Going through all windows is probably faster
792 * than going through all buffers (there could be many buffers). */
Bram Moolenaar29323592016-07-24 22:04:11 +0200793 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 wp->w_buffer->b_mod_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +0200796#ifdef FEAT_TEXT_PROP
797 // Display popup windows on top of the windows and command line.
798 update_popups(win_update);
799#endif
800
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200801 after_updating_screen(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802
803 /* Clear or redraw the command line. Done last, because scrolling may
804 * mess up the command line. */
Bram Moolenaar4c25bd72019-04-20 23:38:07 +0200805 if (clear_cmdline || redraw_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 showmode();
807
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200808 if (no_update)
809 --no_win_do_lines_ins;
810
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200812 if (!did_intro)
813 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 did_intro = TRUE;
815
816#ifdef FEAT_GUI
817 /* Redraw the cursor and update the scrollbars when all screen updating is
818 * done. */
819 if (gui.in_use)
820 {
Bram Moolenaar107abd22016-08-12 14:08:25 +0200821 if (did_undraw && !gui_mch_is_blink_off())
Bram Moolenaar144445d2016-07-08 21:41:54 +0200822 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100823 mch_disable_flush();
824 out_flush(); /* required before updating the cursor */
825 mch_enable_flush();
826
Bram Moolenaar144445d2016-07-08 21:41:54 +0200827 /* Put the GUI position where the cursor was, gui_update_cursor()
828 * uses that. */
829 gui.col = gui_cursor_col;
830 gui.row = gui_cursor_row;
Bram Moolenaar84dbd492016-10-02 23:09:31 +0200831 gui.col = mb_fix_col(gui.col, gui.row);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara338adc2018-01-31 20:51:47 +0100833 gui_may_flush();
Bram Moolenaar65549bd2016-07-08 22:52:37 +0200834 screen_cur_col = gui.col;
835 screen_cur_row = gui.row;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200836 }
Bram Moolenaara338adc2018-01-31 20:51:47 +0100837 else
838 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 gui_update_scrollbars(FALSE);
840 }
841#endif
Bram Moolenaar072412e2017-09-13 22:11:35 +0200842 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843}
844
Bram Moolenaar1fa8fdd2019-02-25 05:41:15 +0100845#if defined(FEAT_NETBEANS_INTG) || defined(FEAT_GUI)
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100846/*
847 * Prepare for updating one or more windows.
848 * Caller must check for "updating_screen" already set to avoid recursiveness.
849 */
850 static void
851update_prepare(void)
852{
853 cursor_off();
854 updating_screen = TRUE;
855#ifdef FEAT_GUI
856 /* Remove the cursor before starting to do anything, because scrolling may
857 * make it difficult to redraw the text under it. */
858 if (gui.in_use)
859 gui_undraw_cursor();
860#endif
861#ifdef FEAT_SEARCH_EXTRA
862 start_search_hl();
863#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +0200864#ifdef FEAT_TEXT_PROP
865 // Update popup_mask if needed.
Bram Moolenaar68acb412019-06-26 03:40:36 +0200866 may_update_popup_mask(must_redraw);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200867#endif
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100868}
869
870/*
871 * Finish updating one or more windows.
872 */
873 static void
874update_finish(void)
875{
Bram Moolenaar4c25bd72019-04-20 23:38:07 +0200876 if (redraw_cmdline || redraw_mode)
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100877 showmode();
878
879# ifdef FEAT_SEARCH_EXTRA
880 end_search_hl();
881# endif
882
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200883 after_updating_screen(TRUE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100884
885# ifdef FEAT_GUI
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100886 /* Redraw the cursor and update the scrollbars when all screen updating is
887 * done. */
888 if (gui.in_use)
889 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100890 out_flush_cursor(FALSE, FALSE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100891 gui_update_scrollbars(FALSE);
892 }
893# endif
894}
895#endif
896
Bram Moolenaar860cae12010-06-05 23:22:07 +0200897#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200898/*
899 * Return TRUE if the cursor line in window "wp" may be concealed, according
900 * to the 'concealcursor' option.
901 */
902 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100903conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200904{
905 int c;
906
907 if (*wp->w_p_cocu == NUL)
908 return FALSE;
909 if (get_real_state() & VISUAL)
910 c = 'v';
911 else if (State & INSERT)
912 c = 'i';
913 else if (State & NORMAL)
914 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200915 else if (State & CMDLINE)
916 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200917 else
918 return FALSE;
919 return vim_strchr(wp->w_p_cocu, c) != NULL;
920}
921
922/*
923 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
924 */
925 void
Bram Moolenaarb9464822018-05-10 15:09:49 +0200926conceal_check_cursor_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200927{
928 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
929 {
930 need_cursor_line_redraw = TRUE;
931 /* Need to recompute cursor column, e.g., when starting Visual mode
932 * without concealing. */
933 curs_columns(TRUE);
934 }
935}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936#endif
937
Bram Moolenaar113e1072019-01-20 15:30:40 +0100938#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100940update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941{
942 win_T *wp;
943 int doit = FALSE;
944
945# ifdef FEAT_FOLDING
946 win_foldinfo.fi_level = 0;
947# endif
948
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100949 // update/delete a specific sign
950 redraw_buf_line_later(buf, lnum);
951
952 // check if it resulted in the need to redraw a window
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 if (wp->w_redr_type != 0)
955 doit = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100957 /* Return when there is nothing to do, screen updating is already
Bram Moolenaar07920482017-08-01 20:53:30 +0200958 * happening (recursive call), messages on the screen or still starting up.
959 */
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100960 if (!doit || updating_screen
Bram Moolenaar07920482017-08-01 20:53:30 +0200961 || State == ASKMORE || State == HITRETURN
962 || msg_scrolled
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100963#ifdef FEAT_GUI
964 || gui.starting
965#endif
966 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 return;
968
969 /* update all windows that need updating */
970 update_prepare();
971
Bram Moolenaar29323592016-07-24 22:04:11 +0200972 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 {
974 if (wp->w_redr_type != 0)
975 win_update(wp);
976 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200977 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979
980 update_finish();
981}
982#endif
983
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200984/*
985 * Get 'wincolor' attribute for window "wp". If not set and "wp" is a popup
986 * window then get the "Pmenu" highlight attribute.
987 */
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200988 int
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200989get_wcr_attr(win_T *wp)
990{
991 int wcr_attr = 0;
992
993 if (*wp->w_p_wcr != NUL)
994 wcr_attr = syn_name2attr(wp->w_p_wcr);
995#ifdef FEAT_TEXT_PROP
Bram Moolenaarc363fe12019-08-04 18:13:46 +0200996 else if (WIN_IS_POPUP(wp))
Bram Moolenaar62a0cb42019-08-18 16:35:23 +0200997 {
998 if (wp->w_popup_flags & POPF_INFO)
999 wcr_attr = HL_ATTR(HLF_PSI); // PmenuSel
1000 else
1001 wcr_attr = HL_ATTR(HLF_PNI); // Pmenu
1002 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001003#endif
1004 return wcr_attr;
1005}
1006
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007#if defined(FEAT_GUI) || defined(PROTO)
1008/*
1009 * Update a single window, its status line and maybe the command line msg.
1010 * Used for the GUI scrollbar.
1011 */
1012 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001013updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014{
Bram Moolenaar19f990e2009-11-25 12:08:03 +00001015 /* return if already busy updating */
1016 if (updating_screen)
1017 return;
1018
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 update_prepare();
1020
1021#ifdef FEAT_CLIPBOARD
1022 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001023 if (clip_star.available && clip_isautosel_star())
1024 clip_update_selection(&clip_star);
1025 if (clip_plus.available && clip_isautosel_plus())
1026 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001028
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001030
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001031 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001032 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001033 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001034
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 if (wp->w_redr_status
1036# ifdef FEAT_CMDL_INFO
1037 || p_ru
1038# endif
1039# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001040 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041# endif
1042 )
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001043 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044
Bram Moolenaar988c4332019-06-02 14:12:11 +02001045#ifdef FEAT_TEXT_PROP
1046 // Display popup windows on top of everything.
Bram Moolenaara540f8a2019-06-14 19:23:57 +02001047 update_popups(win_update);
Bram Moolenaar988c4332019-06-02 14:12:11 +02001048#endif
1049
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 update_finish();
1051}
1052#endif
1053
1054/*
1055 * Update a single window.
1056 *
1057 * This may cause the windows below it also to be redrawn (when clearing the
1058 * screen or scrolling lines).
1059 *
1060 * How the window is redrawn depends on wp->w_redr_type. Each type also
1061 * implies the one below it.
1062 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001063 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1065 * INVERTED redraw the changed part of the Visual area
1066 * INVERTED_ALL redraw the whole Visual area
1067 * VALID 1. scroll up/down to adjust for a changed w_topline
1068 * 2. update lines at the top when scrolled down
1069 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001070 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 * b_mod_top and b_mod_bot.
1072 * - if wp->w_redraw_top non-zero, redraw lines between
1073 * wp->w_redraw_top and wp->w_redr_bot.
1074 * - continue redrawing when syntax status is invalid.
1075 * 4. if scrolled up, update lines at the bottom.
1076 * This results in three areas that may need updating:
1077 * top: from first row to top_end (when scrolled down)
1078 * mid: from mid_start to mid_end (update inversion or changed text)
1079 * bot: from bot_start to last row (when scrolled up)
1080 */
1081 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001082win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083{
1084 buf_T *buf = wp->w_buffer;
1085 int type;
1086 int top_end = 0; /* Below last row of the top area that needs
1087 updating. 0 when no top area updating. */
1088 int mid_start = 999;/* first row of the mid area that needs
1089 updating. 999 when no mid area updating. */
1090 int mid_end = 0; /* Below last row of the mid area that needs
1091 updating. 0 when no mid area updating. */
1092 int bot_start = 999;/* first row of the bot area that needs
1093 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 int scrolled_down = FALSE; /* TRUE when scrolled down when
1095 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarfbfb7572019-07-25 20:53:03 +02001097 int top_to_mod = FALSE; // redraw above mod_top
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098#endif
1099
1100 int row; /* current window row to display */
1101 linenr_T lnum; /* current buffer lnum to display */
1102 int idx; /* current index in w_lines[] */
1103 int srow; /* starting row of the current line */
1104
1105 int eof = FALSE; /* if TRUE, we hit the end of the file */
1106 int didline = FALSE; /* if TRUE, we finished the last line */
1107 int i;
1108 long j;
1109 static int recursive = FALSE; /* being called recursively */
1110 int old_botline = wp->w_botline;
1111#ifdef FEAT_FOLDING
1112 long fold_count;
1113#endif
1114#ifdef FEAT_SYN_HL
1115 /* remember what happened to the previous line, to know if
1116 * check_visual_highlight() can be used */
1117#define DID_NONE 1 /* didn't update a line */
1118#define DID_LINE 2 /* updated a normal line */
1119#define DID_FOLD 3 /* updated a folded line */
1120 int did_update = DID_NONE;
1121 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1122#endif
1123 linenr_T mod_top = 0;
1124 linenr_T mod_bot = 0;
1125#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1126 int save_got_int;
1127#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001128#ifdef SYN_TIME_LIMIT
1129 proftime_T syntax_tm;
1130#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131
1132 type = wp->w_redr_type;
1133
1134 if (type == NOT_VALID)
1135 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 wp->w_lines_valid = 0;
1138 }
1139
1140 /* Window is zero-height: nothing to draw. */
Bram Moolenaar415a6932017-12-05 20:31:07 +01001141 if (wp->w_height + WINBAR_HEIGHT(wp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 {
1143 wp->w_redr_type = 0;
1144 return;
1145 }
1146
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 /* Window is zero-width: Only need to draw the separator. */
1148 if (wp->w_width == 0)
1149 {
1150 /* draw the vertical separator right of this window */
1151 draw_vsep_win(wp, 0);
1152 wp->w_redr_type = 0;
1153 return;
1154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001156#ifdef FEAT_TERMINAL
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001157 // If this window contains a terminal, redraw works completely differently.
1158 if (term_do_update_window(wp))
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001159 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001160 term_update_window(wp);
Bram Moolenaar181ca992018-02-13 21:19:21 +01001161# ifdef FEAT_MENU
1162 /* Draw the window toolbar, if there is one. */
1163 if (winbar_height(wp) > 0)
1164 redraw_win_toolbar(wp);
1165# endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001166 wp->w_redr_type = 0;
1167 return;
1168 }
1169#endif
1170
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarbbca7732019-07-24 18:13:16 +02001172 init_search_hl(wp, &search_hl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173#endif
1174
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001175#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001176 /* Force redraw when width of 'number' or 'relativenumber' column
1177 * changes. */
1178 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001179 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001180 {
1181 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001182 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001183 }
1184 else
1185#endif
1186
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1188 {
1189 /*
1190 * When there are both inserted/deleted lines and specific lines to be
1191 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1192 * everything (only happens when redrawing is off for while).
1193 */
1194 type = NOT_VALID;
1195 }
1196 else
1197 {
1198 /*
1199 * Set mod_top to the first line that needs displaying because of
1200 * changes. Set mod_bot to the first line after the changes.
1201 */
1202 mod_top = wp->w_redraw_top;
1203 if (wp->w_redraw_bot != 0)
1204 mod_bot = wp->w_redraw_bot + 1;
1205 else
1206 mod_bot = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 if (buf->b_mod_set)
1208 {
1209 if (mod_top == 0 || mod_top > buf->b_mod_top)
1210 {
1211 mod_top = buf->b_mod_top;
1212#ifdef FEAT_SYN_HL
1213 /* Need to redraw lines above the change that may be included
1214 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001215 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001217 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 if (mod_top < 1)
1219 mod_top = 1;
1220 }
1221#endif
1222 }
1223 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1224 mod_bot = buf->b_mod_bot;
1225
1226#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarfbfb7572019-07-25 20:53:03 +02001227 // When 'hlsearch' is on and using a multi-line search pattern, a
1228 // change in one line may make the Search highlighting in a
1229 // previous line invalid. Simple solution: redraw all visible
1230 // lines above the change.
1231 // Same for a match pattern.
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001232 if (search_hl.rm.regprog != NULL
1233 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001235 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001236 {
Bram Moolenaarfbfb7572019-07-25 20:53:03 +02001237 matchitem_T *cur = wp->w_match_head;
1238
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001239 while (cur != NULL)
1240 {
1241 if (cur->match.regprog != NULL
1242 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001243 {
1244 top_to_mod = TRUE;
1245 break;
1246 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001247 cur = cur->next;
1248 }
1249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250#endif
1251 }
1252#ifdef FEAT_FOLDING
1253 if (mod_top != 0 && hasAnyFolding(wp))
1254 {
1255 linenr_T lnumt, lnumb;
1256
1257 /*
1258 * A change in a line can cause lines above it to become folded or
1259 * unfolded. Find the top most buffer line that may be affected.
1260 * If the line was previously folded and displayed, get the first
1261 * line of that fold. If the line is folded now, get the first
1262 * folded line. Use the minimum of these two.
1263 */
1264
1265 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1266 * the line below it. If there is no valid entry, use w_topline.
1267 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1268 * to this line. If there is no valid entry, use MAXLNUM. */
1269 lnumt = wp->w_topline;
1270 lnumb = MAXLNUM;
1271 for (i = 0; i < wp->w_lines_valid; ++i)
1272 if (wp->w_lines[i].wl_valid)
1273 {
1274 if (wp->w_lines[i].wl_lastlnum < mod_top)
1275 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1276 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1277 {
1278 lnumb = wp->w_lines[i].wl_lnum;
1279 /* When there is a fold column it might need updating
1280 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001281 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 ++lnumb;
1283 }
1284 }
1285
1286 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1287 if (mod_top > lnumt)
1288 mod_top = lnumt;
1289
1290 /* Now do the same for the bottom line (one above mod_bot). */
1291 --mod_bot;
1292 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1293 ++mod_bot;
1294 if (mod_bot < lnumb)
1295 mod_bot = lnumb;
1296 }
1297#endif
1298
1299 /* When a change starts above w_topline and the end is below
1300 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001301 * If the end of the change is above w_topline: do like no change was
1302 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 if (mod_top != 0 && mod_top < wp->w_topline)
1304 {
1305 if (mod_bot > wp->w_topline)
1306 mod_top = wp->w_topline;
1307#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001308 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 top_end = 1;
1310#endif
1311 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001312
1313 /* When line numbers are displayed need to redraw all lines below
1314 * inserted/deleted lines. */
1315 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1316 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 }
Bram Moolenaar895d9662019-01-31 21:57:21 +01001318 wp->w_redraw_top = 0; // reset for next time
1319 wp->w_redraw_bot = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320
1321 /*
1322 * When only displaying the lines at the top, set top_end. Used when
1323 * window has scrolled down for msg_scrolled.
1324 */
1325 if (type == REDRAW_TOP)
1326 {
1327 j = 0;
1328 for (i = 0; i < wp->w_lines_valid; ++i)
1329 {
1330 j += wp->w_lines[i].wl_size;
1331 if (j >= wp->w_upd_rows)
1332 {
1333 top_end = j;
1334 break;
1335 }
1336 }
1337 if (top_end == 0)
1338 /* not found (cannot happen?): redraw everything */
1339 type = NOT_VALID;
1340 else
1341 /* top area defined, the rest is VALID */
1342 type = VALID;
1343 }
1344
Bram Moolenaar367329b2007-08-30 11:53:22 +00001345 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001346 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1347 * non-zero and thus not FALSE) will indicate that screenclear() was not
1348 * called. */
1349 if (screen_cleared)
1350 screen_cleared = MAYBE;
1351
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 /*
1353 * If there are no changes on the screen that require a complete redraw,
1354 * handle three cases:
1355 * 1: we are off the top of the screen by a few lines: scroll down
1356 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1357 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1358 * w_lines[] that needs updating.
1359 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001360 if ((type == VALID || type == SOME_VALID
1361 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362#ifdef FEAT_DIFF
1363 && !wp->w_botfill && !wp->w_old_botfill
1364#endif
1365 )
1366 {
1367 if (mod_top != 0 && wp->w_topline == mod_top)
1368 {
1369 /*
1370 * w_topline is the first changed line, the scrolling will be done
1371 * further down.
1372 */
1373 }
1374 else if (wp->w_lines[0].wl_valid
1375 && (wp->w_topline < wp->w_lines[0].wl_lnum
1376#ifdef FEAT_DIFF
1377 || (wp->w_topline == wp->w_lines[0].wl_lnum
1378 && wp->w_topfill > wp->w_old_topfill)
1379#endif
1380 ))
1381 {
1382 /*
1383 * New topline is above old topline: May scroll down.
1384 */
1385#ifdef FEAT_FOLDING
1386 if (hasAnyFolding(wp))
1387 {
1388 linenr_T ln;
1389
1390 /* count the number of lines we are off, counting a sequence
1391 * of folded lines as one */
1392 j = 0;
1393 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1394 {
1395 ++j;
1396 if (j >= wp->w_height - 2)
1397 break;
1398 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1399 }
1400 }
1401 else
1402#endif
1403 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1404 if (j < wp->w_height - 2) /* not too far off */
1405 {
1406 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1407#ifdef FEAT_DIFF
1408 /* insert extra lines for previously invisible filler lines */
1409 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1410 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1411 - wp->w_old_topfill;
1412#endif
1413 if (i < wp->w_height - 2) /* less than a screen off */
1414 {
1415 /*
1416 * Try to insert the correct number of lines.
1417 * If not the last window, delete the lines at the bottom.
1418 * win_ins_lines may fail when the terminal can't do it.
1419 */
1420 if (i > 0)
1421 check_for_delay(FALSE);
1422 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1423 {
1424 if (wp->w_lines_valid != 0)
1425 {
1426 /* Need to update rows that are new, stop at the
1427 * first one that scrolled down. */
1428 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430
1431 /* Move the entries that were scrolled, disable
1432 * the entries for the lines to be redrawn. */
1433 if ((wp->w_lines_valid += j) > wp->w_height)
1434 wp->w_lines_valid = wp->w_height;
1435 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1436 wp->w_lines[idx] = wp->w_lines[idx - j];
1437 while (idx >= 0)
1438 wp->w_lines[idx--].wl_valid = FALSE;
1439 }
1440 }
1441 else
1442 mid_start = 0; /* redraw all lines */
1443 }
1444 else
1445 mid_start = 0; /* redraw all lines */
1446 }
1447 else
1448 mid_start = 0; /* redraw all lines */
1449 }
1450 else
1451 {
1452 /*
1453 * New topline is at or below old topline: May scroll up.
1454 * When topline didn't change, find first entry in w_lines[] that
1455 * needs updating.
1456 */
1457
1458 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1459 j = -1;
1460 row = 0;
1461 for (i = 0; i < wp->w_lines_valid; i++)
1462 {
1463 if (wp->w_lines[i].wl_valid
1464 && wp->w_lines[i].wl_lnum == wp->w_topline)
1465 {
1466 j = i;
1467 break;
1468 }
1469 row += wp->w_lines[i].wl_size;
1470 }
1471 if (j == -1)
1472 {
1473 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1474 * lines */
1475 mid_start = 0;
1476 }
1477 else
1478 {
1479 /*
1480 * Try to delete the correct number of lines.
1481 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1482 */
1483#ifdef FEAT_DIFF
1484 /* If the topline didn't change, delete old filler lines,
1485 * otherwise delete filler lines of the new topline... */
1486 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1487 row += wp->w_old_topfill;
1488 else
1489 row += diff_check_fill(wp, wp->w_topline);
1490 /* ... but don't delete new filler lines. */
1491 row -= wp->w_topfill;
1492#endif
1493 if (row > 0)
1494 {
1495 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001496 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1497 == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 bot_start = wp->w_height - row;
1499 else
1500 mid_start = 0; /* redraw all lines */
1501 }
1502 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1503 {
1504 /*
1505 * Skip the lines (below the deleted lines) that are still
1506 * valid and don't need redrawing. Copy their info
1507 * upwards, to compensate for the deleted lines. Set
1508 * bot_start to the first row that needs redrawing.
1509 */
1510 bot_start = 0;
1511 idx = 0;
1512 for (;;)
1513 {
1514 wp->w_lines[idx] = wp->w_lines[j];
1515 /* stop at line that didn't fit, unless it is still
1516 * valid (no lines deleted) */
1517 if (row > 0 && bot_start + row
1518 + (int)wp->w_lines[j].wl_size > wp->w_height)
1519 {
1520 wp->w_lines_valid = idx + 1;
1521 break;
1522 }
1523 bot_start += wp->w_lines[idx++].wl_size;
1524
1525 /* stop at the last valid entry in w_lines[].wl_size */
1526 if (++j >= wp->w_lines_valid)
1527 {
1528 wp->w_lines_valid = idx;
1529 break;
1530 }
1531 }
1532#ifdef FEAT_DIFF
1533 /* Correct the first entry for filler lines at the top
1534 * when it won't get updated below. */
1535 if (wp->w_p_diff && bot_start > 0)
1536 wp->w_lines[0].wl_size =
1537 plines_win_nofill(wp, wp->w_topline, TRUE)
1538 + wp->w_topfill;
1539#endif
1540 }
1541 }
1542 }
1543
1544 /* When starting redraw in the first line, redraw all lines. When
1545 * there is only one window it's probably faster to clear the screen
1546 * first. */
1547 if (mid_start == 0)
1548 {
1549 mid_end = wp->w_height;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001550 if (ONE_WINDOW && !WIN_IS_POPUP(wp))
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001551 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001552 /* Clear the screen when it was not done by win_del_lines() or
1553 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1554 * then. */
1555 if (screen_cleared != TRUE)
1556 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001557 /* The screen was cleared, redraw the tab pages line. */
1558 if (redraw_tabline)
1559 draw_tabline();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001562
1563 /* When win_del_lines() or win_ins_lines() caused the screen to be
1564 * cleared (only happens for the first window) or when screenclear()
1565 * was called directly above, "must_redraw" will have been set to
1566 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1567 if (screen_cleared == TRUE)
1568 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 }
1570 else
1571 {
1572 /* Not VALID or INVERTED: redraw all lines. */
1573 mid_start = 0;
1574 mid_end = wp->w_height;
1575 }
1576
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001577 if (type == SOME_VALID)
1578 {
1579 /* SOME_VALID: redraw all lines. */
1580 mid_start = 0;
1581 mid_end = wp->w_height;
1582 type = NOT_VALID;
1583 }
1584
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 /* check if we are updating or removing the inverted part */
1586 if ((VIsual_active && buf == curwin->w_buffer)
1587 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1588 {
1589 linenr_T from, to;
1590
1591 if (VIsual_active)
1592 {
1593 if (VIsual_active
1594 && (VIsual_mode != wp->w_old_visual_mode
1595 || type == INVERTED_ALL))
1596 {
1597 /*
1598 * If the type of Visual selection changed, redraw the whole
1599 * selection. Also when the ownership of the X selection is
1600 * gained or lost.
1601 */
1602 if (curwin->w_cursor.lnum < VIsual.lnum)
1603 {
1604 from = curwin->w_cursor.lnum;
1605 to = VIsual.lnum;
1606 }
1607 else
1608 {
1609 from = VIsual.lnum;
1610 to = curwin->w_cursor.lnum;
1611 }
1612 /* redraw more when the cursor moved as well */
1613 if (wp->w_old_cursor_lnum < from)
1614 from = wp->w_old_cursor_lnum;
1615 if (wp->w_old_cursor_lnum > to)
1616 to = wp->w_old_cursor_lnum;
1617 if (wp->w_old_visual_lnum < from)
1618 from = wp->w_old_visual_lnum;
1619 if (wp->w_old_visual_lnum > to)
1620 to = wp->w_old_visual_lnum;
1621 }
1622 else
1623 {
1624 /*
1625 * Find the line numbers that need to be updated: The lines
1626 * between the old cursor position and the current cursor
1627 * position. Also check if the Visual position changed.
1628 */
1629 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1630 {
1631 from = curwin->w_cursor.lnum;
1632 to = wp->w_old_cursor_lnum;
1633 }
1634 else
1635 {
1636 from = wp->w_old_cursor_lnum;
1637 to = curwin->w_cursor.lnum;
1638 if (from == 0) /* Visual mode just started */
1639 from = to;
1640 }
1641
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001642 if (VIsual.lnum != wp->w_old_visual_lnum
1643 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 {
1645 if (wp->w_old_visual_lnum < from
1646 && wp->w_old_visual_lnum != 0)
1647 from = wp->w_old_visual_lnum;
1648 if (wp->w_old_visual_lnum > to)
1649 to = wp->w_old_visual_lnum;
1650 if (VIsual.lnum < from)
1651 from = VIsual.lnum;
1652 if (VIsual.lnum > to)
1653 to = VIsual.lnum;
1654 }
1655 }
1656
1657 /*
1658 * If in block mode and changed column or curwin->w_curswant:
1659 * update all lines.
1660 * First compute the actual start and end column.
1661 */
1662 if (VIsual_mode == Ctrl_V)
1663 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001664 colnr_T fromc, toc;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001665#if defined(FEAT_LINEBREAK)
Bram Moolenaar404406a2014-10-09 13:24:43 +02001666 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667
Bram Moolenaar404406a2014-10-09 13:24:43 +02001668 if (curwin->w_p_lbr)
1669 ve_flags = VE_ALL;
1670#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001672#if defined(FEAT_LINEBREAK)
Bram Moolenaar404406a2014-10-09 13:24:43 +02001673 ve_flags = save_ve_flags;
1674#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 ++toc;
1676 if (curwin->w_curswant == MAXCOL)
1677 toc = MAXCOL;
1678
1679 if (fromc != wp->w_old_cursor_fcol
1680 || toc != wp->w_old_cursor_lcol)
1681 {
1682 if (from > VIsual.lnum)
1683 from = VIsual.lnum;
1684 if (to < VIsual.lnum)
1685 to = VIsual.lnum;
1686 }
1687 wp->w_old_cursor_fcol = fromc;
1688 wp->w_old_cursor_lcol = toc;
1689 }
1690 }
1691 else
1692 {
1693 /* Use the line numbers of the old Visual area. */
1694 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1695 {
1696 from = wp->w_old_cursor_lnum;
1697 to = wp->w_old_visual_lnum;
1698 }
1699 else
1700 {
1701 from = wp->w_old_visual_lnum;
1702 to = wp->w_old_cursor_lnum;
1703 }
1704 }
1705
1706 /*
1707 * There is no need to update lines above the top of the window.
1708 */
1709 if (from < wp->w_topline)
1710 from = wp->w_topline;
1711
1712 /*
1713 * If we know the value of w_botline, use it to restrict the update to
1714 * the lines that are visible in the window.
1715 */
1716 if (wp->w_valid & VALID_BOTLINE)
1717 {
1718 if (from >= wp->w_botline)
1719 from = wp->w_botline - 1;
1720 if (to >= wp->w_botline)
1721 to = wp->w_botline - 1;
1722 }
1723
1724 /*
1725 * Find the minimal part to be updated.
1726 * Watch out for scrolling that made entries in w_lines[] invalid.
1727 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1728 * top_end; need to redraw from top_end to the "to" line.
1729 * A middle mouse click with a Visual selection may change the text
1730 * above the Visual area and reset wl_valid, do count these for
1731 * mid_end (in srow).
1732 */
1733 if (mid_start > 0)
1734 {
1735 lnum = wp->w_topline;
1736 idx = 0;
1737 srow = 0;
1738 if (scrolled_down)
1739 mid_start = top_end;
1740 else
1741 mid_start = 0;
1742 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1743 {
1744 if (wp->w_lines[idx].wl_valid)
1745 mid_start += wp->w_lines[idx].wl_size;
1746 else if (!scrolled_down)
1747 srow += wp->w_lines[idx].wl_size;
1748 ++idx;
1749# ifdef FEAT_FOLDING
1750 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1751 lnum = wp->w_lines[idx].wl_lnum;
1752 else
1753# endif
1754 ++lnum;
1755 }
1756 srow += mid_start;
1757 mid_end = wp->w_height;
1758 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1759 {
1760 if (wp->w_lines[idx].wl_valid
1761 && wp->w_lines[idx].wl_lnum >= to + 1)
1762 {
1763 /* Only update until first row of this line */
1764 mid_end = srow;
1765 break;
1766 }
1767 srow += wp->w_lines[idx].wl_size;
1768 }
1769 }
1770 }
1771
1772 if (VIsual_active && buf == curwin->w_buffer)
1773 {
1774 wp->w_old_visual_mode = VIsual_mode;
1775 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1776 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001777 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 wp->w_old_curswant = curwin->w_curswant;
1779 }
1780 else
1781 {
1782 wp->w_old_visual_mode = 0;
1783 wp->w_old_cursor_lnum = 0;
1784 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001785 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787
1788#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1789 /* reset got_int, otherwise regexp won't work */
1790 save_got_int = got_int;
1791 got_int = 0;
1792#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001793#ifdef SYN_TIME_LIMIT
1794 /* Set the time limit to 'redrawtime'. */
1795 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02001796 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001797#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798#ifdef FEAT_FOLDING
1799 win_foldinfo.fi_level = 0;
1800#endif
1801
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001802#ifdef FEAT_MENU
1803 /*
1804 * Draw the window toolbar, if there is one.
1805 * TODO: only when needed.
1806 */
1807 if (winbar_height(wp) > 0)
1808 redraw_win_toolbar(wp);
1809#endif
1810
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 /*
1812 * Update all the window rows.
1813 */
1814 idx = 0; /* first entry in w_lines[].wl_size */
1815 row = 0;
1816 srow = 0;
1817 lnum = wp->w_topline; /* first line shown in window */
1818 for (;;)
1819 {
1820 /* stop updating when reached the end of the window (check for _past_
1821 * the end of the window is at the end of the loop) */
1822 if (row == wp->w_height)
1823 {
1824 didline = TRUE;
1825 break;
1826 }
1827
1828 /* stop updating when hit the end of the file */
1829 if (lnum > buf->b_ml.ml_line_count)
1830 {
1831 eof = TRUE;
1832 break;
1833 }
1834
1835 /* Remember the starting row of the line that is going to be dealt
1836 * with. It is used further down when the line doesn't fit. */
1837 srow = row;
1838
1839 /*
1840 * Update a line when it is in an area that needs updating, when it
1841 * has changes or w_lines[idx] is invalid.
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02001842 * "bot_start" may be halfway a wrapped line after using
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 * win_del_lines(), check if the current line includes it.
1844 * When syntax folding is being used, the saved syntax states will
1845 * already have been updated, we can't see where the syntax state is
1846 * the same again, just update until the end of the window.
1847 */
1848 if (row < top_end
1849 || (row >= mid_start && row < mid_end)
1850#ifdef FEAT_SEARCH_EXTRA
1851 || top_to_mod
1852#endif
1853 || idx >= wp->w_lines_valid
1854 || (row + wp->w_lines[idx].wl_size > bot_start)
1855 || (mod_top != 0
1856 && (lnum == mod_top
1857 || (lnum >= mod_top
1858 && (lnum < mod_bot
1859#ifdef FEAT_SYN_HL
1860 || did_update == DID_FOLD
1861 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001862 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 && (
1864# ifdef FEAT_FOLDING
1865 (foldmethodIsSyntax(wp)
1866 && hasAnyFolding(wp)) ||
1867# endif
1868 syntax_check_changed(lnum)))
1869#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001870#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001871 /* match in fixed position might need redraw
1872 * if lines were inserted or deleted */
1873 || (wp->w_match_head != NULL
1874 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001875#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 )))))
1877 {
1878#ifdef FEAT_SEARCH_EXTRA
1879 if (lnum == mod_top)
1880 top_to_mod = FALSE;
1881#endif
1882
1883 /*
1884 * When at start of changed lines: May scroll following lines
1885 * up or down to minimize redrawing.
1886 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001887 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 */
1889 if (lnum == mod_top
1890 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001891 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 {
1893 int old_rows = 0;
1894 int new_rows = 0;
1895 int xtra_rows;
1896 linenr_T l;
1897
1898 /* Count the old number of window rows, using w_lines[], which
1899 * should still contain the sizes for the lines as they are
1900 * currently displayed. */
1901 for (i = idx; i < wp->w_lines_valid; ++i)
1902 {
1903 /* Only valid lines have a meaningful wl_lnum. Invalid
1904 * lines are part of the changed area. */
1905 if (wp->w_lines[i].wl_valid
1906 && wp->w_lines[i].wl_lnum == mod_bot)
1907 break;
1908 old_rows += wp->w_lines[i].wl_size;
1909#ifdef FEAT_FOLDING
1910 if (wp->w_lines[i].wl_valid
1911 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1912 {
1913 /* Must have found the last valid entry above mod_bot.
1914 * Add following invalid entries. */
1915 ++i;
1916 while (i < wp->w_lines_valid
1917 && !wp->w_lines[i].wl_valid)
1918 old_rows += wp->w_lines[i++].wl_size;
1919 break;
1920 }
1921#endif
1922 }
1923
1924 if (i >= wp->w_lines_valid)
1925 {
1926 /* We can't find a valid line below the changed lines,
1927 * need to redraw until the end of the window.
1928 * Inserting/deleting lines has no use. */
1929 bot_start = 0;
1930 }
1931 else
1932 {
1933 /* Able to count old number of rows: Count new window
1934 * rows, and may insert/delete lines */
1935 j = idx;
1936 for (l = lnum; l < mod_bot; ++l)
1937 {
1938#ifdef FEAT_FOLDING
1939 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1940 ++new_rows;
1941 else
1942#endif
1943#ifdef FEAT_DIFF
1944 if (l == wp->w_topline)
1945 new_rows += plines_win_nofill(wp, l, TRUE)
1946 + wp->w_topfill;
1947 else
1948#endif
1949 new_rows += plines_win(wp, l, TRUE);
1950 ++j;
1951 if (new_rows > wp->w_height - row - 2)
1952 {
1953 /* it's getting too much, must redraw the rest */
1954 new_rows = 9999;
1955 break;
1956 }
1957 }
1958 xtra_rows = new_rows - old_rows;
1959 if (xtra_rows < 0)
1960 {
1961 /* May scroll text up. If there is not enough
1962 * remaining text or scrolling fails, must redraw the
1963 * rest. If scrolling works, must redraw the text
1964 * below the scrolled text. */
1965 if (row - xtra_rows >= wp->w_height - 2)
1966 mod_bot = MAXLNUM;
1967 else
1968 {
1969 check_for_delay(FALSE);
1970 if (win_del_lines(wp, row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001971 -xtra_rows, FALSE, FALSE, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 mod_bot = MAXLNUM;
1973 else
1974 bot_start = wp->w_height + xtra_rows;
1975 }
1976 }
1977 else if (xtra_rows > 0)
1978 {
1979 /* May scroll text down. If there is not enough
1980 * remaining text of scrolling fails, must redraw the
1981 * rest. */
1982 if (row + xtra_rows >= wp->w_height - 2)
1983 mod_bot = MAXLNUM;
1984 else
1985 {
1986 check_for_delay(FALSE);
1987 if (win_ins_lines(wp, row + old_rows,
1988 xtra_rows, FALSE, FALSE) == FAIL)
1989 mod_bot = MAXLNUM;
1990 else if (top_end > row + old_rows)
1991 /* Scrolled the part at the top that requires
1992 * updating down. */
1993 top_end += xtra_rows;
1994 }
1995 }
1996
1997 /* When not updating the rest, may need to move w_lines[]
1998 * entries. */
1999 if (mod_bot != MAXLNUM && i != j)
2000 {
2001 if (j < i)
2002 {
2003 int x = row + new_rows;
2004
2005 /* move entries in w_lines[] upwards */
2006 for (;;)
2007 {
2008 /* stop at last valid entry in w_lines[] */
2009 if (i >= wp->w_lines_valid)
2010 {
2011 wp->w_lines_valid = j;
2012 break;
2013 }
2014 wp->w_lines[j] = wp->w_lines[i];
2015 /* stop at a line that won't fit */
2016 if (x + (int)wp->w_lines[j].wl_size
2017 > wp->w_height)
2018 {
2019 wp->w_lines_valid = j + 1;
2020 break;
2021 }
2022 x += wp->w_lines[j++].wl_size;
2023 ++i;
2024 }
2025 if (bot_start > x)
2026 bot_start = x;
2027 }
2028 else /* j > i */
2029 {
2030 /* move entries in w_lines[] downwards */
2031 j -= i;
2032 wp->w_lines_valid += j;
2033 if (wp->w_lines_valid > wp->w_height)
2034 wp->w_lines_valid = wp->w_height;
2035 for (i = wp->w_lines_valid; i - j >= idx; --i)
2036 wp->w_lines[i] = wp->w_lines[i - j];
2037
2038 /* The w_lines[] entries for inserted lines are
2039 * now invalid, but wl_size may be used above.
2040 * Reset to zero. */
2041 while (i >= idx)
2042 {
2043 wp->w_lines[i].wl_size = 0;
2044 wp->w_lines[i--].wl_valid = FALSE;
2045 }
2046 }
2047 }
2048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 }
2050
2051#ifdef FEAT_FOLDING
2052 /*
2053 * When lines are folded, display one line for all of them.
2054 * Otherwise, display normally (can be several display lines when
2055 * 'wrap' is on).
2056 */
2057 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2058 if (fold_count != 0)
2059 {
2060 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2061 ++row;
2062 --fold_count;
2063 wp->w_lines[idx].wl_folded = TRUE;
2064 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2065# ifdef FEAT_SYN_HL
2066 did_update = DID_FOLD;
2067# endif
2068 }
2069 else
2070#endif
2071 if (idx < wp->w_lines_valid
2072 && wp->w_lines[idx].wl_valid
2073 && wp->w_lines[idx].wl_lnum == lnum
2074 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002075 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002076 && !WIN_IS_POPUP(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 && srow + wp->w_lines[idx].wl_size > wp->w_height
2078#ifdef FEAT_DIFF
2079 && diff_check_fill(wp, lnum) == 0
2080#endif
2081 )
2082 {
2083 /* This line is not going to fit. Don't draw anything here,
2084 * will draw "@ " lines below. */
2085 row = wp->w_height + 1;
2086 }
2087 else
2088 {
2089#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarbbca7732019-07-24 18:13:16 +02002090 prepare_search_hl(wp, &search_hl, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091#endif
2092#ifdef FEAT_SYN_HL
2093 /* Let the syntax stuff know we skipped a few lines. */
2094 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002095 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 syntax_end_parsing(syntax_last_parsed + 1);
2097#endif
2098
2099 /*
2100 * Display one line.
2101 */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002102 row = win_line(wp, lnum, srow, wp->w_height,
2103 mod_top == 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104
2105#ifdef FEAT_FOLDING
2106 wp->w_lines[idx].wl_folded = FALSE;
2107 wp->w_lines[idx].wl_lastlnum = lnum;
2108#endif
2109#ifdef FEAT_SYN_HL
2110 did_update = DID_LINE;
2111 syntax_last_parsed = lnum;
2112#endif
2113 }
2114
2115 wp->w_lines[idx].wl_lnum = lnum;
2116 wp->w_lines[idx].wl_valid = TRUE;
Bram Moolenaar0e19fc02017-10-28 14:45:16 +02002117
2118 /* Past end of the window or end of the screen. Note that after
2119 * resizing wp->w_height may be end up too big. That's a problem
2120 * elsewhere, but prevent a crash here. */
2121 if (row > wp->w_height || row + wp->w_winrow >= Rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 {
2123 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002124 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2126 ++idx;
2127 break;
2128 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002129 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 wp->w_lines[idx].wl_size = row - srow;
2131 ++idx;
2132#ifdef FEAT_FOLDING
2133 lnum += fold_count + 1;
2134#else
2135 ++lnum;
2136#endif
2137 }
2138 else
2139 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002140 if (wp->w_p_rnu)
2141 {
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002142#ifdef FEAT_FOLDING
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002143 // 'relativenumber' set: The text doesn't need to be drawn, but
2144 // the number column nearly always does.
Bram Moolenaar7701f302018-10-02 21:20:32 +02002145 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2146 if (fold_count != 0)
Bram Moolenaar7701f302018-10-02 21:20:32 +02002147 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
Bram Moolenaar7701f302018-10-02 21:20:32 +02002148 else
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002149#endif
Bram Moolenaar7701f302018-10-02 21:20:32 +02002150 (void)win_line(wp, lnum, srow, wp->w_height, TRUE, TRUE);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002151 }
2152
2153 // This line does not need to be drawn, advance to the next one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 row += wp->w_lines[idx++].wl_size;
2155 if (row > wp->w_height) /* past end of screen */
2156 break;
2157#ifdef FEAT_FOLDING
2158 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2159#else
2160 ++lnum;
2161#endif
2162#ifdef FEAT_SYN_HL
2163 did_update = DID_NONE;
2164#endif
2165 }
2166
2167 if (lnum > buf->b_ml.ml_line_count)
2168 {
2169 eof = TRUE;
2170 break;
2171 }
2172 }
2173 /*
2174 * End of loop over all window lines.
2175 */
2176
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002177#ifdef FEAT_VTP
2178 /* Rewrite the character at the end of the screen line. */
2179 if (use_vtp())
2180 {
2181 int i;
2182
2183 for (i = 0; i < Rows; ++i)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002184 if (enc_utf8)
2185 if ((*mb_off2cells)(LineOffset[i] + Columns - 2,
2186 LineOffset[i] + screen_Columns) > 1)
2187 screen_draw_rectangle(i, Columns - 2, 1, 2, FALSE);
2188 else
2189 screen_draw_rectangle(i, Columns - 1, 1, 1, FALSE);
2190 else
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002191 screen_char(LineOffset[i] + Columns - 1, i, Columns - 1);
2192 }
2193#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194
2195 if (idx > wp->w_lines_valid)
2196 wp->w_lines_valid = idx;
2197
2198#ifdef FEAT_SYN_HL
2199 /*
2200 * Let the syntax stuff know we stop parsing here.
2201 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002202 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 syntax_end_parsing(syntax_last_parsed + 1);
2204#endif
2205
2206 /*
2207 * If we didn't hit the end of the file, and we didn't finish the last
2208 * line we were working on, then the line didn't fit.
2209 */
2210 wp->w_empty_rows = 0;
2211#ifdef FEAT_DIFF
2212 wp->w_filler_rows = 0;
2213#endif
2214 if (!eof && !didline)
2215 {
2216 if (lnum == wp->w_topline)
2217 {
2218 /*
2219 * Single line that does not fit!
2220 * Don't overwrite it, it can be edited.
2221 */
2222 wp->w_botline = lnum + 1;
2223 }
2224#ifdef FEAT_DIFF
2225 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2226 {
2227 /* Window ends in filler lines. */
2228 wp->w_botline = lnum;
2229 wp->w_filler_rows = wp->w_height - srow;
2230 }
2231#endif
Bram Moolenaar17146962019-05-30 00:12:11 +02002232#ifdef FEAT_TEXT_PROP
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002233 else if (WIN_IS_POPUP(wp))
Bram Moolenaar17146962019-05-30 00:12:11 +02002234 {
2235 // popup line that doesn't fit is left as-is
2236 wp->w_botline = lnum;
2237 }
2238#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002239 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2240 {
2241 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2242
2243 /*
2244 * Last line isn't finished: Display "@@@" in the last screen line.
2245 */
Bram Moolenaar53f81742017-09-22 14:35:51 +02002246 screen_puts_len((char_u *)"@@", 2, scr_row, wp->w_wincol,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002247 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002248 screen_fill(scr_row, scr_row + 1,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002249 (int)wp->w_wincol + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002250 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002251 set_empty_rows(wp, srow);
2252 wp->w_botline = lnum;
2253 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2255 {
2256 /*
2257 * Last line isn't finished: Display "@@@" at the end.
2258 */
2259 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2260 W_WINROW(wp) + wp->w_height,
2261 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002262 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 set_empty_rows(wp, srow);
2264 wp->w_botline = lnum;
2265 }
2266 else
2267 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002268 win_draw_end(wp, '@', ' ', TRUE, srow, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 wp->w_botline = lnum;
2270 }
2271 }
2272 else
2273 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 if (eof) /* we hit the end of the file */
2276 {
2277 wp->w_botline = buf->b_ml.ml_line_count + 1;
2278#ifdef FEAT_DIFF
2279 j = diff_check_fill(wp, wp->w_botline);
2280 if (j > 0 && !wp->w_botfill)
2281 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002282 // Display filler lines at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 if (char2cells(fill_diff) > 1)
2284 i = '-';
2285 else
2286 i = fill_diff;
2287 if (row + j > wp->w_height)
2288 j = wp->w_height - row;
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002289 win_draw_end(wp, i, i, TRUE, row, row + (int)j, HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 row += j;
2291 }
2292#endif
2293 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002294 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 wp->w_botline = lnum;
2296
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002297 // Make sure the rest of the screen is blank
2298 // put '~'s on rows that aren't part of the file.
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002299 win_draw_end(wp, WIN_IS_POPUP(wp) ? ' ' : '~',
2300 ' ', FALSE, row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 }
2302
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002303#ifdef SYN_TIME_LIMIT
2304 syn_set_timeout(NULL);
2305#endif
2306
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 /* Reset the type of redrawing required, the window has been updated. */
2308 wp->w_redr_type = 0;
2309#ifdef FEAT_DIFF
2310 wp->w_old_topfill = wp->w_topfill;
2311 wp->w_old_botfill = wp->w_botfill;
2312#endif
2313
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002314 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 {
2316 /*
2317 * There is a trick with w_botline. If we invalidate it on each
2318 * change that might modify it, this will cause a lot of expensive
2319 * calls to plines() in update_topline() each time. Therefore the
2320 * value of w_botline is often approximated, and this value is used to
2321 * compute the value of w_topline. If the value of w_botline was
2322 * wrong, check that the value of w_topline is correct (cursor is on
2323 * the visible part of the text). If it's not, we need to redraw
2324 * again. Mostly this just means scrolling up a few lines, so it
2325 * doesn't look too bad. Only do this for the current window (where
2326 * changes are relevant).
2327 */
2328 wp->w_valid |= VALID_BOTLINE;
2329 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2330 {
2331 recursive = TRUE;
2332 curwin->w_valid &= ~VALID_TOPLINE;
2333 update_topline(); /* may invalidate w_botline again */
2334 if (must_redraw != 0)
2335 {
2336 /* Don't update for changes in buffer again. */
2337 i = curbuf->b_mod_set;
2338 curbuf->b_mod_set = FALSE;
2339 win_update(curwin);
2340 must_redraw = 0;
2341 curbuf->b_mod_set = i;
2342 }
2343 recursive = FALSE;
2344 }
2345 }
2346
2347#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2348 /* restore got_int, unless CTRL-C was hit while redrawing */
2349 if (!got_int)
2350 got_int = save_got_int;
2351#endif
2352}
2353
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354/*
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002355 * Call screen_fill() with the columns adjusted for 'rightleft' if needed.
2356 * Return the new offset.
2357 */
2358 static int
2359screen_fill_end(
2360 win_T *wp,
2361 int c1,
2362 int c2,
2363 int off,
2364 int width,
2365 int row,
2366 int endrow,
2367 int attr)
2368{
2369 int nn = off + width;
2370
2371 if (nn > wp->w_width)
2372 nn = wp->w_width;
2373#ifdef FEAT_RIGHTLEFT
2374 if (wp->w_p_rl)
2375 {
2376 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2377 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - off,
2378 c1, c2, attr);
2379 }
2380 else
2381#endif
2382 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2383 wp->w_wincol + off, (int)wp->w_wincol + nn,
2384 c1, c2, attr);
2385 return nn;
2386}
2387
2388/*
2389 * Clear lines near the end the window and mark the unused lines with "c1".
2390 * use "c2" as the filler character.
2391 * When "draw_margin" is TRUE then draw the sign, fold and number columns.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 */
2393 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002394win_draw_end(
2395 win_T *wp,
2396 int c1,
2397 int c2,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002398 int draw_margin,
Bram Moolenaar05540972016-01-30 20:31:25 +01002399 int row,
2400 int endrow,
2401 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 int n = 0;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002404 int attr = HL_ATTR(hl);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002405 int wcr_attr = get_wcr_attr(wp);
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002406
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002407 attr = hl_combine_attr(wcr_attr, attr);
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002408
2409 if (draw_margin)
2410 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002411#ifdef FEAT_FOLDING
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002412 int fdc = compute_foldcolumn(wp, 0);
2413
2414 if (fdc > 0)
2415 // draw the fold column
2416 n = screen_fill_end(wp, ' ', ' ', n, fdc,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002417 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_FC)));
Bram Moolenaar1c934292015-01-27 16:39:29 +01002418#endif
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002419#ifdef FEAT_SIGNS
2420 if (signcolumn_on(wp))
2421 // draw the sign column
2422 n = screen_fill_end(wp, ' ', ' ', n, 2,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002423 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_SC)));
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002424#endif
2425 if ((wp->w_p_nu || wp->w_p_rnu)
2426 && vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
2427 // draw the number column
2428 n = screen_fill_end(wp, ' ', ' ', n, number_width(wp) + 1,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002429 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_N)));
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431
2432#ifdef FEAT_RIGHTLEFT
2433 if (wp->w_p_rl)
2434 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002436 wp->w_wincol, W_ENDCOL(wp) - 1 - n,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002437 c2, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002439 W_ENDCOL(wp) - 1 - n, W_ENDCOL(wp) - n,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002440 c1, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 }
2442 else
2443#endif
2444 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002446 wp->w_wincol + n, (int)W_ENDCOL(wp),
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002447 c1, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 }
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002449
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 set_empty_rows(wp, row);
2451}
2452
Bram Moolenaar1a384422010-07-14 19:53:30 +02002453#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02002454/*
2455 * Advance **color_cols and return TRUE when there are columns to draw.
2456 */
2457 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002458advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002459{
2460 while (**color_cols >= 0 && vcol > **color_cols)
2461 ++*color_cols;
2462 return (**color_cols >= 0);
2463}
2464#endif
2465
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002466#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
2467/*
2468 * Copy "text" to ScreenLines using "attr".
2469 * Returns the next screen column.
2470 */
2471 static int
2472text_to_screenline(win_T *wp, char_u *text, int col)
2473{
2474 int off = (int)(current_ScreenLine - ScreenLines);
2475
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002476 if (has_mbyte)
2477 {
2478 int cells;
2479 int u8c, u8cc[MAX_MCO];
2480 int i;
2481 int idx;
2482 int c_len;
2483 char_u *p;
2484# ifdef FEAT_ARABIC
2485 int prev_c = 0; /* previous Arabic character */
2486 int prev_c1 = 0; /* first composing char for prev_c */
2487# endif
2488
2489# ifdef FEAT_RIGHTLEFT
2490 if (wp->w_p_rl)
2491 idx = off;
2492 else
2493# endif
2494 idx = off + col;
2495
2496 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2497 for (p = text; *p != NUL; )
2498 {
2499 cells = (*mb_ptr2cells)(p);
2500 c_len = (*mb_ptr2len)(p);
Bram Moolenaar02631462017-09-22 15:20:32 +02002501 if (col + cells > wp->w_width
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002502# ifdef FEAT_RIGHTLEFT
2503 - (wp->w_p_rl ? col : 0)
2504# endif
2505 )
2506 break;
2507 ScreenLines[idx] = *p;
2508 if (enc_utf8)
2509 {
2510 u8c = utfc_ptr2char(p, u8cc);
2511 if (*p < 0x80 && u8cc[0] == 0)
2512 {
2513 ScreenLinesUC[idx] = 0;
2514#ifdef FEAT_ARABIC
2515 prev_c = u8c;
2516#endif
2517 }
2518 else
2519 {
2520#ifdef FEAT_ARABIC
2521 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2522 {
2523 /* Do Arabic shaping. */
2524 int pc, pc1, nc;
2525 int pcc[MAX_MCO];
2526 int firstbyte = *p;
2527
2528 /* The idea of what is the previous and next
2529 * character depends on 'rightleft'. */
2530 if (wp->w_p_rl)
2531 {
2532 pc = prev_c;
2533 pc1 = prev_c1;
2534 nc = utf_ptr2char(p + c_len);
2535 prev_c1 = u8cc[0];
2536 }
2537 else
2538 {
2539 pc = utfc_ptr2char(p + c_len, pcc);
2540 nc = prev_c;
2541 pc1 = pcc[0];
2542 }
2543 prev_c = u8c;
2544
2545 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
2546 pc, pc1, nc);
2547 ScreenLines[idx] = firstbyte;
2548 }
2549 else
2550 prev_c = u8c;
2551#endif
2552 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar9ba61172019-01-24 18:20:17 +01002553 ScreenLinesUC[idx] = u8c;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002554 for (i = 0; i < Screen_mco; ++i)
2555 {
2556 ScreenLinesC[i][idx] = u8cc[i];
2557 if (u8cc[i] == 0)
2558 break;
2559 }
2560 }
2561 if (cells > 1)
2562 ScreenLines[idx + 1] = 0;
2563 }
2564 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2565 /* double-byte single width character */
2566 ScreenLines2[idx] = p[1];
2567 else if (cells > 1)
2568 /* double-width character */
2569 ScreenLines[idx + 1] = p[1];
2570 col += cells;
2571 idx += cells;
2572 p += c_len;
2573 }
2574 }
2575 else
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002576 {
2577 int len = (int)STRLEN(text);
2578
Bram Moolenaar02631462017-09-22 15:20:32 +02002579 if (len > wp->w_width - col)
2580 len = wp->w_width - col;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002581 if (len > 0)
2582 {
2583#ifdef FEAT_RIGHTLEFT
2584 if (wp->w_p_rl)
Bram Moolenaarc6663882019-02-22 19:14:54 +01002585 mch_memmove(current_ScreenLine, text, len);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002586 else
2587#endif
Bram Moolenaarc6663882019-02-22 19:14:54 +01002588 mch_memmove(current_ScreenLine + col, text, len);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002589 col += len;
2590 }
2591 }
2592 return col;
2593}
2594#endif
2595
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596#ifdef FEAT_FOLDING
2597/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002598 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2599 * space is available for window "wp", minus "col".
2600 */
2601 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002602compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002603{
2604 int fdc = wp->w_p_fdc;
2605 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
Bram Moolenaar02631462017-09-22 15:20:32 +02002606 int wwidth = wp->w_width;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002607
2608 if (fdc > wwidth - (col + wmw))
2609 fdc = wwidth - (col + wmw);
2610 return fdc;
2611}
2612
2613/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 * Display one folded line.
2615 */
2616 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002617fold_line(
2618 win_T *wp,
2619 long fold_count,
2620 foldinfo_T *foldinfo,
2621 linenr_T lnum,
2622 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002624 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 pos_T *top, *bot;
2626 linenr_T lnume = lnum + fold_count - 1;
2627 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002628 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 int col;
2631 int txtcol;
2632 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002633 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634
2635 /* Build the fold line:
2636 * 1. Add the cmdwin_type for the command-line window
2637 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002638 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 * 4. Compose the text
2640 * 5. Add the text
2641 * 6. set highlighting for the Visual area an other text
2642 */
2643 col = 0;
2644
2645 /*
2646 * 1. Add the cmdwin_type for the command-line window
2647 * Ignores 'rightleft', this window is never right-left.
2648 */
2649#ifdef FEAT_CMDWIN
2650 if (cmdwin_type != 0 && wp == curwin)
2651 {
2652 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002653 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 if (enc_utf8)
2655 ScreenLinesUC[off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 ++col;
2657 }
2658#endif
2659
2660 /*
2661 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002662 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002664 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 if (fdc > 0)
2666 {
2667 fill_foldcolumn(buf, wp, TRUE, lnum);
2668#ifdef FEAT_RIGHTLEFT
2669 if (wp->w_p_rl)
2670 {
2671 int i;
2672
Bram Moolenaar02631462017-09-22 15:20:32 +02002673 copy_text_attr(off + wp->w_width - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002674 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 /* reverse the fold column */
2676 for (i = 0; i < fdc; ++i)
Bram Moolenaar02631462017-09-22 15:20:32 +02002677 ScreenLines[off + wp->w_width - i - 1 - col] = buf[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 }
2679 else
2680#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002681 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 col += fdc;
2683 }
2684
2685#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6f470022018-04-10 18:47:20 +02002686# define RL_MEMSET(p, v, l) \
2687 do { \
2688 if (wp->w_p_rl) \
2689 for (ri = 0; ri < l; ++ri) \
2690 ScreenAttrs[off + (wp->w_width - (p) - (l)) + ri] = v; \
2691 else \
2692 for (ri = 0; ri < l; ++ri) \
2693 ScreenAttrs[off + (p) + ri] = v; \
2694 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695#else
Bram Moolenaar6f470022018-04-10 18:47:20 +02002696# define RL_MEMSET(p, v, l) \
2697 do { \
2698 for (ri = 0; ri < l; ++ri) \
2699 ScreenAttrs[off + (p) + ri] = v; \
2700 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701#endif
2702
Bram Moolenaar64486672010-05-16 15:46:46 +02002703 /* Set all attributes of the 'number' or 'relativenumber' column and the
2704 * text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002705 RL_MEMSET(col, HL_ATTR(HLF_FL), wp->w_width - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706
2707#ifdef FEAT_SIGNS
2708 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002709 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002711 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 if (len > 0)
2713 {
2714 if (len > 2)
2715 len = 2;
2716# ifdef FEAT_RIGHTLEFT
2717 if (wp->w_p_rl)
2718 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002719 copy_text_attr(off + wp->w_width - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002720 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 else
2722# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002723 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 col += len;
2725 }
2726 }
2727#endif
2728
2729 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002730 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002732 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002734 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 if (len > 0)
2736 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002737 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002738 long num;
2739 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002740
2741 if (len > w + 1)
2742 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002743
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002744 if (wp->w_p_nu && !wp->w_p_rnu)
2745 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002746 num = (long)lnum;
2747 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002748 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002749 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002750 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002751 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002752 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002753 /* 'number' + 'relativenumber': cursor line shows absolute
2754 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002755 num = lnum;
2756 fmt = "%-*ld ";
2757 }
2758 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002759
Bram Moolenaar700e7342013-01-30 12:31:36 +01002760 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761#ifdef FEAT_RIGHTLEFT
2762 if (wp->w_p_rl)
2763 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002764 copy_text_attr(off + wp->w_width - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002765 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 else
2767#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002768 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 col += len;
2770 }
2771 }
2772
2773 /*
2774 * 4. Compose the folded-line string with 'foldtext', if set.
2775 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002776 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777
2778 txtcol = col; /* remember where text starts */
2779
2780 /*
2781 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2782 * Right-left text is put in columns 0 - number-col, normal text is put
2783 * in columns number-col - window-width.
2784 */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002785 col = text_to_screenline(wp, text, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786
2787 /* Fill the rest of the line with the fold filler */
2788#ifdef FEAT_RIGHTLEFT
2789 if (wp->w_p_rl)
2790 col -= txtcol;
2791#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02002792 while (col < wp->w_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793#ifdef FEAT_RIGHTLEFT
2794 - (wp->w_p_rl ? txtcol : 0)
2795#endif
2796 )
2797 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 if (enc_utf8)
2799 {
2800 if (fill_fold >= 0x80)
2801 {
2802 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002803 ScreenLinesC[0][off + col] = 0;
Bram Moolenaaracda04f2018-02-08 09:57:28 +01002804 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 }
2806 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002807 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002809 ScreenLines[off + col] = fill_fold;
2810 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002811 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002813 else
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002814 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 }
2816
2817 if (text != buf)
2818 vim_free(text);
2819
2820 /*
2821 * 6. set highlighting for the Visual area an other text.
2822 * If all folded lines are in the Visual area, highlight the line.
2823 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2825 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002826 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 {
2828 /* Visual is after curwin->w_cursor */
2829 top = &curwin->w_cursor;
2830 bot = &VIsual;
2831 }
2832 else
2833 {
2834 /* Visual is before curwin->w_cursor */
2835 top = &VIsual;
2836 bot = &curwin->w_cursor;
2837 }
2838 if (lnum >= top->lnum
2839 && lnume <= bot->lnum
2840 && (VIsual_mode != 'v'
2841 || ((lnum > top->lnum
2842 || (lnum == top->lnum
2843 && top->col == 0))
2844 && (lnume < bot->lnum
2845 || (lnume == bot->lnum
2846 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002847 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 {
2849 if (VIsual_mode == Ctrl_V)
2850 {
2851 /* Visual block mode: highlight the chars part of the block */
Bram Moolenaar02631462017-09-22 15:20:32 +02002852 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002854 if (wp->w_old_cursor_lcol != MAXCOL
2855 && wp->w_old_cursor_lcol + txtcol
Bram Moolenaar02631462017-09-22 15:20:32 +02002856 < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 len = wp->w_old_cursor_lcol;
2858 else
Bram Moolenaar02631462017-09-22 15:20:32 +02002859 len = wp->w_width - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002860 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002861 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 }
2863 }
2864 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002865 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 /* Set all attributes of the text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002867 RL_MEMSET(txtcol, HL_ATTR(HLF_V), wp->w_width - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 }
2870 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002872#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002873 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2874 if (wp->w_p_cc_cols)
2875 {
2876 int i = 0;
2877 int j = wp->w_p_cc_cols[i];
2878 int old_txtcol = txtcol;
2879
2880 while (j > -1)
2881 {
2882 txtcol += j;
2883 if (wp->w_p_wrap)
2884 txtcol -= wp->w_skipcol;
2885 else
2886 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002887 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002888 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002889 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002890 txtcol = old_txtcol;
2891 j = wp->w_p_cc_cols[++i];
2892 }
2893 }
2894
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002895 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002896 if (wp->w_p_cuc)
2897 {
2898 txtcol += wp->w_virtcol;
2899 if (wp->w_p_wrap)
2900 txtcol -= wp->w_skipcol;
2901 else
2902 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002903 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaar85595c52008-10-02 16:04:05 +00002904 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002905 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00002906 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002907#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908
Bram Moolenaar02631462017-09-22 15:20:32 +02002909 screen_line(row + W_WINROW(wp), wp->w_wincol, (int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002910 (int)wp->w_width, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911
2912 /*
2913 * Update w_cline_height and w_cline_folded if the cursor line was
2914 * updated (saves a call to plines() later).
2915 */
2916 if (wp == curwin
2917 && lnum <= curwin->w_cursor.lnum
2918 && lnume >= curwin->w_cursor.lnum)
2919 {
2920 curwin->w_cline_row = row;
2921 curwin->w_cline_height = 1;
2922 curwin->w_cline_folded = TRUE;
2923 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2924 }
2925}
2926
2927/*
2928 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2929 */
2930 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002931copy_text_attr(
2932 int off,
2933 char_u *buf,
2934 int len,
2935 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002937 int i;
2938
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 mch_memmove(ScreenLines + off, buf, (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 if (enc_utf8)
2941 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002942 for (i = 0; i < len; ++i)
2943 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944}
2945
2946/*
2947 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002948 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 */
2950 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002951fill_foldcolumn(
2952 char_u *p,
2953 win_T *wp,
2954 int closed, /* TRUE of FALSE */
2955 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956{
2957 int i = 0;
2958 int level;
2959 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002960 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002961 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962
2963 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002964 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965
2966 level = win_foldinfo.fi_level;
2967 if (level > 0)
2968 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002969 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002970 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002971
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 /* If the column is too narrow, we start at the lowest level that
2973 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002974 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 if (first_level < 1)
2976 first_level = 1;
2977
Bram Moolenaar1c934292015-01-27 16:39:29 +01002978 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 {
2980 if (win_foldinfo.fi_lnum == lnum
2981 && first_level + i >= win_foldinfo.fi_low_level)
2982 p[i] = '-';
2983 else if (first_level == 1)
2984 p[i] = '|';
2985 else if (first_level + i <= 9)
2986 p[i] = '0' + first_level + i;
2987 else
2988 p[i] = '>';
2989 if (first_level + i == level)
2990 break;
2991 }
2992 }
2993 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002994 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995}
2996#endif /* FEAT_FOLDING */
2997
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01002998#ifdef FEAT_TEXT_PROP
2999static textprop_T *current_text_props = NULL;
3000static buf_T *current_buf = NULL;
3001
3002 static int
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01003003text_prop_compare(const void *s1, const void *s2)
3004{
3005 int idx1, idx2;
3006 proptype_T *pt1, *pt2;
3007 colnr_T col1, col2;
3008
3009 idx1 = *(int *)s1;
3010 idx2 = *(int *)s2;
3011 pt1 = text_prop_type_by_id(current_buf, current_text_props[idx1].tp_type);
3012 pt2 = text_prop_type_by_id(current_buf, current_text_props[idx2].tp_type);
3013 if (pt1 == pt2)
3014 return 0;
3015 if (pt1 == NULL)
3016 return -1;
3017 if (pt2 == NULL)
3018 return 1;
3019 if (pt1->pt_priority != pt2->pt_priority)
3020 return pt1->pt_priority > pt2->pt_priority ? 1 : -1;
3021 col1 = current_text_props[idx1].tp_col;
3022 col2 = current_text_props[idx2].tp_col;
3023 return col1 == col2 ? 0 : col1 > col2 ? 1 : -1;
3024}
3025#endif
3026
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003027#ifdef FEAT_SIGNS
3028/*
3029 * Get information needed to display the sign in line 'lnum' in window 'wp'.
3030 * If 'nrcol' is TRUE, the sign is going to be displayed in the number column.
3031 * Otherwise the sign is going to be displayed in the sign column.
3032 */
3033 static void
3034get_sign_display_info(
3035 int nrcol,
3036 win_T *wp,
Bram Moolenaar4e038572019-07-04 18:28:35 +02003037 linenr_T lnum UNUSED,
3038 sign_attrs_T *sattr,
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003039 int wcr_attr,
3040 int row,
3041 int startrow,
Bram Moolenaarbf8c3ad2019-06-19 14:28:43 +02003042 int filler_lines UNUSED,
3043 int filler_todo UNUSED,
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003044 int *c_extrap,
3045 int *c_finalp,
3046 char_u *extra,
3047 char_u **pp_extra,
3048 int *n_extrap,
3049 int *char_attrp)
3050{
3051 int text_sign;
3052# ifdef FEAT_SIGN_ICONS
3053 int icon_sign;
3054# endif
3055
3056 // Draw two cells with the sign value or blank.
3057 *c_extrap = ' ';
3058 *c_finalp = NUL;
3059 if (nrcol)
3060 *n_extrap = number_width(wp) + 1;
3061 else
3062 {
3063 *char_attrp = hl_combine_attr(wcr_attr, HL_ATTR(HLF_SC));
3064 *n_extrap = 2;
3065 }
3066
3067 if (row == startrow
3068#ifdef FEAT_DIFF
3069 + filler_lines && filler_todo <= 0
3070#endif
3071 )
3072 {
Bram Moolenaar4e038572019-07-04 18:28:35 +02003073 text_sign = (sattr->text != NULL) ? sattr->typenr : 0;
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003074# ifdef FEAT_SIGN_ICONS
Bram Moolenaar4e038572019-07-04 18:28:35 +02003075 icon_sign = (sattr->icon != NULL) ? sattr->typenr : 0;
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003076 if (gui.in_use && icon_sign != 0)
3077 {
3078 // Use the image in this position.
Bram Moolenaar4dff4ae2019-06-19 16:31:28 +02003079 if (nrcol)
3080 {
3081 *c_extrap = NUL;
3082 sprintf((char *)extra, "%-*c ", number_width(wp), SIGN_BYTE);
3083 *pp_extra = extra;
3084 *n_extrap = (int)STRLEN(*pp_extra);
3085 }
3086 else
3087 *c_extrap = SIGN_BYTE;
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003088# ifdef FEAT_NETBEANS_INTG
Bram Moolenaar4e038572019-07-04 18:28:35 +02003089 if (netbeans_active() && (buf_signcount(wp->w_buffer, lnum) > 1))
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003090 {
Bram Moolenaar4dff4ae2019-06-19 16:31:28 +02003091 if (nrcol)
3092 {
3093 *c_extrap = NUL;
3094 sprintf((char *)extra, "%-*c ", number_width(wp),
3095 MULTISIGN_BYTE);
3096 *pp_extra = extra;
3097 *n_extrap = (int)STRLEN(*pp_extra);
3098 }
3099 else
3100 *c_extrap = MULTISIGN_BYTE;
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003101 }
3102# endif
Bram Moolenaar4dff4ae2019-06-19 16:31:28 +02003103 *c_finalp = NUL;
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003104 *char_attrp = icon_sign;
3105 }
3106 else
3107# endif
3108 if (text_sign != 0)
3109 {
Bram Moolenaar4e038572019-07-04 18:28:35 +02003110 *pp_extra = sattr->text;
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003111 if (*pp_extra != NULL)
3112 {
3113 if (nrcol)
3114 {
Bram Moolenaard6bcff42019-07-18 12:48:16 +02003115 int n, width = number_width(wp) - 2;
3116
3117 for (n = 0; n < width; n++)
3118 extra[n] = ' ';
3119 extra[n] = 0;
3120 STRCAT(extra, *pp_extra);
3121 STRCAT(extra, " ");
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003122 *pp_extra = extra;
3123 }
3124 *c_extrap = NUL;
3125 *c_finalp = NUL;
3126 *n_extrap = (int)STRLEN(*pp_extra);
3127 }
Bram Moolenaar4e038572019-07-04 18:28:35 +02003128 *char_attrp = sattr->texthl;
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003129 }
3130 }
3131}
3132#endif
3133
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134/*
3135 * Display line "lnum" of window 'wp' on the screen.
3136 * Start at row "startrow", stop when "endrow" is reached.
3137 * wp->w_virtcol needs to be valid.
3138 *
3139 * Return the number of last row the line occupies.
3140 */
3141 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003142win_line(
3143 win_T *wp,
3144 linenr_T lnum,
3145 int startrow,
3146 int endrow,
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003147 int nochange UNUSED, // not updating for changed text
3148 int number_only) // only update the number column
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149{
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003150 int col = 0; // visual column on screen
3151 unsigned off; // offset in ScreenLines/ScreenAttrs
3152 int c = 0; // init for GCC
3153 long vcol = 0; // virtual column (for tabs)
Bram Moolenaard574ea22015-01-14 19:35:14 +01003154#ifdef FEAT_LINEBREAK
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003155 long vcol_sbr = -1; // virtual column after showbreak
Bram Moolenaard574ea22015-01-14 19:35:14 +01003156#endif
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003157 long vcol_prev = -1; // "vcol" of previous character
3158 char_u *line; // current line
3159 char_u *ptr; // current position in "line"
3160 int row; // row in the window, excl w_winrow
3161 int screen_row; // row on the screen, incl w_winrow
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003163 char_u extra[21]; // "%ld " and 'fdc' must fit in here
3164 int n_extra = 0; // number of extra chars
3165 char_u *p_extra = NULL; // string of extra chars, plus NUL
3166 char_u *p_extra_free = NULL; // p_extra needs to be freed
3167 int c_extra = NUL; // extra chars, all the same
3168 int c_final = NUL; // final char, mandatory if set
3169 int extra_attr = 0; // attributes when n_extra != 0
3170 static char_u *at_end_str = (char_u *)""; // used for p_extra when
3171 // displaying lcs_eol at end-of-line
3172 int lcs_eol_one = lcs_eol; // lcs_eol until it's been used
3173 int lcs_prec_todo = lcs_prec; // lcs_prec until it's been used
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003175 // saved "extra" items for when draw_state becomes WL_LINE (again)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 int saved_n_extra = 0;
3177 char_u *saved_p_extra = NULL;
3178 int saved_c_extra = 0;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003179 int saved_c_final = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 int saved_char_attr = 0;
3181
3182 int n_attr = 0; /* chars with special attr */
3183 int saved_attr2 = 0; /* char_attr saved for n_attr */
3184 int n_attr3 = 0; /* chars with overruling special attr */
3185 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
3186
3187 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
3188
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003189 int fromcol = -10; // start of inverting
3190 int tocol = MAXCOL; // end of inverting
3191 int fromcol_prev = -2; // start of inverting after cursor
3192 int noinvcur = FALSE; // don't invert the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003194 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 pos_T pos;
3196 long v;
3197
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003198 int char_attr = 0; // attributes for next character
3199 int attr_pri = FALSE; // char_attr has priority
3200 int area_highlighting = FALSE; // Visual or incsearch highlighting
3201 // in this line
3202 int vi_attr = 0; // attributes for Visual and incsearch
3203 // highlighting
3204 int wcr_attr = 0; // attributes from 'wincolor'
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003205 int win_attr = 0; // background for whole window, except
3206 // margins and "~" lines.
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003207 int area_attr = 0; // attributes desired by highlighting
3208 int search_attr = 0; // attributes desired by 'hlsearch'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003210 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 int syntax_attr = 0; /* attributes desired by syntax */
3212 int has_syntax = FALSE; /* this buffer has syntax highl. */
3213 int save_did_emsg;
Bram Moolenaar1a384422010-07-14 19:53:30 +02003214 int draw_color_col = FALSE; /* highlight colorcolumn */
3215 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003216#endif
Bram Moolenaarf914a332019-07-20 15:09:56 +02003217 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003218#ifdef FEAT_TEXT_PROP
3219 int text_prop_count;
3220 int text_prop_next = 0; // next text property to use
3221 textprop_T *text_props = NULL;
3222 int *text_prop_idxs = NULL;
3223 int text_props_active = 0;
3224 proptype_T *text_prop_type = NULL;
3225 int text_prop_attr = 0;
Bram Moolenaarde24a872019-05-05 15:48:00 +02003226 int text_prop_combine = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003227#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003228#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003229 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003230# define SPWORDLEN 150
3231 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003232 int nextlinecol = 0; /* column where nextline[] starts */
3233 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003234 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003235 int spell_attr = 0; /* attributes desired by spelling */
3236 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003237 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3238 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003239 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003240 static int cap_col = -1; /* column to check for Cap word */
3241 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003242 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003244 int extra_check = 0; // has extra highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 int multi_attr = 0; /* attributes desired by multibyte */
3246 int mb_l = 1; /* multi-byte byte length */
3247 int mb_c = 0; /* decoded multi-byte character */
3248 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003249 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaare9726e32019-06-19 18:01:21 +02003250#if defined(FEAT_DIFF) || defined(FEAT_SIGNS)
Bram Moolenaarbf8c3ad2019-06-19 14:28:43 +02003251 int filler_lines = 0; /* nr of filler lines to be drawn */
3252 int filler_todo = 0; /* nr of filler lines still to do + 1 */
Bram Moolenaare9726e32019-06-19 18:01:21 +02003253#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254#ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003255 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 int change_start = MAXCOL; /* first col of changed area */
3257 int change_end = -1; /* last col of changed area */
3258#endif
3259 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3260#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003261 int need_showbreak = FALSE; /* overlong line, skipping first x
3262 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003264#if defined(FEAT_SIGNS) || defined(FEAT_QUICKFIX) \
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003265 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003267 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268#endif
Bram Moolenaarb4d9b892019-07-04 22:59:06 +02003269#ifdef FEAT_SIGNS
3270 int sign_present = FALSE;
3271 sign_attrs_T sattr;
3272#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273#ifdef FEAT_ARABIC
3274 int prev_c = 0; /* previous Arabic character */
3275 int prev_c1 = 0; /* first composing char for prev_c */
3276#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003277#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003278 int did_line_attr = 0;
3279#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003280#ifdef FEAT_TERMINAL
3281 int get_term_attr = FALSE;
3282#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283
3284 /* draw_state: items that are drawn in sequence: */
3285#define WL_START 0 /* nothing done yet */
3286#ifdef FEAT_CMDWIN
3287# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3288#else
3289# define WL_CMDLINE WL_START
3290#endif
3291#ifdef FEAT_FOLDING
3292# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3293#else
3294# define WL_FOLD WL_CMDLINE
3295#endif
3296#ifdef FEAT_SIGNS
3297# define WL_SIGN WL_FOLD + 1 /* column for signs */
3298#else
3299# define WL_SIGN WL_FOLD /* column for signs */
3300#endif
3301#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003302#ifdef FEAT_LINEBREAK
3303# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003305# define WL_BRI WL_NR
3306#endif
3307#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3308# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3309#else
3310# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311#endif
3312#define WL_LINE WL_SBR + 1 /* text in the line */
3313 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003314#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 int feedback_col = 0;
3316 int feedback_old_attr = -1;
3317#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003318 int screen_line_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319
Bram Moolenaarfbfb7572019-07-25 20:53:03 +02003320#if defined(FEAT_CONCEAL) || defined(FEAT_SEARCH_EXTRA)
Bram Moolenaar07d13562019-07-24 18:43:08 +02003321 int match_conc = 0; // cchar for match functions
Bram Moolenaarfbfb7572019-07-25 20:53:03 +02003322#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02003323#ifdef FEAT_CONCEAL
3324 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003325 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003326 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003327 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003328 int is_concealing = FALSE;
Bram Moolenaar07d13562019-07-24 18:43:08 +02003329 int boguscols = 0; // nonexistent columns added to force
3330 // wrapping
3331 int vcol_off = 0; // offset for concealed characters
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003332 int did_wcol = FALSE;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003333 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003334# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003335# define FIX_FOR_BOGUSCOLS \
3336 { \
3337 n_extra += vcol_off; \
3338 vcol -= vcol_off; \
3339 vcol_off = 0; \
3340 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003341 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003342 boguscols = 0; \
3343 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003344#else
3345# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003346#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347
3348 if (startrow > endrow) /* past the end already! */
3349 return startrow;
3350
3351 row = startrow;
3352 screen_row = row + W_WINROW(wp);
3353
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003354 if (!number_only)
3355 {
3356 /*
3357 * To speed up the loop below, set extra_check when there is linebreak,
3358 * trailing white space and/or syntax processing to be done.
3359 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360#ifdef FEAT_LINEBREAK
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003361 extra_check = wp->w_p_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362#endif
3363#ifdef FEAT_SYN_HL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003364 if (syntax_present(wp) && !wp->w_s->b_syn_error
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003365# ifdef SYN_TIME_LIMIT
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003366 && !wp->w_s->b_syn_slow
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003367# endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003368 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003370 /* Prepare for syntax highlighting in this line. When there is an
3371 * error, stop syntax highlighting. */
3372 save_did_emsg = did_emsg;
3373 did_emsg = FALSE;
3374 syntax_start(wp, lnum);
3375 if (did_emsg)
3376 wp->w_s->b_syn_error = TRUE;
3377 else
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003378 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003379 did_emsg = save_did_emsg;
3380#ifdef SYN_TIME_LIMIT
3381 if (!wp->w_s->b_syn_slow)
3382#endif
3383 {
3384 has_syntax = TRUE;
3385 extra_check = TRUE;
3386 }
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003389
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003390 /* Check for columns to display for 'colorcolumn'. */
3391 color_cols = wp->w_p_cc_cols;
3392 if (color_cols != NULL)
3393 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003394#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003395
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003396#ifdef FEAT_TERMINAL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003397 if (term_show_buffer(wp->w_buffer))
3398 {
3399 extra_check = TRUE;
3400 get_term_attr = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003401 win_attr = term_get_attr(wp->w_buffer, lnum, -1);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003402 }
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003403#endif
3404
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003405#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003406 if (wp->w_p_spell
3407 && *wp->w_s->b_p_spl != NUL
3408 && wp->w_s->b_langp.ga_len > 0
3409 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003410 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003411 /* Prepare for spell checking. */
3412 has_spell = TRUE;
3413 extra_check = TRUE;
3414
Bram Moolenaar7701f302018-10-02 21:20:32 +02003415 /* Get the start of the next line, so that words that wrap to the
3416 * next line are found too: "et<line-break>al.".
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003417 * Trick: skip a few chars for C/shell/Vim comments */
3418 nextline[SPWORDLEN] = NUL;
3419 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3420 {
3421 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3422 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3423 }
3424
Bram Moolenaar7701f302018-10-02 21:20:32 +02003425 /* When a word wrapped from the previous line the start of the
3426 * current line is valid. */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003427 if (lnum == checked_lnum)
3428 cur_checked_col = checked_col;
3429 checked_lnum = 0;
3430
3431 /* When there was a sentence end in the previous line may require a
3432 * word starting with capital in this line. In line 1 always check
3433 * the first word. */
3434 if (lnum != capcol_lnum)
3435 cap_col = -1;
3436 if (lnum == 1)
3437 cap_col = 0;
3438 capcol_lnum = 0;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440#endif
3441
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003442 /*
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003443 * handle Visual active in this window
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003444 */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003445 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003447 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 {
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003449 // Visual is after curwin->w_cursor
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003450 top = &curwin->w_cursor;
3451 bot = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 }
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003453 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 {
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003455 // Visual is before curwin->w_cursor
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003456 top = &VIsual;
3457 bot = &curwin->w_cursor;
3458 }
3459 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003460 if (VIsual_mode == Ctrl_V)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003461 {
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003462 // block mode
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003463 if (lnum_in_visual_area)
3464 {
3465 fromcol = wp->w_old_cursor_fcol;
3466 tocol = wp->w_old_cursor_lcol;
3467 }
3468 }
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003469 else
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003470 {
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003471 // non-block mode
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003472 if (lnum > top->lnum && lnum <= bot->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 fromcol = 0;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003474 else if (lnum == top->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 {
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003476 if (VIsual_mode == 'V') // linewise
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003477 fromcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 else
3479 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003480 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3481 if (gchar_pos(top) == NUL)
3482 tocol = fromcol + 1;
3483 }
3484 }
3485 if (VIsual_mode != 'V' && lnum == bot->lnum)
3486 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003487 if (*p_sel == 'e' && bot->col == 0 && bot->coladd == 0)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003488 {
3489 fromcol = -10;
3490 tocol = MAXCOL;
3491 }
3492 else if (bot->col == MAXCOL)
3493 tocol = MAXCOL;
3494 else
3495 {
3496 pos = *bot;
3497 if (*p_sel == 'e')
3498 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3499 else
3500 {
3501 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3502 ++tocol;
3503 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 }
3505 }
3506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003508 /* Check if the character under the cursor should not be inverted */
3509 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003510#ifdef FEAT_GUI
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003511 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003512#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003513 )
3514 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003516 /* if inverting in this line set area_highlighting */
3517 if (fromcol >= 0)
3518 {
3519 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003520 vi_attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003522 if ((clip_star.available && !clip_star.owned
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003523 && clip_isautosel_star())
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003524 || (clip_plus.available && !clip_plus.owned
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003525 && clip_isautosel_plus()))
3526 vi_attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003531 /*
3532 * handle 'incsearch' and ":s///c" highlighting
3533 */
3534 else if (highlight_match
3535 && wp == curwin
3536 && lnum >= curwin->w_cursor.lnum
3537 && lnum <= curwin->w_cursor.lnum + search_match_lines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003539 if (lnum == curwin->w_cursor.lnum)
3540 getvcol(curwin, &(curwin->w_cursor),
Bram Moolenaarc6663882019-02-22 19:14:54 +01003541 (colnr_T *)&fromcol, NULL, NULL);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003542 else
3543 fromcol = 0;
3544 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3545 {
3546 pos.lnum = lnum;
3547 pos.col = search_match_endcol;
3548 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3549 }
3550 else
3551 tocol = MAXCOL;
3552 /* do at least one character; happens when past end of line */
3553 if (fromcol == tocol)
3554 tocol = fromcol + 1;
3555 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003556 vi_attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 }
3559
3560#ifdef FEAT_DIFF
3561 filler_lines = diff_check(wp, lnum);
3562 if (filler_lines < 0)
3563 {
3564 if (filler_lines == -1)
3565 {
3566 if (diff_find_change(wp, lnum, &change_start, &change_end))
3567 diff_hlf = HLF_ADD; /* added line */
3568 else if (change_start == 0)
3569 diff_hlf = HLF_TXD; /* changed text */
3570 else
3571 diff_hlf = HLF_CHD; /* changed line */
3572 }
3573 else
3574 diff_hlf = HLF_ADD; /* added line */
3575 filler_lines = 0;
3576 area_highlighting = TRUE;
3577 }
3578 if (lnum == wp->w_topline)
3579 filler_lines = wp->w_topfill;
3580 filler_todo = filler_lines;
3581#endif
3582
Bram Moolenaar4e038572019-07-04 18:28:35 +02003583#ifdef FEAT_SIGNS
3584 sign_present = buf_get_signattrs(wp->w_buffer, lnum, &sattr);
3585#endif
3586
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587#ifdef LINE_ATTR
3588# ifdef FEAT_SIGNS
3589 /* If this line has a sign with line highlighting set line_attr. */
Bram Moolenaar4e038572019-07-04 18:28:35 +02003590 if (sign_present)
3591 line_attr = sattr.linehl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003593# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003595 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003596 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597# endif
3598 if (line_attr != 0)
3599 area_highlighting = TRUE;
3600#endif
3601
3602 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3603 ptr = line;
3604
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003605#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003606 if (has_spell && !number_only)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003607 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003608 /* For checking first word with a capital skip white space. */
3609 if (cap_col == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003610 cap_col = getwhitecols(line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003611
Bram Moolenaar30abd282005-06-22 22:35:10 +00003612 /* To be able to spell-check over line boundaries copy the end of the
3613 * current line into nextline[]. Above the start of the next line was
3614 * copied to nextline[SPWORDLEN]. */
3615 if (nextline[SPWORDLEN] == NUL)
3616 {
3617 /* No next line or it is empty. */
3618 nextlinecol = MAXCOL;
3619 nextline_idx = 0;
3620 }
3621 else
3622 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003623 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003624 if (v < SPWORDLEN)
3625 {
3626 /* Short line, use it completely and append the start of the
3627 * next line. */
3628 nextlinecol = 0;
3629 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003630 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003631 nextline_idx = v + 1;
3632 }
3633 else
3634 {
3635 /* Long line, use only the last SPWORDLEN bytes. */
3636 nextlinecol = v - SPWORDLEN;
3637 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3638 nextline_idx = SPWORDLEN + 1;
3639 }
3640 }
3641 }
3642#endif
3643
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003644 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 {
Bram Moolenaar895d9662019-01-31 21:57:21 +01003646 if (lcs_space || lcs_trail || lcs_nbsp)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003647 extra_check = TRUE;
3648 /* find start of trailing whitespace */
3649 if (lcs_trail)
3650 {
3651 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003652 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003653 --trailcol;
3654 trailcol += (colnr_T) (ptr - line);
3655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 }
3657
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003658 wcr_attr = get_wcr_attr(wp);
3659 if (wcr_attr != 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003660 {
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003661 win_attr = wcr_attr;
3662 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003663 }
3664#ifdef FEAT_TEXT_PROP
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02003665 if (WIN_IS_POPUP(wp))
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003666 screen_line_flags |= SLF_POPUP;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003667#endif
3668
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 /*
3670 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3671 * first character to be displayed.
3672 */
3673 if (wp->w_p_wrap)
3674 v = wp->w_skipcol;
3675 else
3676 v = wp->w_leftcol;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003677 if (v > 0 && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 char_u *prev_ptr = ptr;
Bram Moolenaara12a1612019-01-24 16:39:02 +01003680
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 while (vcol < v && *ptr != NUL)
3682 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003683 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 vcol += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 prev_ptr = ptr;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003686 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 }
3688
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003689 /* When:
3690 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003691 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003692 * - 'virtualedit' is set, or
3693 * - the visual mode is active,
3694 * the end of the line may be before the start of the displayed part.
3695 */
3696 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003697#ifdef FEAT_SYN_HL
3698 wp->w_p_cuc || draw_color_col ||
3699#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003700 virtual_active() ||
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003701 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 vcol = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703
3704 /* Handle a character that's not completely on the screen: Put ptr at
3705 * that character but skip the first few screen characters. */
3706 if (vcol > v)
3707 {
3708 vcol -= c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 ptr = prev_ptr;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003710 /* If the character fits on the screen, don't need to skip it.
3711 * Except for a TAB. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01003712 if (( (*mb_ptr2cells)(ptr) >= c || *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003713 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 }
3715
3716 /*
3717 * Adjust for when the inverted text is before the screen,
3718 * and when the start of the inverted text is before the screen.
3719 */
3720 if (tocol <= vcol)
3721 fromcol = 0;
3722 else if (fromcol >= 0 && fromcol < vcol)
3723 fromcol = vcol;
3724
3725#ifdef FEAT_LINEBREAK
3726 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3727 if (wp->w_p_wrap)
3728 need_showbreak = TRUE;
3729#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003730#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003731 /* When spell checking a word we need to figure out the start of the
3732 * word and if it's badly spelled or not. */
3733 if (has_spell)
3734 {
3735 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003736 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003737 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003738
3739 pos = wp->w_cursor;
3740 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003741 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003742 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003743
3744 /* spell_move_to() may call ml_get() and make "line" invalid */
3745 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3746 ptr = line + linecol;
3747
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003748 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003749 {
3750 /* no bad word found at line start, don't check until end of a
3751 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003752 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003753 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003754 }
3755 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003756 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003757 /* bad word found, use attributes until end of word */
3758 word_end = wp->w_cursor.col + len + 1;
3759
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003760 /* Turn index into actual attributes. */
3761 if (spell_hlf != HLF_COUNT)
3762 spell_attr = highlight_attr[spell_hlf];
3763 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003764 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003765
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003766# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003767 /* Need to restart syntax highlighting for this line. */
3768 if (has_syntax)
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003769 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003770# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003771 }
3772#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 }
3774
3775 /*
3776 * Correct highlighting for cursor that can't be disabled.
3777 * Avoids having to check this for each character.
3778 */
3779 if (fromcol >= 0)
3780 {
3781 if (noinvcur)
3782 {
3783 if ((colnr_T)fromcol == wp->w_virtcol)
3784 {
3785 /* highlighting starts at cursor, let it start just after the
3786 * cursor */
3787 fromcol_prev = fromcol;
3788 fromcol = -1;
3789 }
3790 else if ((colnr_T)fromcol < wp->w_virtcol)
3791 /* restart highlighting after the cursor */
3792 fromcol_prev = wp->w_virtcol;
3793 }
3794 if (fromcol >= tocol)
3795 fromcol = -1;
3796 }
3797
3798#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarbbca7732019-07-24 18:13:16 +02003799 if (!number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003801 v = (long)(ptr - line);
Bram Moolenaarbbca7732019-07-24 18:13:16 +02003802 area_highlighting |= prepare_search_hl_line(wp, lnum, (colnr_T)v,
3803 &line, &search_hl, &search_attr);
3804 ptr = line + v; // "line" may have been updated
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 }
3806#endif
3807
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003808#ifdef FEAT_SYN_HL
Bram Moolenaar8156ed32019-03-09 11:46:15 +01003809 // Cursor line highlighting for 'cursorline' in the current window.
3810 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003811 {
Bram Moolenaar8156ed32019-03-09 11:46:15 +01003812 // Do not show the cursor line when Visual mode is active, because it's
3813 // not clear what is selected then. Do update w_last_cursorline.
3814 if (!(wp == curwin && VIsual_active))
3815 {
3816 line_attr = HL_ATTR(HLF_CUL);
3817 area_highlighting = TRUE;
3818 }
Bram Moolenaarc07ff5c2019-01-30 21:41:14 +01003819 wp->w_last_cursorline = wp->w_cursor.lnum;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003820 }
3821#endif
3822
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003823#ifdef FEAT_TEXT_PROP
3824 {
3825 char_u *prop_start;
3826
3827 text_prop_count = get_text_props(wp->w_buffer, lnum,
3828 &prop_start, FALSE);
3829 if (text_prop_count > 0)
3830 {
3831 // Make a copy of the properties, so that they are properly
3832 // aligned.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003833 text_props = ALLOC_MULT(textprop_T, text_prop_count);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003834 if (text_props != NULL)
3835 mch_memmove(text_props, prop_start,
3836 text_prop_count * sizeof(textprop_T));
3837
3838 // Allocate an array for the indexes.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003839 text_prop_idxs = ALLOC_MULT(int, text_prop_count);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003840 area_highlighting = TRUE;
3841 extra_check = TRUE;
3842 }
3843 }
3844#endif
3845
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003846 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 col = 0;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003848
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849#ifdef FEAT_RIGHTLEFT
3850 if (wp->w_p_rl)
3851 {
3852 /* Rightleft window: process the text in the normal direction, but put
3853 * it in current_ScreenLine[] from right to left. Start at the
3854 * rightmost column of the window. */
Bram Moolenaar02631462017-09-22 15:20:32 +02003855 col = wp->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 off += col;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003857 screen_line_flags |= SLF_RIGHTLEFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 }
3859#endif
3860
3861 /*
3862 * Repeat for the whole displayed line.
3863 */
3864 for (;;)
3865 {
Bram Moolenaarfbfb7572019-07-25 20:53:03 +02003866#if defined(FEAT_CONCEAL) || defined(FEAT_SEARCH_EXTRA)
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02003867 int has_match_conc = 0; // match wants to conceal
Bram Moolenaarfbfb7572019-07-25 20:53:03 +02003868#endif
Bram Moolenaar07d13562019-07-24 18:43:08 +02003869#ifdef FEAT_CONCEAL
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02003870 int did_decrement_ptr = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003871#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 /* Skip this quickly when working on the text. */
3873 if (draw_state != WL_LINE)
3874 {
3875#ifdef FEAT_CMDWIN
3876 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3877 {
3878 draw_state = WL_CMDLINE;
3879 if (cmdwin_type != 0 && wp == curwin)
3880 {
3881 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003883 c_extra = cmdwin_type;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003884 c_final = NUL;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003885 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 }
3887 }
3888#endif
3889
3890#ifdef FEAT_FOLDING
3891 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3892 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003893 int fdc = compute_foldcolumn(wp, 0);
3894
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003896 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003898 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003899 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003900 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003901 p_extra_free = alloc(12 + 1);
3902
3903 if (p_extra_free != NULL)
3904 {
3905 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3906 n_extra = fdc;
3907 p_extra_free[n_extra] = NUL;
3908 p_extra = p_extra_free;
3909 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003910 c_final = NUL;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003911 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_FC));
Bram Moolenaar62706602016-12-09 19:28:48 +01003912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 }
3914 }
3915#endif
3916
3917#ifdef FEAT_SIGNS
3918 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3919 {
3920 draw_state = WL_SIGN;
3921 /* Show the sign column when there are any signs in this
3922 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003923 if (signcolumn_on(wp))
Bram Moolenaar4e038572019-07-04 18:28:35 +02003924 get_sign_display_info(FALSE, wp, lnum, &sattr, wcr_attr,
3925 row, startrow, filler_lines, filler_todo, &c_extra,
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003926 &c_final, extra, &p_extra, &n_extra, &char_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 }
3928#endif
3929
3930 if (draw_state == WL_NR - 1 && n_extra == 0)
3931 {
3932 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003933 /* Display the absolute or relative line number. After the
3934 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3935 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 && (row == startrow
3937#ifdef FEAT_DIFF
3938 + filler_lines
3939#endif
3940 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3941 {
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003942#ifdef FEAT_SIGNS
3943 // If 'signcolumn' is set to 'number' and a sign is present
3944 // in 'lnum', then display the sign instead of the line
3945 // number.
3946 if ((*wp->w_p_scl == 'n' && *(wp->w_p_scl + 1) == 'u')
Bram Moolenaar4e038572019-07-04 18:28:35 +02003947 && sign_present)
3948 get_sign_display_info(TRUE, wp, lnum, &sattr, wcr_attr,
3949 row, startrow, filler_lines, filler_todo,
3950 &c_extra, &c_final, extra, &p_extra, &n_extra,
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003951 &char_attr);
3952 else
3953#endif
3954 {
3955 /* Draw the line number (empty space after wrapping). */
3956 if (row == startrow
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957#ifdef FEAT_DIFF
3958 + filler_lines
3959#endif
3960 )
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003961 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003962 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003963 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003964
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003965 if (wp->w_p_nu && !wp->w_p_rnu)
3966 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003967 num = (long)lnum;
3968 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003969 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003970 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003971 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003972 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003973 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003974 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003975 num = lnum;
3976 fmt = "%-*ld ";
3977 }
3978 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003979
Bram Moolenaar700e7342013-01-30 12:31:36 +01003980 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003981 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 if (wp->w_skipcol > 0)
3983 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3984 *p_extra = '-';
3985#ifdef FEAT_RIGHTLEFT
3986 if (wp->w_p_rl) /* reverse line numbers */
Bram Moolenaare73f9112019-03-29 18:29:54 +01003987 {
3988 char_u *p1, *p2;
3989 int t;
3990
3991 // like rl_mirror(), but keep the space at the end
3992 p2 = skiptowhite(extra) - 1;
3993 for (p1 = extra; p1 < p2; ++p1, --p2)
3994 {
3995 t = *p1;
3996 *p1 = *p2;
3997 *p2 = t;
3998 }
3999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000#endif
4001 p_extra = extra;
4002 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004003 c_final = NUL;
Bram Moolenaar394c5d82019-06-17 21:48:05 +02004004 }
4005 else
4006 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004008 c_final = NUL;
Bram Moolenaar394c5d82019-06-17 21:48:05 +02004009 }
4010 n_extra = number_width(wp) + 1;
4011 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_N));
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004012#ifdef FEAT_SYN_HL
Bram Moolenaar394c5d82019-06-17 21:48:05 +02004013 /* When 'cursorline' is set highlight the line number of
4014 * the current line differently.
4015 * TODO: Can we use CursorLine instead of CursorLineNr
4016 * when CursorLineNr isn't set? */
4017 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01004018 && lnum == wp->w_cursor.lnum)
Bram Moolenaar193ffd12019-05-25 22:57:30 +02004019 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_CLN));
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004020#endif
Bram Moolenaar394c5d82019-06-17 21:48:05 +02004021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 }
4023 }
4024
Bram Moolenaar597a4222014-06-25 14:39:50 +02004025#ifdef FEAT_LINEBREAK
4026 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
4027 && n_extra == 0 && *p_sbr != NUL)
4028 /* draw indent after showbreak value */
4029 draw_state = WL_BRI;
4030 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
4031 /* After the showbreak, draw the breakindent */
4032 draw_state = WL_BRI - 1;
4033
4034 /* draw 'breakindent': indent wrapped text accordingly */
4035 if (draw_state == WL_BRI - 1 && n_extra == 0)
4036 {
4037 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01004038 /* if need_showbreak is set, breakindent also applies */
4039 if (wp->w_p_bri && n_extra == 0
4040 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004041# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004042 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004043# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004044 )
4045 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01004046 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004047# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004048 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004049 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004050 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004051# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02004052 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4053 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004054 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004055# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02004056 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004057# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02004058 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02004059 c_extra = ' ';
4060 n_extra = get_breakindent_win(wp,
4061 ml_get_buf(wp->w_buffer, lnum, FALSE));
4062 /* Correct end of highlighted area for 'breakindent',
4063 * required when 'linebreak' is also set. */
4064 if (tocol == vcol)
4065 tocol += n_extra;
4066 }
4067 }
4068#endif
4069
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
4071 if (draw_state == WL_SBR - 1 && n_extra == 0)
4072 {
4073 draw_state = WL_SBR;
4074# ifdef FEAT_DIFF
4075 if (filler_todo > 0)
4076 {
4077 /* Draw "deleted" diff line(s). */
4078 if (char2cells(fill_diff) > 1)
Bram Moolenaar83a52172019-01-16 22:41:54 +01004079 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 c_extra = '-';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004081 c_final = NUL;
4082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 else
Bram Moolenaar83a52172019-01-16 22:41:54 +01004084 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 c_extra = fill_diff;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004086 c_final = NUL;
4087 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088# ifdef FEAT_RIGHTLEFT
4089 if (wp->w_p_rl)
4090 n_extra = col + 1;
4091 else
4092# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004093 n_extra = wp->w_width - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004094 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 }
4096# endif
4097# ifdef FEAT_LINEBREAK
4098 if (*p_sbr != NUL && need_showbreak)
4099 {
4100 /* Draw 'showbreak' at the start of each broken line. */
4101 p_extra = p_sbr;
4102 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004103 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004105 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004107 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 /* Correct end of highlighted area for 'showbreak',
4109 * required when 'linebreak' is also set. */
4110 if (tocol == vcol)
4111 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004112#ifdef FEAT_SYN_HL
4113 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004114 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004115 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004116 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004117#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 }
4119# endif
4120 }
4121#endif
4122
4123 if (draw_state == WL_LINE - 1 && n_extra == 0)
4124 {
4125 draw_state = WL_LINE;
4126 if (saved_n_extra)
4127 {
4128 /* Continue item from end of wrapped line. */
4129 n_extra = saved_n_extra;
4130 c_extra = saved_c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004131 c_final = saved_c_final;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 p_extra = saved_p_extra;
4133 char_attr = saved_char_attr;
4134 }
4135 else
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004136 char_attr = win_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 }
4138 }
4139
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004140 // When still displaying '$' of change command, stop at cursor.
4141 // When only displaying the (relative) line number and that's done,
4142 // stop here.
4143 if ((dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004144 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145#ifdef FEAT_DIFF
4146 && filler_todo <= 0
4147#endif
4148 )
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004149 || (number_only && draw_state > WL_NR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 {
Bram Moolenaar02631462017-09-22 15:20:32 +02004151 screen_line(screen_row, wp->w_wincol, col, -(int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004152 screen_line_flags);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004153 /* Pretend we have finished updating the window. Except when
4154 * 'cursorcolumn' is set. */
4155#ifdef FEAT_SYN_HL
4156 if (wp->w_p_cuc)
4157 row = wp->w_cline_row + wp->w_cline_height;
4158 else
4159#endif
4160 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 break;
4162 }
4163
Bram Moolenaar637532b2019-01-03 21:44:40 +01004164 if (draw_state == WL_LINE && (area_highlighting
4165#ifdef FEAT_SPELL
4166 || has_spell
4167#endif
4168 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 {
4170 /* handle Visual or match highlighting in this line */
4171 if (vcol == fromcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4173 && (*mb_ptr2cells)(ptr) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004175 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 && vcol < tocol))
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004177 area_attr = vi_attr; /* start highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 else if (area_attr != 0
4179 && (vcol == tocol
4180 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182
4183#ifdef FEAT_SEARCH_EXTRA
4184 if (!n_extra)
4185 {
4186 /*
Bram Moolenaarbbca7732019-07-24 18:13:16 +02004187 * Check for start/end of 'hlsearch' and other matches.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 * After end, check for start/end of next match.
4189 * When another match, have to check for start again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004191 v = (long)(ptr - line);
Bram Moolenaarbbca7732019-07-24 18:13:16 +02004192 search_attr = update_search_hl(wp, lnum, (colnr_T)v, &line,
4193 &search_hl, &has_match_conc, &match_conc,
4194 did_line_attr, lcs_eol_one);
4195 ptr = line + v; // "line" may have been changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 }
4197#endif
4198
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004200 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004202 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4203 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004205 if (diff_hlf == HLF_TXD && ptr - line > change_end
4206 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004208 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004209 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004210 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 }
4212#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004213
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004214#ifdef FEAT_TEXT_PROP
4215 if (text_props != NULL)
4216 {
4217 int pi;
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004218 int bcol = (int)(ptr - line);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004219
Bram Moolenaara956bf62019-06-19 17:34:24 +02004220 if (n_extra > 0)
4221 --bcol; // still working on the previous char, e.g. Tab
4222
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004223 // Check if any active property ends.
4224 for (pi = 0; pi < text_props_active; ++pi)
4225 {
4226 int tpi = text_prop_idxs[pi];
4227
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004228 if (bcol >= text_props[tpi].tp_col - 1
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004229 + text_props[tpi].tp_len)
4230 {
4231 if (pi + 1 < text_props_active)
4232 mch_memmove(text_prop_idxs + pi,
4233 text_prop_idxs + pi + 1,
4234 sizeof(int)
4235 * (text_props_active - (pi + 1)));
4236 --text_props_active;
4237 --pi;
4238 }
4239 }
4240
4241 // Add any text property that starts in this column.
4242 while (text_prop_next < text_prop_count
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004243 && bcol >= text_props[text_prop_next].tp_col - 1)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004244 text_prop_idxs[text_props_active++] = text_prop_next++;
4245
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004246 text_prop_attr = 0;
Bram Moolenaarde24a872019-05-05 15:48:00 +02004247 text_prop_combine = FALSE;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004248 if (text_props_active > 0)
4249 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004250 // Sort the properties on priority and/or starting last.
4251 // Then combine the attributes, highest priority last.
4252 current_text_props = text_props;
4253 current_buf = wp->w_buffer;
4254 qsort((void *)text_prop_idxs, (size_t)text_props_active,
4255 sizeof(int), text_prop_compare);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004256
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004257 for (pi = 0; pi < text_props_active; ++pi)
4258 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004259 int tpi = text_prop_idxs[pi];
Bram Moolenaarde24a872019-05-05 15:48:00 +02004260 proptype_T *pt = text_prop_type_by_id(
4261 wp->w_buffer, text_props[tpi].tp_type);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004262
Bram Moolenaard74af422019-06-28 21:38:00 +02004263 if (pt != NULL && pt->pt_hl_id > 0)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004264 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004265 int pt_attr = syn_id2attr(pt->pt_hl_id);
4266
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004267 text_prop_type = pt;
Bram Moolenaarde24a872019-05-05 15:48:00 +02004268 text_prop_attr =
4269 hl_combine_attr(text_prop_attr, pt_attr);
4270 text_prop_combine = pt->pt_flags & PT_FLAG_COMBINE;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004271 }
4272 }
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004273 }
4274 }
4275#endif
4276
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004277 /* Decide which of the highlight attributes to use. */
4278 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004279#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004280 if (area_attr != 0)
4281 char_attr = hl_combine_attr(line_attr, area_attr);
4282 else if (search_attr != 0)
4283 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004284# ifdef FEAT_TEXT_PROP
4285 else if (text_prop_type != NULL)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02004286 {
4287 char_attr = hl_combine_attr(
4288 line_attr != 0 ? line_attr : win_attr, text_prop_attr);
4289 }
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004290# endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004291 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004292 || vcol < fromcol || vcol_prev < fromcol_prev
4293 || vcol >= tocol))
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004294 // Use line_attr when not in the Visual or 'incsearch' area
4295 // (area_attr may be 0 when "noinvcur" is set).
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004296 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004297#else
4298 if (area_attr != 0)
4299 char_attr = area_attr;
4300 else if (search_attr != 0)
4301 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004302#endif
4303 else
4304 {
4305 attr_pri = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004306#ifdef FEAT_TEXT_PROP
4307 if (text_prop_type != NULL)
Bram Moolenaarde24a872019-05-05 15:48:00 +02004308 {
4309 if (text_prop_combine)
4310 char_attr = hl_combine_attr(
4311 syntax_attr, text_prop_attr);
4312 else
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02004313 char_attr = hl_combine_attr(
4314 win_attr, text_prop_attr);
Bram Moolenaarde24a872019-05-05 15:48:00 +02004315 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004316 else
4317#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004318#ifdef FEAT_SYN_HL
4319 if (has_syntax)
4320 char_attr = syntax_attr;
4321 else
4322#endif
4323 char_attr = 0;
4324 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004326 if (char_attr == 0)
4327 char_attr = win_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328
4329 /*
4330 * Get the next character to put on the screen.
4331 */
4332 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004333 * The "p_extra" points to the extra stuff that is inserted to
4334 * represent special characters (non-printable stuff) and other
4335 * things. When all characters are the same, c_extra is used.
Bram Moolenaar83a52172019-01-16 22:41:54 +01004336 * If c_final is set, it will compulsorily be used at the end.
Bram Moolenaara064ac82007-08-05 18:10:54 +00004337 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4338 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4340 */
4341 if (n_extra > 0)
4342 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004343 if (c_extra != NUL || (n_extra == 1 && c_final != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004345 c = (n_extra == 1 && c_final != NUL) ? c_final : c_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004347 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 {
4349 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004350 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004351 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 }
4353 else
4354 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 }
4356 else
4357 {
4358 c = *p_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 if (has_mbyte)
4360 {
4361 mb_c = c;
4362 if (enc_utf8)
4363 {
4364 /* If the UTF-8 character is more than one byte:
4365 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004366 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 mb_utf8 = FALSE;
4368 if (mb_l > n_extra)
4369 mb_l = 1;
4370 else if (mb_l > 1)
4371 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004372 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004374 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 }
4376 }
4377 else
4378 {
4379 /* if this is a DBCS character, put it in "mb_c" */
4380 mb_l = MB_BYTE2LEN(c);
4381 if (mb_l >= n_extra)
4382 mb_l = 1;
4383 else if (mb_l > 1)
4384 mb_c = (c << 8) + p_extra[1];
4385 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004386 if (mb_l == 0) /* at the NUL at end-of-line */
4387 mb_l = 1;
4388
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 /* If a double-width char doesn't fit display a '>' in the
4390 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004391 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392# ifdef FEAT_RIGHTLEFT
4393 wp->w_p_rl ? (col <= 0) :
4394# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004395 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 && (*mb_char2cells)(mb_c) == 2)
4397 {
4398 c = '>';
4399 mb_c = c;
4400 mb_l = 1;
4401 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004402 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 /* put the pointer back to output the double-width
4404 * character at the start of the next line. */
4405 ++n_extra;
4406 --p_extra;
4407 }
4408 else
4409 {
4410 n_extra -= mb_l - 1;
4411 p_extra += mb_l - 1;
4412 }
4413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 ++p_extra;
4415 }
4416 --n_extra;
4417 }
4418 else
4419 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004420#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004421 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004422#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004423
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004424 if (p_extra_free != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01004425 VIM_CLEAR(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 /*
4427 * Get a character from the line itself.
4428 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004429 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004430#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004431 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004432#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 if (has_mbyte)
4434 {
4435 mb_c = c;
4436 if (enc_utf8)
4437 {
4438 /* If the UTF-8 character is more than one byte: Decode it
4439 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004440 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 mb_utf8 = FALSE;
4442 if (mb_l > 1)
4443 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004444 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 /* Overlong encoded ASCII or ASCII with composing char
4446 * is displayed normally, except a NUL. */
4447 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004448 {
4449 c = mb_c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004450#ifdef FEAT_LINEBREAK
Bram Moolenaar88e76882017-02-27 20:33:46 +01004451 c0 = mb_c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004452#endif
Bram Moolenaar88e76882017-02-27 20:33:46 +01004453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004455
4456 /* At start of the line we can have a composing char.
4457 * Draw it as a space with a composing char. */
4458 if (utf_iscomposing(mb_c))
4459 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004460 int i;
4461
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004462 for (i = Screen_mco - 1; i > 0; --i)
4463 u8cc[i] = u8cc[i - 1];
4464 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004465 mb_c = ' ';
4466 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 }
4468
4469 if ((mb_l == 1 && c >= 0x80)
4470 || (mb_l >= 1 && mb_c == 0)
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004471 || (mb_l > 1 && (!vim_isprintc(mb_c))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 {
4473 /*
4474 * Illegal UTF-8 byte: display as <xx>.
4475 * Non-BMP character : display as ? or fullwidth ?.
4476 */
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004477 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004478# ifdef FEAT_RIGHTLEFT
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004479 if (wp->w_p_rl) /* reverse */
4480 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004481# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 p_extra = extra;
4483 c = *p_extra;
4484 mb_c = mb_ptr2char_adv(&p_extra);
4485 mb_utf8 = (c >= 0x80);
4486 n_extra = (int)STRLEN(p_extra);
4487 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004488 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 if (area_attr == 0 && search_attr == 0)
4490 {
4491 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004492 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 saved_attr2 = char_attr; /* save current attr */
4494 }
4495 }
4496 else if (mb_l == 0) /* at the NUL at end-of-line */
4497 mb_l = 1;
4498#ifdef FEAT_ARABIC
4499 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4500 {
4501 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004502 int pc, pc1, nc;
4503 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504
4505 /* The idea of what is the previous and next
4506 * character depends on 'rightleft'. */
4507 if (wp->w_p_rl)
4508 {
4509 pc = prev_c;
4510 pc1 = prev_c1;
4511 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004512 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 }
4514 else
4515 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004516 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004518 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 }
4520 prev_c = mb_c;
4521
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004522 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 }
4524 else
4525 prev_c = mb_c;
4526#endif
4527 }
4528 else /* enc_dbcs */
4529 {
4530 mb_l = MB_BYTE2LEN(c);
4531 if (mb_l == 0) /* at the NUL at end-of-line */
4532 mb_l = 1;
4533 else if (mb_l > 1)
4534 {
4535 /* We assume a second byte below 32 is illegal.
4536 * Hopefully this is OK for all double-byte encodings!
4537 */
4538 if (ptr[1] >= 32)
4539 mb_c = (c << 8) + ptr[1];
4540 else
4541 {
4542 if (ptr[1] == NUL)
4543 {
4544 /* head byte at end of line */
4545 mb_l = 1;
4546 transchar_nonprint(extra, c);
4547 }
4548 else
4549 {
4550 /* illegal tail byte */
4551 mb_l = 2;
4552 STRCPY(extra, "XX");
4553 }
4554 p_extra = extra;
4555 n_extra = (int)STRLEN(extra) - 1;
4556 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004557 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 c = *p_extra++;
4559 if (area_attr == 0 && search_attr == 0)
4560 {
4561 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004562 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 saved_attr2 = char_attr; /* save current attr */
4564 }
4565 mb_c = c;
4566 }
4567 }
4568 }
4569 /* If a double-width char doesn't fit display a '>' in the
4570 * last column; the character is displayed at the start of the
4571 * next line. */
4572 if ((
4573# ifdef FEAT_RIGHTLEFT
4574 wp->w_p_rl ? (col <= 0) :
4575# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004576 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 && (*mb_char2cells)(mb_c) == 2)
4578 {
4579 c = '>';
4580 mb_c = c;
4581 mb_utf8 = FALSE;
4582 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004583 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02004584 // Put pointer back so that the character will be
4585 // displayed at the start of the next line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 --ptr;
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02004587#ifdef FEAT_CONCEAL
4588 did_decrement_ptr = TRUE;
4589#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 }
4591 else if (*ptr != NUL)
4592 ptr += mb_l - 1;
4593
4594 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004595 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004596 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004597 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004600 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004601 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 c = ' ';
4603 if (area_attr == 0 && search_attr == 0)
4604 {
4605 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004606 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 saved_attr2 = char_attr; /* save current attr */
4608 }
4609 mb_c = c;
4610 mb_utf8 = FALSE;
4611 mb_l = 1;
4612 }
4613
4614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 ++ptr;
4616
4617 if (extra_check)
4618 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004619#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004620 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004621#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004622
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004623#ifdef FEAT_TERMINAL
4624 if (get_term_attr)
4625 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02004626 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004627
4628 if (!attr_pri)
4629 char_attr = syntax_attr;
4630 else
4631 char_attr = hl_combine_attr(syntax_attr, char_attr);
4632 }
4633#endif
4634
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004635#ifdef FEAT_SYN_HL
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004636 // Get syntax attribute, unless still at the start of the line
4637 // (double-wide char that doesn't fit).
Bram Moolenaar217ad922005-03-20 22:37:15 +00004638 v = (long)(ptr - line);
4639 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 {
4641 /* Get the syntax attribute for the character. If there
4642 * is an error, disable syntax highlighting. */
4643 save_did_emsg = did_emsg;
4644 did_emsg = FALSE;
4645
Bram Moolenaar217ad922005-03-20 22:37:15 +00004646 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004647# ifdef FEAT_SPELL
4648 has_spell ? &can_spell :
4649# endif
4650 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651
4652 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004653 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004654 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004655 has_syntax = FALSE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004656 syntax_attr = 0;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 else
4659 did_emsg = save_did_emsg;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004660
4661 // combine syntax attribute with 'wincolor'
4662 if (win_attr != 0)
4663 syntax_attr = hl_combine_attr(win_attr, syntax_attr);
4664
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004665#ifdef SYN_TIME_LIMIT
4666 if (wp->w_s->b_syn_slow)
4667 has_syntax = FALSE;
4668#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669
4670 /* Need to get the line again, a multi-line regexp may
4671 * have made it invalid. */
4672 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4673 ptr = line + v;
4674
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004675# ifdef FEAT_TEXT_PROP
Bram Moolenaarde24a872019-05-05 15:48:00 +02004676 // Text properties overrule syntax highlighting or combine.
4677 if (text_prop_attr == 0 || text_prop_combine)
4678# endif
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004679 {
Bram Moolenaarde24a872019-05-05 15:48:00 +02004680 int comb_attr = syntax_attr;
4681# ifdef FEAT_TEXT_PROP
4682 comb_attr = hl_combine_attr(text_prop_attr, comb_attr);
4683# endif
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004684 if (!attr_pri)
Bram Moolenaarde24a872019-05-05 15:48:00 +02004685 char_attr = comb_attr;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004686 else
Bram Moolenaarde24a872019-05-05 15:48:00 +02004687 char_attr = hl_combine_attr(comb_attr, char_attr);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004688 }
Bram Moolenaarc095b282010-07-20 22:33:34 +02004689# ifdef FEAT_CONCEAL
4690 /* no concealing past the end of the line, it interferes
4691 * with line highlighting */
4692 if (c == NUL)
4693 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004694 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004695 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004696# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004698#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004699
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004700#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004701 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004702 * Only do this when there is no syntax highlighting, the
4703 * @Spell cluster is not used or the current syntax item
4704 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004705 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004706 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004707 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004708 if (c != 0 && (
4709# ifdef FEAT_SYN_HL
4710 !has_syntax ||
4711# endif
4712 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004713 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004714 char_u *prev_ptr, *p;
4715 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004716 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaare7566042005-06-17 22:00:15 +00004717 if (has_mbyte)
4718 {
4719 prev_ptr = ptr - mb_l;
4720 v -= mb_l - 1;
4721 }
4722 else
Bram Moolenaare7566042005-06-17 22:00:15 +00004723 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004724
4725 /* Use nextline[] if possible, it has the start of the
4726 * next line concatenated. */
4727 if ((prev_ptr - line) - nextlinecol >= 0)
4728 p = nextline + (prev_ptr - line) - nextlinecol;
4729 else
4730 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004731 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004732 len = spell_check(wp, p, &spell_hlf, &cap_col,
4733 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004734 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004735
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004736 /* In Insert mode only highlight a word that
4737 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004738 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004739 && (State & INSERT) != 0
4740 && wp->w_cursor.lnum == lnum
4741 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004742 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004743 && wp->w_cursor.col < (colnr_T)word_end)
4744 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004745 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004746 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004747 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004748
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004749 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004750 && (p - nextline) + len > nextline_idx)
4751 {
4752 /* Remember that the good word continues at the
4753 * start of the next line. */
4754 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004755 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004756 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004757
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004758 /* Turn index into actual attributes. */
4759 if (spell_hlf != HLF_COUNT)
4760 spell_attr = highlight_attr[spell_hlf];
4761
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004762 if (cap_col > 0)
4763 {
4764 if (p != prev_ptr
4765 && (p - nextline) + cap_col >= nextline_idx)
4766 {
4767 /* Remember that the word in the next line
4768 * must start with a capital. */
4769 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004770 cap_col = (int)((p - nextline) + cap_col
4771 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004772 }
4773 else
4774 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004775 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004776 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004777 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004778 }
4779 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004780 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004781 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004782 char_attr = hl_combine_attr(char_attr, spell_attr);
4783 else
4784 char_attr = hl_combine_attr(spell_attr, char_attr);
4785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786#endif
4787#ifdef FEAT_LINEBREAK
4788 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004789 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004791 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004792 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 {
Bram Moolenaar4df70292015-03-21 14:20:16 +01004794 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004795 char_u *p = ptr - (mb_off + 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004796
Bram Moolenaar597a4222014-06-25 14:39:50 +02004797 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004798 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004799 NULL) - 1;
Bram Moolenaar02631462017-09-22 15:20:32 +02004800 if (c == TAB && n_extra + col > wp->w_width)
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004801# ifdef FEAT_VARTABS
Bram Moolenaara87b72c2018-06-25 21:24:51 +02004802 n_extra = tabstop_padding(vcol, wp->w_buffer->b_p_ts,
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004803 wp->w_buffer->b_p_vts_array) - 1;
4804# else
Bram Moolenaara3650912014-11-19 13:21:57 +01004805 n_extra = (int)wp->w_buffer->b_p_ts
4806 - vcol % (int)wp->w_buffer->b_p_ts - 1;
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004807# endif
Bram Moolenaara3650912014-11-19 13:21:57 +01004808
Bram Moolenaar4df70292015-03-21 14:20:16 +01004809 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004810 c_final = NUL;
Bram Moolenaar1c465442017-03-12 20:10:05 +01004811 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004812 {
4813#ifdef FEAT_CONCEAL
4814 if (c == TAB)
4815 /* See "Tab alignment" below. */
4816 FIX_FOR_BOGUSCOLS;
4817#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004818 if (!wp->w_p_list)
4819 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 }
4822#endif
4823
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004824 // 'list': Change char 160 to lcs_nbsp and space to lcs_space.
4825 // But not when the character is followed by a composing
4826 // character (use mb_l to check that).
4827 if (wp->w_p_list
4828 && ((((c == 160 && mb_l == 1)
4829 || (mb_utf8
4830 && ((mb_c == 160 && mb_l == 2)
4831 || (mb_c == 0x202f && mb_l == 3))))
4832 && lcs_nbsp)
4833 || (c == ' '
4834 && mb_l == 1
4835 && lcs_space
4836 && ptr - line <= trailcol)))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004837 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004838 c = (c == ' ') ? lcs_space : lcs_nbsp;
4839 if (area_attr == 0 && search_attr == 0)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004840 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004841 n_attr = 1;
4842 extra_attr = HL_ATTR(HLF_8);
4843 saved_attr2 = char_attr; /* save current attr */
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004844 }
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004845 mb_c = c;
4846 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004847 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004848 mb_utf8 = TRUE;
4849 u8cc[0] = 0;
4850 c = 0xc0;
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004851 }
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004852 else
4853 mb_utf8 = FALSE;
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004854 }
4855
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4857 {
4858 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004859 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 {
4861 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004862 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 saved_attr2 = char_attr; /* save current attr */
4864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004866 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 {
4868 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004869 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004870 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 }
4872 else
4873 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 }
4875 }
4876
4877 /*
4878 * Handling of non-printable characters.
4879 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004880 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 {
4882 /*
4883 * when getting a character from the file, we may have to
4884 * turn it into something else on the way to putting it
4885 * into "ScreenLines".
4886 */
4887 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4888 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004889 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004890 long vcol_adjusted = vcol; /* removed showbreak length */
4891#ifdef FEAT_LINEBREAK
4892 /* only adjust the tab_len, when at the first column
4893 * after the showbreak value was drawn */
4894 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4895 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4896#endif
Bram Moolenaar69cbbec2019-08-17 14:10:56 +02004897 // tab amount depends on current column
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004898#ifdef FEAT_VARTABS
4899 tab_len = tabstop_padding(vcol_adjusted,
4900 wp->w_buffer->b_p_ts,
4901 wp->w_buffer->b_p_vts_array) - 1;
4902#else
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004903 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004904 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4905#endif
Bram Moolenaard574ea22015-01-14 19:35:14 +01004906
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004907#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004908 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004909#endif
Bram Moolenaar69cbbec2019-08-17 14:10:56 +02004910 // tab amount depends on current column
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004911 n_extra = tab_len;
4912#ifdef FEAT_LINEBREAK
4913 else
4914 {
Bram Moolenaar69cbbec2019-08-17 14:10:56 +02004915 char_u *p;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01004916 int len;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004917 int i;
4918 int saved_nextra = n_extra;
4919
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004920#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004921 if (vcol_off > 0)
Bram Moolenaar69cbbec2019-08-17 14:10:56 +02004922 // there are characters to conceal
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004923 tab_len += vcol_off;
Bram Moolenaar69cbbec2019-08-17 14:10:56 +02004924 // boguscols before FIX_FOR_BOGUSCOLS macro from above
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004925 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
4926 && n_extra > tab_len)
4927 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004928#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004929
Bram Moolenaar69cbbec2019-08-17 14:10:56 +02004930 // if n_extra > 0, it gives the number of chars, to
4931 // use for a tab, else we need to calculate the width
4932 // for a tab
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004933 len = (tab_len * mb_char2len(lcs_tab2));
4934 if (n_extra > 0)
4935 len += n_extra - tab_len;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004936 c = lcs_tab1;
Bram Moolenaar964b3742019-05-24 18:54:09 +02004937 p = alloc(len + 1);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004938 vim_memset(p, ' ', len);
4939 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004940 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004941 p_extra_free = p;
4942 for (i = 0; i < tab_len; i++)
4943 {
Bram Moolenaar69cbbec2019-08-17 14:10:56 +02004944 int lcs = lcs_tab2;
4945
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004946 if (*p == NUL)
4947 {
4948 tab_len = i;
4949 break;
4950 }
Bram Moolenaar69cbbec2019-08-17 14:10:56 +02004951
4952 // if lcs_tab3 is given, need to change the char
4953 // for tab
4954 if (lcs_tab3 && i == tab_len - 1)
4955 lcs = lcs_tab3;
4956 mb_char2bytes(lcs, p);
4957 p += mb_char2len(lcs);
4958 n_extra += mb_char2len(lcs)
4959 - (saved_nextra > 0 ? 1 : 0);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004960 }
4961 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004962#ifdef FEAT_CONCEAL
Bram Moolenaar69cbbec2019-08-17 14:10:56 +02004963 // n_extra will be increased by FIX_FOX_BOGUSCOLS
4964 // macro below, so need to adjust for that here
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004965 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004966 n_extra -= vcol_off;
4967#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004968 }
4969#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004970#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004971 {
4972 int vc_saved = vcol_off;
4973
4974 /* Tab alignment should be identical regardless of
4975 * 'conceallevel' value. So tab compensates of all
4976 * previous concealed characters, and thus resets
4977 * vcol_off and boguscols accumulated so far in the
4978 * line. Note that the tab can be longer than
4979 * 'tabstop' when there are concealed characters. */
4980 FIX_FOR_BOGUSCOLS;
4981
4982 /* Make sure, the highlighting for the tab char will be
4983 * correctly set further below (effectively reverts the
4984 * FIX_FOR_BOGSUCOLS macro */
4985 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004986 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004987 tab_len += vc_saved;
4988 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004989#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 if (wp->w_p_list)
4992 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004993 c = (n_extra == 0 && lcs_tab3) ? lcs_tab3 : lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004994#ifdef FEAT_LINEBREAK
4995 if (wp->w_p_lbr)
4996 c_extra = NUL; /* using p_extra from above */
4997 else
4998#endif
4999 c_extra = lcs_tab2;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005000 c_final = lcs_tab3;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005001 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005002 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 saved_attr2 = char_attr; /* save current attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005005 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 {
5007 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005008 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005009 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 }
5012 else
5013 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01005014 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 c_extra = ' ';
5016 c = ' ';
5017 }
5018 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005019 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02005020 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005021 || ((fromcol >= 0 || fromcol_prev >= 0)
5022 && tocol > vcol
5023 && VIsual_mode != Ctrl_V
5024 && (
5025# ifdef FEAT_RIGHTLEFT
5026 wp->w_p_rl ? (col >= 0) :
5027# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005028 (col < wp->w_width))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005029 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005030 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005031 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02005032 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005034 /* Display a '$' after the line or highlight an extra
5035 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036#if defined(FEAT_DIFF) || defined(LINE_ATTR)
5037 /* For a diff line the highlighting continues after the
5038 * "$". */
5039 if (
5040# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005041 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042# ifdef LINE_ATTR
5043 &&
5044# endif
5045# endif
5046# ifdef LINE_ATTR
5047 line_attr == 0
5048# endif
5049 )
5050#endif
5051 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 /* In virtualedit, visual selections may extend
5053 * beyond end of line. */
5054 if (area_highlighting && virtual_active()
5055 && tocol != MAXCOL && vcol < tocol)
5056 n_extra = 0;
5057 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 {
5059 p_extra = at_end_str;
5060 n_extra = 1;
5061 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005062 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 }
5064 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02005065 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005066 c = lcs_eol;
5067 else
5068 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 lcs_eol_one = -1;
5070 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005071 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005073 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 n_attr = 1;
5075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005077 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 {
5079 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005080 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005081 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 }
5083 else
5084 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 }
5086 else if (c != NUL)
5087 {
5088 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02005089 if (n_extra == 0)
5090 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091#ifdef FEAT_RIGHTLEFT
5092 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
5093 rl_mirror(p_extra); /* reverse "<12>" */
5094#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005096 c_final = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005097#ifdef FEAT_LINEBREAK
5098 if (wp->w_p_lbr)
5099 {
5100 char_u *p;
5101
5102 c = *p_extra;
Bram Moolenaar964b3742019-05-24 18:54:09 +02005103 p = alloc(n_extra + 1);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005104 vim_memset(p, ' ', n_extra);
5105 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5106 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005107 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005108 p_extra_free = p_extra = p;
5109 }
5110 else
5111#endif
5112 {
5113 n_extra = byte2cells(c) - 1;
5114 c = *p_extra++;
5115 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005116 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 {
5118 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005119 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 saved_attr2 = char_attr; /* save current attr */
5121 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 else if (VIsual_active
5125 && (VIsual_mode == Ctrl_V
5126 || VIsual_mode == 'v')
5127 && virtual_active()
5128 && tocol != MAXCOL
5129 && vcol < tocol
5130 && (
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005131#ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 wp->w_p_rl ? (col >= 0) :
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005133#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005134 (col < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 {
5136 c = ' ';
5137 --ptr; /* put it back at the NUL */
5138 }
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005139#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 else if ((
5141# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005142 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005144# ifdef FEAT_TERMINAL
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005145 win_attr != 0 ||
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005146# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 ) && (
5149# ifdef FEAT_RIGHTLEFT
5150 wp->w_p_rl ? (col >= 0) :
5151# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005152 (col
5153# ifdef FEAT_CONCEAL
5154 - boguscols
5155# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005156 < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 {
5158 /* Highlight until the right side of the window */
5159 c = ' ';
5160 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005161
5162 /* Remember we do the char for line highlighting. */
5163 ++did_line_attr;
5164
5165 /* don't do search HL for the rest of the line */
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005166 if (line_attr != 0 && char_attr == search_attr
5167 && (did_line_attr > 1
5168 || (wp->w_p_list && lcs_eol > 0)))
Bram Moolenaar91170f82006-05-05 21:15:17 +00005169 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170# ifdef FEAT_DIFF
5171 if (diff_hlf == HLF_TXD)
5172 {
5173 diff_hlf = HLF_CHD;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005174 if (vi_attr == 0 || char_attr != vi_attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005175 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005176 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005177 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5178 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005179 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 }
5182# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005183# ifdef FEAT_TERMINAL
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005184 if (win_attr != 0)
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005185 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005186 char_attr = win_attr;
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005187 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5188 char_attr = hl_combine_attr(char_attr,
5189 HL_ATTR(HLF_CUL));
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02005190 else if (line_attr)
5191 char_attr = hl_combine_attr(char_attr, line_attr);
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005192 }
5193# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 }
5195#endif
5196 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005197
5198#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005199 if ( wp->w_p_cole > 0
5200 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaarab62c192019-03-30 16:39:05 +01005201 conceal_cursor_line(wp))
5202 && ((syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005203 && !(lnum_in_visual_area
5204 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005205 {
5206 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005207 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005208 && (syn_get_sub_char() != NUL || match_conc
5209 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005210 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005211 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005212 /* First time at this concealed item: display one
5213 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005214 if (match_conc)
5215 c = match_conc;
5216 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005217 c = syn_get_sub_char();
5218 else if (lcs_conceal != NUL)
5219 c = lcs_conceal;
5220 else
5221 c = ' ';
5222
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005223 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005224
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005225 if (n_extra > 0)
5226 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005227 vcol += n_extra;
5228 if (wp->w_p_wrap && n_extra > 0)
5229 {
5230# ifdef FEAT_RIGHTLEFT
5231 if (wp->w_p_rl)
5232 {
5233 col -= n_extra;
5234 boguscols -= n_extra;
5235 }
5236 else
5237# endif
5238 {
5239 boguscols += n_extra;
5240 col += n_extra;
5241 }
5242 }
5243 n_extra = 0;
5244 n_attr = 0;
5245 }
5246 else if (n_skip == 0)
5247 {
5248 is_concealing = TRUE;
5249 n_skip = 1;
5250 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005251 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005252 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005253 {
5254 mb_utf8 = TRUE;
5255 u8cc[0] = 0;
5256 c = 0xc0;
5257 }
5258 else
5259 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005260 }
5261 else
5262 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005263 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005264 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005265 }
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02005266
5267 if (n_skip > 0 && did_decrement_ptr)
5268 // not showing the '>', put pointer back to avoid getting stuck
5269 ++ptr;
5270
5271#endif // FEAT_CONCEAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 }
5273
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005274#ifdef FEAT_CONCEAL
5275 /* In the cursor line and we may be concealing characters: correct
5276 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005277 if (!did_wcol && draw_state == WL_LINE
5278 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005279 && conceal_cursor_line(wp)
5280 && (int)wp->w_virtcol <= vcol + n_skip)
5281 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005282# ifdef FEAT_RIGHTLEFT
5283 if (wp->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02005284 wp->w_wcol = wp->w_width - col + boguscols - 1;
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005285 else
5286# endif
5287 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005288 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005289 did_wcol = TRUE;
5290 }
5291#endif
5292
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 /* Don't override visual selection highlighting. */
5294 if (n_attr > 0
5295 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005296 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 char_attr = extra_attr;
5298
Bram Moolenaar81695252004-12-29 20:58:21 +00005299#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 /* XIM don't send preedit_start and preedit_end, but they send
5301 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5302 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005303 if (p_imst == IM_ON_THE_SPOT
5304 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005305 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 && (State & INSERT)
5307 && !p_imdisable
5308 && im_is_preediting()
5309 && draw_state == WL_LINE)
5310 {
5311 colnr_T tcol;
5312
5313 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005314 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 else
5316 tcol = preedit_end_col;
5317 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5318 {
5319 if (feedback_old_attr < 0)
5320 {
5321 feedback_col = 0;
5322 feedback_old_attr = char_attr;
5323 }
5324 char_attr = im_get_feedback_attr(feedback_col);
5325 if (char_attr < 0)
5326 char_attr = feedback_old_attr;
5327 feedback_col++;
5328 }
5329 else if (feedback_old_attr >= 0)
5330 {
5331 char_attr = feedback_old_attr;
5332 feedback_old_attr = -1;
5333 feedback_col = 0;
5334 }
5335 }
5336#endif
5337 /*
5338 * Handle the case where we are in column 0 but not on the first
5339 * character of the line and the user wants us to show us a
5340 * special character (via 'listchars' option "precedes:<char>".
5341 */
5342 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005343 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5345#ifdef FEAT_DIFF
5346 && filler_todo <= 0
5347#endif
5348 && draw_state > WL_NR
5349 && c != NUL)
5350 {
5351 c = lcs_prec;
5352 lcs_prec_todo = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005353 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5354 {
5355 /* Double-width character being overwritten by the "precedes"
5356 * character, need to fill up half the character. */
5357 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005358 c_final = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005359 n_extra = 1;
5360 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005361 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005364 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 {
5366 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005367 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005368 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 }
5370 else
5371 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005372 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 {
5374 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005375 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 n_attr3 = 1;
5377 }
5378 }
5379
5380 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005381 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 */
Bram Moolenaarf914a332019-07-20 15:09:56 +02005383 if ((c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005384#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005385 || did_line_attr == 1
5386#endif
Bram Moolenaarf914a332019-07-20 15:09:56 +02005387 ) && eol_hl_off == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005389#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarbbca7732019-07-24 18:13:16 +02005390 // flag to indicate whether prevcol equals startcol of search_hl or
5391 // one of the matches
5392 int prevcol_hl_flag = get_prevcol_hl_flag(wp, &search_hl,
5393 (long)(ptr - line) - (c == NUL));
Bram Moolenaar91170f82006-05-05 21:15:17 +00005394#endif
Bram Moolenaarbbca7732019-07-24 18:13:16 +02005395 // Invert at least one char, used for Visual and empty line or
5396 // highlight match at end of line. If it's beyond the last
5397 // char on the screen, just overwrite that one (tricky!) Not
5398 // needed when a '$' was displayed for 'list'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005400 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005401 && (VIsual_mode != Ctrl_V
5402 || lnum == VIsual.lnum
5403 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005404 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarbbca7732019-07-24 18:13:16 +02005406 // highlight 'hlsearch' match at end of line
5407 || (prevcol_hl_flag
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005408# ifdef FEAT_SYN_HL
5409 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
5410 && !(wp == curwin && VIsual_active))
5411# endif
5412# ifdef FEAT_DIFF
5413 && diff_hlf == (hlf_T)0
5414# endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005415# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005416 && did_line_attr <= 1
5417# endif
5418 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419#endif
5420 ))
5421 {
5422 int n = 0;
5423
5424#ifdef FEAT_RIGHTLEFT
5425 if (wp->w_p_rl)
5426 {
5427 if (col < 0)
5428 n = 1;
5429 }
5430 else
5431#endif
5432 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005433 if (col >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 n = -1;
5435 }
5436 if (n != 0)
5437 {
5438 /* At the window boundary, highlight the last character
5439 * instead (better than nothing). */
5440 off += n;
5441 col += n;
5442 }
5443 else
5444 {
5445 /* Add a blank character to highlight. */
5446 ScreenLines[off] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 if (enc_utf8)
5448 ScreenLinesUC[off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449 }
5450#ifdef FEAT_SEARCH_EXTRA
5451 if (area_attr == 0)
5452 {
Bram Moolenaarbbca7732019-07-24 18:13:16 +02005453 // Use attributes from match with highest priority among
5454 // 'search_hl' and the match list.
5455 get_search_match_hl(wp, &search_hl,
5456 (long)(ptr - line), &char_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 }
5458#endif
5459 ScreenAttrs[off] = char_attr;
5460#ifdef FEAT_RIGHTLEFT
5461 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005462 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005464 --off;
5465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 else
5467#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005468 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005470 ++off;
5471 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005472 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005473 eol_hl_off = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005475 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476
Bram Moolenaar91170f82006-05-05 21:15:17 +00005477 /*
5478 * At end of the text line.
5479 */
5480 if (c == NUL)
5481 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005482#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005483 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005484 if (wp->w_p_wrap)
5485 v = wp->w_skipcol;
5486 else
5487 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005488
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005489 /* check if line ends before left margin */
5490 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005491 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005492#ifdef FEAT_CONCEAL
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01005493 // Get rid of the boguscols now, we want to draw until the right
5494 // edge for 'cursorcolumn'.
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005495 col -= boguscols;
5496 boguscols = 0;
5497#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005498
5499 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005500 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005501
5502 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005503 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5504 && (int)wp->w_virtcol <
Bram Moolenaar02631462017-09-22 15:20:32 +02005505 wp->w_width * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005506 && lnum != wp->w_cursor.lnum)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005507 || draw_color_col
5508 || win_attr != 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005509# ifdef FEAT_RIGHTLEFT
5510 && !wp->w_p_rl
5511# endif
5512 )
5513 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005514 int rightmost_vcol = 0;
5515 int i;
5516
5517 if (wp->w_p_cuc)
5518 rightmost_vcol = wp->w_virtcol;
5519 if (draw_color_col)
5520 /* determine rightmost colorcolumn to possibly draw */
5521 for (i = 0; color_cols[i] >= 0; ++i)
5522 if (rightmost_vcol < color_cols[i])
5523 rightmost_vcol = color_cols[i];
5524
Bram Moolenaar02631462017-09-22 15:20:32 +02005525 while (col < wp->w_width)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005526 {
5527 ScreenLines[off] = ' ';
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005528 if (enc_utf8)
5529 ScreenLinesUC[off] = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005530 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005531 if (draw_color_col)
5532 draw_color_col = advance_color_col(VCOL_HLC,
5533 &color_cols);
5534
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005535 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005536 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005537 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005538 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005539 else
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005540 ScreenAttrs[off++] = win_attr;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005541
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005542 if (VCOL_HLC >= rightmost_vcol && win_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005543 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005544
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005545 ++vcol;
5546 }
5547 }
5548#endif
5549
Bram Moolenaar53f81742017-09-22 14:35:51 +02005550 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005551 (int)wp->w_width, screen_line_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 row++;
5553
5554 /*
5555 * Update w_cline_height and w_cline_folded if the cursor line was
5556 * updated (saves a call to plines() later).
5557 */
5558 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5559 {
5560 curwin->w_cline_row = startrow;
5561 curwin->w_cline_height = row - startrow;
5562#ifdef FEAT_FOLDING
5563 curwin->w_cline_folded = FALSE;
5564#endif
5565 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5566 }
5567
5568 break;
5569 }
5570
Bram Moolenaara5c6a0b2019-05-08 20:20:46 +02005571 // Show "extends" character from 'listchars' if beyond the line end and
5572 // 'list' is set.
5573 if (lcs_ext != NUL
5574 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 && !wp->w_p_wrap
5576#ifdef FEAT_DIFF
5577 && filler_todo <= 0
5578#endif
5579 && (
5580#ifdef FEAT_RIGHTLEFT
5581 wp->w_p_rl ? col == 0 :
5582#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005583 col == wp->w_width - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005585 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5587 {
5588 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005589 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005591 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 {
5593 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005594 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005595 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 }
5597 else
5598 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 }
5600
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005601#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005602 /* advance to the next 'colorcolumn' */
5603 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005604 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005605
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005606 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005607 * highlight the cursor position itself.
5608 * Also highlight the 'colorcolumn' if it is different than
5609 * 'cursorcolumn' */
5610 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005611 if (draw_state == WL_LINE && !lnum_in_visual_area
5612 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005613 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005614 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005615 && lnum != wp->w_cursor.lnum)
5616 {
5617 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005618 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005619 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005620 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005621 {
5622 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005623 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005624 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005625 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005626#endif
5627
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 /*
5629 * Store character to be displayed.
5630 * Skip characters that are left of the screen for 'nowrap'.
5631 */
5632 vcol_prev = vcol;
5633 if (draw_state < WL_LINE || n_skip <= 0)
5634 {
5635 /*
5636 * Store the character.
5637 */
Bram Moolenaara12a1612019-01-24 16:39:02 +01005638#if defined(FEAT_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5640 {
5641 /* A double-wide character is: put first halve in left cell. */
5642 --off;
5643 --col;
5644 }
5645#endif
5646 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005648 {
5649 if ((mb_c & 0xff00) == 0x8e00)
5650 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 else if (enc_utf8)
5654 {
5655 if (mb_utf8)
5656 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005657 int i;
5658
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005660 if ((c & 0xff) == 0)
5661 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005662 for (i = 0; i < Screen_mco; ++i)
5663 {
5664 ScreenLinesC[i][off] = u8cc[i];
5665 if (u8cc[i] == 0)
5666 break;
5667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 }
5669 else
5670 ScreenLinesUC[off] = 0;
5671 }
5672 if (multi_attr)
5673 {
5674 ScreenAttrs[off] = multi_attr;
5675 multi_attr = 0;
5676 }
5677 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 ScreenAttrs[off] = char_attr;
5679
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5681 {
5682 /* Need to fill two screen columns. */
5683 ++off;
5684 ++col;
5685 if (enc_utf8)
5686 /* UTF-8: Put a 0 in the second screen char. */
5687 ScreenLines[off] = 0;
5688 else
5689 /* DBCS: Put second byte in the second screen char. */
5690 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005691 if (draw_state > WL_NR
5692#ifdef FEAT_DIFF
5693 && filler_todo <= 0
5694#endif
5695 )
5696 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 /* When "tocol" is halfway a character, set it to the end of
5698 * the character, otherwise highlighting won't stop. */
5699 if (tocol == vcol)
5700 ++tocol;
5701#ifdef FEAT_RIGHTLEFT
5702 if (wp->w_p_rl)
5703 {
5704 /* now it's time to backup one cell */
5705 --off;
5706 --col;
5707 }
5708#endif
5709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710#ifdef FEAT_RIGHTLEFT
5711 if (wp->w_p_rl)
5712 {
5713 --off;
5714 --col;
5715 }
5716 else
5717#endif
5718 {
5719 ++off;
5720 ++col;
5721 }
5722 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005723#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005724 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005725 {
5726 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005727 ++vcol_off;
5728 if (n_extra > 0)
5729 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005730 if (wp->w_p_wrap)
5731 {
5732 /*
5733 * Special voodoo required if 'wrap' is on.
5734 *
5735 * Advance the column indicator to force the line
5736 * drawing to wrap early. This will make the line
5737 * take up the same screen space when parts are concealed,
5738 * so that cursor line computations aren't messed up.
5739 *
5740 * To avoid the fictitious advance of 'col' causing
5741 * trailing junk to be written out of the screen line
5742 * we are building, 'boguscols' keeps track of the number
5743 * of bad columns we have advanced.
5744 */
5745 if (n_extra > 0)
5746 {
5747 vcol += n_extra;
5748# ifdef FEAT_RIGHTLEFT
5749 if (wp->w_p_rl)
5750 {
5751 col -= n_extra;
5752 boguscols -= n_extra;
5753 }
5754 else
5755# endif
5756 {
5757 col += n_extra;
5758 boguscols += n_extra;
5759 }
5760 n_extra = 0;
5761 n_attr = 0;
5762 }
5763
5764
Bram Moolenaar860cae12010-06-05 23:22:07 +02005765 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5766 {
5767 /* Need to fill two screen columns. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01005768# ifdef FEAT_RIGHTLEFT
Bram Moolenaar860cae12010-06-05 23:22:07 +02005769 if (wp->w_p_rl)
5770 {
5771 --boguscols;
5772 --col;
5773 }
5774 else
Bram Moolenaara12a1612019-01-24 16:39:02 +01005775# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02005776 {
5777 ++boguscols;
5778 ++col;
5779 }
5780 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005781
5782# ifdef FEAT_RIGHTLEFT
5783 if (wp->w_p_rl)
5784 {
5785 --boguscols;
5786 --col;
5787 }
5788 else
5789# endif
5790 {
5791 ++boguscols;
5792 ++col;
5793 }
5794 }
5795 else
5796 {
5797 if (n_extra > 0)
5798 {
5799 vcol += n_extra;
5800 n_extra = 0;
5801 n_attr = 0;
5802 }
5803 }
5804
5805 }
5806#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 else
5808 --n_skip;
5809
Bram Moolenaar64486672010-05-16 15:46:46 +02005810 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5811 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005812 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813#ifdef FEAT_DIFF
5814 && filler_todo <= 0
5815#endif
5816 )
5817 ++vcol;
5818
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005819#ifdef FEAT_SYN_HL
5820 if (vcol_save_attr >= 0)
5821 char_attr = vcol_save_attr;
5822#endif
5823
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 /* restore attributes after "predeces" in 'listchars' */
5825 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5826 char_attr = saved_attr3;
5827
5828 /* restore attributes after last 'listchars' or 'number' char */
5829 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5830 char_attr = saved_attr2;
5831
5832 /*
5833 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005834 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 */
5836 if ((
5837#ifdef FEAT_RIGHTLEFT
5838 wp->w_p_rl ? (col < 0) :
5839#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005840 (col >= wp->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 && (*ptr != NUL
5842#ifdef FEAT_DIFF
5843 || filler_todo > 0
5844#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005845 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5847 )
5848 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005849#ifdef FEAT_CONCEAL
Bram Moolenaar53f81742017-09-22 14:35:51 +02005850 screen_line(screen_row, wp->w_wincol, col - boguscols,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005851 (int)wp->w_width, screen_line_flags);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005852 boguscols = 0;
5853#else
Bram Moolenaar53f81742017-09-22 14:35:51 +02005854 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005855 (int)wp->w_width, screen_line_flags);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005856#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 ++row;
5858 ++screen_row;
5859
5860 /* When not wrapping and finished diff lines, or when displayed
5861 * '$' and highlighting until last column, break here. */
5862 if ((!wp->w_p_wrap
5863#ifdef FEAT_DIFF
5864 && filler_todo <= 0
5865#endif
5866 ) || lcs_eol_one == -1)
5867 break;
5868
5869 /* When the window is too narrow draw all "@" lines. */
5870 if (draw_state != WL_LINE
5871#ifdef FEAT_DIFF
5872 && filler_todo <= 0
5873#endif
5874 )
5875 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01005876 win_draw_end(wp, '@', ' ', TRUE, row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 row = endrow;
5879 }
5880
5881 /* When line got too long for screen break here. */
5882 if (row == endrow)
5883 {
5884 ++row;
5885 break;
5886 }
5887
5888 if (screen_cur_row == screen_row - 1
5889#ifdef FEAT_DIFF
5890 && filler_todo <= 0
5891#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005892 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893 {
5894 /* Remember that the line wraps, used for modeless copy. */
5895 LineWraps[screen_row - 1] = TRUE;
5896
5897 /*
5898 * Special trick to make copy/paste of wrapped lines work with
5899 * xterm/screen: write an extra character beyond the end of
5900 * the line. This will work with all terminal types
5901 * (regardless of the xn,am settings).
5902 * Only do this on a fast tty.
5903 * Only do this if the cursor is on the current line
5904 * (something has been written in it).
5905 * Don't do this for the GUI.
5906 * Don't do this for double-width characters.
5907 * Don't do this for a window not at the right screen border.
5908 */
5909 if (p_tf
5910#ifdef FEAT_GUI
5911 && !gui.in_use
5912#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005914 && ((*mb_off2cells)(LineOffset[screen_row],
5915 LineOffset[screen_row] + screen_Columns)
5916 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005918 + (int)Columns - 2,
5919 LineOffset[screen_row] + screen_Columns)
Bram Moolenaara12a1612019-01-24 16:39:02 +01005920 == 2)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921 {
5922 /* First make sure we are at the end of the screen line,
5923 * then output the same character again to let the
5924 * terminal know about the wrap. If the terminal doesn't
5925 * auto-wrap, we overwrite the character. */
Bram Moolenaar02631462017-09-22 15:20:32 +02005926 if (screen_cur_col != wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927 screen_char(LineOffset[screen_row - 1]
5928 + (unsigned)Columns - 1,
5929 screen_row - 1, (int)(Columns - 1));
5930
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931 /* When there is a multi-byte character, just output a
5932 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005933 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5934 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935 out_char(' ');
5936 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 out_char(ScreenLines[LineOffset[screen_row - 1]
5938 + (Columns - 1)]);
5939 /* force a redraw of the first char on the next line */
5940 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5941 screen_start(); /* don't know where cursor is now */
5942 }
5943 }
5944
5945 col = 0;
5946 off = (unsigned)(current_ScreenLine - ScreenLines);
5947#ifdef FEAT_RIGHTLEFT
5948 if (wp->w_p_rl)
5949 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005950 col = wp->w_width - 1; /* col is not used if breaking! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951 off += col;
5952 }
5953#endif
5954
5955 /* reset the drawing state for the start of a wrapped line */
5956 draw_state = WL_START;
5957 saved_n_extra = n_extra;
5958 saved_p_extra = p_extra;
5959 saved_c_extra = c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005960 saved_c_final = c_final;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961 saved_char_attr = char_attr;
5962 n_extra = 0;
5963 lcs_prec_todo = lcs_prec;
5964#ifdef FEAT_LINEBREAK
5965# ifdef FEAT_DIFF
5966 if (filler_todo <= 0)
5967# endif
5968 need_showbreak = TRUE;
5969#endif
5970#ifdef FEAT_DIFF
5971 --filler_todo;
5972 /* When the filler lines are actually below the last line of the
5973 * file, don't draw the line itself, break here. */
5974 if (filler_todo == 0 && wp->w_botfill)
5975 break;
5976#endif
5977 }
5978
5979 } /* for every character in the line */
5980
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005981#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005982 /* After an empty line check first word for capital. */
5983 if (*skipwhite(line) == NUL)
5984 {
5985 capcol_lnum = lnum + 1;
5986 cap_col = 0;
5987 }
5988#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01005989#ifdef FEAT_TEXT_PROP
5990 vim_free(text_props);
5991 vim_free(text_prop_idxs);
5992#endif
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005993
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005994 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995 return row;
5996}
5997
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005998/*
5999 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006000 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006001 */
6002 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006003comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006004{
6005 int i;
6006
6007 for (i = 0; i < Screen_mco; ++i)
6008 {
6009 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
6010 return TRUE;
6011 if (ScreenLinesC[i][off_from] == 0)
6012 break;
6013 }
6014 return FALSE;
6015}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006016
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017/*
6018 * Check whether the given character needs redrawing:
6019 * - the (first byte of the) character is different
6020 * - the attributes are different
6021 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006022 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023 */
6024 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006025char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026{
6027 if (cols > 0
6028 && ((ScreenLines[off_from] != ScreenLines[off_to]
6029 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030 || (enc_dbcs != 0
6031 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
6032 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
6033 ? ScreenLines2[off_from] != ScreenLines2[off_to]
6034 : (cols > 1 && ScreenLines[off_from + 1]
6035 != ScreenLines[off_to + 1])))
6036 || (enc_utf8
6037 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6038 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006039 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006040 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6041 && ScreenLines[off_from + 1]
Bram Moolenaara12a1612019-01-24 16:39:02 +01006042 != ScreenLines[off_to + 1])))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043 return TRUE;
6044 return FALSE;
6045}
6046
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006047#if defined(FEAT_TERMINAL) || defined(PROTO)
6048/*
6049 * Return the index in ScreenLines[] for the current screen line.
6050 */
6051 int
6052screen_get_current_line_off()
6053{
6054 return (int)(current_ScreenLine - ScreenLines);
6055}
6056#endif
6057
Bram Moolenaarc662ec92019-06-23 00:15:57 +02006058#ifdef FEAT_TEXT_PROP
6059/*
6060 * Return TRUE if this position has a higher level popup or this cell is
6061 * transparent in the current popup.
6062 */
6063 static int
6064blocked_by_popup(int row, int col)
6065{
6066 int off;
6067
6068 if (!popup_visible)
6069 return FALSE;
6070 off = row * screen_Columns + col;
6071 return popup_mask[off] > screen_zindex || popup_transparent[off];
6072}
6073#endif
6074
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075/*
6076 * Move one "cooked" screen line to the screen, but only the characters that
6077 * have actually changed. Handle insert/delete character.
6078 * "coloff" gives the first column on the screen for this line.
6079 * "endcol" gives the columns where valid characters are.
6080 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6081 * needs to be cleared, negative otherwise.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006082 * "flags" can have bits:
6083 * SLF_POPUP popup window
6084 * SLF_RIGHTLEFT rightleft window:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6086 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6087 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006088 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006089screen_line(
6090 int row,
6091 int coloff,
6092 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006093 int clear_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006094 int flags UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095{
6096 unsigned off_from;
6097 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006098 unsigned max_off_from;
6099 unsigned max_off_to;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102 int force = FALSE; /* force update rest of the line */
6103 int redraw_this /* bool: does character need redraw? */
6104#ifdef FEAT_GUI
6105 = TRUE /* For GUI when while-loop empty */
6106#endif
6107 ;
6108 int redraw_next; /* redraw_this for next character */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 int clear_next = FALSE;
6110 int char_cells; /* 1: normal char */
6111 /* 2: occupies two display cells */
6112# define CHAR_CELLS char_cells
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006114 /* Check for illegal row and col, just in case. */
6115 if (row >= Rows)
6116 row = Rows - 1;
6117 if (endcol > Columns)
6118 endcol = Columns;
6119
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120# ifdef FEAT_CLIPBOARD
6121 clip_may_clear_selection(row, row);
6122# endif
6123
6124 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6125 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006126 max_off_from = off_from + screen_Columns;
6127 max_off_to = LineOffset[row] + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128
6129#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006130 if (flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 {
6132 /* Clear rest first, because it's left of the text. */
6133 if (clear_width > 0)
6134 {
6135 while (col <= endcol && ScreenLines[off_to] == ' '
6136 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006137 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138 {
6139 ++off_to;
6140 ++col;
6141 }
6142 if (col <= endcol)
6143 screen_fill(row, row + 1, col + coloff,
6144 endcol + coloff + 1, ' ', ' ', 0);
6145 }
6146 col = endcol + 1;
6147 off_to = LineOffset[row] + col + coloff;
6148 off_from += col;
6149 endcol = (clear_width > 0 ? clear_width : -clear_width);
6150 }
6151#endif /* FEAT_RIGHTLEFT */
6152
6153 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6154
6155 while (col < endcol)
6156 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006158 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159 else
6160 char_cells = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161
6162 redraw_this = redraw_next;
6163 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6164 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6165
6166#ifdef FEAT_GUI
6167 /* If the next character was bold, then redraw the current character to
6168 * remove any pixels that might have spilt over into us. This only
6169 * happens in the GUI.
6170 */
6171 if (redraw_next && gui.in_use)
6172 {
6173 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006174 if (hl > HL_ALL)
6175 hl = syn_attr2attr(hl);
6176 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177 redraw_this = TRUE;
6178 }
6179#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02006180#ifdef FEAT_TEXT_PROP
Bram Moolenaarc662ec92019-06-23 00:15:57 +02006181 if (blocked_by_popup(row, col + coloff))
Bram Moolenaar33796b32019-06-08 16:01:13 +02006182 redraw_this = FALSE;
6183#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 if (redraw_this)
6185 {
6186 /*
6187 * Special handling when 'xs' termcap flag set (hpterm):
6188 * Attributes for characters are stored at the position where the
6189 * cursor is when writing the highlighting code. The
6190 * start-highlighting code must be written with the cursor on the
6191 * first highlighted character. The stop-highlighting code must
6192 * be written with the cursor just after the last highlighted
6193 * character.
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01006194 * Overwriting a character doesn't remove its highlighting. Need
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 * to clear the rest of the line, and force redrawing it
6196 * completely.
6197 */
6198 if ( p_wiv
6199 && !force
6200#ifdef FEAT_GUI
6201 && !gui.in_use
6202#endif
6203 && ScreenAttrs[off_to] != 0
6204 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6205 {
6206 /*
6207 * Need to remove highlighting attributes here.
6208 */
6209 windgoto(row, col + coloff);
6210 out_str(T_CE); /* clear rest of this screen line */
6211 screen_start(); /* don't know where cursor is now */
6212 force = TRUE; /* force redraw of rest of the line */
6213 redraw_next = TRUE; /* or else next char would miss out */
6214
6215 /*
6216 * If the previous character was highlighted, need to stop
6217 * highlighting at this character.
6218 */
6219 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6220 {
6221 screen_attr = ScreenAttrs[off_to - 1];
6222 term_windgoto(row, col + coloff);
6223 screen_stop_highlight();
6224 }
6225 else
6226 screen_attr = 0; /* highlighting has stopped */
6227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228 if (enc_dbcs != 0)
6229 {
6230 /* Check if overwriting a double-byte with a single-byte or
6231 * the other way around requires another character to be
6232 * redrawn. For UTF-8 this isn't needed, because comparing
6233 * ScreenLinesUC[] is sufficient. */
6234 if (char_cells == 1
6235 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006236 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 {
6238 /* Writing a single-cell character over a double-cell
6239 * character: need to redraw the next cell. */
6240 ScreenLines[off_to + 1] = 0;
6241 redraw_next = TRUE;
6242 }
6243 else if (char_cells == 2
6244 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006245 && (*mb_off2cells)(off_to, max_off_to) == 1
6246 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 {
6248 /* Writing the second half of a double-cell character over
6249 * a double-cell character: need to redraw the second
6250 * cell. */
6251 ScreenLines[off_to + 2] = 0;
6252 redraw_next = TRUE;
6253 }
6254
6255 if (enc_dbcs == DBCS_JPNU)
6256 ScreenLines2[off_to] = ScreenLines2[off_from];
6257 }
6258 /* When writing a single-width character over a double-width
6259 * character and at the end of the redrawn text, need to clear out
6260 * the right halve of the old character.
6261 * Also required when writing the right halve of a double-width
6262 * char over the left halve of an existing one. */
6263 if (has_mbyte && col + char_cells == endcol
6264 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006265 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006267 && (*mb_off2cells)(off_to, max_off_to) == 1
6268 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269 clear_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270
6271 ScreenLines[off_to] = ScreenLines[off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 if (enc_utf8)
6273 {
6274 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6275 if (ScreenLinesUC[off_from] != 0)
6276 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006277 int i;
6278
6279 for (i = 0; i < Screen_mco; ++i)
6280 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281 }
6282 }
6283 if (char_cells == 2)
6284 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285
6286#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006287 /* The bold trick makes a single column of pixels appear in the
6288 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289 * character should be redrawn too. This happens for our own GUI
6290 * and for some xterms. */
6291 if (
6292# ifdef FEAT_GUI
6293 gui.in_use
6294# endif
6295# if defined(FEAT_GUI) && defined(UNIX)
6296 ||
6297# endif
6298# ifdef UNIX
6299 term_is_xterm
6300# endif
6301 )
6302 {
6303 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006304 if (hl > HL_ALL)
6305 hl = syn_attr2attr(hl);
6306 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 redraw_next = TRUE;
6308 }
6309#endif
6310 ScreenAttrs[off_to] = ScreenAttrs[off_from];
Bram Moolenaara12a1612019-01-24 16:39:02 +01006311
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006312 /* For simplicity set the attributes of second half of a
6313 * double-wide character equal to the first half. */
6314 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006316
6317 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320 screen_char(off_to, row, col + coloff);
6321 }
6322 else if ( p_wiv
6323#ifdef FEAT_GUI
6324 && !gui.in_use
6325#endif
6326 && col + coloff > 0)
6327 {
6328 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6329 {
6330 /*
6331 * Don't output stop-highlight when moving the cursor, it will
6332 * stop the highlighting when it should continue.
6333 */
6334 screen_attr = 0;
6335 }
6336 else if (screen_attr != 0)
6337 screen_stop_highlight();
6338 }
6339
6340 off_to += CHAR_CELLS;
6341 off_from += CHAR_CELLS;
6342 col += CHAR_CELLS;
6343 }
6344
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345 if (clear_next)
6346 {
6347 /* Clear the second half of a double-wide character of which the left
6348 * half was overwritten with a single-wide character. */
6349 ScreenLines[off_to] = ' ';
6350 if (enc_utf8)
6351 ScreenLinesUC[off_to] = 0;
6352 screen_char(off_to, row, col + coloff);
6353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354
6355 if (clear_width > 0
6356#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006357 && !(flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358#endif
6359 )
6360 {
6361#ifdef FEAT_GUI
6362 int startCol = col;
6363#endif
6364
6365 /* blank out the rest of the line */
6366 while (col < clear_width && ScreenLines[off_to] == ' '
6367 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006368 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369 {
6370 ++off_to;
6371 ++col;
6372 }
6373 if (col < clear_width)
6374 {
6375#ifdef FEAT_GUI
6376 /*
6377 * In the GUI, clearing the rest of the line may leave pixels
6378 * behind if the first character cleared was bold. Some bold
6379 * fonts spill over the left. In this case we redraw the previous
6380 * character too. If we didn't skip any blanks above, then we
6381 * only redraw if the character wasn't already redrawn anyway.
6382 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006383 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384 {
6385 hl = ScreenAttrs[off_to];
6386 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006387 {
6388 int prev_cells = 1;
Bram Moolenaara12a1612019-01-24 16:39:02 +01006389
Bram Moolenaar9c697322006-10-09 20:11:17 +00006390 if (enc_utf8)
6391 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6392 * that its width is 2. */
6393 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6394 else if (enc_dbcs != 0)
6395 {
6396 /* find previous character by counting from first
6397 * column and get its width. */
6398 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006399 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006400
6401 while (off < off_to)
6402 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006403 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006404 off += prev_cells;
6405 }
6406 }
6407
6408 if (enc_dbcs != 0 && prev_cells > 1)
6409 screen_char_2(off_to - prev_cells, row,
6410 col + coloff - prev_cells);
6411 else
Bram Moolenaar9c697322006-10-09 20:11:17 +00006412 screen_char(off_to - prev_cells, row,
6413 col + coloff - prev_cells);
6414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 }
6416#endif
6417 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6418 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 off_to += clear_width - col;
6420 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421 }
6422 }
6423
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006424 if (clear_width > 0
6425#ifdef FEAT_TEXT_PROP
6426 && !(flags & SLF_POPUP) // no separator for popup window
6427#endif
6428 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006430 // For a window that has a right neighbor, draw the separator char
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006431 // right of the window contents. But not on top of a popup window.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006432 if (coloff + col < Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006434#ifdef FEAT_TEXT_PROP
Bram Moolenaarc662ec92019-06-23 00:15:57 +02006435 if (!blocked_by_popup(row, col + coloff))
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006436#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006438 int c;
6439
6440 c = fillchar_vsep(&hl);
6441 if (ScreenLines[off_to] != (schar_T)c
6442 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6443 != (c >= 0x80 ? c : 0))
6444 || ScreenAttrs[off_to] != hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006446 ScreenLines[off_to] = c;
6447 ScreenAttrs[off_to] = hl;
6448 if (enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006450 if (c >= 0x80)
6451 {
6452 ScreenLinesUC[off_to] = c;
6453 ScreenLinesC[0][off_to] = 0;
6454 }
6455 else
6456 ScreenLinesUC[off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457 }
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006458 screen_char(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 }
6461 }
6462 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 LineWraps[row] = FALSE;
6464 }
6465}
6466
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006467#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006469 * Mirror text "str" for right-left displaying.
6470 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006472 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006473rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474{
6475 char_u *p1, *p2;
6476 int t;
6477
6478 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6479 {
6480 t = *p1;
6481 *p1 = *p2;
6482 *p2 = t;
6483 }
6484}
6485#endif
6486
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487/*
6488 * mark all status lines for redraw; used after first :cd
6489 */
6490 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006491status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492{
6493 win_T *wp;
6494
Bram Moolenaar29323592016-07-24 22:04:11 +02006495 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496 if (wp->w_status_height)
6497 {
6498 wp->w_redr_status = TRUE;
6499 redraw_later(VALID);
6500 }
6501}
6502
6503/*
6504 * mark all status lines of the current buffer for redraw
6505 */
6506 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006507status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508{
6509 win_T *wp;
6510
Bram Moolenaar29323592016-07-24 22:04:11 +02006511 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6513 {
6514 wp->w_redr_status = TRUE;
6515 redraw_later(VALID);
6516 }
6517}
6518
6519/*
6520 * Redraw all status lines that need to be redrawn.
6521 */
6522 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006523redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524{
6525 win_T *wp;
6526
Bram Moolenaar29323592016-07-24 22:04:11 +02006527 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006529 win_redr_status(wp, FALSE);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006530 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006531 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533
Bram Moolenaar4033c552017-09-16 20:54:51 +02006534#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535/*
6536 * Redraw all status lines at the bottom of frame "frp".
6537 */
6538 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006539win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540{
6541 if (frp->fr_layout == FR_LEAF)
6542 frp->fr_win->w_redr_status = TRUE;
6543 else if (frp->fr_layout == FR_ROW)
6544 {
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006545 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546 win_redraw_last_status(frp);
6547 }
6548 else /* frp->fr_layout == FR_COL */
6549 {
6550 frp = frp->fr_child;
6551 while (frp->fr_next != NULL)
6552 frp = frp->fr_next;
6553 win_redraw_last_status(frp);
6554 }
6555}
6556#endif
6557
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558/*
6559 * Draw the verticap separator right of window "wp" starting with line "row".
6560 */
6561 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006562draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563{
6564 int hl;
6565 int c;
6566
6567 if (wp->w_vsep_width)
6568 {
6569 /* draw the vertical separator right of this window */
6570 c = fillchar_vsep(&hl);
6571 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6572 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6573 c, ' ', hl);
6574 }
6575}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576
6577#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006578static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579
6580/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006581 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006582 */
6583 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006584status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585{
6586 int len = 0;
6587
6588#ifdef FEAT_MENU
6589 int emenu = (xp->xp_context == EXPAND_MENUS
6590 || xp->xp_context == EXPAND_MENUNAMES);
6591
6592 /* Check for menu separators - replace with '|'. */
6593 if (emenu && menu_is_separator(s))
6594 return 1;
6595#endif
6596
6597 while (*s != NUL)
6598 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006599 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006600 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006601 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602 }
6603
6604 return len;
6605}
6606
6607/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006608 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006609 * These are backslashes used for escaping. Do show backslashes in help tags.
6610 */
6611 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006612skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006613{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006614 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006615#ifdef FEAT_MENU
6616 || ((xp->xp_context == EXPAND_MENUS
6617 || xp->xp_context == EXPAND_MENUNAMES)
6618 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6619#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006620 )
6621 {
6622#ifndef BACKSLASH_IN_FILENAME
6623 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6624 return 2;
6625#endif
6626 return 1;
6627 }
6628 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006629}
6630
6631/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632 * Show wildchar matches in the status line.
6633 * Show at least the "match" item.
6634 * We start at item 'first_match' in the list and show all matches that fit.
6635 *
6636 * If inversion is possible we use it. Else '=' characters are used.
6637 */
6638 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006639win_redr_status_matches(
6640 expand_T *xp,
6641 int num_matches,
6642 char_u **matches, /* list of matches */
6643 int match,
6644 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645{
6646#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6647 int row;
6648 char_u *buf;
6649 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006650 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651 int fillchar;
6652 int attr;
6653 int i;
6654 int highlight = TRUE;
6655 char_u *selstart = NULL;
6656 int selstart_col = 0;
6657 char_u *selend = NULL;
6658 static int first_match = 0;
6659 int add_left = FALSE;
6660 char_u *s;
6661#ifdef FEAT_MENU
6662 int emenu;
6663#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665
6666 if (matches == NULL) /* interrupted completion? */
6667 return;
6668
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006669 if (has_mbyte)
Bram Moolenaar964b3742019-05-24 18:54:09 +02006670 buf = alloc(Columns * MB_MAXBYTES + 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006671 else
Bram Moolenaar964b3742019-05-24 18:54:09 +02006672 buf = alloc(Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673 if (buf == NULL)
6674 return;
6675
6676 if (match == -1) /* don't show match but original text */
6677 {
6678 match = 0;
6679 highlight = FALSE;
6680 }
6681 /* count 1 for the ending ">" */
6682 clen = status_match_len(xp, L_MATCH(match)) + 3;
6683 if (match == 0)
6684 first_match = 0;
6685 else if (match < first_match)
6686 {
6687 /* jumping left, as far as we can go */
6688 first_match = match;
6689 add_left = TRUE;
6690 }
6691 else
6692 {
6693 /* check if match fits on the screen */
6694 for (i = first_match; i < match; ++i)
6695 clen += status_match_len(xp, L_MATCH(i)) + 2;
6696 if (first_match > 0)
6697 clen += 2;
6698 /* jumping right, put match at the left */
6699 if ((long)clen > Columns)
6700 {
6701 first_match = match;
6702 /* if showing the last match, we can add some on the left */
6703 clen = 2;
6704 for (i = match; i < num_matches; ++i)
6705 {
6706 clen += status_match_len(xp, L_MATCH(i)) + 2;
6707 if ((long)clen >= Columns)
6708 break;
6709 }
6710 if (i == num_matches)
6711 add_left = TRUE;
6712 }
6713 }
6714 if (add_left)
6715 while (first_match > 0)
6716 {
6717 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6718 if ((long)clen >= Columns)
6719 break;
6720 --first_match;
6721 }
6722
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006723 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724
6725 if (first_match == 0)
6726 {
6727 *buf = NUL;
6728 len = 0;
6729 }
6730 else
6731 {
6732 STRCPY(buf, "< ");
6733 len = 2;
6734 }
6735 clen = len;
6736
6737 i = first_match;
6738 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6739 {
6740 if (i == match)
6741 {
6742 selstart = buf + len;
6743 selstart_col = clen;
6744 }
6745
6746 s = L_MATCH(i);
6747 /* Check for menu separators - replace with '|' */
6748#ifdef FEAT_MENU
6749 emenu = (xp->xp_context == EXPAND_MENUS
6750 || xp->xp_context == EXPAND_MENUNAMES);
6751 if (emenu && menu_is_separator(s))
6752 {
6753 STRCPY(buf + len, transchar('|'));
6754 l = (int)STRLEN(buf + len);
6755 len += l;
6756 clen += l;
6757 }
6758 else
6759#endif
6760 for ( ; *s != NUL; ++s)
6761 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006762 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763 clen += ptr2cells(s);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006764 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765 {
6766 STRNCPY(buf + len, s, l);
6767 s += l - 1;
6768 len += l;
6769 }
6770 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771 {
6772 STRCPY(buf + len, transchar_byte(*s));
6773 len += (int)STRLEN(buf + len);
6774 }
6775 }
6776 if (i == match)
6777 selend = buf + len;
6778
6779 *(buf + len++) = ' ';
6780 *(buf + len++) = ' ';
6781 clen += 2;
6782 if (++i == num_matches)
6783 break;
6784 }
6785
6786 if (i != num_matches)
6787 {
6788 *(buf + len++) = '>';
6789 ++clen;
6790 }
6791
6792 buf[len] = NUL;
6793
6794 row = cmdline_row - 1;
6795 if (row >= 0)
6796 {
6797 if (wild_menu_showing == 0)
6798 {
6799 if (msg_scrolled > 0)
6800 {
6801 /* Put the wildmenu just above the command line. If there is
6802 * no room, scroll the screen one line up. */
6803 if (cmdline_row == Rows - 1)
6804 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02006805 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 ++msg_scrolled;
6807 }
6808 else
6809 {
6810 ++cmdline_row;
6811 ++row;
6812 }
6813 wild_menu_showing = WM_SCROLLED;
6814 }
6815 else
6816 {
6817 /* Create status line if needed by setting 'laststatus' to 2.
6818 * Set 'winminheight' to zero to avoid that the window is
6819 * resized. */
6820 if (lastwin->w_status_height == 0)
6821 {
6822 save_p_ls = p_ls;
6823 save_p_wmh = p_wmh;
6824 p_ls = 2;
6825 p_wmh = 0;
6826 last_status(FALSE);
6827 }
6828 wild_menu_showing = WM_SHOWN;
6829 }
6830 }
6831
6832 screen_puts(buf, row, 0, attr);
6833 if (selstart != NULL && highlight)
6834 {
6835 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006836 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837 }
6838
6839 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6840 }
6841
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843 vim_free(buf);
6844}
6845#endif
6846
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847/*
6848 * Redraw the status line of window wp.
6849 *
6850 * If inversion is possible we use it. Else '=' characters are used.
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006851 * If "ignore_pum" is TRUE, also redraw statusline when the popup menu is
6852 * displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853 */
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006854 static void
Bram Moolenaar829adb72018-06-24 19:24:03 +02006855win_redr_status(win_T *wp, int ignore_pum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856{
6857 int row;
6858 char_u *p;
6859 int len;
6860 int fillchar;
6861 int attr;
6862 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006863 static int busy = FALSE;
6864
6865 /* It's possible to get here recursively when 'statusline' (indirectly)
6866 * invokes ":redrawstatus". Simply ignore the call then. */
6867 if (busy)
6868 return;
6869 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870
6871 wp->w_redr_status = FALSE;
6872 if (wp->w_status_height == 0)
6873 {
6874 /* no status line, can only be last window */
6875 redraw_cmdline = TRUE;
6876 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006877 else if (!redrawing()
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006878 // don't update status line when popup menu is visible and may be
6879 // drawn over it, unless it will be redrawn later
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006880 || (!ignore_pum && pum_visible()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881 {
6882 /* Don't redraw right now, do it later. */
6883 wp->w_redr_status = TRUE;
6884 }
6885#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006886 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 {
6888 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006889 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890 }
6891#endif
6892 else
6893 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006894 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006896 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897 p = NameBuff;
6898 len = (int)STRLEN(p);
6899
Bram Moolenaard85f2712017-07-28 21:51:57 +02006900 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901#ifdef FEAT_QUICKFIX
6902 || wp->w_p_pvw
6903#endif
6904 || bufIsChanged(wp->w_buffer)
6905 || wp->w_buffer->b_p_ro)
6906 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02006907 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006909 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910 len += (int)STRLEN(p + len);
6911 }
6912#ifdef FEAT_QUICKFIX
6913 if (wp->w_p_pvw)
6914 {
6915 STRCPY(p + len, _("[Preview]"));
6916 len += (int)STRLEN(p + len);
6917 }
6918#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02006919 if (bufIsChanged(wp->w_buffer)
6920#ifdef FEAT_TERMINAL
6921 && !bt_terminal(wp->w_buffer)
6922#endif
6923 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924 {
6925 STRCPY(p + len, "[+]");
6926 len += 3;
6927 }
6928 if (wp->w_buffer->b_p_ro)
6929 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006930 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01006931 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932 }
6933
Bram Moolenaar02631462017-09-22 15:20:32 +02006934 this_ru_col = ru_col - (Columns - wp->w_width);
6935 if (this_ru_col < (wp->w_width + 1) / 2)
6936 this_ru_col = (wp->w_width + 1) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 if (this_ru_col <= 1)
6938 {
6939 p = (char_u *)"<"; /* No room for file name! */
6940 len = 1;
6941 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01006942 else if (has_mbyte)
6943 {
6944 int clen = 0, i;
6945
6946 /* Count total number of display cells. */
6947 clen = mb_string2cells(p, -1);
6948
6949 /* Find first character that will fit.
6950 * Going from start to end is much faster for DBCS. */
6951 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
6952 i += (*mb_ptr2len)(p + i))
6953 clen -= (*mb_ptr2cells)(p + i);
6954 len = clen;
6955 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 {
Bram Moolenaara12a1612019-01-24 16:39:02 +01006957 p = p + i - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 *p = '<';
Bram Moolenaara12a1612019-01-24 16:39:02 +01006959 ++len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 }
6961
Bram Moolenaara12a1612019-01-24 16:39:02 +01006962 }
6963 else if (len > this_ru_col - 1)
6964 {
6965 p += len - (this_ru_col - 1);
6966 *p = '<';
6967 len = this_ru_col - 1;
6968 }
6969
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar53f81742017-09-22 14:35:51 +02006971 screen_puts(p, row, wp->w_wincol, attr);
6972 screen_fill(row, row + 1, len + wp->w_wincol,
6973 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02006975 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6977 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
Bram Moolenaar53f81742017-09-22 14:35:51 +02006978 - 1 + wp->w_wincol), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979
6980#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +02006981 win_redr_ruler(wp, TRUE, ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982#endif
6983 }
6984
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985 /*
6986 * May need to draw the character below the vertical separator.
6987 */
6988 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6989 {
6990 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006991 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 else
6993 fillchar = fillchar_vsep(&attr);
6994 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6995 attr);
6996 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006997 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998}
6999
Bram Moolenaar238a5642006-02-21 22:12:05 +00007000#ifdef FEAT_STL_OPT
7001/*
7002 * Redraw the status line according to 'statusline' and take care of any
7003 * errors encountered.
7004 */
7005 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007006redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00007007{
Bram Moolenaar362f3562009-11-03 16:20:34 +00007008 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02007009 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007010
7011 /* When called recursively return. This can happen when the statusline
7012 * contains an expression that triggers a redraw. */
7013 if (entered)
7014 return;
7015 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007016
Bram Moolenaara742e082016-04-05 21:10:38 +02007017 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007018 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02007019 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007020 {
7021 /* When there is an error disable the statusline, otherwise the
7022 * display is messed up with errors and a redraw triggers the problem
7023 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007024 set_string_option_direct((char_u *)"statusline", -1,
7025 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007026 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007027 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007028 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007029 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007030}
7031#endif
7032
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033/*
7034 * Return TRUE if the status line of window "wp" is connected to the status
7035 * line of the window right of it. If not, then it's a vertical separator.
7036 * Only call if (wp->w_vsep_width != 0).
7037 */
7038 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007039stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040{
7041 frame_T *fr;
7042
7043 fr = wp->w_frame;
7044 while (fr->fr_parent != NULL)
7045 {
7046 if (fr->fr_parent->fr_layout == FR_COL)
7047 {
7048 if (fr->fr_next != NULL)
7049 break;
7050 }
7051 else
7052 {
7053 if (fr->fr_next != NULL)
7054 return TRUE;
7055 }
7056 fr = fr->fr_parent;
7057 }
7058 return FALSE;
7059}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062/*
7063 * Get the value to show for the language mappings, active 'keymap'.
7064 */
7065 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007066get_keymap_str(
7067 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007068 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007069 char_u *buf, /* buffer for the result */
7070 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071{
7072 char_u *p;
7073
7074 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7075 return FALSE;
7076
7077 {
7078#ifdef FEAT_EVAL
7079 buf_T *old_curbuf = curbuf;
7080 win_T *old_curwin = curwin;
7081 char_u *s;
7082
7083 curbuf = wp->w_buffer;
7084 curwin = wp;
7085 STRCPY(buf, "b:keymap_name"); /* must be writable */
7086 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007087 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 --emsg_skip;
7089 curbuf = old_curbuf;
7090 curwin = old_curwin;
7091 if (p == NULL || *p == NUL)
7092#endif
7093 {
7094#ifdef FEAT_KEYMAP
7095 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7096 p = wp->w_buffer->b_p_keymap;
7097 else
7098#endif
7099 p = (char_u *)"lang";
7100 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007101 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102 buf[0] = NUL;
7103#ifdef FEAT_EVAL
7104 vim_free(s);
7105#endif
7106 }
7107 return buf[0] != NUL;
7108}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109
7110#if defined(FEAT_STL_OPT) || defined(PROTO)
7111/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007112 * Redraw the status line or ruler of window "wp".
7113 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114 */
7115 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007116win_redr_custom(
7117 win_T *wp,
7118 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007120 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121 int attr;
7122 int curattr;
7123 int row;
7124 int col = 0;
7125 int maxwidth;
7126 int width;
7127 int n;
7128 int len;
7129 int fillchar;
7130 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007131 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007133 struct stl_hlrec hltab[STL_MAX_ITEM];
7134 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007135 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007136 win_T *ewp;
7137 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138
Bram Moolenaar1d633412013-12-11 15:52:01 +01007139 /* There is a tiny chance that this gets called recursively: When
7140 * redrawing a status line triggers redrawing the ruler or tabline.
7141 * Avoid trouble by not allowing recursion. */
7142 if (entered)
7143 return;
7144 entered = TRUE;
7145
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007147 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007149 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007150 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007151 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007152 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007153 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007154 maxwidth = Columns;
7155# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007156 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007157# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007159 else
7160 {
7161 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007162 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02007163 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007164
7165 if (draw_ruler)
7166 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007167 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007168 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007169 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007170 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007171 if (*++stl == '-')
7172 stl++;
7173 if (atoi((char *)stl))
7174 while (VIM_ISDIGIT(*stl))
7175 stl++;
7176 if (*stl++ != '(')
7177 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007178 }
Bram Moolenaar02631462017-09-22 15:20:32 +02007179 col = ru_col - (Columns - wp->w_width);
7180 if (col < (wp->w_width + 1) / 2)
7181 col = (wp->w_width + 1) / 2;
7182 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007183 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007184 {
7185 row = Rows - 1;
7186 --maxwidth; /* writing in last column may cause scrolling */
7187 fillchar = ' ';
7188 attr = 0;
7189 }
7190
7191# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007192 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007193# endif
7194 }
7195 else
7196 {
7197 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007198 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007199 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007200 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007201# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007202 use_sandbox = was_set_insecurely((char_u *)"statusline",
7203 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007204# endif
7205 }
7206
Bram Moolenaar53f81742017-09-22 14:35:51 +02007207 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007208 }
7209
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007211 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212
Bram Moolenaar61452852011-02-01 18:01:11 +01007213 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7214 * the cursor away and back. */
7215 ewp = wp == NULL ? curwin : wp;
7216 p_crb_save = ewp->w_p_crb;
7217 ewp->w_p_crb = FALSE;
7218
Bram Moolenaar362f3562009-11-03 16:20:34 +00007219 /* Make a copy, because the statusline may include a function call that
7220 * might change the option value and free the memory. */
7221 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007222 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007223 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007224 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007225 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007226 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007228 /* Make all characters printable. */
7229 p = transstr(buf);
7230 if (p != NULL)
7231 {
7232 vim_strncpy(buf, p, sizeof(buf) - 1);
7233 vim_free(p);
7234 }
7235
7236 /* fill up with "fillchar" */
7237 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007238 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240 len += (*mb_char2bytes)(fillchar, buf + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241 ++width;
7242 }
7243 buf[len] = NUL;
7244
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007245 /*
7246 * Draw each snippet with the specified highlighting.
7247 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248 curattr = attr;
7249 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007250 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007252 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 screen_puts_len(p, len, row, col, curattr);
7254 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007255 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007257 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007259 else if (hltab[n].userhl < 0)
7260 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007261#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007262 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7263 && wp->w_status_height != 0)
7264 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007265 else if (wp != NULL && bt_terminal(wp->w_buffer)
7266 && wp->w_status_height != 0)
7267 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02007268#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007269 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007270 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007272 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 }
7274 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007275
7276 if (wp == NULL)
7277 {
7278 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7279 col = 0;
7280 len = 0;
7281 p = buf;
7282 fillchar = 0;
7283 for (n = 0; tabtab[n].start != NULL; n++)
7284 {
7285 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7286 while (col < len)
7287 TabPageIdxs[col++] = fillchar;
7288 p = tabtab[n].start;
7289 fillchar = tabtab[n].userhl;
7290 }
7291 while (col < Columns)
7292 TabPageIdxs[col++] = fillchar;
7293 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007294
7295theend:
7296 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297}
7298
7299#endif /* FEAT_STL_OPT */
7300
7301/*
7302 * Output a single character directly to the screen and update ScreenLines.
7303 */
7304 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007305screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307 char_u buf[MB_MAXBYTES + 1];
7308
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007309 if (has_mbyte)
7310 buf[(*mb_char2bytes)(c, buf)] = NUL;
7311 else
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007312 {
7313 buf[0] = c;
7314 buf[1] = NUL;
7315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 screen_puts(buf, row, col, attr);
7317}
7318
7319/*
7320 * Get a single character directly from ScreenLines into "bytes[]".
7321 * Also return its attribute in *attrp;
7322 */
7323 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007324screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325{
7326 unsigned off;
7327
7328 /* safety check */
7329 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7330 {
7331 off = LineOffset[row] + col;
7332 *attrp = ScreenAttrs[off];
7333 bytes[0] = ScreenLines[off];
7334 bytes[1] = NUL;
7335
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336 if (enc_utf8 && ScreenLinesUC[off] != 0)
7337 bytes[utfc_char2bytes(off, bytes)] = NUL;
7338 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7339 {
7340 bytes[0] = ScreenLines[off];
7341 bytes[1] = ScreenLines2[off];
7342 bytes[2] = NUL;
7343 }
7344 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7345 {
7346 bytes[1] = ScreenLines[off + 1];
7347 bytes[2] = NUL;
7348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349 }
7350}
7351
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007352/*
7353 * Return TRUE if composing characters for screen posn "off" differs from
7354 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007355 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007356 */
7357 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007358screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007359{
7360 int i;
7361
7362 for (i = 0; i < Screen_mco; ++i)
7363 {
7364 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7365 return TRUE;
7366 if (u8cc[i] == 0)
7367 break;
7368 }
7369 return FALSE;
7370}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007371
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372/*
7373 * Put string '*text' on the screen at position 'row' and 'col', with
7374 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7375 * Note: only outputs within one row, message is truncated at screen boundary!
7376 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7377 */
7378 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007379screen_puts(
7380 char_u *text,
7381 int row,
7382 int col,
7383 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384{
7385 screen_puts_len(text, -1, row, col, attr);
7386}
7387
7388/*
7389 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7390 * a NUL.
7391 */
7392 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007393screen_puts_len(
7394 char_u *text,
7395 int textlen,
7396 int row,
7397 int col,
7398 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399{
7400 unsigned off;
7401 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007402 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403 int c;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007404 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 int mbyte_blen = 1;
7406 int mbyte_cells = 1;
7407 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007408 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 int clear_next_cell = FALSE;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007410#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007412 int pc, nc, nc1;
7413 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007415 int force_redraw_this;
7416 int force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007417 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418
Bram Moolenaar0b4c9ed2019-06-03 22:04:23 +02007419 // Safety check. The check for negative row and column is to fix issue
7420 // #4102. TODO: find out why row/col could be negative.
7421 if (ScreenLines == NULL
7422 || row >= screen_Rows || row < 0
7423 || col >= screen_Columns || col < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007425 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426
Bram Moolenaarc236c162008-07-13 17:41:49 +00007427 /* When drawing over the right halve of a double-wide char clear out the
7428 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007429 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaara12a1612019-01-24 16:39:02 +01007430#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00007431 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01007432#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007433 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007434 {
7435 ScreenLines[off - 1] = ' ';
7436 ScreenAttrs[off - 1] = 0;
7437 if (enc_utf8)
7438 {
7439 ScreenLinesUC[off - 1] = 0;
7440 ScreenLinesC[0][off - 1] = 0;
7441 }
7442 /* redraw the previous cell, make it empty */
7443 screen_char(off - 1, row, col - 1);
7444 /* force the cell at "col" to be redrawn */
7445 force_redraw_next = TRUE;
7446 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007447
Bram Moolenaar367329b2007-08-30 11:53:22 +00007448 max_off = LineOffset[row] + screen_Columns;
Bram Moolenaara064ac82007-08-05 18:10:54 +00007449 while (col < screen_Columns
7450 && (len < 0 || (int)(ptr - text) < len)
7451 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452 {
7453 c = *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454 /* check if this is the first byte of a multibyte */
7455 if (has_mbyte)
7456 {
7457 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007458 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007460 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7462 mbyte_cells = 1;
7463 else if (enc_dbcs != 0)
7464 mbyte_cells = mbyte_blen;
7465 else /* enc_utf8 */
7466 {
7467 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007468 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469 (int)((text + len) - ptr));
7470 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007471 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007473#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7475 {
7476 /* Do Arabic shaping. */
7477 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7478 {
7479 /* Past end of string to be displayed. */
7480 nc = NUL;
7481 nc1 = NUL;
7482 }
7483 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007484 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007485 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7486 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007487 nc1 = pcc[0];
7488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 pc = prev_c;
7490 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007491 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492 }
7493 else
7494 prev_c = u8c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007495#endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007496 if (col + mbyte_cells > screen_Columns)
7497 {
7498 /* Only 1 cell left, but character requires 2 cells:
7499 * display a '>' in the last column to avoid wrapping. */
7500 c = '>';
7501 mbyte_cells = 1;
7502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503 }
7504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007506 force_redraw_this = force_redraw_next;
7507 force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007508
7509 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510 || (mbyte_cells == 2
7511 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7512 || (enc_dbcs == DBCS_JPNU
7513 && c == 0x8e
7514 && ScreenLines2[off] != ptr[1])
7515 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007516 && (ScreenLinesUC[off] !=
7517 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7518 || (ScreenLinesUC[off] != 0
7519 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007521 || exmode_active;
7522
Bram Moolenaar24a5ac52019-06-08 19:01:18 +02007523 if ((need_redraw || force_redraw_this)
7524#ifdef FEAT_TEXT_PROP
Bram Moolenaarc662ec92019-06-23 00:15:57 +02007525 && !blocked_by_popup(row, col)
Bram Moolenaar24a5ac52019-06-08 19:01:18 +02007526#endif
7527 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528 {
7529#if defined(FEAT_GUI) || defined(UNIX)
7530 /* The bold trick makes a single row of pixels appear in the next
7531 * character. When a bold character is removed, the next
7532 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007533 * and for some xterms. */
7534 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535# ifdef FEAT_GUI
7536 gui.in_use
7537# endif
7538# if defined(FEAT_GUI) && defined(UNIX)
7539 ||
7540# endif
7541# ifdef UNIX
7542 term_is_xterm
7543# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007544 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007545 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007546 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007548 if (n > HL_ALL)
7549 n = syn_attr2attr(n);
7550 if (n & HL_BOLD)
7551 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552 }
7553#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554 /* When at the end of the text and overwriting a two-cell
7555 * character with a one-cell character, need to clear the next
7556 * cell. Also when overwriting the left halve of a two-cell char
7557 * with the right halve of a two-cell char. Do this only once
7558 * (mb_off2cells() may return 2 on the right halve). */
7559 if (clear_next_cell)
7560 clear_next_cell = FALSE;
7561 else if (has_mbyte
7562 && (len < 0 ? ptr[mbyte_blen] == NUL
7563 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007564 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007566 && (*mb_off2cells)(off, max_off) == 1
7567 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568 clear_next_cell = TRUE;
7569
7570 /* Make sure we never leave a second byte of a double-byte behind,
7571 * it confuses mb_off2cells(). */
7572 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007573 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007575 && (*mb_off2cells)(off, max_off) == 1
7576 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007577 ScreenLines[off + mbyte_blen] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578 ScreenLines[off] = c;
7579 ScreenAttrs[off] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580 if (enc_utf8)
7581 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007582 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583 ScreenLinesUC[off] = 0;
7584 else
7585 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007586 int i;
7587
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007589 for (i = 0; i < Screen_mco; ++i)
7590 {
7591 ScreenLinesC[i][off] = u8cc[i];
7592 if (u8cc[i] == 0)
7593 break;
7594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595 }
7596 if (mbyte_cells == 2)
7597 {
7598 ScreenLines[off + 1] = 0;
7599 ScreenAttrs[off + 1] = attr;
7600 }
7601 screen_char(off, row, col);
7602 }
7603 else if (mbyte_cells == 2)
7604 {
7605 ScreenLines[off + 1] = ptr[1];
7606 ScreenAttrs[off + 1] = attr;
7607 screen_char_2(off, row, col);
7608 }
7609 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7610 {
7611 ScreenLines2[off] = ptr[1];
7612 screen_char(off, row, col);
7613 }
7614 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615 screen_char(off, row, col);
7616 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617 if (has_mbyte)
7618 {
7619 off += mbyte_cells;
7620 col += mbyte_cells;
7621 ptr += mbyte_blen;
7622 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007623 {
7624 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007626 len = -1;
7627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628 }
7629 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630 {
7631 ++off;
7632 ++col;
7633 ++ptr;
7634 }
7635 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007636
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007637 /* If we detected the next character needs to be redrawn, but the text
7638 * doesn't extend up to there, update the character here. */
7639 if (force_redraw_next && col < screen_Columns)
7640 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007641 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7642 screen_char_2(off, row, col);
7643 else
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007644 screen_char(off, row, col);
7645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646}
7647
7648#ifdef FEAT_SEARCH_EXTRA
7649/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007650 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651 */
7652 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007653start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654{
7655 if (p_hls && !no_hlsearch)
7656 {
7657 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007658 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007659# ifdef FEAT_RELTIME
7660 /* Set the time limit to 'redrawtime'. */
7661 profile_setlimit(p_rdt, &search_hl.tm);
7662# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663 }
7664}
7665
7666/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007667 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668 */
7669 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007670end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671{
7672 if (search_hl.rm.regprog != NULL)
7673 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007674 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675 search_hl.rm.regprog = NULL;
7676 }
7677}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02007678#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02007679
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007681screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682{
7683 attrentry_T *aep = NULL;
7684
7685 screen_attr = attr;
7686 if (full_screen
Bram Moolenaar4f974752019-02-17 17:44:42 +01007687#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688 && termcap_active
7689#endif
7690 )
7691 {
7692#ifdef FEAT_GUI
7693 if (gui.in_use)
7694 {
7695 char buf[20];
7696
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007697 /* The GUI handles this internally. */
7698 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699 OUT_STR(buf);
7700 }
7701 else
7702#endif
7703 {
7704 if (attr > HL_ALL) /* special HL attr. */
7705 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007706 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707 aep = syn_cterm_attr2entry(attr);
7708 else
7709 aep = syn_term_attr2entry(attr);
7710 if (aep == NULL) /* did ":syntax clear" */
7711 attr = 0;
7712 else
7713 attr = aep->ae_attr;
7714 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01007715 if ((attr & HL_BOLD) && *T_MD != NUL) /* bold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007717 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007718#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007719 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
7720 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
7721 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007722#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007723 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007724 /* If the Normal FG color has BOLD attribute and the new HL
7725 * has a FG color defined, clear BOLD. */
7726 out_str(T_ME);
Bram Moolenaar45a00002017-12-22 21:12:34 +01007727 if ((attr & HL_STANDOUT) && *T_SO != NUL) /* standout */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728 out_str(T_SO);
Bram Moolenaar45a00002017-12-22 21:12:34 +01007729 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) /* undercurl */
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01007730 out_str(T_UCS);
7731 if (((attr & HL_UNDERLINE) /* underline or undercurl */
Bram Moolenaar45a00002017-12-22 21:12:34 +01007732 || ((attr & HL_UNDERCURL) && *T_UCS == NUL))
7733 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734 out_str(T_US);
Bram Moolenaar45a00002017-12-22 21:12:34 +01007735 if ((attr & HL_ITALIC) && *T_CZH != NUL) /* italic */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736 out_str(T_CZH);
Bram Moolenaar45a00002017-12-22 21:12:34 +01007737 if ((attr & HL_INVERSE) && *T_MR != NUL) /* inverse (reverse) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738 out_str(T_MR);
Bram Moolenaar45a00002017-12-22 21:12:34 +01007739 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) /* strike */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02007740 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741
7742 /*
7743 * Output the color or start string after bold etc., in case the
7744 * bold etc. override the color setting.
7745 */
7746 if (aep != NULL)
7747 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007748#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007749 /* When 'termguicolors' is set but fg or bg is unset,
7750 * fall back to the cterm colors. This helps for SpellBad,
7751 * where the GUI uses a red undercurl. */
7752 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007754 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007755 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007756 }
7757 else
7758#endif
7759 if (t_colors > 1)
7760 {
7761 if (aep->ae_u.cterm.fg_color)
7762 term_fg_color(aep->ae_u.cterm.fg_color - 1);
7763 }
7764#ifdef FEAT_TERMGUICOLORS
7765 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
7766 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007767 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007768 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769 }
7770 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007771#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007772 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007774 if (aep->ae_u.cterm.bg_color)
7775 term_bg_color(aep->ae_u.cterm.bg_color - 1);
7776 }
7777
Bram Moolenaarf708ac52018-03-12 21:48:32 +01007778 if (!IS_CTERM)
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007779 {
7780 if (aep->ae_u.term.start != NULL)
7781 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782 }
7783 }
7784 }
7785 }
7786}
7787
7788 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007789screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790{
7791 int do_ME = FALSE; /* output T_ME code */
7792
7793 if (screen_attr != 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01007794#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795 && termcap_active
7796#endif
7797 )
7798 {
7799#ifdef FEAT_GUI
7800 if (gui.in_use)
7801 {
7802 char buf[20];
7803
7804 /* use internal GUI code */
7805 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
7806 OUT_STR(buf);
7807 }
7808 else
7809#endif
7810 {
7811 if (screen_attr > HL_ALL) /* special HL attr. */
7812 {
7813 attrentry_T *aep;
7814
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007815 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816 {
7817 /*
7818 * Assume that t_me restores the original colors!
7819 */
7820 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007821 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007822#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007823 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
7824 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
7825 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007826#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007827 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007828#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007829 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
7830 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
7831 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007832#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007833 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834 do_ME = TRUE;
7835 }
7836 else
7837 {
7838 aep = syn_term_attr2entry(screen_attr);
7839 if (aep != NULL && aep->ae_u.term.stop != NULL)
7840 {
7841 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
7842 do_ME = TRUE;
7843 else
7844 out_str(aep->ae_u.term.stop);
7845 }
7846 }
7847 if (aep == NULL) /* did ":syntax clear" */
7848 screen_attr = 0;
7849 else
7850 screen_attr = aep->ae_attr;
7851 }
7852
7853 /*
7854 * Often all ending-codes are equal to T_ME. Avoid outputting the
7855 * same sequence several times.
7856 */
7857 if (screen_attr & HL_STANDOUT)
7858 {
7859 if (STRCMP(T_SE, T_ME) == 0)
7860 do_ME = TRUE;
7861 else
7862 out_str(T_SE);
7863 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01007864 if ((screen_attr & HL_UNDERCURL) && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01007865 {
7866 if (STRCMP(T_UCE, T_ME) == 0)
7867 do_ME = TRUE;
7868 else
7869 out_str(T_UCE);
7870 }
7871 if ((screen_attr & HL_UNDERLINE)
Bram Moolenaar45a00002017-12-22 21:12:34 +01007872 || ((screen_attr & HL_UNDERCURL) && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873 {
7874 if (STRCMP(T_UE, T_ME) == 0)
7875 do_ME = TRUE;
7876 else
7877 out_str(T_UE);
7878 }
7879 if (screen_attr & HL_ITALIC)
7880 {
7881 if (STRCMP(T_CZR, T_ME) == 0)
7882 do_ME = TRUE;
7883 else
7884 out_str(T_CZR);
7885 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02007886 if (screen_attr & HL_STRIKETHROUGH)
7887 {
7888 if (STRCMP(T_STE, T_ME) == 0)
7889 do_ME = TRUE;
7890 else
7891 out_str(T_STE);
7892 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
7894 out_str(T_ME);
7895
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007896#ifdef FEAT_TERMGUICOLORS
7897 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007899 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007900 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007901 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007902 term_bg_rgb_color(cterm_normal_bg_gui_color);
7903 }
7904 else
7905#endif
7906 {
7907 if (t_colors > 1)
7908 {
7909 /* set Normal cterm colors */
7910 if (cterm_normal_fg_color != 0)
7911 term_fg_color(cterm_normal_fg_color - 1);
7912 if (cterm_normal_bg_color != 0)
7913 term_bg_color(cterm_normal_bg_color - 1);
7914 if (cterm_normal_fg_bold)
7915 out_str(T_MD);
7916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007917 }
7918 }
7919 }
7920 screen_attr = 0;
7921}
7922
7923/*
7924 * Reset the colors for a cterm. Used when leaving Vim.
7925 * The machine specific code may override this again.
7926 */
7927 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007928reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007930 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931 {
7932 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007933#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007934 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
7935 || cterm_normal_bg_gui_color != INVALCOLOR)
7936 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007937#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007939#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940 {
7941 out_str(T_OP);
7942 screen_attr = -1;
7943 }
7944 if (cterm_normal_fg_bold)
7945 {
7946 out_str(T_ME);
7947 screen_attr = -1;
7948 }
7949 }
7950}
7951
7952/*
7953 * Put character ScreenLines["off"] on the screen at position "row" and "col",
7954 * using the attributes from ScreenAttrs["off"].
7955 */
7956 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007957screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958{
7959 int attr;
7960
7961 /* Check for illegal values, just in case (could happen just after
7962 * resizing). */
7963 if (row >= screen_Rows || col >= screen_Columns)
7964 return;
7965
Bram Moolenaar33796b32019-06-08 16:01:13 +02007966 // Skip if under the popup menu.
7967 // Popup windows with zindex higher than POPUPMENU_ZINDEX go on top.
7968 if (pum_under_menu(row, col)
Bram Moolenaare2c453d2019-08-21 14:37:09 +02007969#ifdef FEAT_TEXT_PROP
Bram Moolenaar33796b32019-06-08 16:01:13 +02007970 && screen_zindex <= POPUPMENU_ZINDEX
Bram Moolenaare2c453d2019-08-21 14:37:09 +02007971#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02007972 )
Bram Moolenaarae654382019-01-17 21:09:05 +01007973 return;
Bram Moolenaar33796b32019-06-08 16:01:13 +02007974#ifdef FEAT_TEXT_PROP
Bram Moolenaarc662ec92019-06-23 00:15:57 +02007975 if (blocked_by_popup(row, col))
Bram Moolenaar33796b32019-06-08 16:01:13 +02007976 return;
7977#endif
7978
Bram Moolenaar494838a2015-02-10 19:20:37 +01007979 /* Outputting a character in the last cell on the screen may scroll the
7980 * screen up. Only do it when the "xn" termcap property is set, otherwise
7981 * mark the character invalid (update it when scrolled up). */
7982 if (*T_XN == NUL
7983 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984#ifdef FEAT_RIGHTLEFT
7985 /* account for first command-line character in rightleft mode */
7986 && !cmdmsg_rl
7987#endif
7988 )
7989 {
7990 ScreenAttrs[off] = (sattr_T)-1;
7991 return;
7992 }
7993
7994 /*
7995 * Stop highlighting first, so it's easier to move the cursor.
7996 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997 if (screen_char_attr != 0)
7998 attr = screen_char_attr;
7999 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000 attr = ScreenAttrs[off];
8001 if (screen_attr != attr)
8002 screen_stop_highlight();
8003
8004 windgoto(row, col);
8005
8006 if (screen_attr != attr)
8007 screen_start_highlight(attr);
8008
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009 if (enc_utf8 && ScreenLinesUC[off] != 0)
8010 {
8011 char_u buf[MB_MAXBYTES + 1];
8012
Bram Moolenaarcb070082016-04-02 22:14:51 +02008013 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008014 {
8015 if (*p_ambw == 'd'
Bram Moolenaara12a1612019-01-24 16:39:02 +01008016#ifdef FEAT_GUI
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008017 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008018#endif
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008019 )
8020 {
8021 /* Clear the two screen cells. If the character is actually
8022 * single width it won't change the second cell. */
8023 out_str((char_u *)" ");
8024 term_windgoto(row, col);
8025 }
8026 /* not sure where the cursor is after drawing the ambiguous width
8027 * character */
Bram Moolenaarcb070082016-04-02 22:14:51 +02008028 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008029 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02008030 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008032
8033 /* Convert the UTF-8 character to bytes and write it. */
8034 buf[utfc_char2bytes(off, buf)] = NUL;
8035 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036 }
8037 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008038 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041 /* double-byte character in single-width cell */
8042 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8043 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044 }
8045
8046 screen_cur_col++;
8047}
8048
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049/*
8050 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8051 * on the screen at position 'row' and 'col'.
8052 * The attributes of the first byte is used for all. This is required to
8053 * output the two bytes of a double-byte character with nothing in between.
8054 */
8055 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008056screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057{
8058 /* Check for illegal values (could be wrong when screen was resized). */
8059 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8060 return;
8061
8062 /* Outputting the last character on the screen may scrollup the screen.
8063 * Don't to it! Mark the character invalid (update it when scrolled up) */
8064 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8065 {
8066 ScreenAttrs[off] = (sattr_T)-1;
8067 return;
8068 }
8069
8070 /* Output the first byte normally (positions the cursor), then write the
8071 * second byte directly. */
8072 screen_char(off, row, col);
8073 out_char(ScreenLines[off + 1]);
8074 ++screen_cur_col;
8075}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077/*
8078 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8079 * This uses the contents of ScreenLines[] and doesn't change it.
8080 */
8081 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008082screen_draw_rectangle(
8083 int row,
8084 int col,
8085 int height,
8086 int width,
8087 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088{
8089 int r, c;
8090 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008091 int max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008093 /* Can't use ScreenLines unless initialized */
8094 if (ScreenLines == NULL)
8095 return;
8096
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097 if (invert)
8098 screen_char_attr = HL_INVERSE;
8099 for (r = row; r < row + height; ++r)
8100 {
8101 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008102 max_off = off + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 for (c = col; c < col + width; ++c)
8104 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00008105 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106 {
8107 screen_char_2(off + c, r, c);
8108 ++c;
8109 }
8110 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111 {
8112 screen_char(off + c, r, c);
Bram Moolenaar367329b2007-08-30 11:53:22 +00008113 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114 ++c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115 }
8116 }
8117 }
8118 screen_char_attr = 0;
8119}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121/*
8122 * Redraw the characters for a vertically split window.
8123 */
8124 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008125redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126{
8127 int col;
8128 int width;
8129
8130# ifdef FEAT_CLIPBOARD
8131 clip_may_clear_selection(row, end - 1);
8132# endif
8133
8134 if (wp == NULL)
8135 {
8136 col = 0;
8137 width = Columns;
8138 }
8139 else
8140 {
8141 col = wp->w_wincol;
8142 width = wp->w_width;
8143 }
8144 screen_draw_rectangle(row, col, end - row, width, FALSE);
8145}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008147 static void
8148space_to_screenline(int off, int attr)
8149{
8150 ScreenLines[off] = ' ';
8151 ScreenAttrs[off] = attr;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008152 if (enc_utf8)
8153 ScreenLinesUC[off] = 0;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008154}
8155
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156/*
8157 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8158 * with character 'c1' in first column followed by 'c2' in the other columns.
8159 * Use attributes 'attr'.
8160 */
8161 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008162screen_fill(
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008163 int start_row,
8164 int end_row,
8165 int start_col,
8166 int end_col,
8167 int c1,
8168 int c2,
8169 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008171 int row;
8172 int col;
8173 int off;
8174 int end_off;
8175 int did_delete;
8176 int c;
8177 int norm_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008179 int force_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180#endif
8181
8182 if (end_row > screen_Rows) /* safety check */
8183 end_row = screen_Rows;
8184 if (end_col > screen_Columns) /* safety check */
8185 end_col = screen_Columns;
8186 if (ScreenLines == NULL
8187 || start_row >= end_row
8188 || start_col >= end_col) /* nothing to do */
8189 return;
8190
8191 /* it's a "normal" terminal when not in a GUI or cterm */
8192 norm_term = (
8193#ifdef FEAT_GUI
8194 !gui.in_use &&
8195#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008196 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197 for (row = start_row; row < end_row; ++row)
8198 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008199 if (has_mbyte
Bram Moolenaara12a1612019-01-24 16:39:02 +01008200#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00008201 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008202#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00008203 )
8204 {
8205 /* When drawing over the right halve of a double-wide char clear
8206 * out the left halve. When drawing over the left halve of a
8207 * double wide-char clear out the right halve. Only needed in a
8208 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008209 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008210 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008211 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008212 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214 /*
8215 * Try to use delete-line termcap code, when no attributes or in a
8216 * "normal" terminal, where a bold/italic space is just a
8217 * space.
8218 */
8219 did_delete = FALSE;
8220 if (c2 == ' '
8221 && end_col == Columns
8222 && can_clear(T_CE)
8223 && (attr == 0
8224 || (norm_term
8225 && attr <= HL_ALL
8226 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8227 {
8228 /*
8229 * check if we really need to clear something
8230 */
8231 col = start_col;
8232 if (c1 != ' ') /* don't clear first char */
8233 ++col;
8234
8235 off = LineOffset[row] + col;
8236 end_off = LineOffset[row] + end_col;
8237
8238 /* skip blanks (used often, keep it fast!) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239 if (enc_utf8)
8240 while (off < end_off && ScreenLines[off] == ' '
8241 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8242 ++off;
8243 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244 while (off < end_off && ScreenLines[off] == ' '
8245 && ScreenAttrs[off] == 0)
8246 ++off;
8247 if (off < end_off) /* something to be cleared */
8248 {
8249 col = off - LineOffset[row];
8250 screen_stop_highlight();
8251 term_windgoto(row, col);/* clear rest of this screen line */
8252 out_str(T_CE);
8253 screen_start(); /* don't know where cursor is now */
8254 col = end_col - col;
8255 while (col--) /* clear chars in ScreenLines */
8256 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008257 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258 ++off;
8259 }
8260 }
8261 did_delete = TRUE; /* the chars are cleared now */
8262 }
8263
8264 off = LineOffset[row] + start_col;
8265 c = c1;
8266 for (col = start_col; col < end_col; ++col)
8267 {
Bram Moolenaar33796b32019-06-08 16:01:13 +02008268 if ((ScreenLines[off] != c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008269 || (enc_utf8 && (int)ScreenLinesUC[off]
8270 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 || ScreenAttrs[off] != attr
8272#if defined(FEAT_GUI) || defined(UNIX)
8273 || force_next
8274#endif
8275 )
Bram Moolenaar33796b32019-06-08 16:01:13 +02008276#ifdef FEAT_TEXT_PROP
8277 // Skip if under a(nother) popup.
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008278 && !blocked_by_popup(row, col)
Bram Moolenaar33796b32019-06-08 16:01:13 +02008279#endif
8280 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281 {
8282#if defined(FEAT_GUI) || defined(UNIX)
8283 /* The bold trick may make a single row of pixels appear in
8284 * the next character. When a bold character is removed, the
8285 * next character should be redrawn too. This happens for our
8286 * own GUI and for some xterms. */
8287 if (
8288# ifdef FEAT_GUI
8289 gui.in_use
8290# endif
8291# if defined(FEAT_GUI) && defined(UNIX)
8292 ||
8293# endif
8294# ifdef UNIX
8295 term_is_xterm
8296# endif
8297 )
8298 {
8299 if (ScreenLines[off] != ' '
8300 && (ScreenAttrs[off] > HL_ALL
8301 || ScreenAttrs[off] & HL_BOLD))
8302 force_next = TRUE;
8303 else
8304 force_next = FALSE;
8305 }
8306#endif
8307 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308 if (enc_utf8)
8309 {
8310 if (c >= 0x80)
8311 {
8312 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008313 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314 }
8315 else
8316 ScreenLinesUC[off] = 0;
8317 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 ScreenAttrs[off] = attr;
8319 if (!did_delete || c != ' ')
8320 screen_char(off, row, col);
8321 }
8322 ++off;
8323 if (col == start_col)
8324 {
8325 if (did_delete)
8326 break;
8327 c = c2;
8328 }
8329 }
8330 if (end_col == Columns)
8331 LineWraps[row] = FALSE;
8332 if (row == Rows - 1) /* overwritten the command line */
8333 {
8334 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02008335 if (start_col == 0 && end_col == Columns
8336 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008338 if (start_col == 0)
8339 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 }
8341 }
8342}
8343
8344/*
8345 * Check if there should be a delay. Used before clearing or redrawing the
8346 * screen or the command line.
8347 */
8348 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008349check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350{
8351 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8352 && !did_wait_return
8353 && emsg_silent == 0)
8354 {
8355 out_flush();
8356 ui_delay(1000L, TRUE);
8357 emsg_on_display = FALSE;
8358 if (check_msg_scroll)
8359 msg_scroll = FALSE;
8360 }
8361}
8362
8363/*
Bram Moolenaarca57ab52019-04-13 14:53:16 +02008364 * Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect.
8365 */
8366 static void
8367clear_TabPageIdxs(void)
8368{
8369 int scol;
8370
8371 for (scol = 0; scol < Columns; ++scol)
8372 TabPageIdxs[scol] = 0;
8373}
8374
8375/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008377 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378 * Returns TRUE if there is a valid screen to write to.
8379 * Returns FALSE when starting up and screen not initialized yet.
8380 */
8381 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008382screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008384 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385 return (ScreenLines != NULL);
8386}
8387
8388/*
8389 * Resize the shell to Rows and Columns.
8390 * Allocate ScreenLines[] and associated items.
8391 *
8392 * There may be some time between setting Rows and Columns and (re)allocating
8393 * ScreenLines[]. This happens when starting up and when (manually) changing
8394 * the shell size. Always use screen_Rows and screen_Columns to access items
8395 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8396 * final size of the shell is needed.
8397 */
8398 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008399screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400{
8401 int new_row, old_row;
8402#ifdef FEAT_GUI
8403 int old_Rows;
8404#endif
8405 win_T *wp;
8406 int outofmem = FALSE;
8407 int len;
8408 schar_T *new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008410 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008412 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413 sattr_T *new_ScreenAttrs;
8414 unsigned *new_LineOffset;
8415 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008416 short *new_TabPageIdxs;
Bram Moolenaar33796b32019-06-08 16:01:13 +02008417#ifdef FEAT_TEXT_PROP
8418 short *new_popup_mask;
Bram Moolenaar4c063a02019-06-10 21:24:12 +02008419 short *new_popup_mask_next;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008420 char *new_popup_transparent;
Bram Moolenaar33796b32019-06-08 16:01:13 +02008421#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00008422 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008424 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008425 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008427retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428 /*
8429 * Allocation of the screen buffers is done only when the size changes and
8430 * when Rows and Columns have been set and we have started doing full
8431 * screen stuff.
8432 */
8433 if ((ScreenLines != NULL
8434 && Rows == screen_Rows
8435 && Columns == screen_Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436 && enc_utf8 == (ScreenLinesUC != NULL)
8437 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaara12a1612019-01-24 16:39:02 +01008438 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439 || Rows == 0
8440 || Columns == 0
8441 || (!full_screen && ScreenLines == NULL))
8442 return;
8443
8444 /*
8445 * It's possible that we produce an out-of-memory message below, which
8446 * will cause this function to be called again. To break the loop, just
8447 * return here.
8448 */
8449 if (entered)
8450 return;
8451 entered = TRUE;
8452
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008453 /*
8454 * Note that the window sizes are updated before reallocating the arrays,
8455 * thus we must not redraw here!
8456 */
8457 ++RedrawingDisabled;
8458
Bram Moolenaar071d4272004-06-13 20:20:40 +00008459 win_new_shellsize(); /* fit the windows in the new sized shell */
8460
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461 comp_col(); /* recompute columns for shown command and ruler */
8462
8463 /*
8464 * We're changing the size of the screen.
8465 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8466 * - Move lines from the old arrays into the new arrays, clear extra
8467 * lines (unless the screen is going to be cleared).
8468 * - Free the old arrays.
8469 *
8470 * If anything fails, make ScreenLines NULL, so we don't do anything!
8471 * Continuing with the old ScreenLines may result in a crash, because the
8472 * size is wrong.
8473 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008474 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008476 if (aucmd_win != NULL)
8477 win_free_lsize(aucmd_win);
Bram Moolenaar8caaf822019-06-01 18:11:22 +02008478#ifdef FEAT_TEXT_PROP
8479 // global popup windows
8480 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
8481 win_free_lsize(wp);
8482 // tab-local popup windows
8483 FOR_ALL_TABPAGES(tp)
8484 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
8485 win_free_lsize(wp);
8486#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008488 new_ScreenLines = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
Bram Moolenaar216b7102010-03-23 13:56:59 +01008489 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008490 if (enc_utf8)
8491 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008492 new_ScreenLinesUC = LALLOC_MULT(u8char_T, (Rows + 1) * Columns);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008493 for (i = 0; i < p_mco; ++i)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008494 new_ScreenLinesC[i] = LALLOC_CLEAR_MULT(u8char_T,
8495 (Rows + 1) * Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496 }
8497 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008498 new_ScreenLines2 = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
8499 new_ScreenAttrs = LALLOC_MULT(sattr_T, (Rows + 1) * Columns);
8500 new_LineOffset = LALLOC_MULT(unsigned, Rows);
8501 new_LineWraps = LALLOC_MULT(char_u, Rows);
8502 new_TabPageIdxs = LALLOC_MULT(short, Columns);
Bram Moolenaar33796b32019-06-08 16:01:13 +02008503#ifdef FEAT_TEXT_PROP
8504 new_popup_mask = LALLOC_MULT(short, Rows * Columns);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02008505 new_popup_mask_next = LALLOC_MULT(short, Rows * Columns);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008506 new_popup_transparent = LALLOC_MULT(char, Rows * Columns);
Bram Moolenaar33796b32019-06-08 16:01:13 +02008507#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008509 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510 {
8511 if (win_alloc_lines(wp) == FAIL)
8512 {
8513 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008514 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515 }
8516 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008517 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8518 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008519 outofmem = TRUE;
Bram Moolenaar8caaf822019-06-01 18:11:22 +02008520#ifdef FEAT_TEXT_PROP
8521 // global popup windows
8522 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
8523 if (win_alloc_lines(wp) == FAIL)
8524 {
8525 outofmem = TRUE;
8526 goto give_up;
8527 }
8528 // tab-local popup windows
8529 FOR_ALL_TABPAGES(tp)
8530 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
8531 if (win_alloc_lines(wp) == FAIL)
8532 {
8533 outofmem = TRUE;
8534 goto give_up;
8535 }
8536#endif
8537
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008538give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008540 for (i = 0; i < p_mco; ++i)
8541 if (new_ScreenLinesC[i] == NULL)
8542 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543 if (new_ScreenLines == NULL
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008544 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546 || new_ScreenAttrs == NULL
8547 || new_LineOffset == NULL
8548 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008549 || new_TabPageIdxs == NULL
Bram Moolenaar33796b32019-06-08 16:01:13 +02008550#ifdef FEAT_TEXT_PROP
8551 || new_popup_mask == NULL
Bram Moolenaar4c063a02019-06-10 21:24:12 +02008552 || new_popup_mask_next == NULL
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008553 || new_popup_transparent == NULL
Bram Moolenaar33796b32019-06-08 16:01:13 +02008554#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555 || outofmem)
8556 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008557 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008558 {
8559 /* guess the size */
8560 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8561
8562 /* Remember we did this to avoid getting outofmem messages over
8563 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008564 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008565 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01008566 VIM_CLEAR(new_ScreenLines);
Bram Moolenaard23a8232018-02-10 18:45:26 +01008567 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008568 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01008569 VIM_CLEAR(new_ScreenLinesC[i]);
8570 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaard23a8232018-02-10 18:45:26 +01008571 VIM_CLEAR(new_ScreenAttrs);
8572 VIM_CLEAR(new_LineOffset);
8573 VIM_CLEAR(new_LineWraps);
8574 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar33796b32019-06-08 16:01:13 +02008575#ifdef FEAT_TEXT_PROP
8576 VIM_CLEAR(new_popup_mask);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02008577 VIM_CLEAR(new_popup_mask_next);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008578 VIM_CLEAR(new_popup_transparent);
Bram Moolenaar33796b32019-06-08 16:01:13 +02008579#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580 }
8581 else
8582 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008583 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008584
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585 for (new_row = 0; new_row < Rows; ++new_row)
8586 {
8587 new_LineOffset[new_row] = new_row * Columns;
8588 new_LineWraps[new_row] = FALSE;
8589
8590 /*
8591 * If the screen is not going to be cleared, copy as much as
8592 * possible from the old screen to the new one and clear the rest
8593 * (used when resizing the window at the "--more--" prompt or when
8594 * executing an external command, for the GUI).
8595 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008596 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597 {
8598 (void)vim_memset(new_ScreenLines + new_row * Columns,
8599 ' ', (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600 if (enc_utf8)
8601 {
8602 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8603 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008604 for (i = 0; i < p_mco; ++i)
8605 (void)vim_memset(new_ScreenLinesC[i]
8606 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607 0, (size_t)Columns * sizeof(u8char_T));
8608 }
8609 if (enc_dbcs == DBCS_JPNU)
8610 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8611 0, (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8613 0, (size_t)Columns * sizeof(sattr_T));
8614 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008615 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616 {
8617 if (screen_Columns < Columns)
8618 len = screen_Columns;
8619 else
8620 len = Columns;
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008621 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008622 * may be invalid now. Also when p_mco changes. */
8623 if (!(enc_utf8 && ScreenLinesUC == NULL)
8624 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008625 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8626 ScreenLines + LineOffset[old_row],
8627 (size_t)len * sizeof(schar_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008628 if (enc_utf8 && ScreenLinesUC != NULL
8629 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630 {
8631 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8632 ScreenLinesUC + LineOffset[old_row],
8633 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008634 for (i = 0; i < p_mco; ++i)
8635 mch_memmove(new_ScreenLinesC[i]
8636 + new_LineOffset[new_row],
8637 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638 (size_t)len * sizeof(u8char_T));
8639 }
8640 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8641 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8642 ScreenLines2 + LineOffset[old_row],
8643 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8645 ScreenAttrs + LineOffset[old_row],
8646 (size_t)len * sizeof(sattr_T));
8647 }
8648 }
8649 }
8650 /* Use the last line of the screen for the current line. */
8651 current_ScreenLine = new_ScreenLines + Rows * Columns;
Bram Moolenaar6ace95e2019-08-13 23:09:49 +02008652
8653#ifdef FEAT_TEXT_PROP
8654 vim_memset(new_popup_mask, 0, Rows * Columns * sizeof(short));
8655 vim_memset(new_popup_transparent, 0, Rows * Columns * sizeof(char));
8656#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008657 }
8658
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008659 free_screenlines();
8660
Bram Moolenaar6ace95e2019-08-13 23:09:49 +02008661 // NOTE: this may result in all pointers to become NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008662 ScreenLines = new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008664 for (i = 0; i < p_mco; ++i)
8665 ScreenLinesC[i] = new_ScreenLinesC[i];
8666 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667 ScreenLines2 = new_ScreenLines2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668 ScreenAttrs = new_ScreenAttrs;
8669 LineOffset = new_LineOffset;
8670 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008671 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar33796b32019-06-08 16:01:13 +02008672#ifdef FEAT_TEXT_PROP
8673 popup_mask = new_popup_mask;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008674 popup_mask_next = new_popup_mask_next;
8675 popup_transparent = new_popup_transparent;
Bram Moolenaar33796b32019-06-08 16:01:13 +02008676 popup_mask_refresh = TRUE;
8677#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678
8679 /* It's important that screen_Rows and screen_Columns reflect the actual
8680 * size of ScreenLines[]. Set them before calling anything. */
8681#ifdef FEAT_GUI
8682 old_Rows = screen_Rows;
8683#endif
8684 screen_Rows = Rows;
8685 screen_Columns = Columns;
8686
8687 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008688 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689 screenclear2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008690#ifdef FEAT_GUI
8691 else if (gui.in_use
8692 && !gui.starting
8693 && ScreenLines != NULL
8694 && old_Rows != Rows)
8695 {
8696 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8697 /*
8698 * Adjust the position of the cursor, for when executing an external
8699 * command.
8700 */
8701 if (msg_row >= Rows) /* Rows got smaller */
8702 msg_row = Rows - 1; /* put cursor at last row */
8703 else if (Rows > old_Rows) /* Rows got bigger */
8704 msg_row += Rows - old_Rows; /* put cursor in same place */
8705 if (msg_col >= Columns) /* Columns got smaller */
8706 msg_col = Columns - 1; /* put cursor at last column */
8707 }
8708#endif
Bram Moolenaarca57ab52019-04-13 14:53:16 +02008709 clear_TabPageIdxs();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008710
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008712 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008713
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008714 /*
8715 * Do not apply autocommands more than 3 times to avoid an endless loop
8716 * in case applying autocommands always changes Rows or Columns.
8717 */
8718 if (starting == 0 && ++retry_count <= 3)
8719 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008720 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008721 /* In rare cases, autocommands may have altered Rows or Columns,
8722 * jump back to check if we need to allocate the screen again. */
8723 goto retry;
8724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008725}
8726
8727 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008728free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008729{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008730 int i;
8731
Bram Moolenaar33796b32019-06-08 16:01:13 +02008732 VIM_CLEAR(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008733 for (i = 0; i < Screen_mco; ++i)
Bram Moolenaar33796b32019-06-08 16:01:13 +02008734 VIM_CLEAR(ScreenLinesC[i]);
8735 VIM_CLEAR(ScreenLines2);
8736 VIM_CLEAR(ScreenLines);
8737 VIM_CLEAR(ScreenAttrs);
8738 VIM_CLEAR(LineOffset);
8739 VIM_CLEAR(LineWraps);
8740 VIM_CLEAR(TabPageIdxs);
8741#ifdef FEAT_TEXT_PROP
8742 VIM_CLEAR(popup_mask);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02008743 VIM_CLEAR(popup_mask_next);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008744 VIM_CLEAR(popup_transparent);
Bram Moolenaar33796b32019-06-08 16:01:13 +02008745#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008746}
8747
8748 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008749screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750{
8751 check_for_delay(FALSE);
8752 screenalloc(FALSE); /* allocate screen buffers if size changed */
8753 screenclear2(); /* clear the screen */
8754}
8755
8756 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008757screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758{
8759 int i;
8760
8761 if (starting == NO_SCREEN || ScreenLines == NULL
8762#ifdef FEAT_GUI
8763 || (gui.in_use && gui.starting)
8764#endif
8765 )
8766 return;
8767
8768#ifdef FEAT_GUI
8769 if (!gui.in_use)
8770#endif
8771 screen_attr = -1; /* force setting the Normal colors */
8772 screen_stop_highlight(); /* don't want highlighting here */
8773
8774#ifdef FEAT_CLIPBOARD
8775 /* disable selection without redrawing it */
8776 clip_scroll_selection(9999);
8777#endif
8778
8779 /* blank out ScreenLines */
8780 for (i = 0; i < Rows; ++i)
8781 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02008782 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783 LineWraps[i] = FALSE;
8784 }
8785
8786 if (can_clear(T_CL))
8787 {
8788 out_str(T_CL); /* clear the display */
8789 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008790 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008791 }
8792 else
8793 {
8794 /* can't clear the screen, mark all chars with invalid attributes */
8795 for (i = 0; i < Rows; ++i)
8796 lineinvalid(LineOffset[i], (int)Columns);
8797 clear_cmdline = TRUE;
8798 }
8799
8800 screen_cleared = TRUE; /* can use contents of ScreenLines now */
8801
8802 win_rest_invalid(firstwin);
8803 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008804 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805 if (must_redraw == CLEAR) /* no need to clear again */
8806 must_redraw = NOT_VALID;
8807 compute_cmdrow();
8808 msg_row = cmdline_row; /* put cursor on last line for messages */
8809 msg_col = 0;
8810 screen_start(); /* don't know where cursor is now */
8811 msg_scrolled = 0; /* can't scroll back */
8812 msg_didany = FALSE;
8813 msg_didout = FALSE;
8814}
8815
8816/*
8817 * Clear one line in ScreenLines.
8818 */
8819 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02008820lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821{
8822 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008823 if (enc_utf8)
8824 (void)vim_memset(ScreenLinesUC + off, 0,
8825 (size_t)width * sizeof(u8char_T));
Bram Moolenaarcfce7172017-08-17 20:31:48 +02008826 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827}
8828
8829/*
8830 * Mark one line in ScreenLines invalid by setting the attributes to an
8831 * invalid value.
8832 */
8833 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008834lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835{
8836 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
8837}
8838
Bram Moolenaar071d4272004-06-13 20:20:40 +00008839/*
8840 * Copy part of a Screenline for vertically split window "wp".
8841 */
8842 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008843linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008844{
8845 unsigned off_to = LineOffset[to] + wp->w_wincol;
8846 unsigned off_from = LineOffset[from] + wp->w_wincol;
8847
8848 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
8849 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850 if (enc_utf8)
8851 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008852 int i;
8853
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
8855 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008856 for (i = 0; i < p_mco; ++i)
8857 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
8858 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859 }
8860 if (enc_dbcs == DBCS_JPNU)
8861 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
8862 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008863 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
8864 wp->w_width * sizeof(sattr_T));
8865}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866
8867/*
8868 * Return TRUE if clearing with term string "p" would work.
8869 * It can't work when the string is empty or it won't set the right background.
Bram Moolenaar33796b32019-06-08 16:01:13 +02008870 * Don't clear to end-of-line when there are popups, it may cause flicker.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871 */
8872 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008873can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874{
8875 return (*p != NUL && (t_colors <= 1
8876#ifdef FEAT_GUI
8877 || gui.in_use
8878#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008879#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008880 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02008881 || (!p_tgc && cterm_normal_bg_color == 0)
8882#else
8883 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008884#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02008885 || *T_UT != NUL)
8886#ifdef FEAT_TEXT_PROP
8887 && !(p == T_CE && popup_visible)
8888#endif
8889 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890}
8891
8892/*
8893 * Reset cursor position. Use whenever cursor was moved because of outputting
8894 * something directly to the screen (shell commands) or a terminal control
8895 * code.
8896 */
8897 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008898screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899{
8900 screen_cur_row = screen_cur_col = 9999;
8901}
8902
8903/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904 * Move the cursor to position "row","col" in the screen.
8905 * This tries to find the most efficient way to move, minimizing the number of
8906 * characters sent to the terminal.
8907 */
8908 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008909windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008910{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008911 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912 int i;
8913 int plan;
8914 int cost;
8915 int wouldbe_col;
8916 int noinvcurs;
8917 char_u *bs;
8918 int goto_cost;
8919 int attr;
8920
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008921#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
8923
8924#define PLAN_LE 1
8925#define PLAN_CR 2
8926#define PLAN_NL 3
8927#define PLAN_WRITE 4
8928 /* Can't use ScreenLines unless initialized */
8929 if (ScreenLines == NULL)
8930 return;
8931
8932 if (col != screen_cur_col || row != screen_cur_row)
8933 {
8934 /* Check for valid position. */
8935 if (row < 0) /* window without text lines? */
8936 row = 0;
8937 if (row >= screen_Rows)
8938 row = screen_Rows - 1;
8939 if (col >= screen_Columns)
8940 col = screen_Columns - 1;
8941
8942 /* check if no cursor movement is allowed in highlight mode */
8943 if (screen_attr && *T_MS == NUL)
8944 noinvcurs = HIGHL_COST;
8945 else
8946 noinvcurs = 0;
8947 goto_cost = GOTO_COST + noinvcurs;
8948
8949 /*
8950 * Plan how to do the positioning:
8951 * 1. Use CR to move it to column 0, same row.
8952 * 2. Use T_LE to move it a few columns to the left.
8953 * 3. Use NL to move a few lines down, column 0.
8954 * 4. Move a few columns to the right with T_ND or by writing chars.
8955 *
8956 * Don't do this if the cursor went beyond the last column, the cursor
8957 * position is unknown then (some terminals wrap, some don't )
8958 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008959 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960 * characters to move the cursor to the right.
8961 */
8962 if (row >= screen_cur_row && screen_cur_col < Columns)
8963 {
8964 /*
8965 * If the cursor is in the same row, bigger col, we can use CR
8966 * or T_LE.
8967 */
8968 bs = NULL; /* init for GCC */
8969 attr = screen_attr;
8970 if (row == screen_cur_row && col < screen_cur_col)
8971 {
8972 /* "le" is preferred over "bc", because "bc" is obsolete */
8973 if (*T_LE)
8974 bs = T_LE; /* "cursor left" */
8975 else
8976 bs = T_BC; /* "backspace character (old) */
8977 if (*bs)
8978 cost = (screen_cur_col - col) * (int)STRLEN(bs);
8979 else
8980 cost = 999;
8981 if (col + 1 < cost) /* using CR is less characters */
8982 {
8983 plan = PLAN_CR;
8984 wouldbe_col = 0;
8985 cost = 1; /* CR is just one character */
8986 }
8987 else
8988 {
8989 plan = PLAN_LE;
8990 wouldbe_col = col;
8991 }
8992 if (noinvcurs) /* will stop highlighting */
8993 {
8994 cost += noinvcurs;
8995 attr = 0;
8996 }
8997 }
8998
8999 /*
9000 * If the cursor is above where we want to be, we can use CR LF.
9001 */
9002 else if (row > screen_cur_row)
9003 {
9004 plan = PLAN_NL;
9005 wouldbe_col = 0;
9006 cost = (row - screen_cur_row) * 2; /* CR LF */
9007 if (noinvcurs) /* will stop highlighting */
9008 {
9009 cost += noinvcurs;
9010 attr = 0;
9011 }
9012 }
9013
9014 /*
9015 * If the cursor is in the same row, smaller col, just use write.
9016 */
9017 else
9018 {
9019 plan = PLAN_WRITE;
9020 wouldbe_col = screen_cur_col;
9021 cost = 0;
9022 }
9023
9024 /*
9025 * Check if any characters that need to be written have the
9026 * correct attributes. Also avoid UTF-8 characters.
9027 */
9028 i = col - wouldbe_col;
9029 if (i > 0)
9030 cost += i;
9031 if (cost < goto_cost && i > 0)
9032 {
9033 /*
9034 * Check if the attributes are correct without additionally
9035 * stopping highlighting.
9036 */
9037 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9038 while (i && *p++ == attr)
9039 --i;
9040 if (i != 0)
9041 {
9042 /*
9043 * Try if it works when highlighting is stopped here.
9044 */
9045 if (*--p == 0)
9046 {
9047 cost += noinvcurs;
9048 while (i && *p++ == 0)
9049 --i;
9050 }
9051 if (i != 0)
9052 cost = 999; /* different attributes, don't do it */
9053 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009054 if (enc_utf8)
9055 {
9056 /* Don't use an UTF-8 char for positioning, it's slow. */
9057 for (i = wouldbe_col; i < col; ++i)
9058 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9059 {
9060 cost = 999;
9061 break;
9062 }
9063 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064 }
9065
9066 /*
9067 * We can do it without term_windgoto()!
9068 */
9069 if (cost < goto_cost)
9070 {
9071 if (plan == PLAN_LE)
9072 {
9073 if (noinvcurs)
9074 screen_stop_highlight();
9075 while (screen_cur_col > col)
9076 {
9077 out_str(bs);
9078 --screen_cur_col;
9079 }
9080 }
9081 else if (plan == PLAN_CR)
9082 {
9083 if (noinvcurs)
9084 screen_stop_highlight();
9085 out_char('\r');
9086 screen_cur_col = 0;
9087 }
9088 else if (plan == PLAN_NL)
9089 {
9090 if (noinvcurs)
9091 screen_stop_highlight();
9092 while (screen_cur_row < row)
9093 {
9094 out_char('\n');
9095 ++screen_cur_row;
9096 }
9097 screen_cur_col = 0;
9098 }
9099
9100 i = col - screen_cur_col;
9101 if (i > 0)
9102 {
9103 /*
9104 * Use cursor-right if it's one character only. Avoids
9105 * removing a line of pixels from the last bold char, when
9106 * using the bold trick in the GUI.
9107 */
9108 if (T_ND[0] != NUL && T_ND[1] == NUL)
9109 {
9110 while (i-- > 0)
9111 out_char(*T_ND);
9112 }
9113 else
9114 {
9115 int off;
9116
9117 off = LineOffset[row] + screen_cur_col;
9118 while (i-- > 0)
9119 {
9120 if (ScreenAttrs[off] != screen_attr)
9121 screen_stop_highlight();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009122 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009123 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009124 if (enc_dbcs == DBCS_JPNU
9125 && ScreenLines[off] == 0x8e)
9126 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009127 ++off;
9128 }
9129 }
9130 }
9131 }
9132 }
9133 else
9134 cost = 999;
9135
9136 if (cost >= goto_cost)
9137 {
9138 if (noinvcurs)
9139 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009140 if (row == screen_cur_row && (col > screen_cur_col)
9141 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009142 term_cursor_right(col - screen_cur_col);
9143 else
9144 term_windgoto(row, col);
9145 }
9146 screen_cur_row = row;
9147 screen_cur_col = col;
9148 }
9149}
9150
9151/*
9152 * Set cursor to its position in the current window.
9153 */
9154 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009155setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156{
Bram Moolenaar987723e2018-03-06 11:43:04 +01009157 setcursor_mayforce(FALSE);
9158}
9159
9160/*
9161 * Set cursor to its position in the current window.
9162 * When "force" is TRUE also when not redrawing.
9163 */
9164 void
9165setcursor_mayforce(int force)
9166{
9167 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168 {
9169 validate_cursor();
9170 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009171 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009172#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009173 /* With 'rightleft' set and the cursor on a double-wide
9174 * character, position it on the leftmost column. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01009175 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol
9176 - ((has_mbyte
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009177 && (*mb_ptr2cells)(ml_get_cursor()) == 2
Bram Moolenaara12a1612019-01-24 16:39:02 +01009178 && vim_isprintc(gchar_cursor())) ? 2 : 1)) :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179#endif
9180 curwin->w_wcol));
9181 }
9182}
9183
9184
9185/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009186 * Insert 'line_count' lines at 'row' in window 'wp'.
9187 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9188 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009189 * scrolling.
9190 * Returns FAIL if the lines are not inserted, OK for success.
9191 */
9192 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009193win_ins_lines(
9194 win_T *wp,
9195 int row,
9196 int line_count,
9197 int invalid,
9198 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199{
9200 int did_delete;
9201 int nextrow;
9202 int lastrow;
9203 int retval;
9204
9205 if (invalid)
9206 wp->w_lines_valid = 0;
9207
9208 if (wp->w_height < 5)
9209 return FAIL;
9210
9211 if (line_count > wp->w_height - row)
9212 line_count = wp->w_height - row;
9213
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009214 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009215 if (retval != MAYBE)
9216 return retval;
9217
9218 /*
9219 * If there is a next window or a status line, we first try to delete the
9220 * lines at the bottom to avoid messing what is after the window.
Bram Moolenaarc363fe12019-08-04 18:13:46 +02009221 * If this fails and there are following windows, don't do anything to
9222 * avoid messing up those windows, better just redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009223 */
9224 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009225 if (wp->w_next != NULL || wp->w_status_height)
9226 {
9227 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009228 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009229 did_delete = TRUE;
9230 else if (wp->w_next)
9231 return FAIL;
9232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009233 /*
9234 * if no lines deleted, blank the lines that will end up below the window
9235 */
9236 if (!did_delete)
9237 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009238 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009239 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02009240 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009241 lastrow = nextrow + line_count;
9242 if (lastrow > Rows)
9243 lastrow = Rows;
9244 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009245 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246 ' ', ' ', 0);
9247 }
9248
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009249 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009250 == FAIL)
9251 {
Bram Moolenaarc363fe12019-08-04 18:13:46 +02009252 // deletion will have messed up other windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00009253 if (did_delete)
9254 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009255 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009256 win_rest_invalid(W_NEXT(wp));
9257 }
9258 return FAIL;
9259 }
9260
9261 return OK;
9262}
9263
9264/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009265 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009266 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9267 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9268 * scrolling
9269 * Return OK for success, FAIL if the lines are not deleted.
9270 */
9271 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009272win_del_lines(
9273 win_T *wp,
9274 int row,
9275 int line_count,
9276 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009277 int mayclear,
9278 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009279{
9280 int retval;
9281
9282 if (invalid)
9283 wp->w_lines_valid = 0;
9284
9285 if (line_count > wp->w_height - row)
9286 line_count = wp->w_height - row;
9287
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009288 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009289 if (retval != MAYBE)
9290 return retval;
9291
9292 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009293 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009294 return FAIL;
9295
Bram Moolenaar071d4272004-06-13 20:20:40 +00009296 /*
9297 * If there are windows or status lines below, try to put them at the
9298 * correct place. If we can't do that, they have to be redrawn.
9299 */
9300 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9301 {
9302 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009303 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009304 {
9305 wp->w_redr_status = TRUE;
9306 win_rest_invalid(wp->w_next);
9307 }
9308 }
9309 /*
9310 * If this is the last window and there is no status line, redraw the
9311 * command line later.
9312 */
9313 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009314 redraw_cmdline = TRUE;
9315 return OK;
9316}
9317
9318/*
9319 * Common code for win_ins_lines() and win_del_lines().
9320 * Returns OK or FAIL when the work has been done.
9321 * Returns MAYBE when not finished yet.
9322 */
9323 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009324win_do_lines(
9325 win_T *wp,
9326 int row,
9327 int line_count,
9328 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009329 int del,
9330 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009331{
9332 int retval;
9333
9334 if (!redrawing() || line_count <= 0)
9335 return FAIL;
9336
Bram Moolenaar33796b32019-06-08 16:01:13 +02009337 // When inserting lines would result in loss of command output, just redraw
9338 // the lines.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009339 if (no_win_do_lines_ins && !del)
9340 return FAIL;
9341
Bram Moolenaar33796b32019-06-08 16:01:13 +02009342 // only a few lines left: redraw is faster
Bram Moolenaar4033c552017-09-16 20:54:51 +02009343 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009344 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009345 if (!no_win_do_lines_ins)
Bram Moolenaar33796b32019-06-08 16:01:13 +02009346 screenclear(); // will set wp->w_lines_valid to 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00009347 return FAIL;
9348 }
9349
Bram Moolenaar33796b32019-06-08 16:01:13 +02009350#ifdef FEAT_TEXT_PROP
Bram Moolenaar4c063a02019-06-10 21:24:12 +02009351 // this doesn't work when there are popups visible
Bram Moolenaar33796b32019-06-08 16:01:13 +02009352 if (popup_visible)
9353 return FAIL;
9354#endif
9355
9356 // Delete all remaining lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00009357 if (row + line_count >= wp->w_height)
9358 {
9359 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009360 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009361 ' ', ' ', 0);
9362 return OK;
9363 }
9364
9365 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009366 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009367 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009368 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009370 if (!no_win_do_lines_ins)
9371 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009372
9373 /*
9374 * If the terminal can set a scroll region, use that.
9375 * Always do this in a vertically split window. This will redraw from
9376 * ScreenLines[] when t_CV isn't defined. That's faster than using
9377 * win_line().
9378 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009379 * a character in the lower right corner of the scroll region may cause a
9380 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009381 */
Bram Moolenaar02631462017-09-22 15:20:32 +02009382 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009383 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009384 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009385 scroll_region_set(wp, row);
9386 if (del)
9387 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009388 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009389 else
9390 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009391 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009392 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009393 scroll_region_reset();
9394 return retval;
9395 }
9396
Bram Moolenaar071d4272004-06-13 20:20:40 +00009397 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9398 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009399
9400 return MAYBE;
9401}
9402
9403/*
9404 * window 'wp' and everything after it is messed up, mark it for redraw
9405 */
9406 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009407win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009408{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009409 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009410 {
9411 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009412 wp->w_redr_status = TRUE;
9413 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009414 }
9415 redraw_cmdline = TRUE;
9416}
9417
9418/*
9419 * The rest of the routines in this file perform screen manipulations. The
9420 * given operation is performed physically on the screen. The corresponding
9421 * change is also made to the internal screen image. In this way, the editor
9422 * anticipates the effect of editing changes on the appearance of the screen.
9423 * That way, when we call screenupdate a complete redraw isn't usually
9424 * necessary. Another advantage is that we can keep adding code to anticipate
9425 * screen changes, and in the meantime, everything still works.
9426 */
9427
9428/*
9429 * types for inserting or deleting lines
9430 */
9431#define USE_T_CAL 1
9432#define USE_T_CDL 2
9433#define USE_T_AL 3
9434#define USE_T_CE 4
9435#define USE_T_DL 5
9436#define USE_T_SR 6
9437#define USE_NL 7
9438#define USE_T_CD 8
9439#define USE_REDRAW 9
9440
9441/*
9442 * insert lines on the screen and update ScreenLines[]
9443 * 'end' is the line after the scrolled part. Normally it is Rows.
9444 * When scrolling region used 'off' is the offset from the top for the region.
9445 * 'row' and 'end' are relative to the start of the region.
9446 *
9447 * return FAIL for failure, OK for success.
9448 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009449 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009450screen_ins_lines(
9451 int off,
9452 int row,
9453 int line_count,
9454 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009455 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01009456 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009457{
9458 int i;
9459 int j;
9460 unsigned temp;
9461 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009462 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009463 int type;
9464 int result_empty;
9465 int can_ce = can_clear(T_CE);
9466
9467 /*
9468 * FAIL if
9469 * - there is no valid screen
9470 * - the screen has to be redrawn completely
9471 * - the line count is less than one
9472 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009473 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar33796b32019-06-08 16:01:13 +02009474 * - there is a popup window
Bram Moolenaar071d4272004-06-13 20:20:40 +00009475 */
Bram Moolenaar33796b32019-06-08 16:01:13 +02009476 if (!screen_valid(TRUE)
9477 || line_count <= 0 || line_count > p_ttyscroll
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009478#ifdef FEAT_CLIPBOARD
9479 || (clip_star.state != SELECT_CLEARED
9480 && redrawing_for_callback > 0)
9481#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02009482#ifdef FEAT_TEXT_PROP
9483 || popup_visible
9484#endif
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009485 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009486 return FAIL;
9487
9488 /*
9489 * There are seven ways to insert lines:
9490 * 0. When in a vertically split window and t_CV isn't set, redraw the
9491 * characters from ScreenLines[].
9492 * 1. Use T_CD (clear to end of display) if it exists and the result of
9493 * the insert is just empty lines
9494 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9495 * present or line_count > 1. It looks better if we do all the inserts
9496 * at once.
9497 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9498 * insert is just empty lines and T_CE is not present or line_count >
9499 * 1.
9500 * 4. Use T_AL (insert line) if it exists.
9501 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9502 * just empty lines.
9503 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9504 * just empty lines.
9505 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9506 * the 'da' flag is not set or we have clear line capability.
9507 * 8. redraw the characters from ScreenLines[].
9508 *
9509 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9510 * the scrollbar for the window. It does have insert line, use that if it
9511 * exists.
9512 */
9513 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009514 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9515 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009516 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009517 type = USE_T_CD;
9518 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9519 type = USE_T_CAL;
9520 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9521 type = USE_T_CDL;
9522 else if (*T_AL != NUL)
9523 type = USE_T_AL;
9524 else if (can_ce && result_empty)
9525 type = USE_T_CE;
9526 else if (*T_DL != NUL && result_empty)
9527 type = USE_T_DL;
9528 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9529 type = USE_T_SR;
9530 else
9531 return FAIL;
9532
9533 /*
9534 * For clearing the lines screen_del_lines() is used. This will also take
9535 * care of t_db if necessary.
9536 */
9537 if (type == USE_T_CD || type == USE_T_CDL ||
9538 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009539 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009540
9541 /*
9542 * If text is retained below the screen, first clear or delete as many
9543 * lines at the bottom of the window as are about to be inserted so that
9544 * the deleted lines won't later surface during a screen_del_lines.
9545 */
9546 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009547 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009548
9549#ifdef FEAT_CLIPBOARD
9550 /* Remove a modeless selection when inserting lines halfway the screen
9551 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009552 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009553 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009554 else
9555 clip_scroll_selection(-line_count);
9556#endif
9557
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558#ifdef FEAT_GUI
9559 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9560 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009561 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009562#endif
9563
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009564 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
9565 cursor_col = wp->w_wincol;
9566
Bram Moolenaar071d4272004-06-13 20:20:40 +00009567 if (*T_CCS != NUL) /* cursor relative to region */
9568 cursor_row = row;
9569 else
9570 cursor_row = row + off;
9571
9572 /*
9573 * Shift LineOffset[] line_count down to reflect the inserted lines.
9574 * Clear the inserted lines in ScreenLines[].
9575 */
9576 row += off;
9577 end += off;
9578 for (i = 0; i < line_count; ++i)
9579 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009580 if (wp != NULL && wp->w_width != Columns)
9581 {
9582 /* need to copy part of a line */
9583 j = end - 1 - i;
9584 while ((j -= line_count) >= row)
9585 linecopy(j + line_count, j, wp);
9586 j += line_count;
9587 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009588 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
9589 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009590 else
9591 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9592 LineWraps[j] = FALSE;
9593 }
9594 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595 {
9596 j = end - 1 - i;
9597 temp = LineOffset[j];
9598 while ((j -= line_count) >= row)
9599 {
9600 LineOffset[j + line_count] = LineOffset[j];
9601 LineWraps[j + line_count] = LineWraps[j];
9602 }
9603 LineOffset[j + line_count] = temp;
9604 LineWraps[j + line_count] = FALSE;
9605 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009606 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607 else
9608 lineinvalid(temp, (int)Columns);
9609 }
9610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009611
9612 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009613 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009614 if (clear_attr != 0)
9615 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009616
Bram Moolenaar071d4272004-06-13 20:20:40 +00009617 /* redraw the characters */
9618 if (type == USE_REDRAW)
9619 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02009620 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009621 {
9622 term_append_lines(line_count);
9623 screen_start(); /* don't know where cursor is now */
9624 }
9625 else
9626 {
9627 for (i = 0; i < line_count; i++)
9628 {
9629 if (type == USE_T_AL)
9630 {
9631 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009632 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009633 out_str(T_AL);
9634 }
9635 else /* type == USE_T_SR */
9636 out_str(T_SR);
9637 screen_start(); /* don't know where cursor is now */
9638 }
9639 }
9640
9641 /*
9642 * With scroll-reverse and 'da' flag set we need to clear the lines that
9643 * have been scrolled down into the region.
9644 */
9645 if (type == USE_T_SR && *T_DA)
9646 {
9647 for (i = 0; i < line_count; ++i)
9648 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009649 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009650 out_str(T_CE);
9651 screen_start(); /* don't know where cursor is now */
9652 }
9653 }
9654
9655#ifdef FEAT_GUI
9656 gui_can_update_cursor();
9657 if (gui.in_use)
9658 out_flush(); /* always flush after a scroll */
9659#endif
9660 return OK;
9661}
9662
9663/*
Bram Moolenaar107abd22016-08-12 14:08:25 +02009664 * Delete lines on the screen and update ScreenLines[].
9665 * "end" is the line after the scrolled part. Normally it is Rows.
9666 * When scrolling region used "off" is the offset from the top for the region.
9667 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009668 *
9669 * Return OK for success, FAIL if the lines are not deleted.
9670 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009671 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009672screen_del_lines(
9673 int off,
9674 int row,
9675 int line_count,
9676 int end,
9677 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009678 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +01009679 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009680{
9681 int j;
9682 int i;
9683 unsigned temp;
9684 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009685 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009686 int cursor_end;
9687 int result_empty; /* result is empty until end of region */
9688 int can_delete; /* deleting line codes can be used */
9689 int type;
9690
9691 /*
9692 * FAIL if
9693 * - there is no valid screen
9694 * - the screen has to be redrawn completely
9695 * - the line count is less than one
9696 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009697 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009698 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009699 if (!screen_valid(TRUE) || line_count <= 0
9700 || (!force && line_count > p_ttyscroll)
9701#ifdef FEAT_CLIPBOARD
9702 || (clip_star.state != SELECT_CLEARED
9703 && redrawing_for_callback > 0)
9704#endif
9705 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009706 return FAIL;
9707
9708 /*
9709 * Check if the rest of the current region will become empty.
9710 */
9711 result_empty = row + line_count >= end;
9712
9713 /*
9714 * We can delete lines only when 'db' flag not set or when 'ce' option
9715 * available.
9716 */
9717 can_delete = (*T_DB == NUL || can_clear(T_CE));
9718
9719 /*
9720 * There are six ways to delete lines:
9721 * 0. When in a vertically split window and t_CV isn't set, redraw the
9722 * characters from ScreenLines[].
9723 * 1. Use T_CD if it exists and the result is empty.
9724 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9725 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9726 * none of the other ways work.
9727 * 4. Use T_CE (erase line) if the result is empty.
9728 * 5. Use T_DL (delete line) if it exists.
9729 * 6. redraw the characters from ScreenLines[].
9730 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009731 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9732 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009733 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009734 type = USE_T_CD;
9735#if defined(__BEOS__) && defined(BEOS_DR8)
9736 /*
9737 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9738 * its internal termcap... this works okay for tests which test *T_DB !=
9739 * NUL. It has the disadvantage that the user cannot use any :set t_*
9740 * command to get T_DB (back) to empty_option, only :set term=... will do
9741 * the trick...
9742 * Anyway, this hack will hopefully go away with the next OS release.
9743 * (Olaf Seibert)
9744 */
9745 else if (row == 0 && T_DB == empty_option
9746 && (line_count == 1 || *T_CDL == NUL))
9747#else
9748 else if (row == 0 && (
9749#ifndef AMIGA
9750 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9751 * up, so use delete-line command */
9752 line_count == 1 ||
9753#endif
9754 *T_CDL == NUL))
9755#endif
9756 type = USE_NL;
9757 else if (*T_CDL != NUL && line_count > 1 && can_delete)
9758 type = USE_T_CDL;
9759 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +02009760 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009761 type = USE_T_CE;
9762 else if (*T_DL != NUL && can_delete)
9763 type = USE_T_DL;
9764 else if (*T_CDL != NUL && can_delete)
9765 type = USE_T_CDL;
9766 else
9767 return FAIL;
9768
9769#ifdef FEAT_CLIPBOARD
9770 /* Remove a modeless selection when deleting lines halfway the screen or
9771 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009772 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009773 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009774 else
9775 clip_scroll_selection(line_count);
9776#endif
9777
Bram Moolenaar071d4272004-06-13 20:20:40 +00009778#ifdef FEAT_GUI
9779 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9780 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009781 gui_dont_update_cursor(gui.cursor_row >= row + off
9782 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009783#endif
9784
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009785 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
9786 cursor_col = wp->w_wincol;
9787
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788 if (*T_CCS != NUL) /* cursor relative to region */
9789 {
9790 cursor_row = row;
9791 cursor_end = end;
9792 }
9793 else
9794 {
9795 cursor_row = row + off;
9796 cursor_end = end + off;
9797 }
9798
9799 /*
9800 * Now shift LineOffset[] line_count up to reflect the deleted lines.
9801 * Clear the inserted lines in ScreenLines[].
9802 */
9803 row += off;
9804 end += off;
9805 for (i = 0; i < line_count; ++i)
9806 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009807 if (wp != NULL && wp->w_width != Columns)
9808 {
9809 /* need to copy part of a line */
9810 j = row + i;
9811 while ((j += line_count) <= end - 1)
9812 linecopy(j - line_count, j, wp);
9813 j -= line_count;
9814 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009815 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
9816 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009817 else
9818 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9819 LineWraps[j] = FALSE;
9820 }
9821 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009822 {
9823 /* whole width, moving the line pointers is faster */
9824 j = row + i;
9825 temp = LineOffset[j];
9826 while ((j += line_count) <= end - 1)
9827 {
9828 LineOffset[j - line_count] = LineOffset[j];
9829 LineWraps[j - line_count] = LineWraps[j];
9830 }
9831 LineOffset[j - line_count] = temp;
9832 LineWraps[j - line_count] = FALSE;
9833 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009834 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009835 else
9836 lineinvalid(temp, (int)Columns);
9837 }
9838 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009839
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009840 if (screen_attr != clear_attr)
9841 screen_stop_highlight();
9842 if (clear_attr != 0)
9843 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009844
Bram Moolenaar071d4272004-06-13 20:20:40 +00009845 /* redraw the characters */
9846 if (type == USE_REDRAW)
9847 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02009848 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009849 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009850 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009851 out_str(T_CD);
9852 screen_start(); /* don't know where cursor is now */
9853 }
9854 else if (type == USE_T_CDL)
9855 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009856 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009857 term_delete_lines(line_count);
9858 screen_start(); /* don't know where cursor is now */
9859 }
9860 /*
9861 * Deleting lines at top of the screen or scroll region: Just scroll
9862 * the whole screen (scroll region) up by outputting newlines on the
9863 * last line.
9864 */
9865 else if (type == USE_NL)
9866 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009867 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009868 for (i = line_count; --i >= 0; )
9869 out_char('\n'); /* cursor will remain on same line */
9870 }
9871 else
9872 {
9873 for (i = line_count; --i >= 0; )
9874 {
9875 if (type == USE_T_DL)
9876 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009877 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009878 out_str(T_DL); /* delete a line */
9879 }
9880 else /* type == USE_T_CE */
9881 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009882 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009883 out_str(T_CE); /* erase a line */
9884 }
9885 screen_start(); /* don't know where cursor is now */
9886 }
9887 }
9888
9889 /*
9890 * If the 'db' flag is set, we need to clear the lines that have been
9891 * scrolled up at the bottom of the region.
9892 */
9893 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
9894 {
9895 for (i = line_count; i > 0; --i)
9896 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009897 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009898 out_str(T_CE); /* erase a line */
9899 screen_start(); /* don't know where cursor is now */
9900 }
9901 }
9902
9903#ifdef FEAT_GUI
9904 gui_can_update_cursor();
9905 if (gui.in_use)
9906 out_flush(); /* always flush after a scroll */
9907#endif
9908
9909 return OK;
9910}
9911
9912/*
Bram Moolenaarcb574f42019-01-25 22:29:57 +01009913 * Return TRUE when postponing displaying the mode message: when not redrawing
9914 * or inside a mapping.
9915 */
9916 int
9917skip_showmode()
9918{
9919 // Call char_avail() only when we are going to show something, because it
9920 // takes a bit of time. redrawing() may also call char_avail_avail().
9921 if (global_busy
9922 || msg_silent != 0
9923 || !redrawing()
9924 || (char_avail() && !KeyTyped))
9925 {
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02009926 redraw_mode = TRUE; // show mode later
Bram Moolenaarcb574f42019-01-25 22:29:57 +01009927 return TRUE;
9928 }
9929 return FALSE;
9930}
9931
9932/*
Bram Moolenaar81226e02018-02-20 21:44:45 +01009933 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009934 *
9935 * If clear_cmdline is TRUE, clear the rest of the cmdline.
9936 * If clear_cmdline is FALSE there may be a message there that needs to be
9937 * cleared only if a mode is shown.
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02009938 * If redraw_mode is TRUE show or clear the mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009939 * Return the length of the message (0 if no message).
9940 */
9941 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009942showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009943{
9944 int need_clear;
9945 int length = 0;
9946 int do_mode;
9947 int attr;
9948 int nwr_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009949 int sub_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009950
Bram Moolenaar7df351e2006-01-23 22:30:28 +00009951 do_mode = ((p_smd && msg_silent == 0)
9952 && ((State & INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +02009953 || restart_edit != NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009954 || VIsual_active));
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009955 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009956 {
Bram Moolenaarcb574f42019-01-25 22:29:57 +01009957 if (skip_showmode())
9958 return 0; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00009959
9960 nwr_save = need_wait_return;
9961
9962 /* wait a bit before overwriting an important message */
9963 check_for_delay(FALSE);
9964
9965 /* if the cmdline is more than one line high, erase top lines */
9966 need_clear = clear_cmdline;
9967 if (clear_cmdline && cmdline_row < Rows - 1)
9968 msg_clr_cmdline(); /* will reset clear_cmdline */
9969
9970 /* Position on the last line in the window, column 0 */
9971 msg_pos_mode();
9972 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +01009973 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009974 if (do_mode)
9975 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01009976 msg_puts_attr("--", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009977#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00009978 if (
Bram Moolenaar09092152010-08-08 16:38:42 +02009979# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +00009980 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +02009981# else
Bram Moolenaarc236c162008-07-13 17:41:49 +00009982 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +00009983# endif
Bram Moolenaar09092152010-08-08 16:38:42 +02009984 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02009985# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar32526b32019-01-19 17:43:09 +01009986 msg_puts_attr(" IM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009987# else
Bram Moolenaar32526b32019-01-19 17:43:09 +01009988 msg_puts_attr(" XIM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009989# endif
9990#endif
9991#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
9992 if (gui.in_use)
9993 {
9994 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +01009995 {
9996 /* HANGUL */
9997 if (enc_utf8)
Bram Moolenaar32526b32019-01-19 17:43:09 +01009998 msg_puts_attr(" \355\225\234\352\270\200", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +01009999 else
Bram Moolenaar32526b32019-01-19 17:43:09 +010010000 msg_puts_attr(" \307\321\261\333", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010001 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010002 }
10003#endif
Bram Moolenaarea389e92014-05-28 21:40:52 +020010004 /* CTRL-X in Insert mode */
10005 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010006 {
10007 /* These messages can get long, avoid a wrap in a narrow
10008 * window. Prefer showing edit_submode_extra. */
10009 length = (Rows - msg_row) * Columns - 3;
10010 if (edit_submode_extra != NULL)
10011 length -= vim_strsize(edit_submode_extra);
10012 if (length > 0)
10013 {
10014 if (edit_submode_pre != NULL)
10015 length -= vim_strsize(edit_submode_pre);
10016 if (length - vim_strsize(edit_submode) > 0)
10017 {
10018 if (edit_submode_pre != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010019 msg_puts_attr((char *)edit_submode_pre, attr);
10020 msg_puts_attr((char *)edit_submode, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010021 }
10022 if (edit_submode_extra != NULL)
10023 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010024 msg_puts_attr(" ", attr); /* add a space in between */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010025 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010026 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010027 else
10028 sub_attr = attr;
Bram Moolenaar32526b32019-01-19 17:43:09 +010010029 msg_puts_attr((char *)edit_submode_extra, sub_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010030 }
10031 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010032 }
10033 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010034 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010035 if (State & VREPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010036 msg_puts_attr(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010037 else if (State & REPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010038 msg_puts_attr(_(" REPLACE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010039 else if (State & INSERT)
10040 {
10041#ifdef FEAT_RIGHTLEFT
10042 if (p_ri)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010043 msg_puts_attr(_(" REVERSE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010044#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +010010045 msg_puts_attr(_(" INSERT"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010046 }
Bram Moolenaar942b4542018-06-17 16:23:34 +020010047 else if (restart_edit == 'I' || restart_edit == 'A')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010048 msg_puts_attr(_(" (insert)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010049 else if (restart_edit == 'R')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010050 msg_puts_attr(_(" (replace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010051 else if (restart_edit == 'V')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010052 msg_puts_attr(_(" (vreplace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010053#ifdef FEAT_RIGHTLEFT
10054 if (p_hkmap)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010055 msg_puts_attr(_(" Hebrew"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010056#endif
10057#ifdef FEAT_KEYMAP
10058 if (State & LANGMAP)
10059 {
10060# ifdef FEAT_ARABIC
10061 if (curwin->w_p_arab)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010062 msg_puts_attr(_(" Arabic"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010063 else
10064# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010065 if (get_keymap_str(curwin, (char_u *)" (%s)",
10066 NameBuff, MAXPATHL))
Bram Moolenaar32526b32019-01-19 17:43:09 +010010067 msg_puts_attr((char *)NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010068 }
10069#endif
10070 if ((State & INSERT) && p_paste)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010071 msg_puts_attr(_(" (paste)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010072
Bram Moolenaar071d4272004-06-13 20:20:40 +000010073 if (VIsual_active)
10074 {
10075 char *p;
10076
10077 /* Don't concatenate separate words to avoid translation
10078 * problems. */
10079 switch ((VIsual_select ? 4 : 0)
10080 + (VIsual_mode == Ctrl_V) * 2
10081 + (VIsual_mode == 'V'))
10082 {
10083 case 0: p = N_(" VISUAL"); break;
10084 case 1: p = N_(" VISUAL LINE"); break;
10085 case 2: p = N_(" VISUAL BLOCK"); break;
10086 case 4: p = N_(" SELECT"); break;
10087 case 5: p = N_(" SELECT LINE"); break;
10088 default: p = N_(" SELECT BLOCK"); break;
10089 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010090 msg_puts_attr(_(p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010091 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010092 msg_puts_attr(" --", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010093 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010094
Bram Moolenaar071d4272004-06-13 20:20:40 +000010095 need_clear = TRUE;
10096 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010097 if (reg_recording != 0
Bram Moolenaare2c453d2019-08-21 14:37:09 +020010098 && edit_submode == NULL) // otherwise it gets too long
Bram Moolenaar071d4272004-06-13 20:20:40 +000010099 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010100 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010101 need_clear = TRUE;
10102 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010103
10104 mode_displayed = TRUE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010105 if (need_clear || clear_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010106 msg_clr_eos();
10107 msg_didout = FALSE; /* overwrite this message */
10108 length = msg_col;
10109 msg_col = 0;
10110 need_wait_return = nwr_save; /* never ask for hit-return for this */
10111 }
10112 else if (clear_cmdline && msg_silent == 0)
10113 /* Clear the whole command line. Will reset "clear_cmdline". */
10114 msg_clr_cmdline();
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010115 else if (redraw_mode)
10116 {
10117 msg_pos_mode();
10118 msg_clr_eos();
10119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120
10121#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010122 /* In Visual mode the size of the selected area must be redrawn. */
10123 if (VIsual_active)
10124 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010125
10126 /* If the last window has no status line, the ruler is after the mode
10127 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010128 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +020010129 win_redr_ruler(lastwin, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010130#endif
10131 redraw_cmdline = FALSE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010132 redraw_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010133 clear_cmdline = FALSE;
10134
10135 return length;
10136}
10137
10138/*
10139 * Position for a mode message.
10140 */
10141 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010142msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010143{
10144 msg_col = 0;
10145 msg_row = Rows - 1;
10146}
10147
10148/*
10149 * Delete mode message. Used when ESC is typed which is expected to end
10150 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010151 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010152 */
10153 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010154unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010155{
10156 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010157 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010158 */
10159 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10160 redraw_cmdline = TRUE; /* delete mode later */
10161 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010162 clearmode();
10163}
10164
10165/*
10166 * Clear the mode message.
10167 */
10168 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010169clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010170{
Bram Moolenaar2abad542018-05-19 14:43:45 +020010171 int save_msg_row = msg_row;
10172 int save_msg_col = msg_col;
10173
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010174 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010175 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010176 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010177 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +020010178
10179 msg_col = save_msg_col;
10180 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010181}
10182
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010183 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010184recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010185{
Bram Moolenaar32526b32019-01-19 17:43:09 +010010186 msg_puts_attr(_("recording"), attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010187 if (!shortmess(SHM_RECORDING))
10188 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010189 char s[4];
10190
10191 sprintf(s, " @%c", reg_recording);
10192 msg_puts_attr(s, attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010193 }
10194}
10195
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010196/*
10197 * Draw the tab pages line at the top of the Vim window.
10198 */
Bram Moolenaare12bab32019-01-08 22:02:56 +010010199 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010200draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010201{
10202 int tabcount = 0;
10203 tabpage_T *tp;
10204 int tabwidth;
10205 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010206 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010207 int attr;
10208 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010209 win_T *cwp;
10210 int wincount;
10211 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010212 int c;
10213 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010214 int attr_sel = HL_ATTR(HLF_TPS);
10215 int attr_nosel = HL_ATTR(HLF_TP);
10216 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010217 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010218 int room;
10219 int use_sep_chars = (t_colors < 8
10220#ifdef FEAT_GUI
10221 && !gui.in_use
10222#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010223#ifdef FEAT_TERMGUICOLORS
10224 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010225#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010226 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010227
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010228 if (ScreenLines == NULL)
10229 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010230 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010231
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010232#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010233 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010234 if (gui_use_tabline())
10235 {
10236 gui_update_tabline();
10237 return;
10238 }
10239#endif
10240
10241 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010242 return;
10243
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010244#if defined(FEAT_STL_OPT)
Bram Moolenaarca57ab52019-04-13 14:53:16 +020010245 clear_TabPageIdxs();
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010246
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010247 /* Use the 'tabline' option if it's set. */
10248 if (*p_tal != NUL)
10249 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010250 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010251
10252 /* Check for an error. If there is one we would loop in redrawing the
10253 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010254 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010255 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010256 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010257 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010258 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010259 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010260 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010261 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010262#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010263 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010264 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010265 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010266
Bram Moolenaar238a5642006-02-21 22:12:05 +000010267 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10268 if (tabwidth < 6)
10269 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010270
Bram Moolenaar238a5642006-02-21 22:12:05 +000010271 attr = attr_nosel;
10272 tabcount = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010273 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10274 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010275 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010276 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010277
Bram Moolenaar238a5642006-02-21 22:12:05 +000010278 if (tp->tp_topframe == topframe)
10279 attr = attr_sel;
10280 if (use_sep_chars && col > 0)
10281 screen_putchar('|', 0, col++, attr);
10282
10283 if (tp->tp_topframe != topframe)
10284 attr = attr_nosel;
10285
10286 screen_putchar(' ', 0, col++, attr);
10287
10288 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010289 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010290 cwp = curwin;
10291 wp = firstwin;
10292 }
10293 else
10294 {
10295 cwp = tp->tp_curwin;
10296 wp = tp->tp_firstwin;
10297 }
10298
10299 modified = FALSE;
10300 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10301 if (bufIsChanged(wp->w_buffer))
10302 modified = TRUE;
10303 if (modified || wincount > 1)
10304 {
10305 if (wincount > 1)
10306 {
10307 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010308 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010309 if (col + len >= Columns - 3)
10310 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010311 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010312#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010313 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010314#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010315 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010316#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010317 );
10318 col += len;
10319 }
10320 if (modified)
10321 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10322 screen_putchar(' ', 0, col++, attr);
10323 }
10324
10325 room = scol - col + tabwidth - 1;
10326 if (room > 0)
10327 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010328 /* Get buffer name in NameBuff[] */
10329 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010330 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010331 len = vim_strsize(NameBuff);
10332 p = NameBuff;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010333 if (has_mbyte)
10334 while (len > room)
10335 {
10336 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010337 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010338 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010010339 else if (len > room)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010340 {
10341 p += len - room;
10342 len = room;
10343 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010344 if (len > Columns - col - 1)
10345 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010346
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010347 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010348 col += len;
10349 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010350 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010351
10352 /* Store the tab page number in TabPageIdxs[], so that
10353 * jump_to_mouse() knows where each one is. */
10354 ++tabcount;
10355 while (scol < col)
10356 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010357 }
10358
Bram Moolenaar238a5642006-02-21 22:12:05 +000010359 if (use_sep_chars)
10360 c = '_';
10361 else
10362 c = ' ';
10363 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010364
10365 /* Put an "X" for closing the current tab if there are several. */
10366 if (first_tabpage->tp_next != NULL)
10367 {
10368 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10369 TabPageIdxs[Columns - 1] = -999;
10370 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010371 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010372
10373 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10374 * set. */
10375 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010376}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010377
10378/*
10379 * Get buffer name for "buf" into NameBuff[].
10380 * Takes care of special buffer names and translates special characters.
10381 */
10382 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010383get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010384{
10385 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010386 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010387 else
10388 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10389 trans_characters(NameBuff, MAXPATHL);
10390}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010391
Bram Moolenaar071d4272004-06-13 20:20:40 +000010392/*
10393 * Get the character to use in a status line. Get its attributes in "*attr".
10394 */
10395 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010396fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010397{
10398 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010399
10400#ifdef FEAT_TERMINAL
10401 if (bt_terminal(wp->w_buffer))
10402 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010403 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010404 {
10405 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010406 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010407 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010408 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010409 {
10410 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010411 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010412 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010413 }
10414 else
10415#endif
10416 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010417 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010418 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010419 fill = fill_stl;
10420 }
10421 else
10422 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010423 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010424 fill = fill_stlnc;
10425 }
10426 /* Use fill when there is highlighting, and highlighting of current
10427 * window differs, or the fillchars differ, or this is not the
10428 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010429 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010430 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010431 || (fill_stl != fill_stlnc)))
10432 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010433 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010434 return '^';
10435 return '=';
10436}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010437
Bram Moolenaar071d4272004-06-13 20:20:40 +000010438/*
10439 * Get the character to use in a separator between vertically split windows.
10440 * Get its attributes in "*attr".
10441 */
10442 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010443fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010444{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010445 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010446 if (*attr == 0 && fill_vert == ' ')
10447 return '|';
10448 else
10449 return fill_vert;
10450}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010451
10452/*
10453 * Return TRUE if redrawing should currently be done.
10454 */
10455 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010456redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010457{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010458#ifdef FEAT_EVAL
10459 if (disable_redraw_for_testing)
10460 return 0;
10461 else
10462#endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +020010463 return ((!RedrawingDisabled
10464#ifdef FEAT_EVAL
10465 || ignore_redraw_flag_for_testing
10466#endif
10467 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010468}
10469
10470/*
10471 * Return TRUE if printing messages should currently be done.
10472 */
10473 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010474messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010475{
10476 return (!(p_lz && char_avail() && !KeyTyped));
10477}
10478
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010479#ifdef FEAT_MENU
10480/*
10481 * Draw the window toolbar.
10482 */
10483 static void
10484redraw_win_toolbar(win_T *wp)
10485{
10486 vimmenu_T *menu;
10487 int item_idx = 0;
10488 int item_count = 0;
10489 int col = 0;
10490 int next_col;
10491 int off = (int)(current_ScreenLine - ScreenLines);
10492 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
10493 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
10494
10495 vim_free(wp->w_winbar_items);
10496 for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
10497 ++item_count;
Bram Moolenaarc799fe22019-05-28 23:08:19 +020010498 wp->w_winbar_items = ALLOC_CLEAR_MULT(winbar_item_T, item_count + 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010499
10500 /* TODO: use fewer spaces if there is not enough room */
10501 for (menu = wp->w_winbar->children;
Bram Moolenaar02631462017-09-22 15:20:32 +020010502 menu != NULL && col < wp->w_width; menu = menu->next)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010503 {
10504 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010505 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010506 break;
10507 if (col > 1)
10508 {
10509 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010510 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010511 break;
10512 }
10513
10514 wp->w_winbar_items[item_idx].wb_startcol = col;
10515 space_to_screenline(off + col, button_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010516 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010517 break;
10518
10519 next_col = text_to_screenline(wp, menu->name, col);
10520 while (col < next_col)
10521 {
10522 ScreenAttrs[off + col] = button_attr;
10523 ++col;
10524 }
10525 wp->w_winbar_items[item_idx].wb_endcol = col;
10526 wp->w_winbar_items[item_idx].wb_menu = menu;
10527 ++item_idx;
10528
Bram Moolenaar02631462017-09-22 15:20:32 +020010529 if (col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010530 break;
10531 space_to_screenline(off + col, button_attr);
10532 ++col;
10533 }
Bram Moolenaar02631462017-09-22 15:20:32 +020010534 while (col < wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010535 {
10536 space_to_screenline(off + col, fill_attr);
10537 ++col;
10538 }
10539 wp->w_winbar_items[item_idx].wb_menu = NULL; /* end marker */
10540
Bram Moolenaar02631462017-09-22 15:20:32 +020010541 screen_line(wp->w_winrow, wp->w_wincol, (int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +020010542 (int)wp->w_width, 0);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010543}
10544#endif
Bram Moolenaar491ac282018-06-17 14:47:55 +020010545
Bram Moolenaar071d4272004-06-13 20:20:40 +000010546/*
10547 * Show current status info in ruler and various other places
10548 * If always is FALSE, only show ruler if position has changed.
10549 */
10550 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010551showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010552{
10553 if (!always && !redrawing())
10554 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010555 if (pum_visible())
10556 {
10557 /* Don't redraw right now, do it later. */
10558 curwin->w_redr_status = TRUE;
10559 return;
10560 }
Bram Moolenaar4033c552017-09-16 20:54:51 +020010561#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010562 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar362f3562009-11-03 16:20:34 +000010563 redraw_custom_statusline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010564 else
10565#endif
10566#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +020010567 win_redr_ruler(curwin, always, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010568#endif
10569
10570#ifdef FEAT_TITLE
10571 if (need_maketitle
10572# ifdef FEAT_STL_OPT
10573 || (p_icon && (stl_syntax & STL_IN_ICON))
10574 || (p_title && (stl_syntax & STL_IN_TITLE))
10575# endif
10576 )
10577 maketitle();
10578#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010579 /* Redraw the tab pages line if needed. */
10580 if (redraw_tabline)
10581 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010582}
10583
10584#ifdef FEAT_CMDL_INFO
10585 static void
Bram Moolenaar491ac282018-06-17 14:47:55 +020010586win_redr_ruler(win_T *wp, int always, int ignore_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010587{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010588#define RULER_BUF_LEN 70
10589 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010590 int row;
10591 int fillchar;
10592 int attr;
10593 int empty_line = FALSE;
10594 colnr_T virtcol;
10595 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010596 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010597 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010598 int this_ru_col;
10599 int off = 0;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +010010600 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010601
10602 /* If 'ruler' off or redrawing disabled, don't do anything */
10603 if (!p_ru)
10604 return;
10605
10606 /*
10607 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10608 * after deleting lines, before cursor.lnum is corrected.
10609 */
10610 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10611 return;
10612
Bram Moolenaare2c453d2019-08-21 14:37:09 +020010613 // Don't draw the ruler while doing insert-completion, it might overwrite
10614 // the (long) mode message.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010615 if (wp == lastwin && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010616 if (edit_submode != NULL)
10617 return;
Bram Moolenaar491ac282018-06-17 14:47:55 +020010618 // Don't draw the ruler when the popup menu is visible, it may overlap.
10619 // Except when the popup menu will be redrawn anyway.
10620 if (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010621 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010622
10623#ifdef FEAT_STL_OPT
10624 if (*p_ruf)
10625 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010626 int save_called_emsg = called_emsg;
10627
10628 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010629 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010630 if (called_emsg)
10631 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010632 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010633 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010634 return;
10635 }
10636#endif
10637
10638 /*
10639 * Check if not in Insert mode and the line is empty (will show "0-1").
10640 */
10641 if (!(State & INSERT)
10642 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10643 empty_line = TRUE;
10644
10645 /*
10646 * Only draw the ruler when something changed.
10647 */
10648 validate_virtcol_win(wp);
10649 if ( redraw_cmdline
10650 || always
10651 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10652 || wp->w_cursor.col != wp->w_ru_cursor.col
10653 || wp->w_virtcol != wp->w_ru_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +000010654 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
Bram Moolenaar071d4272004-06-13 20:20:40 +000010655 || wp->w_topline != wp->w_ru_topline
10656 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10657#ifdef FEAT_DIFF
10658 || wp->w_topfill != wp->w_ru_topfill
10659#endif
10660 || empty_line != wp->w_ru_empty)
10661 {
10662 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010663 if (wp->w_status_height)
10664 {
10665 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010666 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar53f81742017-09-22 14:35:51 +020010667 off = wp->w_wincol;
Bram Moolenaar02631462017-09-22 15:20:32 +020010668 width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010669 }
10670 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010671 {
10672 row = Rows - 1;
10673 fillchar = ' ';
10674 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010675 width = Columns;
10676 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010677 }
10678
10679 /* In list mode virtcol needs to be recomputed */
10680 virtcol = wp->w_virtcol;
10681 if (wp->w_p_list && lcs_tab1 == NUL)
10682 {
10683 wp->w_p_list = FALSE;
10684 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10685 wp->w_p_list = TRUE;
10686 }
10687
10688 /*
10689 * Some sprintfs return the length, some return a pointer.
10690 * To avoid portability problems we use strlen() here.
10691 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010692 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010693 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10694 ? 0L
10695 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010696 len = STRLEN(buffer);
10697 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010698 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10699 (int)virtcol + 1);
10700
10701 /*
10702 * Add a "50%" if there is room for it.
10703 * On the last line, don't print in the last column (scrolls the
10704 * screen up on some terminals).
10705 */
10706 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010707 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010708 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010709 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010710 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010711 this_ru_col = ru_col - (Columns - width);
10712 if (this_ru_col < 0)
10713 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010714 /* Never use more than half the window/screen width, leave the other
10715 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010716 if (this_ru_col < (width + 1) / 2)
10717 this_ru_col = (width + 1) / 2;
10718 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010719 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010010720 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010721 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010722 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010723 if (has_mbyte)
10724 i += (*mb_char2bytes)(fillchar, buffer + i);
10725 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010726 buffer[i++] = fillchar;
10727 ++o;
10728 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010729 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010730 }
10731 /* Truncate at window boundary. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010732 if (has_mbyte)
10733 {
10734 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010735 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010736 {
10737 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010738 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010739 {
10740 buffer[i] = NUL;
10741 break;
10742 }
10743 }
10744 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010010745 else if (this_ru_col + (int)STRLEN(buffer) > width)
Bram Moolenaar4033c552017-09-16 20:54:51 +020010746 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010747
Bram Moolenaar4033c552017-09-16 20:54:51 +020010748 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010749 i = redraw_cmdline;
10750 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020010751 this_ru_col + off + (int)STRLEN(buffer),
10752 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010753 fillchar, fillchar, attr);
10754 /* don't redraw the cmdline because of showing the ruler */
10755 redraw_cmdline = i;
10756 wp->w_ru_cursor = wp->w_cursor;
10757 wp->w_ru_virtcol = wp->w_virtcol;
10758 wp->w_ru_empty = empty_line;
10759 wp->w_ru_topline = wp->w_topline;
10760 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10761#ifdef FEAT_DIFF
10762 wp->w_ru_topfill = wp->w_topfill;
10763#endif
10764 }
10765}
10766#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010767
10768#if defined(FEAT_LINEBREAK) || defined(PROTO)
10769/*
Bram Moolenaar64486672010-05-16 15:46:46 +020010770 * Return the width of the 'number' and 'relativenumber' column.
10771 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010772 * Otherwise it depends on 'numberwidth' and the line count.
10773 */
10774 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010775number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010776{
10777 int n;
10778 linenr_T lnum;
10779
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020010780 if (wp->w_p_rnu && !wp->w_p_nu)
10781 /* cursor line shows "0" */
10782 lnum = wp->w_height;
10783 else
10784 /* cursor line shows absolute line number */
10785 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020010786
Bram Moolenaar6b314672015-03-20 15:42:10 +010010787 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010788 return wp->w_nrwidth_width;
10789 wp->w_nrwidth_line_count = lnum;
10790
10791 n = 0;
10792 do
10793 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010794 lnum /= 10;
10795 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010796 } while (lnum > 0);
10797
10798 /* 'numberwidth' gives the minimal width plus one */
10799 if (n < wp->w_p_nuw - 1)
10800 n = wp->w_p_nuw - 1;
10801
Bram Moolenaare4b407f2019-07-04 11:59:28 +020010802# ifdef FEAT_SIGNS
10803 // If 'signcolumn' is set to 'number' and there is a sign to display, then
10804 // the minimal width for the number column is 2.
10805 if (n < 2 && (wp->w_buffer->b_signlist != NULL)
10806 && (*wp->w_p_scl == 'n' && *(wp->w_p_scl + 1) == 'u'))
10807 n = 2;
10808# endif
10809
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010810 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010010811 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010812 return n;
10813}
10814#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010815
Bram Moolenaar113e1072019-01-20 15:30:40 +010010816#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010817/*
10818 * Return the current cursor column. This is the actual position on the
10819 * screen. First column is 0.
10820 */
10821 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010822screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010823{
10824 return screen_cur_col;
10825}
10826
10827/*
10828 * Return the current cursor row. This is the actual position on the screen.
10829 * First row is 0.
10830 */
10831 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010832screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010833{
10834 return screen_cur_row;
10835}
Bram Moolenaar113e1072019-01-20 15:30:40 +010010836#endif