blob: 760cd2c1f6adb0fe16a064d0ce1ba7d448b7a941 [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#ifdef FEAT_INS_EXPAND
789 /* May need to redraw the popup menu. */
Bram Moolenaar491ac282018-06-17 14:47:55 +0200790 pum_may_redraw();
Bram Moolenaar51971b32013-02-13 12:16:05 +0100791#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 /* Reset b_mod_set flags. Going through all windows is probably faster
794 * than going through all buffers (there could be many buffers). */
Bram Moolenaar29323592016-07-24 22:04:11 +0200795 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 wp->w_buffer->b_mod_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +0200798#ifdef FEAT_TEXT_PROP
799 // Display popup windows on top of the windows and command line.
800 update_popups(win_update);
801#endif
802
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200803 after_updating_screen(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804
805 /* Clear or redraw the command line. Done last, because scrolling may
806 * mess up the command line. */
Bram Moolenaar4c25bd72019-04-20 23:38:07 +0200807 if (clear_cmdline || redraw_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 showmode();
809
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200810 if (no_update)
811 --no_win_do_lines_ins;
812
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200814 if (!did_intro)
815 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 did_intro = TRUE;
817
818#ifdef FEAT_GUI
819 /* Redraw the cursor and update the scrollbars when all screen updating is
820 * done. */
821 if (gui.in_use)
822 {
Bram Moolenaar107abd22016-08-12 14:08:25 +0200823 if (did_undraw && !gui_mch_is_blink_off())
Bram Moolenaar144445d2016-07-08 21:41:54 +0200824 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100825 mch_disable_flush();
826 out_flush(); /* required before updating the cursor */
827 mch_enable_flush();
828
Bram Moolenaar144445d2016-07-08 21:41:54 +0200829 /* Put the GUI position where the cursor was, gui_update_cursor()
830 * uses that. */
831 gui.col = gui_cursor_col;
832 gui.row = gui_cursor_row;
Bram Moolenaar84dbd492016-10-02 23:09:31 +0200833 gui.col = mb_fix_col(gui.col, gui.row);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara338adc2018-01-31 20:51:47 +0100835 gui_may_flush();
Bram Moolenaar65549bd2016-07-08 22:52:37 +0200836 screen_cur_col = gui.col;
837 screen_cur_row = gui.row;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200838 }
Bram Moolenaara338adc2018-01-31 20:51:47 +0100839 else
840 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 gui_update_scrollbars(FALSE);
842 }
843#endif
Bram Moolenaar072412e2017-09-13 22:11:35 +0200844 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845}
846
Bram Moolenaar1fa8fdd2019-02-25 05:41:15 +0100847#if defined(FEAT_NETBEANS_INTG) || defined(FEAT_GUI)
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100848/*
849 * Prepare for updating one or more windows.
850 * Caller must check for "updating_screen" already set to avoid recursiveness.
851 */
852 static void
853update_prepare(void)
854{
855 cursor_off();
856 updating_screen = TRUE;
857#ifdef FEAT_GUI
858 /* Remove the cursor before starting to do anything, because scrolling may
859 * make it difficult to redraw the text under it. */
860 if (gui.in_use)
861 gui_undraw_cursor();
862#endif
863#ifdef FEAT_SEARCH_EXTRA
864 start_search_hl();
865#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +0200866#ifdef FEAT_TEXT_PROP
867 // Update popup_mask if needed.
Bram Moolenaar68acb412019-06-26 03:40:36 +0200868 may_update_popup_mask(must_redraw);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200869#endif
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100870}
871
872/*
873 * Finish updating one or more windows.
874 */
875 static void
876update_finish(void)
877{
Bram Moolenaar4c25bd72019-04-20 23:38:07 +0200878 if (redraw_cmdline || redraw_mode)
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100879 showmode();
880
881# ifdef FEAT_SEARCH_EXTRA
882 end_search_hl();
883# endif
884
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200885 after_updating_screen(TRUE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100886
887# ifdef FEAT_GUI
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100888 /* Redraw the cursor and update the scrollbars when all screen updating is
889 * done. */
890 if (gui.in_use)
891 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100892 out_flush_cursor(FALSE, FALSE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100893 gui_update_scrollbars(FALSE);
894 }
895# endif
896}
897#endif
898
Bram Moolenaar860cae12010-06-05 23:22:07 +0200899#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200900/*
901 * Return TRUE if the cursor line in window "wp" may be concealed, according
902 * to the 'concealcursor' option.
903 */
904 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100905conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200906{
907 int c;
908
909 if (*wp->w_p_cocu == NUL)
910 return FALSE;
911 if (get_real_state() & VISUAL)
912 c = 'v';
913 else if (State & INSERT)
914 c = 'i';
915 else if (State & NORMAL)
916 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200917 else if (State & CMDLINE)
918 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200919 else
920 return FALSE;
921 return vim_strchr(wp->w_p_cocu, c) != NULL;
922}
923
924/*
925 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
926 */
927 void
Bram Moolenaarb9464822018-05-10 15:09:49 +0200928conceal_check_cursor_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200929{
930 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
931 {
932 need_cursor_line_redraw = TRUE;
933 /* Need to recompute cursor column, e.g., when starting Visual mode
934 * without concealing. */
935 curs_columns(TRUE);
936 }
937}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938#endif
939
Bram Moolenaar113e1072019-01-20 15:30:40 +0100940#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100942update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943{
944 win_T *wp;
945 int doit = FALSE;
946
947# ifdef FEAT_FOLDING
948 win_foldinfo.fi_level = 0;
949# endif
950
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100951 // update/delete a specific sign
952 redraw_buf_line_later(buf, lnum);
953
954 // check if it resulted in the need to redraw a window
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 if (wp->w_redr_type != 0)
957 doit = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100959 /* Return when there is nothing to do, screen updating is already
Bram Moolenaar07920482017-08-01 20:53:30 +0200960 * happening (recursive call), messages on the screen or still starting up.
961 */
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100962 if (!doit || updating_screen
Bram Moolenaar07920482017-08-01 20:53:30 +0200963 || State == ASKMORE || State == HITRETURN
964 || msg_scrolled
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100965#ifdef FEAT_GUI
966 || gui.starting
967#endif
968 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 return;
970
971 /* update all windows that need updating */
972 update_prepare();
973
Bram Moolenaar29323592016-07-24 22:04:11 +0200974 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 {
976 if (wp->w_redr_type != 0)
977 win_update(wp);
978 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200979 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981
982 update_finish();
983}
984#endif
985
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200986/*
987 * Get 'wincolor' attribute for window "wp". If not set and "wp" is a popup
988 * window then get the "Pmenu" highlight attribute.
989 */
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200990 int
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200991get_wcr_attr(win_T *wp)
992{
993 int wcr_attr = 0;
994
995 if (*wp->w_p_wcr != NUL)
996 wcr_attr = syn_name2attr(wp->w_p_wcr);
997#ifdef FEAT_TEXT_PROP
Bram Moolenaarc363fe12019-08-04 18:13:46 +0200998 else if (WIN_IS_POPUP(wp))
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200999 wcr_attr = HL_ATTR(HLF_PNI);
1000#endif
1001 return wcr_attr;
1002}
1003
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004#if defined(FEAT_GUI) || defined(PROTO)
1005/*
1006 * Update a single window, its status line and maybe the command line msg.
1007 * Used for the GUI scrollbar.
1008 */
1009 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001010updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011{
Bram Moolenaar19f990e2009-11-25 12:08:03 +00001012 /* return if already busy updating */
1013 if (updating_screen)
1014 return;
1015
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 update_prepare();
1017
1018#ifdef FEAT_CLIPBOARD
1019 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001020 if (clip_star.available && clip_isautosel_star())
1021 clip_update_selection(&clip_star);
1022 if (clip_plus.available && clip_isautosel_plus())
1023 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001025
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001027
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001028 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001029 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001030 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001031
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 if (wp->w_redr_status
1033# ifdef FEAT_CMDL_INFO
1034 || p_ru
1035# endif
1036# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001037 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038# endif
1039 )
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001040 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041
Bram Moolenaar988c4332019-06-02 14:12:11 +02001042#ifdef FEAT_TEXT_PROP
1043 // Display popup windows on top of everything.
Bram Moolenaara540f8a2019-06-14 19:23:57 +02001044 update_popups(win_update);
Bram Moolenaar988c4332019-06-02 14:12:11 +02001045#endif
1046
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 update_finish();
1048}
1049#endif
1050
1051/*
1052 * Update a single window.
1053 *
1054 * This may cause the windows below it also to be redrawn (when clearing the
1055 * screen or scrolling lines).
1056 *
1057 * How the window is redrawn depends on wp->w_redr_type. Each type also
1058 * implies the one below it.
1059 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001060 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1062 * INVERTED redraw the changed part of the Visual area
1063 * INVERTED_ALL redraw the whole Visual area
1064 * VALID 1. scroll up/down to adjust for a changed w_topline
1065 * 2. update lines at the top when scrolled down
1066 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001067 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 * b_mod_top and b_mod_bot.
1069 * - if wp->w_redraw_top non-zero, redraw lines between
1070 * wp->w_redraw_top and wp->w_redr_bot.
1071 * - continue redrawing when syntax status is invalid.
1072 * 4. if scrolled up, update lines at the bottom.
1073 * This results in three areas that may need updating:
1074 * top: from first row to top_end (when scrolled down)
1075 * mid: from mid_start to mid_end (update inversion or changed text)
1076 * bot: from bot_start to last row (when scrolled up)
1077 */
1078 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001079win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080{
1081 buf_T *buf = wp->w_buffer;
1082 int type;
1083 int top_end = 0; /* Below last row of the top area that needs
1084 updating. 0 when no top area updating. */
1085 int mid_start = 999;/* first row of the mid area that needs
1086 updating. 999 when no mid area updating. */
1087 int mid_end = 0; /* Below last row of the mid area that needs
1088 updating. 0 when no mid area updating. */
1089 int bot_start = 999;/* first row of the bot area that needs
1090 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 int scrolled_down = FALSE; /* TRUE when scrolled down when
1092 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarfbfb7572019-07-25 20:53:03 +02001094 int top_to_mod = FALSE; // redraw above mod_top
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095#endif
1096
1097 int row; /* current window row to display */
1098 linenr_T lnum; /* current buffer lnum to display */
1099 int idx; /* current index in w_lines[] */
1100 int srow; /* starting row of the current line */
1101
1102 int eof = FALSE; /* if TRUE, we hit the end of the file */
1103 int didline = FALSE; /* if TRUE, we finished the last line */
1104 int i;
1105 long j;
1106 static int recursive = FALSE; /* being called recursively */
1107 int old_botline = wp->w_botline;
1108#ifdef FEAT_FOLDING
1109 long fold_count;
1110#endif
1111#ifdef FEAT_SYN_HL
1112 /* remember what happened to the previous line, to know if
1113 * check_visual_highlight() can be used */
1114#define DID_NONE 1 /* didn't update a line */
1115#define DID_LINE 2 /* updated a normal line */
1116#define DID_FOLD 3 /* updated a folded line */
1117 int did_update = DID_NONE;
1118 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1119#endif
1120 linenr_T mod_top = 0;
1121 linenr_T mod_bot = 0;
1122#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1123 int save_got_int;
1124#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001125#ifdef SYN_TIME_LIMIT
1126 proftime_T syntax_tm;
1127#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128
1129 type = wp->w_redr_type;
1130
1131 if (type == NOT_VALID)
1132 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 wp->w_lines_valid = 0;
1135 }
1136
1137 /* Window is zero-height: nothing to draw. */
Bram Moolenaar415a6932017-12-05 20:31:07 +01001138 if (wp->w_height + WINBAR_HEIGHT(wp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 {
1140 wp->w_redr_type = 0;
1141 return;
1142 }
1143
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 /* Window is zero-width: Only need to draw the separator. */
1145 if (wp->w_width == 0)
1146 {
1147 /* draw the vertical separator right of this window */
1148 draw_vsep_win(wp, 0);
1149 wp->w_redr_type = 0;
1150 return;
1151 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001153#ifdef FEAT_TERMINAL
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001154 // If this window contains a terminal, redraw works completely differently.
1155 if (term_do_update_window(wp))
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001156 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001157 term_update_window(wp);
Bram Moolenaar181ca992018-02-13 21:19:21 +01001158# ifdef FEAT_MENU
1159 /* Draw the window toolbar, if there is one. */
1160 if (winbar_height(wp) > 0)
1161 redraw_win_toolbar(wp);
1162# endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001163 wp->w_redr_type = 0;
1164 return;
1165 }
1166#endif
1167
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarbbca7732019-07-24 18:13:16 +02001169 init_search_hl(wp, &search_hl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170#endif
1171
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001172#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001173 /* Force redraw when width of 'number' or 'relativenumber' column
1174 * changes. */
1175 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001176 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001177 {
1178 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001179 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001180 }
1181 else
1182#endif
1183
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1185 {
1186 /*
1187 * When there are both inserted/deleted lines and specific lines to be
1188 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1189 * everything (only happens when redrawing is off for while).
1190 */
1191 type = NOT_VALID;
1192 }
1193 else
1194 {
1195 /*
1196 * Set mod_top to the first line that needs displaying because of
1197 * changes. Set mod_bot to the first line after the changes.
1198 */
1199 mod_top = wp->w_redraw_top;
1200 if (wp->w_redraw_bot != 0)
1201 mod_bot = wp->w_redraw_bot + 1;
1202 else
1203 mod_bot = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 if (buf->b_mod_set)
1205 {
1206 if (mod_top == 0 || mod_top > buf->b_mod_top)
1207 {
1208 mod_top = buf->b_mod_top;
1209#ifdef FEAT_SYN_HL
1210 /* Need to redraw lines above the change that may be included
1211 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001212 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001214 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 if (mod_top < 1)
1216 mod_top = 1;
1217 }
1218#endif
1219 }
1220 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1221 mod_bot = buf->b_mod_bot;
1222
1223#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarfbfb7572019-07-25 20:53:03 +02001224 // When 'hlsearch' is on and using a multi-line search pattern, a
1225 // change in one line may make the Search highlighting in a
1226 // previous line invalid. Simple solution: redraw all visible
1227 // lines above the change.
1228 // Same for a match pattern.
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001229 if (search_hl.rm.regprog != NULL
1230 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001232 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001233 {
Bram Moolenaarfbfb7572019-07-25 20:53:03 +02001234 matchitem_T *cur = wp->w_match_head;
1235
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001236 while (cur != NULL)
1237 {
1238 if (cur->match.regprog != NULL
1239 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001240 {
1241 top_to_mod = TRUE;
1242 break;
1243 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001244 cur = cur->next;
1245 }
1246 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247#endif
1248 }
1249#ifdef FEAT_FOLDING
1250 if (mod_top != 0 && hasAnyFolding(wp))
1251 {
1252 linenr_T lnumt, lnumb;
1253
1254 /*
1255 * A change in a line can cause lines above it to become folded or
1256 * unfolded. Find the top most buffer line that may be affected.
1257 * If the line was previously folded and displayed, get the first
1258 * line of that fold. If the line is folded now, get the first
1259 * folded line. Use the minimum of these two.
1260 */
1261
1262 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1263 * the line below it. If there is no valid entry, use w_topline.
1264 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1265 * to this line. If there is no valid entry, use MAXLNUM. */
1266 lnumt = wp->w_topline;
1267 lnumb = MAXLNUM;
1268 for (i = 0; i < wp->w_lines_valid; ++i)
1269 if (wp->w_lines[i].wl_valid)
1270 {
1271 if (wp->w_lines[i].wl_lastlnum < mod_top)
1272 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1273 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1274 {
1275 lnumb = wp->w_lines[i].wl_lnum;
1276 /* When there is a fold column it might need updating
1277 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001278 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 ++lnumb;
1280 }
1281 }
1282
1283 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1284 if (mod_top > lnumt)
1285 mod_top = lnumt;
1286
1287 /* Now do the same for the bottom line (one above mod_bot). */
1288 --mod_bot;
1289 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1290 ++mod_bot;
1291 if (mod_bot < lnumb)
1292 mod_bot = lnumb;
1293 }
1294#endif
1295
1296 /* When a change starts above w_topline and the end is below
1297 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001298 * If the end of the change is above w_topline: do like no change was
1299 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 if (mod_top != 0 && mod_top < wp->w_topline)
1301 {
1302 if (mod_bot > wp->w_topline)
1303 mod_top = wp->w_topline;
1304#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001305 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 top_end = 1;
1307#endif
1308 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001309
1310 /* When line numbers are displayed need to redraw all lines below
1311 * inserted/deleted lines. */
1312 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1313 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 }
Bram Moolenaar895d9662019-01-31 21:57:21 +01001315 wp->w_redraw_top = 0; // reset for next time
1316 wp->w_redraw_bot = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317
1318 /*
1319 * When only displaying the lines at the top, set top_end. Used when
1320 * window has scrolled down for msg_scrolled.
1321 */
1322 if (type == REDRAW_TOP)
1323 {
1324 j = 0;
1325 for (i = 0; i < wp->w_lines_valid; ++i)
1326 {
1327 j += wp->w_lines[i].wl_size;
1328 if (j >= wp->w_upd_rows)
1329 {
1330 top_end = j;
1331 break;
1332 }
1333 }
1334 if (top_end == 0)
1335 /* not found (cannot happen?): redraw everything */
1336 type = NOT_VALID;
1337 else
1338 /* top area defined, the rest is VALID */
1339 type = VALID;
1340 }
1341
Bram Moolenaar367329b2007-08-30 11:53:22 +00001342 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001343 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1344 * non-zero and thus not FALSE) will indicate that screenclear() was not
1345 * called. */
1346 if (screen_cleared)
1347 screen_cleared = MAYBE;
1348
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 /*
1350 * If there are no changes on the screen that require a complete redraw,
1351 * handle three cases:
1352 * 1: we are off the top of the screen by a few lines: scroll down
1353 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1354 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1355 * w_lines[] that needs updating.
1356 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001357 if ((type == VALID || type == SOME_VALID
1358 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359#ifdef FEAT_DIFF
1360 && !wp->w_botfill && !wp->w_old_botfill
1361#endif
1362 )
1363 {
1364 if (mod_top != 0 && wp->w_topline == mod_top)
1365 {
1366 /*
1367 * w_topline is the first changed line, the scrolling will be done
1368 * further down.
1369 */
1370 }
1371 else if (wp->w_lines[0].wl_valid
1372 && (wp->w_topline < wp->w_lines[0].wl_lnum
1373#ifdef FEAT_DIFF
1374 || (wp->w_topline == wp->w_lines[0].wl_lnum
1375 && wp->w_topfill > wp->w_old_topfill)
1376#endif
1377 ))
1378 {
1379 /*
1380 * New topline is above old topline: May scroll down.
1381 */
1382#ifdef FEAT_FOLDING
1383 if (hasAnyFolding(wp))
1384 {
1385 linenr_T ln;
1386
1387 /* count the number of lines we are off, counting a sequence
1388 * of folded lines as one */
1389 j = 0;
1390 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1391 {
1392 ++j;
1393 if (j >= wp->w_height - 2)
1394 break;
1395 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1396 }
1397 }
1398 else
1399#endif
1400 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1401 if (j < wp->w_height - 2) /* not too far off */
1402 {
1403 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1404#ifdef FEAT_DIFF
1405 /* insert extra lines for previously invisible filler lines */
1406 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1407 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1408 - wp->w_old_topfill;
1409#endif
1410 if (i < wp->w_height - 2) /* less than a screen off */
1411 {
1412 /*
1413 * Try to insert the correct number of lines.
1414 * If not the last window, delete the lines at the bottom.
1415 * win_ins_lines may fail when the terminal can't do it.
1416 */
1417 if (i > 0)
1418 check_for_delay(FALSE);
1419 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1420 {
1421 if (wp->w_lines_valid != 0)
1422 {
1423 /* Need to update rows that are new, stop at the
1424 * first one that scrolled down. */
1425 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427
1428 /* Move the entries that were scrolled, disable
1429 * the entries for the lines to be redrawn. */
1430 if ((wp->w_lines_valid += j) > wp->w_height)
1431 wp->w_lines_valid = wp->w_height;
1432 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1433 wp->w_lines[idx] = wp->w_lines[idx - j];
1434 while (idx >= 0)
1435 wp->w_lines[idx--].wl_valid = FALSE;
1436 }
1437 }
1438 else
1439 mid_start = 0; /* redraw all lines */
1440 }
1441 else
1442 mid_start = 0; /* redraw all lines */
1443 }
1444 else
1445 mid_start = 0; /* redraw all lines */
1446 }
1447 else
1448 {
1449 /*
1450 * New topline is at or below old topline: May scroll up.
1451 * When topline didn't change, find first entry in w_lines[] that
1452 * needs updating.
1453 */
1454
1455 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1456 j = -1;
1457 row = 0;
1458 for (i = 0; i < wp->w_lines_valid; i++)
1459 {
1460 if (wp->w_lines[i].wl_valid
1461 && wp->w_lines[i].wl_lnum == wp->w_topline)
1462 {
1463 j = i;
1464 break;
1465 }
1466 row += wp->w_lines[i].wl_size;
1467 }
1468 if (j == -1)
1469 {
1470 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1471 * lines */
1472 mid_start = 0;
1473 }
1474 else
1475 {
1476 /*
1477 * Try to delete the correct number of lines.
1478 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1479 */
1480#ifdef FEAT_DIFF
1481 /* If the topline didn't change, delete old filler lines,
1482 * otherwise delete filler lines of the new topline... */
1483 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1484 row += wp->w_old_topfill;
1485 else
1486 row += diff_check_fill(wp, wp->w_topline);
1487 /* ... but don't delete new filler lines. */
1488 row -= wp->w_topfill;
1489#endif
1490 if (row > 0)
1491 {
1492 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001493 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1494 == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 bot_start = wp->w_height - row;
1496 else
1497 mid_start = 0; /* redraw all lines */
1498 }
1499 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1500 {
1501 /*
1502 * Skip the lines (below the deleted lines) that are still
1503 * valid and don't need redrawing. Copy their info
1504 * upwards, to compensate for the deleted lines. Set
1505 * bot_start to the first row that needs redrawing.
1506 */
1507 bot_start = 0;
1508 idx = 0;
1509 for (;;)
1510 {
1511 wp->w_lines[idx] = wp->w_lines[j];
1512 /* stop at line that didn't fit, unless it is still
1513 * valid (no lines deleted) */
1514 if (row > 0 && bot_start + row
1515 + (int)wp->w_lines[j].wl_size > wp->w_height)
1516 {
1517 wp->w_lines_valid = idx + 1;
1518 break;
1519 }
1520 bot_start += wp->w_lines[idx++].wl_size;
1521
1522 /* stop at the last valid entry in w_lines[].wl_size */
1523 if (++j >= wp->w_lines_valid)
1524 {
1525 wp->w_lines_valid = idx;
1526 break;
1527 }
1528 }
1529#ifdef FEAT_DIFF
1530 /* Correct the first entry for filler lines at the top
1531 * when it won't get updated below. */
1532 if (wp->w_p_diff && bot_start > 0)
1533 wp->w_lines[0].wl_size =
1534 plines_win_nofill(wp, wp->w_topline, TRUE)
1535 + wp->w_topfill;
1536#endif
1537 }
1538 }
1539 }
1540
1541 /* When starting redraw in the first line, redraw all lines. When
1542 * there is only one window it's probably faster to clear the screen
1543 * first. */
1544 if (mid_start == 0)
1545 {
1546 mid_end = wp->w_height;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001547 if (ONE_WINDOW && !WIN_IS_POPUP(wp))
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001548 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001549 /* Clear the screen when it was not done by win_del_lines() or
1550 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1551 * then. */
1552 if (screen_cleared != TRUE)
1553 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001554 /* The screen was cleared, redraw the tab pages line. */
1555 if (redraw_tabline)
1556 draw_tabline();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001557 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001559
1560 /* When win_del_lines() or win_ins_lines() caused the screen to be
1561 * cleared (only happens for the first window) or when screenclear()
1562 * was called directly above, "must_redraw" will have been set to
1563 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1564 if (screen_cleared == TRUE)
1565 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 }
1567 else
1568 {
1569 /* Not VALID or INVERTED: redraw all lines. */
1570 mid_start = 0;
1571 mid_end = wp->w_height;
1572 }
1573
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001574 if (type == SOME_VALID)
1575 {
1576 /* SOME_VALID: redraw all lines. */
1577 mid_start = 0;
1578 mid_end = wp->w_height;
1579 type = NOT_VALID;
1580 }
1581
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 /* check if we are updating or removing the inverted part */
1583 if ((VIsual_active && buf == curwin->w_buffer)
1584 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1585 {
1586 linenr_T from, to;
1587
1588 if (VIsual_active)
1589 {
1590 if (VIsual_active
1591 && (VIsual_mode != wp->w_old_visual_mode
1592 || type == INVERTED_ALL))
1593 {
1594 /*
1595 * If the type of Visual selection changed, redraw the whole
1596 * selection. Also when the ownership of the X selection is
1597 * gained or lost.
1598 */
1599 if (curwin->w_cursor.lnum < VIsual.lnum)
1600 {
1601 from = curwin->w_cursor.lnum;
1602 to = VIsual.lnum;
1603 }
1604 else
1605 {
1606 from = VIsual.lnum;
1607 to = curwin->w_cursor.lnum;
1608 }
1609 /* redraw more when the cursor moved as well */
1610 if (wp->w_old_cursor_lnum < from)
1611 from = wp->w_old_cursor_lnum;
1612 if (wp->w_old_cursor_lnum > to)
1613 to = wp->w_old_cursor_lnum;
1614 if (wp->w_old_visual_lnum < from)
1615 from = wp->w_old_visual_lnum;
1616 if (wp->w_old_visual_lnum > to)
1617 to = wp->w_old_visual_lnum;
1618 }
1619 else
1620 {
1621 /*
1622 * Find the line numbers that need to be updated: The lines
1623 * between the old cursor position and the current cursor
1624 * position. Also check if the Visual position changed.
1625 */
1626 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1627 {
1628 from = curwin->w_cursor.lnum;
1629 to = wp->w_old_cursor_lnum;
1630 }
1631 else
1632 {
1633 from = wp->w_old_cursor_lnum;
1634 to = curwin->w_cursor.lnum;
1635 if (from == 0) /* Visual mode just started */
1636 from = to;
1637 }
1638
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001639 if (VIsual.lnum != wp->w_old_visual_lnum
1640 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 {
1642 if (wp->w_old_visual_lnum < from
1643 && wp->w_old_visual_lnum != 0)
1644 from = wp->w_old_visual_lnum;
1645 if (wp->w_old_visual_lnum > to)
1646 to = wp->w_old_visual_lnum;
1647 if (VIsual.lnum < from)
1648 from = VIsual.lnum;
1649 if (VIsual.lnum > to)
1650 to = VIsual.lnum;
1651 }
1652 }
1653
1654 /*
1655 * If in block mode and changed column or curwin->w_curswant:
1656 * update all lines.
1657 * First compute the actual start and end column.
1658 */
1659 if (VIsual_mode == Ctrl_V)
1660 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001661 colnr_T fromc, toc;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001662#if defined(FEAT_LINEBREAK)
Bram Moolenaar404406a2014-10-09 13:24:43 +02001663 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664
Bram Moolenaar404406a2014-10-09 13:24:43 +02001665 if (curwin->w_p_lbr)
1666 ve_flags = VE_ALL;
1667#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001669#if defined(FEAT_LINEBREAK)
Bram Moolenaar404406a2014-10-09 13:24:43 +02001670 ve_flags = save_ve_flags;
1671#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 ++toc;
1673 if (curwin->w_curswant == MAXCOL)
1674 toc = MAXCOL;
1675
1676 if (fromc != wp->w_old_cursor_fcol
1677 || toc != wp->w_old_cursor_lcol)
1678 {
1679 if (from > VIsual.lnum)
1680 from = VIsual.lnum;
1681 if (to < VIsual.lnum)
1682 to = VIsual.lnum;
1683 }
1684 wp->w_old_cursor_fcol = fromc;
1685 wp->w_old_cursor_lcol = toc;
1686 }
1687 }
1688 else
1689 {
1690 /* Use the line numbers of the old Visual area. */
1691 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1692 {
1693 from = wp->w_old_cursor_lnum;
1694 to = wp->w_old_visual_lnum;
1695 }
1696 else
1697 {
1698 from = wp->w_old_visual_lnum;
1699 to = wp->w_old_cursor_lnum;
1700 }
1701 }
1702
1703 /*
1704 * There is no need to update lines above the top of the window.
1705 */
1706 if (from < wp->w_topline)
1707 from = wp->w_topline;
1708
1709 /*
1710 * If we know the value of w_botline, use it to restrict the update to
1711 * the lines that are visible in the window.
1712 */
1713 if (wp->w_valid & VALID_BOTLINE)
1714 {
1715 if (from >= wp->w_botline)
1716 from = wp->w_botline - 1;
1717 if (to >= wp->w_botline)
1718 to = wp->w_botline - 1;
1719 }
1720
1721 /*
1722 * Find the minimal part to be updated.
1723 * Watch out for scrolling that made entries in w_lines[] invalid.
1724 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1725 * top_end; need to redraw from top_end to the "to" line.
1726 * A middle mouse click with a Visual selection may change the text
1727 * above the Visual area and reset wl_valid, do count these for
1728 * mid_end (in srow).
1729 */
1730 if (mid_start > 0)
1731 {
1732 lnum = wp->w_topline;
1733 idx = 0;
1734 srow = 0;
1735 if (scrolled_down)
1736 mid_start = top_end;
1737 else
1738 mid_start = 0;
1739 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1740 {
1741 if (wp->w_lines[idx].wl_valid)
1742 mid_start += wp->w_lines[idx].wl_size;
1743 else if (!scrolled_down)
1744 srow += wp->w_lines[idx].wl_size;
1745 ++idx;
1746# ifdef FEAT_FOLDING
1747 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1748 lnum = wp->w_lines[idx].wl_lnum;
1749 else
1750# endif
1751 ++lnum;
1752 }
1753 srow += mid_start;
1754 mid_end = wp->w_height;
1755 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1756 {
1757 if (wp->w_lines[idx].wl_valid
1758 && wp->w_lines[idx].wl_lnum >= to + 1)
1759 {
1760 /* Only update until first row of this line */
1761 mid_end = srow;
1762 break;
1763 }
1764 srow += wp->w_lines[idx].wl_size;
1765 }
1766 }
1767 }
1768
1769 if (VIsual_active && buf == curwin->w_buffer)
1770 {
1771 wp->w_old_visual_mode = VIsual_mode;
1772 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1773 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001774 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 wp->w_old_curswant = curwin->w_curswant;
1776 }
1777 else
1778 {
1779 wp->w_old_visual_mode = 0;
1780 wp->w_old_cursor_lnum = 0;
1781 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001782 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784
1785#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1786 /* reset got_int, otherwise regexp won't work */
1787 save_got_int = got_int;
1788 got_int = 0;
1789#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001790#ifdef SYN_TIME_LIMIT
1791 /* Set the time limit to 'redrawtime'. */
1792 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02001793 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001794#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795#ifdef FEAT_FOLDING
1796 win_foldinfo.fi_level = 0;
1797#endif
1798
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001799#ifdef FEAT_MENU
1800 /*
1801 * Draw the window toolbar, if there is one.
1802 * TODO: only when needed.
1803 */
1804 if (winbar_height(wp) > 0)
1805 redraw_win_toolbar(wp);
1806#endif
1807
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 /*
1809 * Update all the window rows.
1810 */
1811 idx = 0; /* first entry in w_lines[].wl_size */
1812 row = 0;
1813 srow = 0;
1814 lnum = wp->w_topline; /* first line shown in window */
1815 for (;;)
1816 {
1817 /* stop updating when reached the end of the window (check for _past_
1818 * the end of the window is at the end of the loop) */
1819 if (row == wp->w_height)
1820 {
1821 didline = TRUE;
1822 break;
1823 }
1824
1825 /* stop updating when hit the end of the file */
1826 if (lnum > buf->b_ml.ml_line_count)
1827 {
1828 eof = TRUE;
1829 break;
1830 }
1831
1832 /* Remember the starting row of the line that is going to be dealt
1833 * with. It is used further down when the line doesn't fit. */
1834 srow = row;
1835
1836 /*
1837 * Update a line when it is in an area that needs updating, when it
1838 * has changes or w_lines[idx] is invalid.
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02001839 * "bot_start" may be halfway a wrapped line after using
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 * win_del_lines(), check if the current line includes it.
1841 * When syntax folding is being used, the saved syntax states will
1842 * already have been updated, we can't see where the syntax state is
1843 * the same again, just update until the end of the window.
1844 */
1845 if (row < top_end
1846 || (row >= mid_start && row < mid_end)
1847#ifdef FEAT_SEARCH_EXTRA
1848 || top_to_mod
1849#endif
1850 || idx >= wp->w_lines_valid
1851 || (row + wp->w_lines[idx].wl_size > bot_start)
1852 || (mod_top != 0
1853 && (lnum == mod_top
1854 || (lnum >= mod_top
1855 && (lnum < mod_bot
1856#ifdef FEAT_SYN_HL
1857 || did_update == DID_FOLD
1858 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001859 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 && (
1861# ifdef FEAT_FOLDING
1862 (foldmethodIsSyntax(wp)
1863 && hasAnyFolding(wp)) ||
1864# endif
1865 syntax_check_changed(lnum)))
1866#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001867#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001868 /* match in fixed position might need redraw
1869 * if lines were inserted or deleted */
1870 || (wp->w_match_head != NULL
1871 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001872#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 )))))
1874 {
1875#ifdef FEAT_SEARCH_EXTRA
1876 if (lnum == mod_top)
1877 top_to_mod = FALSE;
1878#endif
1879
1880 /*
1881 * When at start of changed lines: May scroll following lines
1882 * up or down to minimize redrawing.
1883 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001884 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 */
1886 if (lnum == mod_top
1887 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001888 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 {
1890 int old_rows = 0;
1891 int new_rows = 0;
1892 int xtra_rows;
1893 linenr_T l;
1894
1895 /* Count the old number of window rows, using w_lines[], which
1896 * should still contain the sizes for the lines as they are
1897 * currently displayed. */
1898 for (i = idx; i < wp->w_lines_valid; ++i)
1899 {
1900 /* Only valid lines have a meaningful wl_lnum. Invalid
1901 * lines are part of the changed area. */
1902 if (wp->w_lines[i].wl_valid
1903 && wp->w_lines[i].wl_lnum == mod_bot)
1904 break;
1905 old_rows += wp->w_lines[i].wl_size;
1906#ifdef FEAT_FOLDING
1907 if (wp->w_lines[i].wl_valid
1908 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1909 {
1910 /* Must have found the last valid entry above mod_bot.
1911 * Add following invalid entries. */
1912 ++i;
1913 while (i < wp->w_lines_valid
1914 && !wp->w_lines[i].wl_valid)
1915 old_rows += wp->w_lines[i++].wl_size;
1916 break;
1917 }
1918#endif
1919 }
1920
1921 if (i >= wp->w_lines_valid)
1922 {
1923 /* We can't find a valid line below the changed lines,
1924 * need to redraw until the end of the window.
1925 * Inserting/deleting lines has no use. */
1926 bot_start = 0;
1927 }
1928 else
1929 {
1930 /* Able to count old number of rows: Count new window
1931 * rows, and may insert/delete lines */
1932 j = idx;
1933 for (l = lnum; l < mod_bot; ++l)
1934 {
1935#ifdef FEAT_FOLDING
1936 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1937 ++new_rows;
1938 else
1939#endif
1940#ifdef FEAT_DIFF
1941 if (l == wp->w_topline)
1942 new_rows += plines_win_nofill(wp, l, TRUE)
1943 + wp->w_topfill;
1944 else
1945#endif
1946 new_rows += plines_win(wp, l, TRUE);
1947 ++j;
1948 if (new_rows > wp->w_height - row - 2)
1949 {
1950 /* it's getting too much, must redraw the rest */
1951 new_rows = 9999;
1952 break;
1953 }
1954 }
1955 xtra_rows = new_rows - old_rows;
1956 if (xtra_rows < 0)
1957 {
1958 /* May scroll text up. If there is not enough
1959 * remaining text or scrolling fails, must redraw the
1960 * rest. If scrolling works, must redraw the text
1961 * below the scrolled text. */
1962 if (row - xtra_rows >= wp->w_height - 2)
1963 mod_bot = MAXLNUM;
1964 else
1965 {
1966 check_for_delay(FALSE);
1967 if (win_del_lines(wp, row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001968 -xtra_rows, FALSE, FALSE, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 mod_bot = MAXLNUM;
1970 else
1971 bot_start = wp->w_height + xtra_rows;
1972 }
1973 }
1974 else if (xtra_rows > 0)
1975 {
1976 /* May scroll text down. If there is not enough
1977 * remaining text of scrolling fails, must redraw the
1978 * rest. */
1979 if (row + xtra_rows >= wp->w_height - 2)
1980 mod_bot = MAXLNUM;
1981 else
1982 {
1983 check_for_delay(FALSE);
1984 if (win_ins_lines(wp, row + old_rows,
1985 xtra_rows, FALSE, FALSE) == FAIL)
1986 mod_bot = MAXLNUM;
1987 else if (top_end > row + old_rows)
1988 /* Scrolled the part at the top that requires
1989 * updating down. */
1990 top_end += xtra_rows;
1991 }
1992 }
1993
1994 /* When not updating the rest, may need to move w_lines[]
1995 * entries. */
1996 if (mod_bot != MAXLNUM && i != j)
1997 {
1998 if (j < i)
1999 {
2000 int x = row + new_rows;
2001
2002 /* move entries in w_lines[] upwards */
2003 for (;;)
2004 {
2005 /* stop at last valid entry in w_lines[] */
2006 if (i >= wp->w_lines_valid)
2007 {
2008 wp->w_lines_valid = j;
2009 break;
2010 }
2011 wp->w_lines[j] = wp->w_lines[i];
2012 /* stop at a line that won't fit */
2013 if (x + (int)wp->w_lines[j].wl_size
2014 > wp->w_height)
2015 {
2016 wp->w_lines_valid = j + 1;
2017 break;
2018 }
2019 x += wp->w_lines[j++].wl_size;
2020 ++i;
2021 }
2022 if (bot_start > x)
2023 bot_start = x;
2024 }
2025 else /* j > i */
2026 {
2027 /* move entries in w_lines[] downwards */
2028 j -= i;
2029 wp->w_lines_valid += j;
2030 if (wp->w_lines_valid > wp->w_height)
2031 wp->w_lines_valid = wp->w_height;
2032 for (i = wp->w_lines_valid; i - j >= idx; --i)
2033 wp->w_lines[i] = wp->w_lines[i - j];
2034
2035 /* The w_lines[] entries for inserted lines are
2036 * now invalid, but wl_size may be used above.
2037 * Reset to zero. */
2038 while (i >= idx)
2039 {
2040 wp->w_lines[i].wl_size = 0;
2041 wp->w_lines[i--].wl_valid = FALSE;
2042 }
2043 }
2044 }
2045 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 }
2047
2048#ifdef FEAT_FOLDING
2049 /*
2050 * When lines are folded, display one line for all of them.
2051 * Otherwise, display normally (can be several display lines when
2052 * 'wrap' is on).
2053 */
2054 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2055 if (fold_count != 0)
2056 {
2057 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2058 ++row;
2059 --fold_count;
2060 wp->w_lines[idx].wl_folded = TRUE;
2061 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2062# ifdef FEAT_SYN_HL
2063 did_update = DID_FOLD;
2064# endif
2065 }
2066 else
2067#endif
2068 if (idx < wp->w_lines_valid
2069 && wp->w_lines[idx].wl_valid
2070 && wp->w_lines[idx].wl_lnum == lnum
2071 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002072 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002073 && !WIN_IS_POPUP(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 && srow + wp->w_lines[idx].wl_size > wp->w_height
2075#ifdef FEAT_DIFF
2076 && diff_check_fill(wp, lnum) == 0
2077#endif
2078 )
2079 {
2080 /* This line is not going to fit. Don't draw anything here,
2081 * will draw "@ " lines below. */
2082 row = wp->w_height + 1;
2083 }
2084 else
2085 {
2086#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarbbca7732019-07-24 18:13:16 +02002087 prepare_search_hl(wp, &search_hl, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088#endif
2089#ifdef FEAT_SYN_HL
2090 /* Let the syntax stuff know we skipped a few lines. */
2091 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002092 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 syntax_end_parsing(syntax_last_parsed + 1);
2094#endif
2095
2096 /*
2097 * Display one line.
2098 */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002099 row = win_line(wp, lnum, srow, wp->w_height,
2100 mod_top == 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101
2102#ifdef FEAT_FOLDING
2103 wp->w_lines[idx].wl_folded = FALSE;
2104 wp->w_lines[idx].wl_lastlnum = lnum;
2105#endif
2106#ifdef FEAT_SYN_HL
2107 did_update = DID_LINE;
2108 syntax_last_parsed = lnum;
2109#endif
2110 }
2111
2112 wp->w_lines[idx].wl_lnum = lnum;
2113 wp->w_lines[idx].wl_valid = TRUE;
Bram Moolenaar0e19fc02017-10-28 14:45:16 +02002114
2115 /* Past end of the window or end of the screen. Note that after
2116 * resizing wp->w_height may be end up too big. That's a problem
2117 * elsewhere, but prevent a crash here. */
2118 if (row > wp->w_height || row + wp->w_winrow >= Rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 {
2120 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002121 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2123 ++idx;
2124 break;
2125 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002126 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 wp->w_lines[idx].wl_size = row - srow;
2128 ++idx;
2129#ifdef FEAT_FOLDING
2130 lnum += fold_count + 1;
2131#else
2132 ++lnum;
2133#endif
2134 }
2135 else
2136 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002137 if (wp->w_p_rnu)
2138 {
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002139#ifdef FEAT_FOLDING
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002140 // 'relativenumber' set: The text doesn't need to be drawn, but
2141 // the number column nearly always does.
Bram Moolenaar7701f302018-10-02 21:20:32 +02002142 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2143 if (fold_count != 0)
Bram Moolenaar7701f302018-10-02 21:20:32 +02002144 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
Bram Moolenaar7701f302018-10-02 21:20:32 +02002145 else
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002146#endif
Bram Moolenaar7701f302018-10-02 21:20:32 +02002147 (void)win_line(wp, lnum, srow, wp->w_height, TRUE, TRUE);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002148 }
2149
2150 // This line does not need to be drawn, advance to the next one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 row += wp->w_lines[idx++].wl_size;
2152 if (row > wp->w_height) /* past end of screen */
2153 break;
2154#ifdef FEAT_FOLDING
2155 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2156#else
2157 ++lnum;
2158#endif
2159#ifdef FEAT_SYN_HL
2160 did_update = DID_NONE;
2161#endif
2162 }
2163
2164 if (lnum > buf->b_ml.ml_line_count)
2165 {
2166 eof = TRUE;
2167 break;
2168 }
2169 }
2170 /*
2171 * End of loop over all window lines.
2172 */
2173
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002174#ifdef FEAT_VTP
2175 /* Rewrite the character at the end of the screen line. */
2176 if (use_vtp())
2177 {
2178 int i;
2179
2180 for (i = 0; i < Rows; ++i)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002181 if (enc_utf8)
2182 if ((*mb_off2cells)(LineOffset[i] + Columns - 2,
2183 LineOffset[i] + screen_Columns) > 1)
2184 screen_draw_rectangle(i, Columns - 2, 1, 2, FALSE);
2185 else
2186 screen_draw_rectangle(i, Columns - 1, 1, 1, FALSE);
2187 else
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002188 screen_char(LineOffset[i] + Columns - 1, i, Columns - 1);
2189 }
2190#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191
2192 if (idx > wp->w_lines_valid)
2193 wp->w_lines_valid = idx;
2194
2195#ifdef FEAT_SYN_HL
2196 /*
2197 * Let the syntax stuff know we stop parsing here.
2198 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002199 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 syntax_end_parsing(syntax_last_parsed + 1);
2201#endif
2202
2203 /*
2204 * If we didn't hit the end of the file, and we didn't finish the last
2205 * line we were working on, then the line didn't fit.
2206 */
2207 wp->w_empty_rows = 0;
2208#ifdef FEAT_DIFF
2209 wp->w_filler_rows = 0;
2210#endif
2211 if (!eof && !didline)
2212 {
2213 if (lnum == wp->w_topline)
2214 {
2215 /*
2216 * Single line that does not fit!
2217 * Don't overwrite it, it can be edited.
2218 */
2219 wp->w_botline = lnum + 1;
2220 }
2221#ifdef FEAT_DIFF
2222 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2223 {
2224 /* Window ends in filler lines. */
2225 wp->w_botline = lnum;
2226 wp->w_filler_rows = wp->w_height - srow;
2227 }
2228#endif
Bram Moolenaar17146962019-05-30 00:12:11 +02002229#ifdef FEAT_TEXT_PROP
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002230 else if (WIN_IS_POPUP(wp))
Bram Moolenaar17146962019-05-30 00:12:11 +02002231 {
2232 // popup line that doesn't fit is left as-is
2233 wp->w_botline = lnum;
2234 }
2235#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002236 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2237 {
2238 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2239
2240 /*
2241 * Last line isn't finished: Display "@@@" in the last screen line.
2242 */
Bram Moolenaar53f81742017-09-22 14:35:51 +02002243 screen_puts_len((char_u *)"@@", 2, scr_row, wp->w_wincol,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002244 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002245 screen_fill(scr_row, scr_row + 1,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002246 (int)wp->w_wincol + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002247 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002248 set_empty_rows(wp, srow);
2249 wp->w_botline = lnum;
2250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2252 {
2253 /*
2254 * Last line isn't finished: Display "@@@" at the end.
2255 */
2256 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2257 W_WINROW(wp) + wp->w_height,
2258 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002259 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 set_empty_rows(wp, srow);
2261 wp->w_botline = lnum;
2262 }
2263 else
2264 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002265 win_draw_end(wp, '@', ' ', TRUE, srow, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 wp->w_botline = lnum;
2267 }
2268 }
2269 else
2270 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 if (eof) /* we hit the end of the file */
2273 {
2274 wp->w_botline = buf->b_ml.ml_line_count + 1;
2275#ifdef FEAT_DIFF
2276 j = diff_check_fill(wp, wp->w_botline);
2277 if (j > 0 && !wp->w_botfill)
2278 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002279 // Display filler lines at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 if (char2cells(fill_diff) > 1)
2281 i = '-';
2282 else
2283 i = fill_diff;
2284 if (row + j > wp->w_height)
2285 j = wp->w_height - row;
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002286 win_draw_end(wp, i, i, TRUE, row, row + (int)j, HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 row += j;
2288 }
2289#endif
2290 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002291 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 wp->w_botline = lnum;
2293
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002294 // Make sure the rest of the screen is blank
2295 // put '~'s on rows that aren't part of the file.
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002296 win_draw_end(wp, WIN_IS_POPUP(wp) ? ' ' : '~',
2297 ' ', FALSE, row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 }
2299
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002300#ifdef SYN_TIME_LIMIT
2301 syn_set_timeout(NULL);
2302#endif
2303
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 /* Reset the type of redrawing required, the window has been updated. */
2305 wp->w_redr_type = 0;
2306#ifdef FEAT_DIFF
2307 wp->w_old_topfill = wp->w_topfill;
2308 wp->w_old_botfill = wp->w_botfill;
2309#endif
2310
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002311 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 {
2313 /*
2314 * There is a trick with w_botline. If we invalidate it on each
2315 * change that might modify it, this will cause a lot of expensive
2316 * calls to plines() in update_topline() each time. Therefore the
2317 * value of w_botline is often approximated, and this value is used to
2318 * compute the value of w_topline. If the value of w_botline was
2319 * wrong, check that the value of w_topline is correct (cursor is on
2320 * the visible part of the text). If it's not, we need to redraw
2321 * again. Mostly this just means scrolling up a few lines, so it
2322 * doesn't look too bad. Only do this for the current window (where
2323 * changes are relevant).
2324 */
2325 wp->w_valid |= VALID_BOTLINE;
2326 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2327 {
2328 recursive = TRUE;
2329 curwin->w_valid &= ~VALID_TOPLINE;
2330 update_topline(); /* may invalidate w_botline again */
2331 if (must_redraw != 0)
2332 {
2333 /* Don't update for changes in buffer again. */
2334 i = curbuf->b_mod_set;
2335 curbuf->b_mod_set = FALSE;
2336 win_update(curwin);
2337 must_redraw = 0;
2338 curbuf->b_mod_set = i;
2339 }
2340 recursive = FALSE;
2341 }
2342 }
2343
2344#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2345 /* restore got_int, unless CTRL-C was hit while redrawing */
2346 if (!got_int)
2347 got_int = save_got_int;
2348#endif
2349}
2350
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351/*
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002352 * Call screen_fill() with the columns adjusted for 'rightleft' if needed.
2353 * Return the new offset.
2354 */
2355 static int
2356screen_fill_end(
2357 win_T *wp,
2358 int c1,
2359 int c2,
2360 int off,
2361 int width,
2362 int row,
2363 int endrow,
2364 int attr)
2365{
2366 int nn = off + width;
2367
2368 if (nn > wp->w_width)
2369 nn = wp->w_width;
2370#ifdef FEAT_RIGHTLEFT
2371 if (wp->w_p_rl)
2372 {
2373 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2374 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - off,
2375 c1, c2, attr);
2376 }
2377 else
2378#endif
2379 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2380 wp->w_wincol + off, (int)wp->w_wincol + nn,
2381 c1, c2, attr);
2382 return nn;
2383}
2384
2385/*
2386 * Clear lines near the end the window and mark the unused lines with "c1".
2387 * use "c2" as the filler character.
2388 * When "draw_margin" is TRUE then draw the sign, fold and number columns.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 */
2390 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002391win_draw_end(
2392 win_T *wp,
2393 int c1,
2394 int c2,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002395 int draw_margin,
Bram Moolenaar05540972016-01-30 20:31:25 +01002396 int row,
2397 int endrow,
2398 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 int n = 0;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002401 int attr = HL_ATTR(hl);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002402 int wcr_attr = get_wcr_attr(wp);
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002403
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002404 attr = hl_combine_attr(wcr_attr, attr);
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002405
2406 if (draw_margin)
2407 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002408#ifdef FEAT_FOLDING
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002409 int fdc = compute_foldcolumn(wp, 0);
2410
2411 if (fdc > 0)
2412 // draw the fold column
2413 n = screen_fill_end(wp, ' ', ' ', n, fdc,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002414 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_FC)));
Bram Moolenaar1c934292015-01-27 16:39:29 +01002415#endif
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002416#ifdef FEAT_SIGNS
2417 if (signcolumn_on(wp))
2418 // draw the sign column
2419 n = screen_fill_end(wp, ' ', ' ', n, 2,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002420 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_SC)));
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002421#endif
2422 if ((wp->w_p_nu || wp->w_p_rnu)
2423 && vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
2424 // draw the number column
2425 n = screen_fill_end(wp, ' ', ' ', n, number_width(wp) + 1,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002426 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_N)));
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428
2429#ifdef FEAT_RIGHTLEFT
2430 if (wp->w_p_rl)
2431 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002433 wp->w_wincol, W_ENDCOL(wp) - 1 - n,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002434 c2, c2, attr);
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 W_ENDCOL(wp) - 1 - n, W_ENDCOL(wp) - n,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002437 c1, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 }
2439 else
2440#endif
2441 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002443 wp->w_wincol + n, (int)W_ENDCOL(wp),
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002444 c1, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 }
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002446
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 set_empty_rows(wp, row);
2448}
2449
Bram Moolenaar1a384422010-07-14 19:53:30 +02002450#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02002451/*
2452 * Advance **color_cols and return TRUE when there are columns to draw.
2453 */
2454 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002455advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002456{
2457 while (**color_cols >= 0 && vcol > **color_cols)
2458 ++*color_cols;
2459 return (**color_cols >= 0);
2460}
2461#endif
2462
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002463#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
2464/*
2465 * Copy "text" to ScreenLines using "attr".
2466 * Returns the next screen column.
2467 */
2468 static int
2469text_to_screenline(win_T *wp, char_u *text, int col)
2470{
2471 int off = (int)(current_ScreenLine - ScreenLines);
2472
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002473 if (has_mbyte)
2474 {
2475 int cells;
2476 int u8c, u8cc[MAX_MCO];
2477 int i;
2478 int idx;
2479 int c_len;
2480 char_u *p;
2481# ifdef FEAT_ARABIC
2482 int prev_c = 0; /* previous Arabic character */
2483 int prev_c1 = 0; /* first composing char for prev_c */
2484# endif
2485
2486# ifdef FEAT_RIGHTLEFT
2487 if (wp->w_p_rl)
2488 idx = off;
2489 else
2490# endif
2491 idx = off + col;
2492
2493 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2494 for (p = text; *p != NUL; )
2495 {
2496 cells = (*mb_ptr2cells)(p);
2497 c_len = (*mb_ptr2len)(p);
Bram Moolenaar02631462017-09-22 15:20:32 +02002498 if (col + cells > wp->w_width
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002499# ifdef FEAT_RIGHTLEFT
2500 - (wp->w_p_rl ? col : 0)
2501# endif
2502 )
2503 break;
2504 ScreenLines[idx] = *p;
2505 if (enc_utf8)
2506 {
2507 u8c = utfc_ptr2char(p, u8cc);
2508 if (*p < 0x80 && u8cc[0] == 0)
2509 {
2510 ScreenLinesUC[idx] = 0;
2511#ifdef FEAT_ARABIC
2512 prev_c = u8c;
2513#endif
2514 }
2515 else
2516 {
2517#ifdef FEAT_ARABIC
2518 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2519 {
2520 /* Do Arabic shaping. */
2521 int pc, pc1, nc;
2522 int pcc[MAX_MCO];
2523 int firstbyte = *p;
2524
2525 /* The idea of what is the previous and next
2526 * character depends on 'rightleft'. */
2527 if (wp->w_p_rl)
2528 {
2529 pc = prev_c;
2530 pc1 = prev_c1;
2531 nc = utf_ptr2char(p + c_len);
2532 prev_c1 = u8cc[0];
2533 }
2534 else
2535 {
2536 pc = utfc_ptr2char(p + c_len, pcc);
2537 nc = prev_c;
2538 pc1 = pcc[0];
2539 }
2540 prev_c = u8c;
2541
2542 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
2543 pc, pc1, nc);
2544 ScreenLines[idx] = firstbyte;
2545 }
2546 else
2547 prev_c = u8c;
2548#endif
2549 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar9ba61172019-01-24 18:20:17 +01002550 ScreenLinesUC[idx] = u8c;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002551 for (i = 0; i < Screen_mco; ++i)
2552 {
2553 ScreenLinesC[i][idx] = u8cc[i];
2554 if (u8cc[i] == 0)
2555 break;
2556 }
2557 }
2558 if (cells > 1)
2559 ScreenLines[idx + 1] = 0;
2560 }
2561 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2562 /* double-byte single width character */
2563 ScreenLines2[idx] = p[1];
2564 else if (cells > 1)
2565 /* double-width character */
2566 ScreenLines[idx + 1] = p[1];
2567 col += cells;
2568 idx += cells;
2569 p += c_len;
2570 }
2571 }
2572 else
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002573 {
2574 int len = (int)STRLEN(text);
2575
Bram Moolenaar02631462017-09-22 15:20:32 +02002576 if (len > wp->w_width - col)
2577 len = wp->w_width - col;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002578 if (len > 0)
2579 {
2580#ifdef FEAT_RIGHTLEFT
2581 if (wp->w_p_rl)
Bram Moolenaarc6663882019-02-22 19:14:54 +01002582 mch_memmove(current_ScreenLine, text, len);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002583 else
2584#endif
Bram Moolenaarc6663882019-02-22 19:14:54 +01002585 mch_memmove(current_ScreenLine + col, text, len);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002586 col += len;
2587 }
2588 }
2589 return col;
2590}
2591#endif
2592
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593#ifdef FEAT_FOLDING
2594/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002595 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2596 * space is available for window "wp", minus "col".
2597 */
2598 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002599compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002600{
2601 int fdc = wp->w_p_fdc;
2602 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
Bram Moolenaar02631462017-09-22 15:20:32 +02002603 int wwidth = wp->w_width;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002604
2605 if (fdc > wwidth - (col + wmw))
2606 fdc = wwidth - (col + wmw);
2607 return fdc;
2608}
2609
2610/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 * Display one folded line.
2612 */
2613 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002614fold_line(
2615 win_T *wp,
2616 long fold_count,
2617 foldinfo_T *foldinfo,
2618 linenr_T lnum,
2619 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002621 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 pos_T *top, *bot;
2623 linenr_T lnume = lnum + fold_count - 1;
2624 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002625 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 int col;
2628 int txtcol;
2629 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002630 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631
2632 /* Build the fold line:
2633 * 1. Add the cmdwin_type for the command-line window
2634 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002635 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 * 4. Compose the text
2637 * 5. Add the text
2638 * 6. set highlighting for the Visual area an other text
2639 */
2640 col = 0;
2641
2642 /*
2643 * 1. Add the cmdwin_type for the command-line window
2644 * Ignores 'rightleft', this window is never right-left.
2645 */
2646#ifdef FEAT_CMDWIN
2647 if (cmdwin_type != 0 && wp == curwin)
2648 {
2649 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002650 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 if (enc_utf8)
2652 ScreenLinesUC[off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 ++col;
2654 }
2655#endif
2656
2657 /*
2658 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002659 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002661 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 if (fdc > 0)
2663 {
2664 fill_foldcolumn(buf, wp, TRUE, lnum);
2665#ifdef FEAT_RIGHTLEFT
2666 if (wp->w_p_rl)
2667 {
2668 int i;
2669
Bram Moolenaar02631462017-09-22 15:20:32 +02002670 copy_text_attr(off + wp->w_width - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002671 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 /* reverse the fold column */
2673 for (i = 0; i < fdc; ++i)
Bram Moolenaar02631462017-09-22 15:20:32 +02002674 ScreenLines[off + wp->w_width - i - 1 - col] = buf[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 }
2676 else
2677#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002678 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 col += fdc;
2680 }
2681
2682#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6f470022018-04-10 18:47:20 +02002683# define RL_MEMSET(p, v, l) \
2684 do { \
2685 if (wp->w_p_rl) \
2686 for (ri = 0; ri < l; ++ri) \
2687 ScreenAttrs[off + (wp->w_width - (p) - (l)) + ri] = v; \
2688 else \
2689 for (ri = 0; ri < l; ++ri) \
2690 ScreenAttrs[off + (p) + ri] = v; \
2691 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692#else
Bram Moolenaar6f470022018-04-10 18:47:20 +02002693# define RL_MEMSET(p, v, l) \
2694 do { \
2695 for (ri = 0; ri < l; ++ri) \
2696 ScreenAttrs[off + (p) + ri] = v; \
2697 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698#endif
2699
Bram Moolenaar64486672010-05-16 15:46:46 +02002700 /* Set all attributes of the 'number' or 'relativenumber' column and the
2701 * text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002702 RL_MEMSET(col, HL_ATTR(HLF_FL), wp->w_width - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703
2704#ifdef FEAT_SIGNS
2705 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002706 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002708 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 if (len > 0)
2710 {
2711 if (len > 2)
2712 len = 2;
2713# ifdef FEAT_RIGHTLEFT
2714 if (wp->w_p_rl)
2715 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002716 copy_text_attr(off + wp->w_width - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002717 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 else
2719# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002720 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 col += len;
2722 }
2723 }
2724#endif
2725
2726 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002727 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002729 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002731 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 if (len > 0)
2733 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002734 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002735 long num;
2736 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002737
2738 if (len > w + 1)
2739 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002740
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002741 if (wp->w_p_nu && !wp->w_p_rnu)
2742 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002743 num = (long)lnum;
2744 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002745 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002746 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002747 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002748 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002749 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002750 /* 'number' + 'relativenumber': cursor line shows absolute
2751 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002752 num = lnum;
2753 fmt = "%-*ld ";
2754 }
2755 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002756
Bram Moolenaar700e7342013-01-30 12:31:36 +01002757 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758#ifdef FEAT_RIGHTLEFT
2759 if (wp->w_p_rl)
2760 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002761 copy_text_attr(off + wp->w_width - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002762 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 else
2764#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002765 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 col += len;
2767 }
2768 }
2769
2770 /*
2771 * 4. Compose the folded-line string with 'foldtext', if set.
2772 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002773 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774
2775 txtcol = col; /* remember where text starts */
2776
2777 /*
2778 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2779 * Right-left text is put in columns 0 - number-col, normal text is put
2780 * in columns number-col - window-width.
2781 */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002782 col = text_to_screenline(wp, text, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783
2784 /* Fill the rest of the line with the fold filler */
2785#ifdef FEAT_RIGHTLEFT
2786 if (wp->w_p_rl)
2787 col -= txtcol;
2788#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02002789 while (col < wp->w_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790#ifdef FEAT_RIGHTLEFT
2791 - (wp->w_p_rl ? txtcol : 0)
2792#endif
2793 )
2794 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 if (enc_utf8)
2796 {
2797 if (fill_fold >= 0x80)
2798 {
2799 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002800 ScreenLinesC[0][off + col] = 0;
Bram Moolenaaracda04f2018-02-08 09:57:28 +01002801 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 }
2803 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002804 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002806 ScreenLines[off + col] = fill_fold;
2807 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002808 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002810 else
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002811 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 }
2813
2814 if (text != buf)
2815 vim_free(text);
2816
2817 /*
2818 * 6. set highlighting for the Visual area an other text.
2819 * If all folded lines are in the Visual area, highlight the line.
2820 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2822 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002823 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 {
2825 /* Visual is after curwin->w_cursor */
2826 top = &curwin->w_cursor;
2827 bot = &VIsual;
2828 }
2829 else
2830 {
2831 /* Visual is before curwin->w_cursor */
2832 top = &VIsual;
2833 bot = &curwin->w_cursor;
2834 }
2835 if (lnum >= top->lnum
2836 && lnume <= bot->lnum
2837 && (VIsual_mode != 'v'
2838 || ((lnum > top->lnum
2839 || (lnum == top->lnum
2840 && top->col == 0))
2841 && (lnume < bot->lnum
2842 || (lnume == bot->lnum
2843 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002844 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 {
2846 if (VIsual_mode == Ctrl_V)
2847 {
2848 /* Visual block mode: highlight the chars part of the block */
Bram Moolenaar02631462017-09-22 15:20:32 +02002849 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002851 if (wp->w_old_cursor_lcol != MAXCOL
2852 && wp->w_old_cursor_lcol + txtcol
Bram Moolenaar02631462017-09-22 15:20:32 +02002853 < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 len = wp->w_old_cursor_lcol;
2855 else
Bram Moolenaar02631462017-09-22 15:20:32 +02002856 len = wp->w_width - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002857 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002858 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 }
2860 }
2861 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002862 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 /* Set all attributes of the text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002864 RL_MEMSET(txtcol, HL_ATTR(HLF_V), wp->w_width - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 }
2867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002869#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002870 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2871 if (wp->w_p_cc_cols)
2872 {
2873 int i = 0;
2874 int j = wp->w_p_cc_cols[i];
2875 int old_txtcol = txtcol;
2876
2877 while (j > -1)
2878 {
2879 txtcol += j;
2880 if (wp->w_p_wrap)
2881 txtcol -= wp->w_skipcol;
2882 else
2883 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002884 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002885 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002886 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002887 txtcol = old_txtcol;
2888 j = wp->w_p_cc_cols[++i];
2889 }
2890 }
2891
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002892 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002893 if (wp->w_p_cuc)
2894 {
2895 txtcol += wp->w_virtcol;
2896 if (wp->w_p_wrap)
2897 txtcol -= wp->w_skipcol;
2898 else
2899 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002900 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaar85595c52008-10-02 16:04:05 +00002901 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002902 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00002903 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002904#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905
Bram Moolenaar02631462017-09-22 15:20:32 +02002906 screen_line(row + W_WINROW(wp), wp->w_wincol, (int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002907 (int)wp->w_width, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908
2909 /*
2910 * Update w_cline_height and w_cline_folded if the cursor line was
2911 * updated (saves a call to plines() later).
2912 */
2913 if (wp == curwin
2914 && lnum <= curwin->w_cursor.lnum
2915 && lnume >= curwin->w_cursor.lnum)
2916 {
2917 curwin->w_cline_row = row;
2918 curwin->w_cline_height = 1;
2919 curwin->w_cline_folded = TRUE;
2920 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2921 }
2922}
2923
2924/*
2925 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2926 */
2927 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002928copy_text_attr(
2929 int off,
2930 char_u *buf,
2931 int len,
2932 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002934 int i;
2935
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 mch_memmove(ScreenLines + off, buf, (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 if (enc_utf8)
2938 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002939 for (i = 0; i < len; ++i)
2940 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941}
2942
2943/*
2944 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002945 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 */
2947 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002948fill_foldcolumn(
2949 char_u *p,
2950 win_T *wp,
2951 int closed, /* TRUE of FALSE */
2952 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953{
2954 int i = 0;
2955 int level;
2956 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002957 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002958 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959
2960 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002961 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962
2963 level = win_foldinfo.fi_level;
2964 if (level > 0)
2965 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002966 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002967 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002968
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 /* If the column is too narrow, we start at the lowest level that
2970 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002971 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 if (first_level < 1)
2973 first_level = 1;
2974
Bram Moolenaar1c934292015-01-27 16:39:29 +01002975 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 {
2977 if (win_foldinfo.fi_lnum == lnum
2978 && first_level + i >= win_foldinfo.fi_low_level)
2979 p[i] = '-';
2980 else if (first_level == 1)
2981 p[i] = '|';
2982 else if (first_level + i <= 9)
2983 p[i] = '0' + first_level + i;
2984 else
2985 p[i] = '>';
2986 if (first_level + i == level)
2987 break;
2988 }
2989 }
2990 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002991 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992}
2993#endif /* FEAT_FOLDING */
2994
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01002995#ifdef FEAT_TEXT_PROP
2996static textprop_T *current_text_props = NULL;
2997static buf_T *current_buf = NULL;
2998
2999 static int
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01003000text_prop_compare(const void *s1, const void *s2)
3001{
3002 int idx1, idx2;
3003 proptype_T *pt1, *pt2;
3004 colnr_T col1, col2;
3005
3006 idx1 = *(int *)s1;
3007 idx2 = *(int *)s2;
3008 pt1 = text_prop_type_by_id(current_buf, current_text_props[idx1].tp_type);
3009 pt2 = text_prop_type_by_id(current_buf, current_text_props[idx2].tp_type);
3010 if (pt1 == pt2)
3011 return 0;
3012 if (pt1 == NULL)
3013 return -1;
3014 if (pt2 == NULL)
3015 return 1;
3016 if (pt1->pt_priority != pt2->pt_priority)
3017 return pt1->pt_priority > pt2->pt_priority ? 1 : -1;
3018 col1 = current_text_props[idx1].tp_col;
3019 col2 = current_text_props[idx2].tp_col;
3020 return col1 == col2 ? 0 : col1 > col2 ? 1 : -1;
3021}
3022#endif
3023
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003024#ifdef FEAT_SIGNS
3025/*
3026 * Get information needed to display the sign in line 'lnum' in window 'wp'.
3027 * If 'nrcol' is TRUE, the sign is going to be displayed in the number column.
3028 * Otherwise the sign is going to be displayed in the sign column.
3029 */
3030 static void
3031get_sign_display_info(
3032 int nrcol,
3033 win_T *wp,
Bram Moolenaar4e038572019-07-04 18:28:35 +02003034 linenr_T lnum UNUSED,
3035 sign_attrs_T *sattr,
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003036 int wcr_attr,
3037 int row,
3038 int startrow,
Bram Moolenaarbf8c3ad2019-06-19 14:28:43 +02003039 int filler_lines UNUSED,
3040 int filler_todo UNUSED,
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003041 int *c_extrap,
3042 int *c_finalp,
3043 char_u *extra,
3044 char_u **pp_extra,
3045 int *n_extrap,
3046 int *char_attrp)
3047{
3048 int text_sign;
3049# ifdef FEAT_SIGN_ICONS
3050 int icon_sign;
3051# endif
3052
3053 // Draw two cells with the sign value or blank.
3054 *c_extrap = ' ';
3055 *c_finalp = NUL;
3056 if (nrcol)
3057 *n_extrap = number_width(wp) + 1;
3058 else
3059 {
3060 *char_attrp = hl_combine_attr(wcr_attr, HL_ATTR(HLF_SC));
3061 *n_extrap = 2;
3062 }
3063
3064 if (row == startrow
3065#ifdef FEAT_DIFF
3066 + filler_lines && filler_todo <= 0
3067#endif
3068 )
3069 {
Bram Moolenaar4e038572019-07-04 18:28:35 +02003070 text_sign = (sattr->text != NULL) ? sattr->typenr : 0;
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003071# ifdef FEAT_SIGN_ICONS
Bram Moolenaar4e038572019-07-04 18:28:35 +02003072 icon_sign = (sattr->icon != NULL) ? sattr->typenr : 0;
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003073 if (gui.in_use && icon_sign != 0)
3074 {
3075 // Use the image in this position.
Bram Moolenaar4dff4ae2019-06-19 16:31:28 +02003076 if (nrcol)
3077 {
3078 *c_extrap = NUL;
3079 sprintf((char *)extra, "%-*c ", number_width(wp), SIGN_BYTE);
3080 *pp_extra = extra;
3081 *n_extrap = (int)STRLEN(*pp_extra);
3082 }
3083 else
3084 *c_extrap = SIGN_BYTE;
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003085# ifdef FEAT_NETBEANS_INTG
Bram Moolenaar4e038572019-07-04 18:28:35 +02003086 if (netbeans_active() && (buf_signcount(wp->w_buffer, lnum) > 1))
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003087 {
Bram Moolenaar4dff4ae2019-06-19 16:31:28 +02003088 if (nrcol)
3089 {
3090 *c_extrap = NUL;
3091 sprintf((char *)extra, "%-*c ", number_width(wp),
3092 MULTISIGN_BYTE);
3093 *pp_extra = extra;
3094 *n_extrap = (int)STRLEN(*pp_extra);
3095 }
3096 else
3097 *c_extrap = MULTISIGN_BYTE;
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003098 }
3099# endif
Bram Moolenaar4dff4ae2019-06-19 16:31:28 +02003100 *c_finalp = NUL;
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003101 *char_attrp = icon_sign;
3102 }
3103 else
3104# endif
3105 if (text_sign != 0)
3106 {
Bram Moolenaar4e038572019-07-04 18:28:35 +02003107 *pp_extra = sattr->text;
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003108 if (*pp_extra != NULL)
3109 {
3110 if (nrcol)
3111 {
Bram Moolenaard6bcff42019-07-18 12:48:16 +02003112 int n, width = number_width(wp) - 2;
3113
3114 for (n = 0; n < width; n++)
3115 extra[n] = ' ';
3116 extra[n] = 0;
3117 STRCAT(extra, *pp_extra);
3118 STRCAT(extra, " ");
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003119 *pp_extra = extra;
3120 }
3121 *c_extrap = NUL;
3122 *c_finalp = NUL;
3123 *n_extrap = (int)STRLEN(*pp_extra);
3124 }
Bram Moolenaar4e038572019-07-04 18:28:35 +02003125 *char_attrp = sattr->texthl;
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003126 }
3127 }
3128}
3129#endif
3130
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131/*
3132 * Display line "lnum" of window 'wp' on the screen.
3133 * Start at row "startrow", stop when "endrow" is reached.
3134 * wp->w_virtcol needs to be valid.
3135 *
3136 * Return the number of last row the line occupies.
3137 */
3138 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003139win_line(
3140 win_T *wp,
3141 linenr_T lnum,
3142 int startrow,
3143 int endrow,
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003144 int nochange UNUSED, // not updating for changed text
3145 int number_only) // only update the number column
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146{
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003147 int col = 0; // visual column on screen
3148 unsigned off; // offset in ScreenLines/ScreenAttrs
3149 int c = 0; // init for GCC
3150 long vcol = 0; // virtual column (for tabs)
Bram Moolenaard574ea22015-01-14 19:35:14 +01003151#ifdef FEAT_LINEBREAK
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003152 long vcol_sbr = -1; // virtual column after showbreak
Bram Moolenaard574ea22015-01-14 19:35:14 +01003153#endif
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003154 long vcol_prev = -1; // "vcol" of previous character
3155 char_u *line; // current line
3156 char_u *ptr; // current position in "line"
3157 int row; // row in the window, excl w_winrow
3158 int screen_row; // row on the screen, incl w_winrow
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003160 char_u extra[21]; // "%ld " and 'fdc' must fit in here
3161 int n_extra = 0; // number of extra chars
3162 char_u *p_extra = NULL; // string of extra chars, plus NUL
3163 char_u *p_extra_free = NULL; // p_extra needs to be freed
3164 int c_extra = NUL; // extra chars, all the same
3165 int c_final = NUL; // final char, mandatory if set
3166 int extra_attr = 0; // attributes when n_extra != 0
3167 static char_u *at_end_str = (char_u *)""; // used for p_extra when
3168 // displaying lcs_eol at end-of-line
3169 int lcs_eol_one = lcs_eol; // lcs_eol until it's been used
3170 int lcs_prec_todo = lcs_prec; // lcs_prec until it's been used
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003172 // saved "extra" items for when draw_state becomes WL_LINE (again)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 int saved_n_extra = 0;
3174 char_u *saved_p_extra = NULL;
3175 int saved_c_extra = 0;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003176 int saved_c_final = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 int saved_char_attr = 0;
3178
3179 int n_attr = 0; /* chars with special attr */
3180 int saved_attr2 = 0; /* char_attr saved for n_attr */
3181 int n_attr3 = 0; /* chars with overruling special attr */
3182 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
3183
3184 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
3185
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003186 int fromcol = -10; // start of inverting
3187 int tocol = MAXCOL; // end of inverting
3188 int fromcol_prev = -2; // start of inverting after cursor
3189 int noinvcur = FALSE; // don't invert the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003191 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 pos_T pos;
3193 long v;
3194
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003195 int char_attr = 0; // attributes for next character
3196 int attr_pri = FALSE; // char_attr has priority
3197 int area_highlighting = FALSE; // Visual or incsearch highlighting
3198 // in this line
3199 int vi_attr = 0; // attributes for Visual and incsearch
3200 // highlighting
3201 int wcr_attr = 0; // attributes from 'wincolor'
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003202 int win_attr = 0; // background for whole window, except
3203 // margins and "~" lines.
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003204 int area_attr = 0; // attributes desired by highlighting
3205 int search_attr = 0; // attributes desired by 'hlsearch'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003207 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 int syntax_attr = 0; /* attributes desired by syntax */
3209 int has_syntax = FALSE; /* this buffer has syntax highl. */
3210 int save_did_emsg;
Bram Moolenaar1a384422010-07-14 19:53:30 +02003211 int draw_color_col = FALSE; /* highlight colorcolumn */
3212 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003213#endif
Bram Moolenaarf914a332019-07-20 15:09:56 +02003214 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003215#ifdef FEAT_TEXT_PROP
3216 int text_prop_count;
3217 int text_prop_next = 0; // next text property to use
3218 textprop_T *text_props = NULL;
3219 int *text_prop_idxs = NULL;
3220 int text_props_active = 0;
3221 proptype_T *text_prop_type = NULL;
3222 int text_prop_attr = 0;
Bram Moolenaarde24a872019-05-05 15:48:00 +02003223 int text_prop_combine = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003224#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003225#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003226 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003227# define SPWORDLEN 150
3228 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003229 int nextlinecol = 0; /* column where nextline[] starts */
3230 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003231 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003232 int spell_attr = 0; /* attributes desired by spelling */
3233 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003234 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3235 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003236 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003237 static int cap_col = -1; /* column to check for Cap word */
3238 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003239 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003241 int extra_check = 0; // has extra highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 int multi_attr = 0; /* attributes desired by multibyte */
3243 int mb_l = 1; /* multi-byte byte length */
3244 int mb_c = 0; /* decoded multi-byte character */
3245 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003246 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaare9726e32019-06-19 18:01:21 +02003247#if defined(FEAT_DIFF) || defined(FEAT_SIGNS)
Bram Moolenaarbf8c3ad2019-06-19 14:28:43 +02003248 int filler_lines = 0; /* nr of filler lines to be drawn */
3249 int filler_todo = 0; /* nr of filler lines still to do + 1 */
Bram Moolenaare9726e32019-06-19 18:01:21 +02003250#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251#ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003252 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 int change_start = MAXCOL; /* first col of changed area */
3254 int change_end = -1; /* last col of changed area */
3255#endif
3256 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3257#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003258 int need_showbreak = FALSE; /* overlong line, skipping first x
3259 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003261#if defined(FEAT_SIGNS) || defined(FEAT_QUICKFIX) \
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003262 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003264 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265#endif
Bram Moolenaarb4d9b892019-07-04 22:59:06 +02003266#ifdef FEAT_SIGNS
3267 int sign_present = FALSE;
3268 sign_attrs_T sattr;
3269#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270#ifdef FEAT_ARABIC
3271 int prev_c = 0; /* previous Arabic character */
3272 int prev_c1 = 0; /* first composing char for prev_c */
3273#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003274#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003275 int did_line_attr = 0;
3276#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003277#ifdef FEAT_TERMINAL
3278 int get_term_attr = FALSE;
3279#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280
3281 /* draw_state: items that are drawn in sequence: */
3282#define WL_START 0 /* nothing done yet */
3283#ifdef FEAT_CMDWIN
3284# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3285#else
3286# define WL_CMDLINE WL_START
3287#endif
3288#ifdef FEAT_FOLDING
3289# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3290#else
3291# define WL_FOLD WL_CMDLINE
3292#endif
3293#ifdef FEAT_SIGNS
3294# define WL_SIGN WL_FOLD + 1 /* column for signs */
3295#else
3296# define WL_SIGN WL_FOLD /* column for signs */
3297#endif
3298#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003299#ifdef FEAT_LINEBREAK
3300# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003302# define WL_BRI WL_NR
3303#endif
3304#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3305# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3306#else
3307# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308#endif
3309#define WL_LINE WL_SBR + 1 /* text in the line */
3310 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003311#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 int feedback_col = 0;
3313 int feedback_old_attr = -1;
3314#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003315 int screen_line_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316
Bram Moolenaarfbfb7572019-07-25 20:53:03 +02003317#if defined(FEAT_CONCEAL) || defined(FEAT_SEARCH_EXTRA)
Bram Moolenaar07d13562019-07-24 18:43:08 +02003318 int match_conc = 0; // cchar for match functions
Bram Moolenaarfbfb7572019-07-25 20:53:03 +02003319#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02003320#ifdef FEAT_CONCEAL
3321 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003322 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003323 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003324 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003325 int is_concealing = FALSE;
Bram Moolenaar07d13562019-07-24 18:43:08 +02003326 int boguscols = 0; // nonexistent columns added to force
3327 // wrapping
3328 int vcol_off = 0; // offset for concealed characters
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003329 int did_wcol = FALSE;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003330 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003331# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003332# define FIX_FOR_BOGUSCOLS \
3333 { \
3334 n_extra += vcol_off; \
3335 vcol -= vcol_off; \
3336 vcol_off = 0; \
3337 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003338 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003339 boguscols = 0; \
3340 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003341#else
3342# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003343#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344
3345 if (startrow > endrow) /* past the end already! */
3346 return startrow;
3347
3348 row = startrow;
3349 screen_row = row + W_WINROW(wp);
3350
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003351 if (!number_only)
3352 {
3353 /*
3354 * To speed up the loop below, set extra_check when there is linebreak,
3355 * trailing white space and/or syntax processing to be done.
3356 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357#ifdef FEAT_LINEBREAK
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003358 extra_check = wp->w_p_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359#endif
3360#ifdef FEAT_SYN_HL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003361 if (syntax_present(wp) && !wp->w_s->b_syn_error
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003362# ifdef SYN_TIME_LIMIT
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003363 && !wp->w_s->b_syn_slow
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003364# endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003365 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003367 /* Prepare for syntax highlighting in this line. When there is an
3368 * error, stop syntax highlighting. */
3369 save_did_emsg = did_emsg;
3370 did_emsg = FALSE;
3371 syntax_start(wp, lnum);
3372 if (did_emsg)
3373 wp->w_s->b_syn_error = TRUE;
3374 else
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003375 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003376 did_emsg = save_did_emsg;
3377#ifdef SYN_TIME_LIMIT
3378 if (!wp->w_s->b_syn_slow)
3379#endif
3380 {
3381 has_syntax = TRUE;
3382 extra_check = TRUE;
3383 }
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003386
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003387 /* Check for columns to display for 'colorcolumn'. */
3388 color_cols = wp->w_p_cc_cols;
3389 if (color_cols != NULL)
3390 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003391#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003392
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003393#ifdef FEAT_TERMINAL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003394 if (term_show_buffer(wp->w_buffer))
3395 {
3396 extra_check = TRUE;
3397 get_term_attr = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003398 win_attr = term_get_attr(wp->w_buffer, lnum, -1);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003399 }
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003400#endif
3401
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003402#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003403 if (wp->w_p_spell
3404 && *wp->w_s->b_p_spl != NUL
3405 && wp->w_s->b_langp.ga_len > 0
3406 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003407 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003408 /* Prepare for spell checking. */
3409 has_spell = TRUE;
3410 extra_check = TRUE;
3411
Bram Moolenaar7701f302018-10-02 21:20:32 +02003412 /* Get the start of the next line, so that words that wrap to the
3413 * next line are found too: "et<line-break>al.".
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003414 * Trick: skip a few chars for C/shell/Vim comments */
3415 nextline[SPWORDLEN] = NUL;
3416 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3417 {
3418 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3419 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3420 }
3421
Bram Moolenaar7701f302018-10-02 21:20:32 +02003422 /* When a word wrapped from the previous line the start of the
3423 * current line is valid. */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003424 if (lnum == checked_lnum)
3425 cur_checked_col = checked_col;
3426 checked_lnum = 0;
3427
3428 /* When there was a sentence end in the previous line may require a
3429 * word starting with capital in this line. In line 1 always check
3430 * the first word. */
3431 if (lnum != capcol_lnum)
3432 cap_col = -1;
3433 if (lnum == 1)
3434 cap_col = 0;
3435 capcol_lnum = 0;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437#endif
3438
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003439 /*
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003440 * handle Visual active in this window
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003441 */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003442 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003444 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 {
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003446 // Visual is after curwin->w_cursor
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003447 top = &curwin->w_cursor;
3448 bot = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 }
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003450 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 {
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003452 // Visual is before curwin->w_cursor
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003453 top = &VIsual;
3454 bot = &curwin->w_cursor;
3455 }
3456 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003457 if (VIsual_mode == Ctrl_V)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003458 {
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003459 // block mode
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003460 if (lnum_in_visual_area)
3461 {
3462 fromcol = wp->w_old_cursor_fcol;
3463 tocol = wp->w_old_cursor_lcol;
3464 }
3465 }
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003466 else
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003467 {
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003468 // non-block mode
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003469 if (lnum > top->lnum && lnum <= bot->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 fromcol = 0;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003471 else if (lnum == top->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 {
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003473 if (VIsual_mode == 'V') // linewise
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003474 fromcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 else
3476 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003477 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3478 if (gchar_pos(top) == NUL)
3479 tocol = fromcol + 1;
3480 }
3481 }
3482 if (VIsual_mode != 'V' && lnum == bot->lnum)
3483 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003484 if (*p_sel == 'e' && bot->col == 0 && bot->coladd == 0)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003485 {
3486 fromcol = -10;
3487 tocol = MAXCOL;
3488 }
3489 else if (bot->col == MAXCOL)
3490 tocol = MAXCOL;
3491 else
3492 {
3493 pos = *bot;
3494 if (*p_sel == 'e')
3495 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3496 else
3497 {
3498 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3499 ++tocol;
3500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 }
3502 }
3503 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003505 /* Check if the character under the cursor should not be inverted */
3506 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003507#ifdef FEAT_GUI
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003508 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003509#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003510 )
3511 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003513 /* if inverting in this line set area_highlighting */
3514 if (fromcol >= 0)
3515 {
3516 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003517 vi_attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003519 if ((clip_star.available && !clip_star.owned
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003520 && clip_isautosel_star())
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003521 || (clip_plus.available && !clip_plus.owned
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003522 && clip_isautosel_plus()))
3523 vi_attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003528 /*
3529 * handle 'incsearch' and ":s///c" highlighting
3530 */
3531 else if (highlight_match
3532 && wp == curwin
3533 && lnum >= curwin->w_cursor.lnum
3534 && lnum <= curwin->w_cursor.lnum + search_match_lines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003536 if (lnum == curwin->w_cursor.lnum)
3537 getvcol(curwin, &(curwin->w_cursor),
Bram Moolenaarc6663882019-02-22 19:14:54 +01003538 (colnr_T *)&fromcol, NULL, NULL);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003539 else
3540 fromcol = 0;
3541 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3542 {
3543 pos.lnum = lnum;
3544 pos.col = search_match_endcol;
3545 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3546 }
3547 else
3548 tocol = MAXCOL;
3549 /* do at least one character; happens when past end of line */
3550 if (fromcol == tocol)
3551 tocol = fromcol + 1;
3552 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003553 vi_attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 }
3556
3557#ifdef FEAT_DIFF
3558 filler_lines = diff_check(wp, lnum);
3559 if (filler_lines < 0)
3560 {
3561 if (filler_lines == -1)
3562 {
3563 if (diff_find_change(wp, lnum, &change_start, &change_end))
3564 diff_hlf = HLF_ADD; /* added line */
3565 else if (change_start == 0)
3566 diff_hlf = HLF_TXD; /* changed text */
3567 else
3568 diff_hlf = HLF_CHD; /* changed line */
3569 }
3570 else
3571 diff_hlf = HLF_ADD; /* added line */
3572 filler_lines = 0;
3573 area_highlighting = TRUE;
3574 }
3575 if (lnum == wp->w_topline)
3576 filler_lines = wp->w_topfill;
3577 filler_todo = filler_lines;
3578#endif
3579
Bram Moolenaar4e038572019-07-04 18:28:35 +02003580#ifdef FEAT_SIGNS
3581 sign_present = buf_get_signattrs(wp->w_buffer, lnum, &sattr);
3582#endif
3583
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584#ifdef LINE_ATTR
3585# ifdef FEAT_SIGNS
3586 /* If this line has a sign with line highlighting set line_attr. */
Bram Moolenaar4e038572019-07-04 18:28:35 +02003587 if (sign_present)
3588 line_attr = sattr.linehl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003590# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003592 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003593 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594# endif
3595 if (line_attr != 0)
3596 area_highlighting = TRUE;
3597#endif
3598
3599 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3600 ptr = line;
3601
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003602#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003603 if (has_spell && !number_only)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003604 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003605 /* For checking first word with a capital skip white space. */
3606 if (cap_col == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003607 cap_col = getwhitecols(line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003608
Bram Moolenaar30abd282005-06-22 22:35:10 +00003609 /* To be able to spell-check over line boundaries copy the end of the
3610 * current line into nextline[]. Above the start of the next line was
3611 * copied to nextline[SPWORDLEN]. */
3612 if (nextline[SPWORDLEN] == NUL)
3613 {
3614 /* No next line or it is empty. */
3615 nextlinecol = MAXCOL;
3616 nextline_idx = 0;
3617 }
3618 else
3619 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003620 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003621 if (v < SPWORDLEN)
3622 {
3623 /* Short line, use it completely and append the start of the
3624 * next line. */
3625 nextlinecol = 0;
3626 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003627 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003628 nextline_idx = v + 1;
3629 }
3630 else
3631 {
3632 /* Long line, use only the last SPWORDLEN bytes. */
3633 nextlinecol = v - SPWORDLEN;
3634 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3635 nextline_idx = SPWORDLEN + 1;
3636 }
3637 }
3638 }
3639#endif
3640
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003641 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 {
Bram Moolenaar895d9662019-01-31 21:57:21 +01003643 if (lcs_space || lcs_trail || lcs_nbsp)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003644 extra_check = TRUE;
3645 /* find start of trailing whitespace */
3646 if (lcs_trail)
3647 {
3648 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003649 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003650 --trailcol;
3651 trailcol += (colnr_T) (ptr - line);
3652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 }
3654
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003655 wcr_attr = get_wcr_attr(wp);
3656 if (wcr_attr != 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003657 {
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003658 win_attr = wcr_attr;
3659 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003660 }
3661#ifdef FEAT_TEXT_PROP
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02003662 if (WIN_IS_POPUP(wp))
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003663 screen_line_flags |= SLF_POPUP;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003664#endif
3665
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 /*
3667 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3668 * first character to be displayed.
3669 */
3670 if (wp->w_p_wrap)
3671 v = wp->w_skipcol;
3672 else
3673 v = wp->w_leftcol;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003674 if (v > 0 && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 char_u *prev_ptr = ptr;
Bram Moolenaara12a1612019-01-24 16:39:02 +01003677
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 while (vcol < v && *ptr != NUL)
3679 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003680 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 vcol += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 prev_ptr = ptr;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003683 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 }
3685
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003686 /* When:
3687 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003688 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003689 * - 'virtualedit' is set, or
3690 * - the visual mode is active,
3691 * the end of the line may be before the start of the displayed part.
3692 */
3693 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003694#ifdef FEAT_SYN_HL
3695 wp->w_p_cuc || draw_color_col ||
3696#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003697 virtual_active() ||
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003698 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 vcol = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700
3701 /* Handle a character that's not completely on the screen: Put ptr at
3702 * that character but skip the first few screen characters. */
3703 if (vcol > v)
3704 {
3705 vcol -= c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 ptr = prev_ptr;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003707 /* If the character fits on the screen, don't need to skip it.
3708 * Except for a TAB. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01003709 if (( (*mb_ptr2cells)(ptr) >= c || *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003710 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 }
3712
3713 /*
3714 * Adjust for when the inverted text is before the screen,
3715 * and when the start of the inverted text is before the screen.
3716 */
3717 if (tocol <= vcol)
3718 fromcol = 0;
3719 else if (fromcol >= 0 && fromcol < vcol)
3720 fromcol = vcol;
3721
3722#ifdef FEAT_LINEBREAK
3723 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3724 if (wp->w_p_wrap)
3725 need_showbreak = TRUE;
3726#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003727#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003728 /* When spell checking a word we need to figure out the start of the
3729 * word and if it's badly spelled or not. */
3730 if (has_spell)
3731 {
3732 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003733 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003734 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003735
3736 pos = wp->w_cursor;
3737 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003738 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003739 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003740
3741 /* spell_move_to() may call ml_get() and make "line" invalid */
3742 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3743 ptr = line + linecol;
3744
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003745 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003746 {
3747 /* no bad word found at line start, don't check until end of a
3748 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003749 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003750 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003751 }
3752 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003753 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003754 /* bad word found, use attributes until end of word */
3755 word_end = wp->w_cursor.col + len + 1;
3756
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003757 /* Turn index into actual attributes. */
3758 if (spell_hlf != HLF_COUNT)
3759 spell_attr = highlight_attr[spell_hlf];
3760 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003761 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003762
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003763# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003764 /* Need to restart syntax highlighting for this line. */
3765 if (has_syntax)
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003766 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003767# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003768 }
3769#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 }
3771
3772 /*
3773 * Correct highlighting for cursor that can't be disabled.
3774 * Avoids having to check this for each character.
3775 */
3776 if (fromcol >= 0)
3777 {
3778 if (noinvcur)
3779 {
3780 if ((colnr_T)fromcol == wp->w_virtcol)
3781 {
3782 /* highlighting starts at cursor, let it start just after the
3783 * cursor */
3784 fromcol_prev = fromcol;
3785 fromcol = -1;
3786 }
3787 else if ((colnr_T)fromcol < wp->w_virtcol)
3788 /* restart highlighting after the cursor */
3789 fromcol_prev = wp->w_virtcol;
3790 }
3791 if (fromcol >= tocol)
3792 fromcol = -1;
3793 }
3794
3795#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarbbca7732019-07-24 18:13:16 +02003796 if (!number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003798 v = (long)(ptr - line);
Bram Moolenaarbbca7732019-07-24 18:13:16 +02003799 area_highlighting |= prepare_search_hl_line(wp, lnum, (colnr_T)v,
3800 &line, &search_hl, &search_attr);
3801 ptr = line + v; // "line" may have been updated
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 }
3803#endif
3804
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003805#ifdef FEAT_SYN_HL
Bram Moolenaar8156ed32019-03-09 11:46:15 +01003806 // Cursor line highlighting for 'cursorline' in the current window.
3807 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003808 {
Bram Moolenaar8156ed32019-03-09 11:46:15 +01003809 // Do not show the cursor line when Visual mode is active, because it's
3810 // not clear what is selected then. Do update w_last_cursorline.
3811 if (!(wp == curwin && VIsual_active))
3812 {
3813 line_attr = HL_ATTR(HLF_CUL);
3814 area_highlighting = TRUE;
3815 }
Bram Moolenaarc07ff5c2019-01-30 21:41:14 +01003816 wp->w_last_cursorline = wp->w_cursor.lnum;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003817 }
3818#endif
3819
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003820#ifdef FEAT_TEXT_PROP
3821 {
3822 char_u *prop_start;
3823
3824 text_prop_count = get_text_props(wp->w_buffer, lnum,
3825 &prop_start, FALSE);
3826 if (text_prop_count > 0)
3827 {
3828 // Make a copy of the properties, so that they are properly
3829 // aligned.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003830 text_props = ALLOC_MULT(textprop_T, text_prop_count);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003831 if (text_props != NULL)
3832 mch_memmove(text_props, prop_start,
3833 text_prop_count * sizeof(textprop_T));
3834
3835 // Allocate an array for the indexes.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003836 text_prop_idxs = ALLOC_MULT(int, text_prop_count);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003837 area_highlighting = TRUE;
3838 extra_check = TRUE;
3839 }
3840 }
3841#endif
3842
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003843 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 col = 0;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003845
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846#ifdef FEAT_RIGHTLEFT
3847 if (wp->w_p_rl)
3848 {
3849 /* Rightleft window: process the text in the normal direction, but put
3850 * it in current_ScreenLine[] from right to left. Start at the
3851 * rightmost column of the window. */
Bram Moolenaar02631462017-09-22 15:20:32 +02003852 col = wp->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 off += col;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003854 screen_line_flags |= SLF_RIGHTLEFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 }
3856#endif
3857
3858 /*
3859 * Repeat for the whole displayed line.
3860 */
3861 for (;;)
3862 {
Bram Moolenaarfbfb7572019-07-25 20:53:03 +02003863#if defined(FEAT_CONCEAL) || defined(FEAT_SEARCH_EXTRA)
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02003864 int has_match_conc = 0; // match wants to conceal
Bram Moolenaarfbfb7572019-07-25 20:53:03 +02003865#endif
Bram Moolenaar07d13562019-07-24 18:43:08 +02003866#ifdef FEAT_CONCEAL
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02003867 int did_decrement_ptr = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003868#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 /* Skip this quickly when working on the text. */
3870 if (draw_state != WL_LINE)
3871 {
3872#ifdef FEAT_CMDWIN
3873 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3874 {
3875 draw_state = WL_CMDLINE;
3876 if (cmdwin_type != 0 && wp == curwin)
3877 {
3878 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003880 c_extra = cmdwin_type;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003881 c_final = NUL;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003882 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 }
3884 }
3885#endif
3886
3887#ifdef FEAT_FOLDING
3888 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3889 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003890 int fdc = compute_foldcolumn(wp, 0);
3891
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003893 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003895 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003896 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003897 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003898 p_extra_free = alloc(12 + 1);
3899
3900 if (p_extra_free != NULL)
3901 {
3902 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3903 n_extra = fdc;
3904 p_extra_free[n_extra] = NUL;
3905 p_extra = p_extra_free;
3906 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003907 c_final = NUL;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003908 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_FC));
Bram Moolenaar62706602016-12-09 19:28:48 +01003909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 }
3911 }
3912#endif
3913
3914#ifdef FEAT_SIGNS
3915 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3916 {
3917 draw_state = WL_SIGN;
3918 /* Show the sign column when there are any signs in this
3919 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003920 if (signcolumn_on(wp))
Bram Moolenaar4e038572019-07-04 18:28:35 +02003921 get_sign_display_info(FALSE, wp, lnum, &sattr, wcr_attr,
3922 row, startrow, filler_lines, filler_todo, &c_extra,
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003923 &c_final, extra, &p_extra, &n_extra, &char_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 }
3925#endif
3926
3927 if (draw_state == WL_NR - 1 && n_extra == 0)
3928 {
3929 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003930 /* Display the absolute or relative line number. After the
3931 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3932 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 && (row == startrow
3934#ifdef FEAT_DIFF
3935 + filler_lines
3936#endif
3937 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3938 {
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003939#ifdef FEAT_SIGNS
3940 // If 'signcolumn' is set to 'number' and a sign is present
3941 // in 'lnum', then display the sign instead of the line
3942 // number.
3943 if ((*wp->w_p_scl == 'n' && *(wp->w_p_scl + 1) == 'u')
Bram Moolenaar4e038572019-07-04 18:28:35 +02003944 && sign_present)
3945 get_sign_display_info(TRUE, wp, lnum, &sattr, wcr_attr,
3946 row, startrow, filler_lines, filler_todo,
3947 &c_extra, &c_final, extra, &p_extra, &n_extra,
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003948 &char_attr);
3949 else
3950#endif
3951 {
3952 /* Draw the line number (empty space after wrapping). */
3953 if (row == startrow
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954#ifdef FEAT_DIFF
3955 + filler_lines
3956#endif
3957 )
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003958 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003959 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003960 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003961
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003962 if (wp->w_p_nu && !wp->w_p_rnu)
3963 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003964 num = (long)lnum;
3965 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003966 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003967 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003968 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003969 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003970 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003971 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003972 num = lnum;
3973 fmt = "%-*ld ";
3974 }
3975 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003976
Bram Moolenaar700e7342013-01-30 12:31:36 +01003977 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003978 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 if (wp->w_skipcol > 0)
3980 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3981 *p_extra = '-';
3982#ifdef FEAT_RIGHTLEFT
3983 if (wp->w_p_rl) /* reverse line numbers */
Bram Moolenaare73f9112019-03-29 18:29:54 +01003984 {
3985 char_u *p1, *p2;
3986 int t;
3987
3988 // like rl_mirror(), but keep the space at the end
3989 p2 = skiptowhite(extra) - 1;
3990 for (p1 = extra; p1 < p2; ++p1, --p2)
3991 {
3992 t = *p1;
3993 *p1 = *p2;
3994 *p2 = t;
3995 }
3996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997#endif
3998 p_extra = extra;
3999 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004000 c_final = NUL;
Bram Moolenaar394c5d82019-06-17 21:48:05 +02004001 }
4002 else
4003 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004005 c_final = NUL;
Bram Moolenaar394c5d82019-06-17 21:48:05 +02004006 }
4007 n_extra = number_width(wp) + 1;
4008 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_N));
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004009#ifdef FEAT_SYN_HL
Bram Moolenaar394c5d82019-06-17 21:48:05 +02004010 /* When 'cursorline' is set highlight the line number of
4011 * the current line differently.
4012 * TODO: Can we use CursorLine instead of CursorLineNr
4013 * when CursorLineNr isn't set? */
4014 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01004015 && lnum == wp->w_cursor.lnum)
Bram Moolenaar193ffd12019-05-25 22:57:30 +02004016 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_CLN));
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004017#endif
Bram Moolenaar394c5d82019-06-17 21:48:05 +02004018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 }
4020 }
4021
Bram Moolenaar597a4222014-06-25 14:39:50 +02004022#ifdef FEAT_LINEBREAK
4023 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
4024 && n_extra == 0 && *p_sbr != NUL)
4025 /* draw indent after showbreak value */
4026 draw_state = WL_BRI;
4027 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
4028 /* After the showbreak, draw the breakindent */
4029 draw_state = WL_BRI - 1;
4030
4031 /* draw 'breakindent': indent wrapped text accordingly */
4032 if (draw_state == WL_BRI - 1 && n_extra == 0)
4033 {
4034 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01004035 /* if need_showbreak is set, breakindent also applies */
4036 if (wp->w_p_bri && n_extra == 0
4037 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004038# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004039 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004040# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004041 )
4042 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01004043 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004044# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004045 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004046 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004047 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004048# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02004049 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4050 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004051 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004052# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02004053 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004054# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02004055 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02004056 c_extra = ' ';
4057 n_extra = get_breakindent_win(wp,
4058 ml_get_buf(wp->w_buffer, lnum, FALSE));
4059 /* Correct end of highlighted area for 'breakindent',
4060 * required when 'linebreak' is also set. */
4061 if (tocol == vcol)
4062 tocol += n_extra;
4063 }
4064 }
4065#endif
4066
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
4068 if (draw_state == WL_SBR - 1 && n_extra == 0)
4069 {
4070 draw_state = WL_SBR;
4071# ifdef FEAT_DIFF
4072 if (filler_todo > 0)
4073 {
4074 /* Draw "deleted" diff line(s). */
4075 if (char2cells(fill_diff) > 1)
Bram Moolenaar83a52172019-01-16 22:41:54 +01004076 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 c_extra = '-';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004078 c_final = NUL;
4079 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 else
Bram Moolenaar83a52172019-01-16 22:41:54 +01004081 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 c_extra = fill_diff;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004083 c_final = NUL;
4084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085# ifdef FEAT_RIGHTLEFT
4086 if (wp->w_p_rl)
4087 n_extra = col + 1;
4088 else
4089# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004090 n_extra = wp->w_width - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004091 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 }
4093# endif
4094# ifdef FEAT_LINEBREAK
4095 if (*p_sbr != NUL && need_showbreak)
4096 {
4097 /* Draw 'showbreak' at the start of each broken line. */
4098 p_extra = p_sbr;
4099 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004100 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004102 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004104 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 /* Correct end of highlighted area for 'showbreak',
4106 * required when 'linebreak' is also set. */
4107 if (tocol == vcol)
4108 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004109#ifdef FEAT_SYN_HL
4110 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004111 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004112 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004113 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004114#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 }
4116# endif
4117 }
4118#endif
4119
4120 if (draw_state == WL_LINE - 1 && n_extra == 0)
4121 {
4122 draw_state = WL_LINE;
4123 if (saved_n_extra)
4124 {
4125 /* Continue item from end of wrapped line. */
4126 n_extra = saved_n_extra;
4127 c_extra = saved_c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004128 c_final = saved_c_final;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 p_extra = saved_p_extra;
4130 char_attr = saved_char_attr;
4131 }
4132 else
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004133 char_attr = win_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 }
4135 }
4136
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004137 // When still displaying '$' of change command, stop at cursor.
4138 // When only displaying the (relative) line number and that's done,
4139 // stop here.
4140 if ((dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004141 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142#ifdef FEAT_DIFF
4143 && filler_todo <= 0
4144#endif
4145 )
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004146 || (number_only && draw_state > WL_NR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 {
Bram Moolenaar02631462017-09-22 15:20:32 +02004148 screen_line(screen_row, wp->w_wincol, col, -(int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004149 screen_line_flags);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004150 /* Pretend we have finished updating the window. Except when
4151 * 'cursorcolumn' is set. */
4152#ifdef FEAT_SYN_HL
4153 if (wp->w_p_cuc)
4154 row = wp->w_cline_row + wp->w_cline_height;
4155 else
4156#endif
4157 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 break;
4159 }
4160
Bram Moolenaar637532b2019-01-03 21:44:40 +01004161 if (draw_state == WL_LINE && (area_highlighting
4162#ifdef FEAT_SPELL
4163 || has_spell
4164#endif
4165 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 {
4167 /* handle Visual or match highlighting in this line */
4168 if (vcol == fromcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4170 && (*mb_ptr2cells)(ptr) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004172 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 && vcol < tocol))
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004174 area_attr = vi_attr; /* start highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 else if (area_attr != 0
4176 && (vcol == tocol
4177 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179
4180#ifdef FEAT_SEARCH_EXTRA
4181 if (!n_extra)
4182 {
4183 /*
Bram Moolenaarbbca7732019-07-24 18:13:16 +02004184 * Check for start/end of 'hlsearch' and other matches.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 * After end, check for start/end of next match.
4186 * When another match, have to check for start again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004188 v = (long)(ptr - line);
Bram Moolenaarbbca7732019-07-24 18:13:16 +02004189 search_attr = update_search_hl(wp, lnum, (colnr_T)v, &line,
4190 &search_hl, &has_match_conc, &match_conc,
4191 did_line_attr, lcs_eol_one);
4192 ptr = line + v; // "line" may have been changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 }
4194#endif
4195
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004197 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004199 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4200 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004202 if (diff_hlf == HLF_TXD && ptr - line > change_end
4203 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004205 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004206 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004207 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 }
4209#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004210
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004211#ifdef FEAT_TEXT_PROP
4212 if (text_props != NULL)
4213 {
4214 int pi;
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004215 int bcol = (int)(ptr - line);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004216
Bram Moolenaara956bf62019-06-19 17:34:24 +02004217 if (n_extra > 0)
4218 --bcol; // still working on the previous char, e.g. Tab
4219
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004220 // Check if any active property ends.
4221 for (pi = 0; pi < text_props_active; ++pi)
4222 {
4223 int tpi = text_prop_idxs[pi];
4224
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004225 if (bcol >= text_props[tpi].tp_col - 1
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004226 + text_props[tpi].tp_len)
4227 {
4228 if (pi + 1 < text_props_active)
4229 mch_memmove(text_prop_idxs + pi,
4230 text_prop_idxs + pi + 1,
4231 sizeof(int)
4232 * (text_props_active - (pi + 1)));
4233 --text_props_active;
4234 --pi;
4235 }
4236 }
4237
4238 // Add any text property that starts in this column.
4239 while (text_prop_next < text_prop_count
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004240 && bcol >= text_props[text_prop_next].tp_col - 1)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004241 text_prop_idxs[text_props_active++] = text_prop_next++;
4242
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004243 text_prop_attr = 0;
Bram Moolenaarde24a872019-05-05 15:48:00 +02004244 text_prop_combine = FALSE;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004245 if (text_props_active > 0)
4246 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004247 // Sort the properties on priority and/or starting last.
4248 // Then combine the attributes, highest priority last.
4249 current_text_props = text_props;
4250 current_buf = wp->w_buffer;
4251 qsort((void *)text_prop_idxs, (size_t)text_props_active,
4252 sizeof(int), text_prop_compare);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004253
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004254 for (pi = 0; pi < text_props_active; ++pi)
4255 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004256 int tpi = text_prop_idxs[pi];
Bram Moolenaarde24a872019-05-05 15:48:00 +02004257 proptype_T *pt = text_prop_type_by_id(
4258 wp->w_buffer, text_props[tpi].tp_type);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004259
Bram Moolenaard74af422019-06-28 21:38:00 +02004260 if (pt != NULL && pt->pt_hl_id > 0)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004261 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004262 int pt_attr = syn_id2attr(pt->pt_hl_id);
4263
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004264 text_prop_type = pt;
Bram Moolenaarde24a872019-05-05 15:48:00 +02004265 text_prop_attr =
4266 hl_combine_attr(text_prop_attr, pt_attr);
4267 text_prop_combine = pt->pt_flags & PT_FLAG_COMBINE;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004268 }
4269 }
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004270 }
4271 }
4272#endif
4273
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004274 /* Decide which of the highlight attributes to use. */
4275 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004276#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004277 if (area_attr != 0)
4278 char_attr = hl_combine_attr(line_attr, area_attr);
4279 else if (search_attr != 0)
4280 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004281# ifdef FEAT_TEXT_PROP
4282 else if (text_prop_type != NULL)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02004283 {
4284 char_attr = hl_combine_attr(
4285 line_attr != 0 ? line_attr : win_attr, text_prop_attr);
4286 }
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004287# endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004288 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004289 || vcol < fromcol || vcol_prev < fromcol_prev
4290 || vcol >= tocol))
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004291 // Use line_attr when not in the Visual or 'incsearch' area
4292 // (area_attr may be 0 when "noinvcur" is set).
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004293 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004294#else
4295 if (area_attr != 0)
4296 char_attr = area_attr;
4297 else if (search_attr != 0)
4298 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004299#endif
4300 else
4301 {
4302 attr_pri = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004303#ifdef FEAT_TEXT_PROP
4304 if (text_prop_type != NULL)
Bram Moolenaarde24a872019-05-05 15:48:00 +02004305 {
4306 if (text_prop_combine)
4307 char_attr = hl_combine_attr(
4308 syntax_attr, text_prop_attr);
4309 else
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02004310 char_attr = hl_combine_attr(
4311 win_attr, text_prop_attr);
Bram Moolenaarde24a872019-05-05 15:48:00 +02004312 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004313 else
4314#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004315#ifdef FEAT_SYN_HL
4316 if (has_syntax)
4317 char_attr = syntax_attr;
4318 else
4319#endif
4320 char_attr = 0;
4321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004323 if (char_attr == 0)
4324 char_attr = win_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325
4326 /*
4327 * Get the next character to put on the screen.
4328 */
4329 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004330 * The "p_extra" points to the extra stuff that is inserted to
4331 * represent special characters (non-printable stuff) and other
4332 * things. When all characters are the same, c_extra is used.
Bram Moolenaar83a52172019-01-16 22:41:54 +01004333 * If c_final is set, it will compulsorily be used at the end.
Bram Moolenaara064ac82007-08-05 18:10:54 +00004334 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4335 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4337 */
4338 if (n_extra > 0)
4339 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004340 if (c_extra != NUL || (n_extra == 1 && c_final != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004342 c = (n_extra == 1 && c_final != NUL) ? c_final : c_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004344 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 {
4346 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004347 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004348 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 }
4350 else
4351 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 }
4353 else
4354 {
4355 c = *p_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 if (has_mbyte)
4357 {
4358 mb_c = c;
4359 if (enc_utf8)
4360 {
4361 /* If the UTF-8 character is more than one byte:
4362 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004363 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 mb_utf8 = FALSE;
4365 if (mb_l > n_extra)
4366 mb_l = 1;
4367 else if (mb_l > 1)
4368 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004369 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004371 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 }
4373 }
4374 else
4375 {
4376 /* if this is a DBCS character, put it in "mb_c" */
4377 mb_l = MB_BYTE2LEN(c);
4378 if (mb_l >= n_extra)
4379 mb_l = 1;
4380 else if (mb_l > 1)
4381 mb_c = (c << 8) + p_extra[1];
4382 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004383 if (mb_l == 0) /* at the NUL at end-of-line */
4384 mb_l = 1;
4385
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 /* If a double-width char doesn't fit display a '>' in the
4387 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004388 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389# ifdef FEAT_RIGHTLEFT
4390 wp->w_p_rl ? (col <= 0) :
4391# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004392 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 && (*mb_char2cells)(mb_c) == 2)
4394 {
4395 c = '>';
4396 mb_c = c;
4397 mb_l = 1;
4398 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004399 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 /* put the pointer back to output the double-width
4401 * character at the start of the next line. */
4402 ++n_extra;
4403 --p_extra;
4404 }
4405 else
4406 {
4407 n_extra -= mb_l - 1;
4408 p_extra += mb_l - 1;
4409 }
4410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 ++p_extra;
4412 }
4413 --n_extra;
4414 }
4415 else
4416 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004417#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004418 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004419#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004420
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004421 if (p_extra_free != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01004422 VIM_CLEAR(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 /*
4424 * Get a character from the line itself.
4425 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004426 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004427#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004428 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004429#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 if (has_mbyte)
4431 {
4432 mb_c = c;
4433 if (enc_utf8)
4434 {
4435 /* If the UTF-8 character is more than one byte: Decode it
4436 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004437 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 mb_utf8 = FALSE;
4439 if (mb_l > 1)
4440 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004441 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 /* Overlong encoded ASCII or ASCII with composing char
4443 * is displayed normally, except a NUL. */
4444 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004445 {
4446 c = mb_c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004447#ifdef FEAT_LINEBREAK
Bram Moolenaar88e76882017-02-27 20:33:46 +01004448 c0 = mb_c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004449#endif
Bram Moolenaar88e76882017-02-27 20:33:46 +01004450 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004452
4453 /* At start of the line we can have a composing char.
4454 * Draw it as a space with a composing char. */
4455 if (utf_iscomposing(mb_c))
4456 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004457 int i;
4458
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004459 for (i = Screen_mco - 1; i > 0; --i)
4460 u8cc[i] = u8cc[i - 1];
4461 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004462 mb_c = ' ';
4463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 }
4465
4466 if ((mb_l == 1 && c >= 0x80)
4467 || (mb_l >= 1 && mb_c == 0)
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004468 || (mb_l > 1 && (!vim_isprintc(mb_c))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 {
4470 /*
4471 * Illegal UTF-8 byte: display as <xx>.
4472 * Non-BMP character : display as ? or fullwidth ?.
4473 */
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004474 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004475# ifdef FEAT_RIGHTLEFT
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004476 if (wp->w_p_rl) /* reverse */
4477 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004478# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 p_extra = extra;
4480 c = *p_extra;
4481 mb_c = mb_ptr2char_adv(&p_extra);
4482 mb_utf8 = (c >= 0x80);
4483 n_extra = (int)STRLEN(p_extra);
4484 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004485 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 if (area_attr == 0 && search_attr == 0)
4487 {
4488 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004489 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 saved_attr2 = char_attr; /* save current attr */
4491 }
4492 }
4493 else if (mb_l == 0) /* at the NUL at end-of-line */
4494 mb_l = 1;
4495#ifdef FEAT_ARABIC
4496 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4497 {
4498 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004499 int pc, pc1, nc;
4500 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501
4502 /* The idea of what is the previous and next
4503 * character depends on 'rightleft'. */
4504 if (wp->w_p_rl)
4505 {
4506 pc = prev_c;
4507 pc1 = prev_c1;
4508 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004509 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 }
4511 else
4512 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004513 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004515 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 }
4517 prev_c = mb_c;
4518
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004519 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 }
4521 else
4522 prev_c = mb_c;
4523#endif
4524 }
4525 else /* enc_dbcs */
4526 {
4527 mb_l = MB_BYTE2LEN(c);
4528 if (mb_l == 0) /* at the NUL at end-of-line */
4529 mb_l = 1;
4530 else if (mb_l > 1)
4531 {
4532 /* We assume a second byte below 32 is illegal.
4533 * Hopefully this is OK for all double-byte encodings!
4534 */
4535 if (ptr[1] >= 32)
4536 mb_c = (c << 8) + ptr[1];
4537 else
4538 {
4539 if (ptr[1] == NUL)
4540 {
4541 /* head byte at end of line */
4542 mb_l = 1;
4543 transchar_nonprint(extra, c);
4544 }
4545 else
4546 {
4547 /* illegal tail byte */
4548 mb_l = 2;
4549 STRCPY(extra, "XX");
4550 }
4551 p_extra = extra;
4552 n_extra = (int)STRLEN(extra) - 1;
4553 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004554 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 c = *p_extra++;
4556 if (area_attr == 0 && search_attr == 0)
4557 {
4558 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004559 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 saved_attr2 = char_attr; /* save current attr */
4561 }
4562 mb_c = c;
4563 }
4564 }
4565 }
4566 /* If a double-width char doesn't fit display a '>' in the
4567 * last column; the character is displayed at the start of the
4568 * next line. */
4569 if ((
4570# ifdef FEAT_RIGHTLEFT
4571 wp->w_p_rl ? (col <= 0) :
4572# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004573 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 && (*mb_char2cells)(mb_c) == 2)
4575 {
4576 c = '>';
4577 mb_c = c;
4578 mb_utf8 = FALSE;
4579 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004580 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02004581 // Put pointer back so that the character will be
4582 // displayed at the start of the next line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 --ptr;
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02004584#ifdef FEAT_CONCEAL
4585 did_decrement_ptr = TRUE;
4586#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 }
4588 else if (*ptr != NUL)
4589 ptr += mb_l - 1;
4590
4591 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004592 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004593 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004594 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004597 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004598 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 c = ' ';
4600 if (area_attr == 0 && search_attr == 0)
4601 {
4602 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004603 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 saved_attr2 = char_attr; /* save current attr */
4605 }
4606 mb_c = c;
4607 mb_utf8 = FALSE;
4608 mb_l = 1;
4609 }
4610
4611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 ++ptr;
4613
4614 if (extra_check)
4615 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004616#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004617 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004618#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004619
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004620#ifdef FEAT_TERMINAL
4621 if (get_term_attr)
4622 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02004623 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004624
4625 if (!attr_pri)
4626 char_attr = syntax_attr;
4627 else
4628 char_attr = hl_combine_attr(syntax_attr, char_attr);
4629 }
4630#endif
4631
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004632#ifdef FEAT_SYN_HL
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004633 // Get syntax attribute, unless still at the start of the line
4634 // (double-wide char that doesn't fit).
Bram Moolenaar217ad922005-03-20 22:37:15 +00004635 v = (long)(ptr - line);
4636 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 {
4638 /* Get the syntax attribute for the character. If there
4639 * is an error, disable syntax highlighting. */
4640 save_did_emsg = did_emsg;
4641 did_emsg = FALSE;
4642
Bram Moolenaar217ad922005-03-20 22:37:15 +00004643 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004644# ifdef FEAT_SPELL
4645 has_spell ? &can_spell :
4646# endif
4647 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648
4649 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004650 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004651 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004652 has_syntax = FALSE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004653 syntax_attr = 0;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 else
4656 did_emsg = save_did_emsg;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004657
4658 // combine syntax attribute with 'wincolor'
4659 if (win_attr != 0)
4660 syntax_attr = hl_combine_attr(win_attr, syntax_attr);
4661
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004662#ifdef SYN_TIME_LIMIT
4663 if (wp->w_s->b_syn_slow)
4664 has_syntax = FALSE;
4665#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666
4667 /* Need to get the line again, a multi-line regexp may
4668 * have made it invalid. */
4669 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4670 ptr = line + v;
4671
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004672# ifdef FEAT_TEXT_PROP
Bram Moolenaarde24a872019-05-05 15:48:00 +02004673 // Text properties overrule syntax highlighting or combine.
4674 if (text_prop_attr == 0 || text_prop_combine)
4675# endif
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004676 {
Bram Moolenaarde24a872019-05-05 15:48:00 +02004677 int comb_attr = syntax_attr;
4678# ifdef FEAT_TEXT_PROP
4679 comb_attr = hl_combine_attr(text_prop_attr, comb_attr);
4680# endif
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004681 if (!attr_pri)
Bram Moolenaarde24a872019-05-05 15:48:00 +02004682 char_attr = comb_attr;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004683 else
Bram Moolenaarde24a872019-05-05 15:48:00 +02004684 char_attr = hl_combine_attr(comb_attr, char_attr);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004685 }
Bram Moolenaarc095b282010-07-20 22:33:34 +02004686# ifdef FEAT_CONCEAL
4687 /* no concealing past the end of the line, it interferes
4688 * with line highlighting */
4689 if (c == NUL)
4690 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004691 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004692 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004693# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004695#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004696
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004697#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004698 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004699 * Only do this when there is no syntax highlighting, the
4700 * @Spell cluster is not used or the current syntax item
4701 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004702 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004703 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004704 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004705 if (c != 0 && (
4706# ifdef FEAT_SYN_HL
4707 !has_syntax ||
4708# endif
4709 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004710 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004711 char_u *prev_ptr, *p;
4712 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004713 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaare7566042005-06-17 22:00:15 +00004714 if (has_mbyte)
4715 {
4716 prev_ptr = ptr - mb_l;
4717 v -= mb_l - 1;
4718 }
4719 else
Bram Moolenaare7566042005-06-17 22:00:15 +00004720 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004721
4722 /* Use nextline[] if possible, it has the start of the
4723 * next line concatenated. */
4724 if ((prev_ptr - line) - nextlinecol >= 0)
4725 p = nextline + (prev_ptr - line) - nextlinecol;
4726 else
4727 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004728 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004729 len = spell_check(wp, p, &spell_hlf, &cap_col,
4730 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004731 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004732
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004733 /* In Insert mode only highlight a word that
4734 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004735 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004736 && (State & INSERT) != 0
4737 && wp->w_cursor.lnum == lnum
4738 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004739 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004740 && wp->w_cursor.col < (colnr_T)word_end)
4741 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004742 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004743 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004744 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004745
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004746 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004747 && (p - nextline) + len > nextline_idx)
4748 {
4749 /* Remember that the good word continues at the
4750 * start of the next line. */
4751 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004752 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004753 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004754
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004755 /* Turn index into actual attributes. */
4756 if (spell_hlf != HLF_COUNT)
4757 spell_attr = highlight_attr[spell_hlf];
4758
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004759 if (cap_col > 0)
4760 {
4761 if (p != prev_ptr
4762 && (p - nextline) + cap_col >= nextline_idx)
4763 {
4764 /* Remember that the word in the next line
4765 * must start with a capital. */
4766 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004767 cap_col = (int)((p - nextline) + cap_col
4768 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004769 }
4770 else
4771 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004772 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004773 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004774 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004775 }
4776 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004777 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004778 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004779 char_attr = hl_combine_attr(char_attr, spell_attr);
4780 else
4781 char_attr = hl_combine_attr(spell_attr, char_attr);
4782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783#endif
4784#ifdef FEAT_LINEBREAK
4785 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004786 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004788 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004789 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 {
Bram Moolenaar4df70292015-03-21 14:20:16 +01004791 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004792 char_u *p = ptr - (mb_off + 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004793
Bram Moolenaar597a4222014-06-25 14:39:50 +02004794 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004795 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004796 NULL) - 1;
Bram Moolenaar02631462017-09-22 15:20:32 +02004797 if (c == TAB && n_extra + col > wp->w_width)
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004798# ifdef FEAT_VARTABS
Bram Moolenaara87b72c2018-06-25 21:24:51 +02004799 n_extra = tabstop_padding(vcol, wp->w_buffer->b_p_ts,
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004800 wp->w_buffer->b_p_vts_array) - 1;
4801# else
Bram Moolenaara3650912014-11-19 13:21:57 +01004802 n_extra = (int)wp->w_buffer->b_p_ts
4803 - vcol % (int)wp->w_buffer->b_p_ts - 1;
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004804# endif
Bram Moolenaara3650912014-11-19 13:21:57 +01004805
Bram Moolenaar4df70292015-03-21 14:20:16 +01004806 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004807 c_final = NUL;
Bram Moolenaar1c465442017-03-12 20:10:05 +01004808 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004809 {
4810#ifdef FEAT_CONCEAL
4811 if (c == TAB)
4812 /* See "Tab alignment" below. */
4813 FIX_FOR_BOGUSCOLS;
4814#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004815 if (!wp->w_p_list)
4816 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 }
4819#endif
4820
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004821 // 'list': Change char 160 to lcs_nbsp and space to lcs_space.
4822 // But not when the character is followed by a composing
4823 // character (use mb_l to check that).
4824 if (wp->w_p_list
4825 && ((((c == 160 && mb_l == 1)
4826 || (mb_utf8
4827 && ((mb_c == 160 && mb_l == 2)
4828 || (mb_c == 0x202f && mb_l == 3))))
4829 && lcs_nbsp)
4830 || (c == ' '
4831 && mb_l == 1
4832 && lcs_space
4833 && ptr - line <= trailcol)))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004834 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004835 c = (c == ' ') ? lcs_space : lcs_nbsp;
4836 if (area_attr == 0 && search_attr == 0)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004837 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004838 n_attr = 1;
4839 extra_attr = HL_ATTR(HLF_8);
4840 saved_attr2 = char_attr; /* save current attr */
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004841 }
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004842 mb_c = c;
4843 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004844 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004845 mb_utf8 = TRUE;
4846 u8cc[0] = 0;
4847 c = 0xc0;
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004848 }
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004849 else
4850 mb_utf8 = FALSE;
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004851 }
4852
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4854 {
4855 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004856 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 {
4858 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004859 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 saved_attr2 = char_attr; /* save current attr */
4861 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004863 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 {
4865 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004866 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004867 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 }
4869 else
4870 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 }
4872 }
4873
4874 /*
4875 * Handling of non-printable characters.
4876 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004877 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 {
4879 /*
4880 * when getting a character from the file, we may have to
4881 * turn it into something else on the way to putting it
4882 * into "ScreenLines".
4883 */
4884 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4885 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004886 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004887 long vcol_adjusted = vcol; /* removed showbreak length */
4888#ifdef FEAT_LINEBREAK
4889 /* only adjust the tab_len, when at the first column
4890 * after the showbreak value was drawn */
4891 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4892 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4893#endif
Bram Moolenaar69cbbec2019-08-17 14:10:56 +02004894 // tab amount depends on current column
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004895#ifdef FEAT_VARTABS
4896 tab_len = tabstop_padding(vcol_adjusted,
4897 wp->w_buffer->b_p_ts,
4898 wp->w_buffer->b_p_vts_array) - 1;
4899#else
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004900 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004901 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4902#endif
Bram Moolenaard574ea22015-01-14 19:35:14 +01004903
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004904#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004905 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004906#endif
Bram Moolenaar69cbbec2019-08-17 14:10:56 +02004907 // tab amount depends on current column
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004908 n_extra = tab_len;
4909#ifdef FEAT_LINEBREAK
4910 else
4911 {
Bram Moolenaar69cbbec2019-08-17 14:10:56 +02004912 char_u *p;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01004913 int len;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004914 int i;
4915 int saved_nextra = n_extra;
4916
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004917#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004918 if (vcol_off > 0)
Bram Moolenaar69cbbec2019-08-17 14:10:56 +02004919 // there are characters to conceal
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004920 tab_len += vcol_off;
Bram Moolenaar69cbbec2019-08-17 14:10:56 +02004921 // boguscols before FIX_FOR_BOGUSCOLS macro from above
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004922 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
4923 && n_extra > tab_len)
4924 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004925#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004926
Bram Moolenaar69cbbec2019-08-17 14:10:56 +02004927 // if n_extra > 0, it gives the number of chars, to
4928 // use for a tab, else we need to calculate the width
4929 // for a tab
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004930 len = (tab_len * mb_char2len(lcs_tab2));
4931 if (n_extra > 0)
4932 len += n_extra - tab_len;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004933 c = lcs_tab1;
Bram Moolenaar964b3742019-05-24 18:54:09 +02004934 p = alloc(len + 1);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004935 vim_memset(p, ' ', len);
4936 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004937 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004938 p_extra_free = p;
4939 for (i = 0; i < tab_len; i++)
4940 {
Bram Moolenaar69cbbec2019-08-17 14:10:56 +02004941 int lcs = lcs_tab2;
4942
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004943 if (*p == NUL)
4944 {
4945 tab_len = i;
4946 break;
4947 }
Bram Moolenaar69cbbec2019-08-17 14:10:56 +02004948
4949 // if lcs_tab3 is given, need to change the char
4950 // for tab
4951 if (lcs_tab3 && i == tab_len - 1)
4952 lcs = lcs_tab3;
4953 mb_char2bytes(lcs, p);
4954 p += mb_char2len(lcs);
4955 n_extra += mb_char2len(lcs)
4956 - (saved_nextra > 0 ? 1 : 0);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004957 }
4958 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004959#ifdef FEAT_CONCEAL
Bram Moolenaar69cbbec2019-08-17 14:10:56 +02004960 // n_extra will be increased by FIX_FOX_BOGUSCOLS
4961 // macro below, so need to adjust for that here
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004962 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004963 n_extra -= vcol_off;
4964#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004965 }
4966#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004967#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004968 {
4969 int vc_saved = vcol_off;
4970
4971 /* Tab alignment should be identical regardless of
4972 * 'conceallevel' value. So tab compensates of all
4973 * previous concealed characters, and thus resets
4974 * vcol_off and boguscols accumulated so far in the
4975 * line. Note that the tab can be longer than
4976 * 'tabstop' when there are concealed characters. */
4977 FIX_FOR_BOGUSCOLS;
4978
4979 /* Make sure, the highlighting for the tab char will be
4980 * correctly set further below (effectively reverts the
4981 * FIX_FOR_BOGSUCOLS macro */
4982 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004983 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004984 tab_len += vc_saved;
4985 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004986#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 if (wp->w_p_list)
4989 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004990 c = (n_extra == 0 && lcs_tab3) ? lcs_tab3 : lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004991#ifdef FEAT_LINEBREAK
4992 if (wp->w_p_lbr)
4993 c_extra = NUL; /* using p_extra from above */
4994 else
4995#endif
4996 c_extra = lcs_tab2;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004997 c_final = lcs_tab3;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004998 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004999 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 saved_attr2 = char_attr; /* save current attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005002 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 {
5004 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005005 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005006 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 }
5009 else
5010 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01005011 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 c_extra = ' ';
5013 c = ' ';
5014 }
5015 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005016 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02005017 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005018 || ((fromcol >= 0 || fromcol_prev >= 0)
5019 && tocol > vcol
5020 && VIsual_mode != Ctrl_V
5021 && (
5022# ifdef FEAT_RIGHTLEFT
5023 wp->w_p_rl ? (col >= 0) :
5024# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005025 (col < wp->w_width))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005026 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005027 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005028 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02005029 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005031 /* Display a '$' after the line or highlight an extra
5032 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033#if defined(FEAT_DIFF) || defined(LINE_ATTR)
5034 /* For a diff line the highlighting continues after the
5035 * "$". */
5036 if (
5037# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005038 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039# ifdef LINE_ATTR
5040 &&
5041# endif
5042# endif
5043# ifdef LINE_ATTR
5044 line_attr == 0
5045# endif
5046 )
5047#endif
5048 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 /* In virtualedit, visual selections may extend
5050 * beyond end of line. */
5051 if (area_highlighting && virtual_active()
5052 && tocol != MAXCOL && vcol < tocol)
5053 n_extra = 0;
5054 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 {
5056 p_extra = at_end_str;
5057 n_extra = 1;
5058 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005059 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 }
5061 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02005062 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005063 c = lcs_eol;
5064 else
5065 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 lcs_eol_one = -1;
5067 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005068 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005070 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 n_attr = 1;
5072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005074 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 {
5076 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005077 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005078 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 }
5080 else
5081 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 }
5083 else if (c != NUL)
5084 {
5085 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02005086 if (n_extra == 0)
5087 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088#ifdef FEAT_RIGHTLEFT
5089 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
5090 rl_mirror(p_extra); /* reverse "<12>" */
5091#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005093 c_final = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005094#ifdef FEAT_LINEBREAK
5095 if (wp->w_p_lbr)
5096 {
5097 char_u *p;
5098
5099 c = *p_extra;
Bram Moolenaar964b3742019-05-24 18:54:09 +02005100 p = alloc(n_extra + 1);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005101 vim_memset(p, ' ', n_extra);
5102 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5103 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005104 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005105 p_extra_free = p_extra = p;
5106 }
5107 else
5108#endif
5109 {
5110 n_extra = byte2cells(c) - 1;
5111 c = *p_extra++;
5112 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005113 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 {
5115 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005116 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 saved_attr2 = char_attr; /* save current attr */
5118 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 else if (VIsual_active
5122 && (VIsual_mode == Ctrl_V
5123 || VIsual_mode == 'v')
5124 && virtual_active()
5125 && tocol != MAXCOL
5126 && vcol < tocol
5127 && (
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005128#ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 wp->w_p_rl ? (col >= 0) :
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005130#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005131 (col < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 {
5133 c = ' ';
5134 --ptr; /* put it back at the NUL */
5135 }
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005136#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 else if ((
5138# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005139 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005141# ifdef FEAT_TERMINAL
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005142 win_attr != 0 ||
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005143# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 ) && (
5146# ifdef FEAT_RIGHTLEFT
5147 wp->w_p_rl ? (col >= 0) :
5148# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005149 (col
5150# ifdef FEAT_CONCEAL
5151 - boguscols
5152# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005153 < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 {
5155 /* Highlight until the right side of the window */
5156 c = ' ';
5157 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005158
5159 /* Remember we do the char for line highlighting. */
5160 ++did_line_attr;
5161
5162 /* don't do search HL for the rest of the line */
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005163 if (line_attr != 0 && char_attr == search_attr
5164 && (did_line_attr > 1
5165 || (wp->w_p_list && lcs_eol > 0)))
Bram Moolenaar91170f82006-05-05 21:15:17 +00005166 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167# ifdef FEAT_DIFF
5168 if (diff_hlf == HLF_TXD)
5169 {
5170 diff_hlf = HLF_CHD;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005171 if (vi_attr == 0 || char_attr != vi_attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005172 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005173 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005174 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5175 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005176 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 }
5179# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005180# ifdef FEAT_TERMINAL
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005181 if (win_attr != 0)
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005182 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005183 char_attr = win_attr;
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005184 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5185 char_attr = hl_combine_attr(char_attr,
5186 HL_ATTR(HLF_CUL));
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02005187 else if (line_attr)
5188 char_attr = hl_combine_attr(char_attr, line_attr);
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005189 }
5190# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 }
5192#endif
5193 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005194
5195#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005196 if ( wp->w_p_cole > 0
5197 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaarab62c192019-03-30 16:39:05 +01005198 conceal_cursor_line(wp))
5199 && ((syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005200 && !(lnum_in_visual_area
5201 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005202 {
5203 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005204 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005205 && (syn_get_sub_char() != NUL || match_conc
5206 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005207 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005208 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005209 /* First time at this concealed item: display one
5210 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005211 if (match_conc)
5212 c = match_conc;
5213 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005214 c = syn_get_sub_char();
5215 else if (lcs_conceal != NUL)
5216 c = lcs_conceal;
5217 else
5218 c = ' ';
5219
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005220 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005221
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005222 if (n_extra > 0)
5223 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005224 vcol += n_extra;
5225 if (wp->w_p_wrap && n_extra > 0)
5226 {
5227# ifdef FEAT_RIGHTLEFT
5228 if (wp->w_p_rl)
5229 {
5230 col -= n_extra;
5231 boguscols -= n_extra;
5232 }
5233 else
5234# endif
5235 {
5236 boguscols += n_extra;
5237 col += n_extra;
5238 }
5239 }
5240 n_extra = 0;
5241 n_attr = 0;
5242 }
5243 else if (n_skip == 0)
5244 {
5245 is_concealing = TRUE;
5246 n_skip = 1;
5247 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005248 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005249 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005250 {
5251 mb_utf8 = TRUE;
5252 u8cc[0] = 0;
5253 c = 0xc0;
5254 }
5255 else
5256 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005257 }
5258 else
5259 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005260 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005261 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005262 }
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02005263
5264 if (n_skip > 0 && did_decrement_ptr)
5265 // not showing the '>', put pointer back to avoid getting stuck
5266 ++ptr;
5267
5268#endif // FEAT_CONCEAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 }
5270
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005271#ifdef FEAT_CONCEAL
5272 /* In the cursor line and we may be concealing characters: correct
5273 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005274 if (!did_wcol && draw_state == WL_LINE
5275 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005276 && conceal_cursor_line(wp)
5277 && (int)wp->w_virtcol <= vcol + n_skip)
5278 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005279# ifdef FEAT_RIGHTLEFT
5280 if (wp->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02005281 wp->w_wcol = wp->w_width - col + boguscols - 1;
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005282 else
5283# endif
5284 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005285 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005286 did_wcol = TRUE;
5287 }
5288#endif
5289
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 /* Don't override visual selection highlighting. */
5291 if (n_attr > 0
5292 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005293 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 char_attr = extra_attr;
5295
Bram Moolenaar81695252004-12-29 20:58:21 +00005296#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 /* XIM don't send preedit_start and preedit_end, but they send
5298 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5299 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005300 if (p_imst == IM_ON_THE_SPOT
5301 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005302 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 && (State & INSERT)
5304 && !p_imdisable
5305 && im_is_preediting()
5306 && draw_state == WL_LINE)
5307 {
5308 colnr_T tcol;
5309
5310 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005311 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 else
5313 tcol = preedit_end_col;
5314 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5315 {
5316 if (feedback_old_attr < 0)
5317 {
5318 feedback_col = 0;
5319 feedback_old_attr = char_attr;
5320 }
5321 char_attr = im_get_feedback_attr(feedback_col);
5322 if (char_attr < 0)
5323 char_attr = feedback_old_attr;
5324 feedback_col++;
5325 }
5326 else if (feedback_old_attr >= 0)
5327 {
5328 char_attr = feedback_old_attr;
5329 feedback_old_attr = -1;
5330 feedback_col = 0;
5331 }
5332 }
5333#endif
5334 /*
5335 * Handle the case where we are in column 0 but not on the first
5336 * character of the line and the user wants us to show us a
5337 * special character (via 'listchars' option "precedes:<char>".
5338 */
5339 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005340 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5342#ifdef FEAT_DIFF
5343 && filler_todo <= 0
5344#endif
5345 && draw_state > WL_NR
5346 && c != NUL)
5347 {
5348 c = lcs_prec;
5349 lcs_prec_todo = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005350 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5351 {
5352 /* Double-width character being overwritten by the "precedes"
5353 * character, need to fill up half the character. */
5354 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005355 c_final = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005356 n_extra = 1;
5357 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005358 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005361 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 {
5363 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005364 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005365 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 }
5367 else
5368 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005369 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 {
5371 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005372 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 n_attr3 = 1;
5374 }
5375 }
5376
5377 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005378 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 */
Bram Moolenaarf914a332019-07-20 15:09:56 +02005380 if ((c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005381#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005382 || did_line_attr == 1
5383#endif
Bram Moolenaarf914a332019-07-20 15:09:56 +02005384 ) && eol_hl_off == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005386#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarbbca7732019-07-24 18:13:16 +02005387 // flag to indicate whether prevcol equals startcol of search_hl or
5388 // one of the matches
5389 int prevcol_hl_flag = get_prevcol_hl_flag(wp, &search_hl,
5390 (long)(ptr - line) - (c == NUL));
Bram Moolenaar91170f82006-05-05 21:15:17 +00005391#endif
Bram Moolenaarbbca7732019-07-24 18:13:16 +02005392 // Invert at least one char, used for Visual and empty line or
5393 // highlight match at end of line. If it's beyond the last
5394 // char on the screen, just overwrite that one (tricky!) Not
5395 // needed when a '$' was displayed for 'list'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005397 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005398 && (VIsual_mode != Ctrl_V
5399 || lnum == VIsual.lnum
5400 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005401 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarbbca7732019-07-24 18:13:16 +02005403 // highlight 'hlsearch' match at end of line
5404 || (prevcol_hl_flag
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005405# ifdef FEAT_SYN_HL
5406 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
5407 && !(wp == curwin && VIsual_active))
5408# endif
5409# ifdef FEAT_DIFF
5410 && diff_hlf == (hlf_T)0
5411# endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005412# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005413 && did_line_attr <= 1
5414# endif
5415 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416#endif
5417 ))
5418 {
5419 int n = 0;
5420
5421#ifdef FEAT_RIGHTLEFT
5422 if (wp->w_p_rl)
5423 {
5424 if (col < 0)
5425 n = 1;
5426 }
5427 else
5428#endif
5429 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005430 if (col >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 n = -1;
5432 }
5433 if (n != 0)
5434 {
5435 /* At the window boundary, highlight the last character
5436 * instead (better than nothing). */
5437 off += n;
5438 col += n;
5439 }
5440 else
5441 {
5442 /* Add a blank character to highlight. */
5443 ScreenLines[off] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 if (enc_utf8)
5445 ScreenLinesUC[off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 }
5447#ifdef FEAT_SEARCH_EXTRA
5448 if (area_attr == 0)
5449 {
Bram Moolenaarbbca7732019-07-24 18:13:16 +02005450 // Use attributes from match with highest priority among
5451 // 'search_hl' and the match list.
5452 get_search_match_hl(wp, &search_hl,
5453 (long)(ptr - line), &char_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454 }
5455#endif
5456 ScreenAttrs[off] = char_attr;
5457#ifdef FEAT_RIGHTLEFT
5458 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005459 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005461 --off;
5462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 else
5464#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005465 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005467 ++off;
5468 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005469 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005470 eol_hl_off = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473
Bram Moolenaar91170f82006-05-05 21:15:17 +00005474 /*
5475 * At end of the text line.
5476 */
5477 if (c == NUL)
5478 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005479#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005480 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005481 if (wp->w_p_wrap)
5482 v = wp->w_skipcol;
5483 else
5484 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005485
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005486 /* check if line ends before left margin */
5487 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005488 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005489#ifdef FEAT_CONCEAL
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01005490 // Get rid of the boguscols now, we want to draw until the right
5491 // edge for 'cursorcolumn'.
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005492 col -= boguscols;
5493 boguscols = 0;
5494#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005495
5496 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005497 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005498
5499 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005500 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5501 && (int)wp->w_virtcol <
Bram Moolenaar02631462017-09-22 15:20:32 +02005502 wp->w_width * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005503 && lnum != wp->w_cursor.lnum)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005504 || draw_color_col
5505 || win_attr != 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005506# ifdef FEAT_RIGHTLEFT
5507 && !wp->w_p_rl
5508# endif
5509 )
5510 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005511 int rightmost_vcol = 0;
5512 int i;
5513
5514 if (wp->w_p_cuc)
5515 rightmost_vcol = wp->w_virtcol;
5516 if (draw_color_col)
5517 /* determine rightmost colorcolumn to possibly draw */
5518 for (i = 0; color_cols[i] >= 0; ++i)
5519 if (rightmost_vcol < color_cols[i])
5520 rightmost_vcol = color_cols[i];
5521
Bram Moolenaar02631462017-09-22 15:20:32 +02005522 while (col < wp->w_width)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005523 {
5524 ScreenLines[off] = ' ';
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005525 if (enc_utf8)
5526 ScreenLinesUC[off] = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005527 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005528 if (draw_color_col)
5529 draw_color_col = advance_color_col(VCOL_HLC,
5530 &color_cols);
5531
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005532 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005533 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005534 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005535 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005536 else
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005537 ScreenAttrs[off++] = win_attr;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005538
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005539 if (VCOL_HLC >= rightmost_vcol && win_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005540 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005541
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005542 ++vcol;
5543 }
5544 }
5545#endif
5546
Bram Moolenaar53f81742017-09-22 14:35:51 +02005547 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005548 (int)wp->w_width, screen_line_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 row++;
5550
5551 /*
5552 * Update w_cline_height and w_cline_folded if the cursor line was
5553 * updated (saves a call to plines() later).
5554 */
5555 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5556 {
5557 curwin->w_cline_row = startrow;
5558 curwin->w_cline_height = row - startrow;
5559#ifdef FEAT_FOLDING
5560 curwin->w_cline_folded = FALSE;
5561#endif
5562 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5563 }
5564
5565 break;
5566 }
5567
Bram Moolenaara5c6a0b2019-05-08 20:20:46 +02005568 // Show "extends" character from 'listchars' if beyond the line end and
5569 // 'list' is set.
5570 if (lcs_ext != NUL
5571 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 && !wp->w_p_wrap
5573#ifdef FEAT_DIFF
5574 && filler_todo <= 0
5575#endif
5576 && (
5577#ifdef FEAT_RIGHTLEFT
5578 wp->w_p_rl ? col == 0 :
5579#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005580 col == wp->w_width - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005582 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5584 {
5585 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005586 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005588 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 {
5590 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005591 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005592 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 }
5594 else
5595 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 }
5597
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005598#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005599 /* advance to the next 'colorcolumn' */
5600 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005601 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005602
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005603 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005604 * highlight the cursor position itself.
5605 * Also highlight the 'colorcolumn' if it is different than
5606 * 'cursorcolumn' */
5607 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005608 if (draw_state == WL_LINE && !lnum_in_visual_area
5609 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005610 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005611 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005612 && lnum != wp->w_cursor.lnum)
5613 {
5614 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005615 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005616 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005617 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005618 {
5619 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005620 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005621 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005622 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005623#endif
5624
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 /*
5626 * Store character to be displayed.
5627 * Skip characters that are left of the screen for 'nowrap'.
5628 */
5629 vcol_prev = vcol;
5630 if (draw_state < WL_LINE || n_skip <= 0)
5631 {
5632 /*
5633 * Store the character.
5634 */
Bram Moolenaara12a1612019-01-24 16:39:02 +01005635#if defined(FEAT_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5637 {
5638 /* A double-wide character is: put first halve in left cell. */
5639 --off;
5640 --col;
5641 }
5642#endif
5643 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005645 {
5646 if ((mb_c & 0xff00) == 0x8e00)
5647 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650 else if (enc_utf8)
5651 {
5652 if (mb_utf8)
5653 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005654 int i;
5655
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005657 if ((c & 0xff) == 0)
5658 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005659 for (i = 0; i < Screen_mco; ++i)
5660 {
5661 ScreenLinesC[i][off] = u8cc[i];
5662 if (u8cc[i] == 0)
5663 break;
5664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 }
5666 else
5667 ScreenLinesUC[off] = 0;
5668 }
5669 if (multi_attr)
5670 {
5671 ScreenAttrs[off] = multi_attr;
5672 multi_attr = 0;
5673 }
5674 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 ScreenAttrs[off] = char_attr;
5676
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5678 {
5679 /* Need to fill two screen columns. */
5680 ++off;
5681 ++col;
5682 if (enc_utf8)
5683 /* UTF-8: Put a 0 in the second screen char. */
5684 ScreenLines[off] = 0;
5685 else
5686 /* DBCS: Put second byte in the second screen char. */
5687 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005688 if (draw_state > WL_NR
5689#ifdef FEAT_DIFF
5690 && filler_todo <= 0
5691#endif
5692 )
5693 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 /* When "tocol" is halfway a character, set it to the end of
5695 * the character, otherwise highlighting won't stop. */
5696 if (tocol == vcol)
5697 ++tocol;
5698#ifdef FEAT_RIGHTLEFT
5699 if (wp->w_p_rl)
5700 {
5701 /* now it's time to backup one cell */
5702 --off;
5703 --col;
5704 }
5705#endif
5706 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707#ifdef FEAT_RIGHTLEFT
5708 if (wp->w_p_rl)
5709 {
5710 --off;
5711 --col;
5712 }
5713 else
5714#endif
5715 {
5716 ++off;
5717 ++col;
5718 }
5719 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005720#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005721 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005722 {
5723 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005724 ++vcol_off;
5725 if (n_extra > 0)
5726 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005727 if (wp->w_p_wrap)
5728 {
5729 /*
5730 * Special voodoo required if 'wrap' is on.
5731 *
5732 * Advance the column indicator to force the line
5733 * drawing to wrap early. This will make the line
5734 * take up the same screen space when parts are concealed,
5735 * so that cursor line computations aren't messed up.
5736 *
5737 * To avoid the fictitious advance of 'col' causing
5738 * trailing junk to be written out of the screen line
5739 * we are building, 'boguscols' keeps track of the number
5740 * of bad columns we have advanced.
5741 */
5742 if (n_extra > 0)
5743 {
5744 vcol += n_extra;
5745# ifdef FEAT_RIGHTLEFT
5746 if (wp->w_p_rl)
5747 {
5748 col -= n_extra;
5749 boguscols -= n_extra;
5750 }
5751 else
5752# endif
5753 {
5754 col += n_extra;
5755 boguscols += n_extra;
5756 }
5757 n_extra = 0;
5758 n_attr = 0;
5759 }
5760
5761
Bram Moolenaar860cae12010-06-05 23:22:07 +02005762 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5763 {
5764 /* Need to fill two screen columns. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01005765# ifdef FEAT_RIGHTLEFT
Bram Moolenaar860cae12010-06-05 23:22:07 +02005766 if (wp->w_p_rl)
5767 {
5768 --boguscols;
5769 --col;
5770 }
5771 else
Bram Moolenaara12a1612019-01-24 16:39:02 +01005772# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02005773 {
5774 ++boguscols;
5775 ++col;
5776 }
5777 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005778
5779# ifdef FEAT_RIGHTLEFT
5780 if (wp->w_p_rl)
5781 {
5782 --boguscols;
5783 --col;
5784 }
5785 else
5786# endif
5787 {
5788 ++boguscols;
5789 ++col;
5790 }
5791 }
5792 else
5793 {
5794 if (n_extra > 0)
5795 {
5796 vcol += n_extra;
5797 n_extra = 0;
5798 n_attr = 0;
5799 }
5800 }
5801
5802 }
5803#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 else
5805 --n_skip;
5806
Bram Moolenaar64486672010-05-16 15:46:46 +02005807 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5808 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005809 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810#ifdef FEAT_DIFF
5811 && filler_todo <= 0
5812#endif
5813 )
5814 ++vcol;
5815
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005816#ifdef FEAT_SYN_HL
5817 if (vcol_save_attr >= 0)
5818 char_attr = vcol_save_attr;
5819#endif
5820
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 /* restore attributes after "predeces" in 'listchars' */
5822 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5823 char_attr = saved_attr3;
5824
5825 /* restore attributes after last 'listchars' or 'number' char */
5826 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5827 char_attr = saved_attr2;
5828
5829 /*
5830 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005831 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 */
5833 if ((
5834#ifdef FEAT_RIGHTLEFT
5835 wp->w_p_rl ? (col < 0) :
5836#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005837 (col >= wp->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 && (*ptr != NUL
5839#ifdef FEAT_DIFF
5840 || filler_todo > 0
5841#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005842 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5844 )
5845 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005846#ifdef FEAT_CONCEAL
Bram Moolenaar53f81742017-09-22 14:35:51 +02005847 screen_line(screen_row, wp->w_wincol, col - boguscols,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005848 (int)wp->w_width, screen_line_flags);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005849 boguscols = 0;
5850#else
Bram Moolenaar53f81742017-09-22 14:35:51 +02005851 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005852 (int)wp->w_width, screen_line_flags);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005853#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854 ++row;
5855 ++screen_row;
5856
5857 /* When not wrapping and finished diff lines, or when displayed
5858 * '$' and highlighting until last column, break here. */
5859 if ((!wp->w_p_wrap
5860#ifdef FEAT_DIFF
5861 && filler_todo <= 0
5862#endif
5863 ) || lcs_eol_one == -1)
5864 break;
5865
5866 /* When the window is too narrow draw all "@" lines. */
5867 if (draw_state != WL_LINE
5868#ifdef FEAT_DIFF
5869 && filler_todo <= 0
5870#endif
5871 )
5872 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01005873 win_draw_end(wp, '@', ' ', TRUE, row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875 row = endrow;
5876 }
5877
5878 /* When line got too long for screen break here. */
5879 if (row == endrow)
5880 {
5881 ++row;
5882 break;
5883 }
5884
5885 if (screen_cur_row == screen_row - 1
5886#ifdef FEAT_DIFF
5887 && filler_todo <= 0
5888#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005889 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 {
5891 /* Remember that the line wraps, used for modeless copy. */
5892 LineWraps[screen_row - 1] = TRUE;
5893
5894 /*
5895 * Special trick to make copy/paste of wrapped lines work with
5896 * xterm/screen: write an extra character beyond the end of
5897 * the line. This will work with all terminal types
5898 * (regardless of the xn,am settings).
5899 * Only do this on a fast tty.
5900 * Only do this if the cursor is on the current line
5901 * (something has been written in it).
5902 * Don't do this for the GUI.
5903 * Don't do this for double-width characters.
5904 * Don't do this for a window not at the right screen border.
5905 */
5906 if (p_tf
5907#ifdef FEAT_GUI
5908 && !gui.in_use
5909#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005911 && ((*mb_off2cells)(LineOffset[screen_row],
5912 LineOffset[screen_row] + screen_Columns)
5913 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005915 + (int)Columns - 2,
5916 LineOffset[screen_row] + screen_Columns)
Bram Moolenaara12a1612019-01-24 16:39:02 +01005917 == 2)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918 {
5919 /* First make sure we are at the end of the screen line,
5920 * then output the same character again to let the
5921 * terminal know about the wrap. If the terminal doesn't
5922 * auto-wrap, we overwrite the character. */
Bram Moolenaar02631462017-09-22 15:20:32 +02005923 if (screen_cur_col != wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924 screen_char(LineOffset[screen_row - 1]
5925 + (unsigned)Columns - 1,
5926 screen_row - 1, (int)(Columns - 1));
5927
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 /* When there is a multi-byte character, just output a
5929 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005930 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5931 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932 out_char(' ');
5933 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 out_char(ScreenLines[LineOffset[screen_row - 1]
5935 + (Columns - 1)]);
5936 /* force a redraw of the first char on the next line */
5937 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5938 screen_start(); /* don't know where cursor is now */
5939 }
5940 }
5941
5942 col = 0;
5943 off = (unsigned)(current_ScreenLine - ScreenLines);
5944#ifdef FEAT_RIGHTLEFT
5945 if (wp->w_p_rl)
5946 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005947 col = wp->w_width - 1; /* col is not used if breaking! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948 off += col;
5949 }
5950#endif
5951
5952 /* reset the drawing state for the start of a wrapped line */
5953 draw_state = WL_START;
5954 saved_n_extra = n_extra;
5955 saved_p_extra = p_extra;
5956 saved_c_extra = c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005957 saved_c_final = c_final;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958 saved_char_attr = char_attr;
5959 n_extra = 0;
5960 lcs_prec_todo = lcs_prec;
5961#ifdef FEAT_LINEBREAK
5962# ifdef FEAT_DIFF
5963 if (filler_todo <= 0)
5964# endif
5965 need_showbreak = TRUE;
5966#endif
5967#ifdef FEAT_DIFF
5968 --filler_todo;
5969 /* When the filler lines are actually below the last line of the
5970 * file, don't draw the line itself, break here. */
5971 if (filler_todo == 0 && wp->w_botfill)
5972 break;
5973#endif
5974 }
5975
5976 } /* for every character in the line */
5977
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005978#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005979 /* After an empty line check first word for capital. */
5980 if (*skipwhite(line) == NUL)
5981 {
5982 capcol_lnum = lnum + 1;
5983 cap_col = 0;
5984 }
5985#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01005986#ifdef FEAT_TEXT_PROP
5987 vim_free(text_props);
5988 vim_free(text_prop_idxs);
5989#endif
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005990
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005991 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992 return row;
5993}
5994
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005995/*
5996 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005997 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005998 */
5999 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006000comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006001{
6002 int i;
6003
6004 for (i = 0; i < Screen_mco; ++i)
6005 {
6006 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
6007 return TRUE;
6008 if (ScreenLinesC[i][off_from] == 0)
6009 break;
6010 }
6011 return FALSE;
6012}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006013
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014/*
6015 * Check whether the given character needs redrawing:
6016 * - the (first byte of the) character is different
6017 * - the attributes are different
6018 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006019 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020 */
6021 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006022char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023{
6024 if (cols > 0
6025 && ((ScreenLines[off_from] != ScreenLines[off_to]
6026 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027 || (enc_dbcs != 0
6028 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
6029 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
6030 ? ScreenLines2[off_from] != ScreenLines2[off_to]
6031 : (cols > 1 && ScreenLines[off_from + 1]
6032 != ScreenLines[off_to + 1])))
6033 || (enc_utf8
6034 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6035 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006036 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006037 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6038 && ScreenLines[off_from + 1]
Bram Moolenaara12a1612019-01-24 16:39:02 +01006039 != ScreenLines[off_to + 1])))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040 return TRUE;
6041 return FALSE;
6042}
6043
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006044#if defined(FEAT_TERMINAL) || defined(PROTO)
6045/*
6046 * Return the index in ScreenLines[] for the current screen line.
6047 */
6048 int
6049screen_get_current_line_off()
6050{
6051 return (int)(current_ScreenLine - ScreenLines);
6052}
6053#endif
6054
Bram Moolenaarc662ec92019-06-23 00:15:57 +02006055#ifdef FEAT_TEXT_PROP
6056/*
6057 * Return TRUE if this position has a higher level popup or this cell is
6058 * transparent in the current popup.
6059 */
6060 static int
6061blocked_by_popup(int row, int col)
6062{
6063 int off;
6064
6065 if (!popup_visible)
6066 return FALSE;
6067 off = row * screen_Columns + col;
6068 return popup_mask[off] > screen_zindex || popup_transparent[off];
6069}
6070#endif
6071
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072/*
6073 * Move one "cooked" screen line to the screen, but only the characters that
6074 * have actually changed. Handle insert/delete character.
6075 * "coloff" gives the first column on the screen for this line.
6076 * "endcol" gives the columns where valid characters are.
6077 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6078 * needs to be cleared, negative otherwise.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006079 * "flags" can have bits:
6080 * SLF_POPUP popup window
6081 * SLF_RIGHTLEFT rightleft window:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6083 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6084 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006085 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006086screen_line(
6087 int row,
6088 int coloff,
6089 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006090 int clear_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006091 int flags UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092{
6093 unsigned off_from;
6094 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006095 unsigned max_off_from;
6096 unsigned max_off_to;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099 int force = FALSE; /* force update rest of the line */
6100 int redraw_this /* bool: does character need redraw? */
6101#ifdef FEAT_GUI
6102 = TRUE /* For GUI when while-loop empty */
6103#endif
6104 ;
6105 int redraw_next; /* redraw_this for next character */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106 int clear_next = FALSE;
6107 int char_cells; /* 1: normal char */
6108 /* 2: occupies two display cells */
6109# define CHAR_CELLS char_cells
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006111 /* Check for illegal row and col, just in case. */
6112 if (row >= Rows)
6113 row = Rows - 1;
6114 if (endcol > Columns)
6115 endcol = Columns;
6116
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117# ifdef FEAT_CLIPBOARD
6118 clip_may_clear_selection(row, row);
6119# endif
6120
6121 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6122 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006123 max_off_from = off_from + screen_Columns;
6124 max_off_to = LineOffset[row] + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125
6126#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006127 if (flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128 {
6129 /* Clear rest first, because it's left of the text. */
6130 if (clear_width > 0)
6131 {
6132 while (col <= endcol && ScreenLines[off_to] == ' '
6133 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006134 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 {
6136 ++off_to;
6137 ++col;
6138 }
6139 if (col <= endcol)
6140 screen_fill(row, row + 1, col + coloff,
6141 endcol + coloff + 1, ' ', ' ', 0);
6142 }
6143 col = endcol + 1;
6144 off_to = LineOffset[row] + col + coloff;
6145 off_from += col;
6146 endcol = (clear_width > 0 ? clear_width : -clear_width);
6147 }
6148#endif /* FEAT_RIGHTLEFT */
6149
6150 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6151
6152 while (col < endcol)
6153 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006155 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 else
6157 char_cells = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158
6159 redraw_this = redraw_next;
6160 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6161 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6162
6163#ifdef FEAT_GUI
6164 /* If the next character was bold, then redraw the current character to
6165 * remove any pixels that might have spilt over into us. This only
6166 * happens in the GUI.
6167 */
6168 if (redraw_next && gui.in_use)
6169 {
6170 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006171 if (hl > HL_ALL)
6172 hl = syn_attr2attr(hl);
6173 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 redraw_this = TRUE;
6175 }
6176#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02006177#ifdef FEAT_TEXT_PROP
Bram Moolenaarc662ec92019-06-23 00:15:57 +02006178 if (blocked_by_popup(row, col + coloff))
Bram Moolenaar33796b32019-06-08 16:01:13 +02006179 redraw_this = FALSE;
6180#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181 if (redraw_this)
6182 {
6183 /*
6184 * Special handling when 'xs' termcap flag set (hpterm):
6185 * Attributes for characters are stored at the position where the
6186 * cursor is when writing the highlighting code. The
6187 * start-highlighting code must be written with the cursor on the
6188 * first highlighted character. The stop-highlighting code must
6189 * be written with the cursor just after the last highlighted
6190 * character.
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01006191 * Overwriting a character doesn't remove its highlighting. Need
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192 * to clear the rest of the line, and force redrawing it
6193 * completely.
6194 */
6195 if ( p_wiv
6196 && !force
6197#ifdef FEAT_GUI
6198 && !gui.in_use
6199#endif
6200 && ScreenAttrs[off_to] != 0
6201 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6202 {
6203 /*
6204 * Need to remove highlighting attributes here.
6205 */
6206 windgoto(row, col + coloff);
6207 out_str(T_CE); /* clear rest of this screen line */
6208 screen_start(); /* don't know where cursor is now */
6209 force = TRUE; /* force redraw of rest of the line */
6210 redraw_next = TRUE; /* or else next char would miss out */
6211
6212 /*
6213 * If the previous character was highlighted, need to stop
6214 * highlighting at this character.
6215 */
6216 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6217 {
6218 screen_attr = ScreenAttrs[off_to - 1];
6219 term_windgoto(row, col + coloff);
6220 screen_stop_highlight();
6221 }
6222 else
6223 screen_attr = 0; /* highlighting has stopped */
6224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 if (enc_dbcs != 0)
6226 {
6227 /* Check if overwriting a double-byte with a single-byte or
6228 * the other way around requires another character to be
6229 * redrawn. For UTF-8 this isn't needed, because comparing
6230 * ScreenLinesUC[] is sufficient. */
6231 if (char_cells == 1
6232 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006233 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 {
6235 /* Writing a single-cell character over a double-cell
6236 * character: need to redraw the next cell. */
6237 ScreenLines[off_to + 1] = 0;
6238 redraw_next = TRUE;
6239 }
6240 else if (char_cells == 2
6241 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006242 && (*mb_off2cells)(off_to, max_off_to) == 1
6243 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 {
6245 /* Writing the second half of a double-cell character over
6246 * a double-cell character: need to redraw the second
6247 * cell. */
6248 ScreenLines[off_to + 2] = 0;
6249 redraw_next = TRUE;
6250 }
6251
6252 if (enc_dbcs == DBCS_JPNU)
6253 ScreenLines2[off_to] = ScreenLines2[off_from];
6254 }
6255 /* When writing a single-width character over a double-width
6256 * character and at the end of the redrawn text, need to clear out
6257 * the right halve of the old character.
6258 * Also required when writing the right halve of a double-width
6259 * char over the left halve of an existing one. */
6260 if (has_mbyte && col + char_cells == endcol
6261 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006262 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006264 && (*mb_off2cells)(off_to, max_off_to) == 1
6265 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266 clear_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267
6268 ScreenLines[off_to] = ScreenLines[off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269 if (enc_utf8)
6270 {
6271 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6272 if (ScreenLinesUC[off_from] != 0)
6273 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006274 int i;
6275
6276 for (i = 0; i < Screen_mco; ++i)
6277 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 }
6279 }
6280 if (char_cells == 2)
6281 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282
6283#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006284 /* The bold trick makes a single column of pixels appear in the
6285 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 * character should be redrawn too. This happens for our own GUI
6287 * and for some xterms. */
6288 if (
6289# ifdef FEAT_GUI
6290 gui.in_use
6291# endif
6292# if defined(FEAT_GUI) && defined(UNIX)
6293 ||
6294# endif
6295# ifdef UNIX
6296 term_is_xterm
6297# endif
6298 )
6299 {
6300 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006301 if (hl > HL_ALL)
6302 hl = syn_attr2attr(hl);
6303 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304 redraw_next = TRUE;
6305 }
6306#endif
6307 ScreenAttrs[off_to] = ScreenAttrs[off_from];
Bram Moolenaara12a1612019-01-24 16:39:02 +01006308
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006309 /* For simplicity set the attributes of second half of a
6310 * double-wide character equal to the first half. */
6311 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006313
6314 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317 screen_char(off_to, row, col + coloff);
6318 }
6319 else if ( p_wiv
6320#ifdef FEAT_GUI
6321 && !gui.in_use
6322#endif
6323 && col + coloff > 0)
6324 {
6325 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6326 {
6327 /*
6328 * Don't output stop-highlight when moving the cursor, it will
6329 * stop the highlighting when it should continue.
6330 */
6331 screen_attr = 0;
6332 }
6333 else if (screen_attr != 0)
6334 screen_stop_highlight();
6335 }
6336
6337 off_to += CHAR_CELLS;
6338 off_from += CHAR_CELLS;
6339 col += CHAR_CELLS;
6340 }
6341
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 if (clear_next)
6343 {
6344 /* Clear the second half of a double-wide character of which the left
6345 * half was overwritten with a single-wide character. */
6346 ScreenLines[off_to] = ' ';
6347 if (enc_utf8)
6348 ScreenLinesUC[off_to] = 0;
6349 screen_char(off_to, row, col + coloff);
6350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351
6352 if (clear_width > 0
6353#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006354 && !(flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355#endif
6356 )
6357 {
6358#ifdef FEAT_GUI
6359 int startCol = col;
6360#endif
6361
6362 /* blank out the rest of the line */
6363 while (col < clear_width && ScreenLines[off_to] == ' '
6364 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006365 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366 {
6367 ++off_to;
6368 ++col;
6369 }
6370 if (col < clear_width)
6371 {
6372#ifdef FEAT_GUI
6373 /*
6374 * In the GUI, clearing the rest of the line may leave pixels
6375 * behind if the first character cleared was bold. Some bold
6376 * fonts spill over the left. In this case we redraw the previous
6377 * character too. If we didn't skip any blanks above, then we
6378 * only redraw if the character wasn't already redrawn anyway.
6379 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006380 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381 {
6382 hl = ScreenAttrs[off_to];
6383 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006384 {
6385 int prev_cells = 1;
Bram Moolenaara12a1612019-01-24 16:39:02 +01006386
Bram Moolenaar9c697322006-10-09 20:11:17 +00006387 if (enc_utf8)
6388 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6389 * that its width is 2. */
6390 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6391 else if (enc_dbcs != 0)
6392 {
6393 /* find previous character by counting from first
6394 * column and get its width. */
6395 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006396 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006397
6398 while (off < off_to)
6399 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006400 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006401 off += prev_cells;
6402 }
6403 }
6404
6405 if (enc_dbcs != 0 && prev_cells > 1)
6406 screen_char_2(off_to - prev_cells, row,
6407 col + coloff - prev_cells);
6408 else
Bram Moolenaar9c697322006-10-09 20:11:17 +00006409 screen_char(off_to - prev_cells, row,
6410 col + coloff - prev_cells);
6411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412 }
6413#endif
6414 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6415 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 off_to += clear_width - col;
6417 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418 }
6419 }
6420
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006421 if (clear_width > 0
6422#ifdef FEAT_TEXT_PROP
6423 && !(flags & SLF_POPUP) // no separator for popup window
6424#endif
6425 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006427 // For a window that has a right neighbor, draw the separator char
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006428 // right of the window contents. But not on top of a popup window.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006429 if (coloff + col < Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006431#ifdef FEAT_TEXT_PROP
Bram Moolenaarc662ec92019-06-23 00:15:57 +02006432 if (!blocked_by_popup(row, col + coloff))
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006433#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006435 int c;
6436
6437 c = fillchar_vsep(&hl);
6438 if (ScreenLines[off_to] != (schar_T)c
6439 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6440 != (c >= 0x80 ? c : 0))
6441 || ScreenAttrs[off_to] != hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006443 ScreenLines[off_to] = c;
6444 ScreenAttrs[off_to] = hl;
6445 if (enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006447 if (c >= 0x80)
6448 {
6449 ScreenLinesUC[off_to] = c;
6450 ScreenLinesC[0][off_to] = 0;
6451 }
6452 else
6453 ScreenLinesUC[off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 }
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006455 screen_char(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457 }
6458 }
6459 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 LineWraps[row] = FALSE;
6461 }
6462}
6463
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006464#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006466 * Mirror text "str" for right-left displaying.
6467 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006469 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006470rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471{
6472 char_u *p1, *p2;
6473 int t;
6474
6475 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6476 {
6477 t = *p1;
6478 *p1 = *p2;
6479 *p2 = t;
6480 }
6481}
6482#endif
6483
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484/*
6485 * mark all status lines for redraw; used after first :cd
6486 */
6487 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006488status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489{
6490 win_T *wp;
6491
Bram Moolenaar29323592016-07-24 22:04:11 +02006492 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493 if (wp->w_status_height)
6494 {
6495 wp->w_redr_status = TRUE;
6496 redraw_later(VALID);
6497 }
6498}
6499
6500/*
6501 * mark all status lines of the current buffer for redraw
6502 */
6503 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006504status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505{
6506 win_T *wp;
6507
Bram Moolenaar29323592016-07-24 22:04:11 +02006508 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6510 {
6511 wp->w_redr_status = TRUE;
6512 redraw_later(VALID);
6513 }
6514}
6515
6516/*
6517 * Redraw all status lines that need to be redrawn.
6518 */
6519 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006520redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521{
6522 win_T *wp;
6523
Bram Moolenaar29323592016-07-24 22:04:11 +02006524 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006526 win_redr_status(wp, FALSE);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006527 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006528 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530
Bram Moolenaar4033c552017-09-16 20:54:51 +02006531#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532/*
6533 * Redraw all status lines at the bottom of frame "frp".
6534 */
6535 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006536win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537{
6538 if (frp->fr_layout == FR_LEAF)
6539 frp->fr_win->w_redr_status = TRUE;
6540 else if (frp->fr_layout == FR_ROW)
6541 {
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006542 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543 win_redraw_last_status(frp);
6544 }
6545 else /* frp->fr_layout == FR_COL */
6546 {
6547 frp = frp->fr_child;
6548 while (frp->fr_next != NULL)
6549 frp = frp->fr_next;
6550 win_redraw_last_status(frp);
6551 }
6552}
6553#endif
6554
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555/*
6556 * Draw the verticap separator right of window "wp" starting with line "row".
6557 */
6558 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006559draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560{
6561 int hl;
6562 int c;
6563
6564 if (wp->w_vsep_width)
6565 {
6566 /* draw the vertical separator right of this window */
6567 c = fillchar_vsep(&hl);
6568 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6569 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6570 c, ' ', hl);
6571 }
6572}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573
6574#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006575static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576
6577/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006578 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579 */
6580 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006581status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006582{
6583 int len = 0;
6584
6585#ifdef FEAT_MENU
6586 int emenu = (xp->xp_context == EXPAND_MENUS
6587 || xp->xp_context == EXPAND_MENUNAMES);
6588
6589 /* Check for menu separators - replace with '|'. */
6590 if (emenu && menu_is_separator(s))
6591 return 1;
6592#endif
6593
6594 while (*s != NUL)
6595 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006596 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006597 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006598 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599 }
6600
6601 return len;
6602}
6603
6604/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006605 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006606 * These are backslashes used for escaping. Do show backslashes in help tags.
6607 */
6608 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006609skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006610{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006611 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006612#ifdef FEAT_MENU
6613 || ((xp->xp_context == EXPAND_MENUS
6614 || xp->xp_context == EXPAND_MENUNAMES)
6615 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6616#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006617 )
6618 {
6619#ifndef BACKSLASH_IN_FILENAME
6620 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6621 return 2;
6622#endif
6623 return 1;
6624 }
6625 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006626}
6627
6628/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629 * Show wildchar matches in the status line.
6630 * Show at least the "match" item.
6631 * We start at item 'first_match' in the list and show all matches that fit.
6632 *
6633 * If inversion is possible we use it. Else '=' characters are used.
6634 */
6635 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006636win_redr_status_matches(
6637 expand_T *xp,
6638 int num_matches,
6639 char_u **matches, /* list of matches */
6640 int match,
6641 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642{
6643#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6644 int row;
6645 char_u *buf;
6646 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006647 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 int fillchar;
6649 int attr;
6650 int i;
6651 int highlight = TRUE;
6652 char_u *selstart = NULL;
6653 int selstart_col = 0;
6654 char_u *selend = NULL;
6655 static int first_match = 0;
6656 int add_left = FALSE;
6657 char_u *s;
6658#ifdef FEAT_MENU
6659 int emenu;
6660#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662
6663 if (matches == NULL) /* interrupted completion? */
6664 return;
6665
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006666 if (has_mbyte)
Bram Moolenaar964b3742019-05-24 18:54:09 +02006667 buf = alloc(Columns * MB_MAXBYTES + 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006668 else
Bram Moolenaar964b3742019-05-24 18:54:09 +02006669 buf = alloc(Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 if (buf == NULL)
6671 return;
6672
6673 if (match == -1) /* don't show match but original text */
6674 {
6675 match = 0;
6676 highlight = FALSE;
6677 }
6678 /* count 1 for the ending ">" */
6679 clen = status_match_len(xp, L_MATCH(match)) + 3;
6680 if (match == 0)
6681 first_match = 0;
6682 else if (match < first_match)
6683 {
6684 /* jumping left, as far as we can go */
6685 first_match = match;
6686 add_left = TRUE;
6687 }
6688 else
6689 {
6690 /* check if match fits on the screen */
6691 for (i = first_match; i < match; ++i)
6692 clen += status_match_len(xp, L_MATCH(i)) + 2;
6693 if (first_match > 0)
6694 clen += 2;
6695 /* jumping right, put match at the left */
6696 if ((long)clen > Columns)
6697 {
6698 first_match = match;
6699 /* if showing the last match, we can add some on the left */
6700 clen = 2;
6701 for (i = match; i < num_matches; ++i)
6702 {
6703 clen += status_match_len(xp, L_MATCH(i)) + 2;
6704 if ((long)clen >= Columns)
6705 break;
6706 }
6707 if (i == num_matches)
6708 add_left = TRUE;
6709 }
6710 }
6711 if (add_left)
6712 while (first_match > 0)
6713 {
6714 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6715 if ((long)clen >= Columns)
6716 break;
6717 --first_match;
6718 }
6719
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006720 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721
6722 if (first_match == 0)
6723 {
6724 *buf = NUL;
6725 len = 0;
6726 }
6727 else
6728 {
6729 STRCPY(buf, "< ");
6730 len = 2;
6731 }
6732 clen = len;
6733
6734 i = first_match;
6735 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6736 {
6737 if (i == match)
6738 {
6739 selstart = buf + len;
6740 selstart_col = clen;
6741 }
6742
6743 s = L_MATCH(i);
6744 /* Check for menu separators - replace with '|' */
6745#ifdef FEAT_MENU
6746 emenu = (xp->xp_context == EXPAND_MENUS
6747 || xp->xp_context == EXPAND_MENUNAMES);
6748 if (emenu && menu_is_separator(s))
6749 {
6750 STRCPY(buf + len, transchar('|'));
6751 l = (int)STRLEN(buf + len);
6752 len += l;
6753 clen += l;
6754 }
6755 else
6756#endif
6757 for ( ; *s != NUL; ++s)
6758 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006759 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760 clen += ptr2cells(s);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006761 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 {
6763 STRNCPY(buf + len, s, l);
6764 s += l - 1;
6765 len += l;
6766 }
6767 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768 {
6769 STRCPY(buf + len, transchar_byte(*s));
6770 len += (int)STRLEN(buf + len);
6771 }
6772 }
6773 if (i == match)
6774 selend = buf + len;
6775
6776 *(buf + len++) = ' ';
6777 *(buf + len++) = ' ';
6778 clen += 2;
6779 if (++i == num_matches)
6780 break;
6781 }
6782
6783 if (i != num_matches)
6784 {
6785 *(buf + len++) = '>';
6786 ++clen;
6787 }
6788
6789 buf[len] = NUL;
6790
6791 row = cmdline_row - 1;
6792 if (row >= 0)
6793 {
6794 if (wild_menu_showing == 0)
6795 {
6796 if (msg_scrolled > 0)
6797 {
6798 /* Put the wildmenu just above the command line. If there is
6799 * no room, scroll the screen one line up. */
6800 if (cmdline_row == Rows - 1)
6801 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02006802 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 ++msg_scrolled;
6804 }
6805 else
6806 {
6807 ++cmdline_row;
6808 ++row;
6809 }
6810 wild_menu_showing = WM_SCROLLED;
6811 }
6812 else
6813 {
6814 /* Create status line if needed by setting 'laststatus' to 2.
6815 * Set 'winminheight' to zero to avoid that the window is
6816 * resized. */
6817 if (lastwin->w_status_height == 0)
6818 {
6819 save_p_ls = p_ls;
6820 save_p_wmh = p_wmh;
6821 p_ls = 2;
6822 p_wmh = 0;
6823 last_status(FALSE);
6824 }
6825 wild_menu_showing = WM_SHOWN;
6826 }
6827 }
6828
6829 screen_puts(buf, row, 0, attr);
6830 if (selstart != NULL && highlight)
6831 {
6832 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006833 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 }
6835
6836 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6837 }
6838
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 vim_free(buf);
6841}
6842#endif
6843
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844/*
6845 * Redraw the status line of window wp.
6846 *
6847 * If inversion is possible we use it. Else '=' characters are used.
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006848 * If "ignore_pum" is TRUE, also redraw statusline when the popup menu is
6849 * displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850 */
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006851 static void
Bram Moolenaar829adb72018-06-24 19:24:03 +02006852win_redr_status(win_T *wp, int ignore_pum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853{
6854 int row;
6855 char_u *p;
6856 int len;
6857 int fillchar;
6858 int attr;
6859 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006860 static int busy = FALSE;
6861
6862 /* It's possible to get here recursively when 'statusline' (indirectly)
6863 * invokes ":redrawstatus". Simply ignore the call then. */
6864 if (busy)
6865 return;
6866 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867
6868 wp->w_redr_status = FALSE;
6869 if (wp->w_status_height == 0)
6870 {
6871 /* no status line, can only be last window */
6872 redraw_cmdline = TRUE;
6873 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006874 else if (!redrawing()
6875#ifdef FEAT_INS_EXPAND
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006876 // don't update status line when popup menu is visible and may be
6877 // drawn over it, unless it will be redrawn later
6878 || (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006879#endif
6880 )
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 Moolenaarae654382019-01-17 21:09:05 +01007966#ifdef FEAT_INS_EXPAND
Bram Moolenaar33796b32019-06-08 16:01:13 +02007967 // Skip if under the popup menu.
7968 // Popup windows with zindex higher than POPUPMENU_ZINDEX go on top.
7969 if (pum_under_menu(row, col)
7970# ifdef FEAT_TEXT_PROP
7971 && screen_zindex <= POPUPMENU_ZINDEX
7972# endif
7973 )
Bram Moolenaarae654382019-01-17 21:09:05 +01007974 return;
7975#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02007976#ifdef FEAT_TEXT_PROP
Bram Moolenaarc662ec92019-06-23 00:15:57 +02007977 if (blocked_by_popup(row, col))
Bram Moolenaar33796b32019-06-08 16:01:13 +02007978 return;
7979#endif
7980
Bram Moolenaar494838a2015-02-10 19:20:37 +01007981 /* Outputting a character in the last cell on the screen may scroll the
7982 * screen up. Only do it when the "xn" termcap property is set, otherwise
7983 * mark the character invalid (update it when scrolled up). */
7984 if (*T_XN == NUL
7985 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986#ifdef FEAT_RIGHTLEFT
7987 /* account for first command-line character in rightleft mode */
7988 && !cmdmsg_rl
7989#endif
7990 )
7991 {
7992 ScreenAttrs[off] = (sattr_T)-1;
7993 return;
7994 }
7995
7996 /*
7997 * Stop highlighting first, so it's easier to move the cursor.
7998 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999 if (screen_char_attr != 0)
8000 attr = screen_char_attr;
8001 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002 attr = ScreenAttrs[off];
8003 if (screen_attr != attr)
8004 screen_stop_highlight();
8005
8006 windgoto(row, col);
8007
8008 if (screen_attr != attr)
8009 screen_start_highlight(attr);
8010
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011 if (enc_utf8 && ScreenLinesUC[off] != 0)
8012 {
8013 char_u buf[MB_MAXBYTES + 1];
8014
Bram Moolenaarcb070082016-04-02 22:14:51 +02008015 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008016 {
8017 if (*p_ambw == 'd'
Bram Moolenaara12a1612019-01-24 16:39:02 +01008018#ifdef FEAT_GUI
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008019 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008020#endif
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008021 )
8022 {
8023 /* Clear the two screen cells. If the character is actually
8024 * single width it won't change the second cell. */
8025 out_str((char_u *)" ");
8026 term_windgoto(row, col);
8027 }
8028 /* not sure where the cursor is after drawing the ambiguous width
8029 * character */
Bram Moolenaarcb070082016-04-02 22:14:51 +02008030 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008031 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02008032 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008034
8035 /* Convert the UTF-8 character to bytes and write it. */
8036 buf[utfc_char2bytes(off, buf)] = NUL;
8037 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008038 }
8039 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043 /* double-byte character in single-width cell */
8044 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8045 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046 }
8047
8048 screen_cur_col++;
8049}
8050
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051/*
8052 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8053 * on the screen at position 'row' and 'col'.
8054 * The attributes of the first byte is used for all. This is required to
8055 * output the two bytes of a double-byte character with nothing in between.
8056 */
8057 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008058screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059{
8060 /* Check for illegal values (could be wrong when screen was resized). */
8061 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8062 return;
8063
8064 /* Outputting the last character on the screen may scrollup the screen.
8065 * Don't to it! Mark the character invalid (update it when scrolled up) */
8066 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8067 {
8068 ScreenAttrs[off] = (sattr_T)-1;
8069 return;
8070 }
8071
8072 /* Output the first byte normally (positions the cursor), then write the
8073 * second byte directly. */
8074 screen_char(off, row, col);
8075 out_char(ScreenLines[off + 1]);
8076 ++screen_cur_col;
8077}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008078
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079/*
8080 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8081 * This uses the contents of ScreenLines[] and doesn't change it.
8082 */
8083 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008084screen_draw_rectangle(
8085 int row,
8086 int col,
8087 int height,
8088 int width,
8089 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090{
8091 int r, c;
8092 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008093 int max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008095 /* Can't use ScreenLines unless initialized */
8096 if (ScreenLines == NULL)
8097 return;
8098
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 if (invert)
8100 screen_char_attr = HL_INVERSE;
8101 for (r = row; r < row + height; ++r)
8102 {
8103 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008104 max_off = off + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105 for (c = col; c < col + width; ++c)
8106 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00008107 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108 {
8109 screen_char_2(off + c, r, c);
8110 ++c;
8111 }
8112 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113 {
8114 screen_char(off + c, r, c);
Bram Moolenaar367329b2007-08-30 11:53:22 +00008115 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116 ++c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117 }
8118 }
8119 }
8120 screen_char_attr = 0;
8121}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123/*
8124 * Redraw the characters for a vertically split window.
8125 */
8126 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008127redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128{
8129 int col;
8130 int width;
8131
8132# ifdef FEAT_CLIPBOARD
8133 clip_may_clear_selection(row, end - 1);
8134# endif
8135
8136 if (wp == NULL)
8137 {
8138 col = 0;
8139 width = Columns;
8140 }
8141 else
8142 {
8143 col = wp->w_wincol;
8144 width = wp->w_width;
8145 }
8146 screen_draw_rectangle(row, col, end - row, width, FALSE);
8147}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008149 static void
8150space_to_screenline(int off, int attr)
8151{
8152 ScreenLines[off] = ' ';
8153 ScreenAttrs[off] = attr;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008154 if (enc_utf8)
8155 ScreenLinesUC[off] = 0;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008156}
8157
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158/*
8159 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8160 * with character 'c1' in first column followed by 'c2' in the other columns.
8161 * Use attributes 'attr'.
8162 */
8163 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008164screen_fill(
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008165 int start_row,
8166 int end_row,
8167 int start_col,
8168 int end_col,
8169 int c1,
8170 int c2,
8171 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008173 int row;
8174 int col;
8175 int off;
8176 int end_off;
8177 int did_delete;
8178 int c;
8179 int norm_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008181 int force_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182#endif
8183
8184 if (end_row > screen_Rows) /* safety check */
8185 end_row = screen_Rows;
8186 if (end_col > screen_Columns) /* safety check */
8187 end_col = screen_Columns;
8188 if (ScreenLines == NULL
8189 || start_row >= end_row
8190 || start_col >= end_col) /* nothing to do */
8191 return;
8192
8193 /* it's a "normal" terminal when not in a GUI or cterm */
8194 norm_term = (
8195#ifdef FEAT_GUI
8196 !gui.in_use &&
8197#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008198 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 for (row = start_row; row < end_row; ++row)
8200 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008201 if (has_mbyte
Bram Moolenaara12a1612019-01-24 16:39:02 +01008202#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00008203 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008204#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00008205 )
8206 {
8207 /* When drawing over the right halve of a double-wide char clear
8208 * out the left halve. When drawing over the left halve of a
8209 * double wide-char clear out the right halve. Only needed in a
8210 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008211 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008212 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008213 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008214 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 /*
8217 * Try to use delete-line termcap code, when no attributes or in a
8218 * "normal" terminal, where a bold/italic space is just a
8219 * space.
8220 */
8221 did_delete = FALSE;
8222 if (c2 == ' '
8223 && end_col == Columns
8224 && can_clear(T_CE)
8225 && (attr == 0
8226 || (norm_term
8227 && attr <= HL_ALL
8228 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8229 {
8230 /*
8231 * check if we really need to clear something
8232 */
8233 col = start_col;
8234 if (c1 != ' ') /* don't clear first char */
8235 ++col;
8236
8237 off = LineOffset[row] + col;
8238 end_off = LineOffset[row] + end_col;
8239
8240 /* skip blanks (used often, keep it fast!) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241 if (enc_utf8)
8242 while (off < end_off && ScreenLines[off] == ' '
8243 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8244 ++off;
8245 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 while (off < end_off && ScreenLines[off] == ' '
8247 && ScreenAttrs[off] == 0)
8248 ++off;
8249 if (off < end_off) /* something to be cleared */
8250 {
8251 col = off - LineOffset[row];
8252 screen_stop_highlight();
8253 term_windgoto(row, col);/* clear rest of this screen line */
8254 out_str(T_CE);
8255 screen_start(); /* don't know where cursor is now */
8256 col = end_col - col;
8257 while (col--) /* clear chars in ScreenLines */
8258 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008259 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260 ++off;
8261 }
8262 }
8263 did_delete = TRUE; /* the chars are cleared now */
8264 }
8265
8266 off = LineOffset[row] + start_col;
8267 c = c1;
8268 for (col = start_col; col < end_col; ++col)
8269 {
Bram Moolenaar33796b32019-06-08 16:01:13 +02008270 if ((ScreenLines[off] != c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008271 || (enc_utf8 && (int)ScreenLinesUC[off]
8272 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273 || ScreenAttrs[off] != attr
8274#if defined(FEAT_GUI) || defined(UNIX)
8275 || force_next
8276#endif
8277 )
Bram Moolenaar33796b32019-06-08 16:01:13 +02008278#ifdef FEAT_TEXT_PROP
8279 // Skip if under a(nother) popup.
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008280 && !blocked_by_popup(row, col)
Bram Moolenaar33796b32019-06-08 16:01:13 +02008281#endif
8282 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283 {
8284#if defined(FEAT_GUI) || defined(UNIX)
8285 /* The bold trick may make a single row of pixels appear in
8286 * the next character. When a bold character is removed, the
8287 * next character should be redrawn too. This happens for our
8288 * own GUI and for some xterms. */
8289 if (
8290# ifdef FEAT_GUI
8291 gui.in_use
8292# endif
8293# if defined(FEAT_GUI) && defined(UNIX)
8294 ||
8295# endif
8296# ifdef UNIX
8297 term_is_xterm
8298# endif
8299 )
8300 {
8301 if (ScreenLines[off] != ' '
8302 && (ScreenAttrs[off] > HL_ALL
8303 || ScreenAttrs[off] & HL_BOLD))
8304 force_next = TRUE;
8305 else
8306 force_next = FALSE;
8307 }
8308#endif
8309 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310 if (enc_utf8)
8311 {
8312 if (c >= 0x80)
8313 {
8314 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008315 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316 }
8317 else
8318 ScreenLinesUC[off] = 0;
8319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320 ScreenAttrs[off] = attr;
8321 if (!did_delete || c != ' ')
8322 screen_char(off, row, col);
8323 }
8324 ++off;
8325 if (col == start_col)
8326 {
8327 if (did_delete)
8328 break;
8329 c = c2;
8330 }
8331 }
8332 if (end_col == Columns)
8333 LineWraps[row] = FALSE;
8334 if (row == Rows - 1) /* overwritten the command line */
8335 {
8336 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02008337 if (start_col == 0 && end_col == Columns
8338 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008340 if (start_col == 0)
8341 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342 }
8343 }
8344}
8345
8346/*
8347 * Check if there should be a delay. Used before clearing or redrawing the
8348 * screen or the command line.
8349 */
8350 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008351check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352{
8353 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8354 && !did_wait_return
8355 && emsg_silent == 0)
8356 {
8357 out_flush();
8358 ui_delay(1000L, TRUE);
8359 emsg_on_display = FALSE;
8360 if (check_msg_scroll)
8361 msg_scroll = FALSE;
8362 }
8363}
8364
8365/*
Bram Moolenaarca57ab52019-04-13 14:53:16 +02008366 * Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect.
8367 */
8368 static void
8369clear_TabPageIdxs(void)
8370{
8371 int scol;
8372
8373 for (scol = 0; scol < Columns; ++scol)
8374 TabPageIdxs[scol] = 0;
8375}
8376
8377/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008379 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380 * Returns TRUE if there is a valid screen to write to.
8381 * Returns FALSE when starting up and screen not initialized yet.
8382 */
8383 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008384screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008386 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387 return (ScreenLines != NULL);
8388}
8389
8390/*
8391 * Resize the shell to Rows and Columns.
8392 * Allocate ScreenLines[] and associated items.
8393 *
8394 * There may be some time between setting Rows and Columns and (re)allocating
8395 * ScreenLines[]. This happens when starting up and when (manually) changing
8396 * the shell size. Always use screen_Rows and screen_Columns to access items
8397 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8398 * final size of the shell is needed.
8399 */
8400 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008401screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402{
8403 int new_row, old_row;
8404#ifdef FEAT_GUI
8405 int old_Rows;
8406#endif
8407 win_T *wp;
8408 int outofmem = FALSE;
8409 int len;
8410 schar_T *new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008412 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008414 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008415 sattr_T *new_ScreenAttrs;
8416 unsigned *new_LineOffset;
8417 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008418 short *new_TabPageIdxs;
Bram Moolenaar33796b32019-06-08 16:01:13 +02008419#ifdef FEAT_TEXT_PROP
8420 short *new_popup_mask;
Bram Moolenaar4c063a02019-06-10 21:24:12 +02008421 short *new_popup_mask_next;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008422 char *new_popup_transparent;
Bram Moolenaar33796b32019-06-08 16:01:13 +02008423#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00008424 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008425 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008426 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008427 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008429retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430 /*
8431 * Allocation of the screen buffers is done only when the size changes and
8432 * when Rows and Columns have been set and we have started doing full
8433 * screen stuff.
8434 */
8435 if ((ScreenLines != NULL
8436 && Rows == screen_Rows
8437 && Columns == screen_Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438 && enc_utf8 == (ScreenLinesUC != NULL)
8439 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaara12a1612019-01-24 16:39:02 +01008440 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008441 || Rows == 0
8442 || Columns == 0
8443 || (!full_screen && ScreenLines == NULL))
8444 return;
8445
8446 /*
8447 * It's possible that we produce an out-of-memory message below, which
8448 * will cause this function to be called again. To break the loop, just
8449 * return here.
8450 */
8451 if (entered)
8452 return;
8453 entered = TRUE;
8454
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008455 /*
8456 * Note that the window sizes are updated before reallocating the arrays,
8457 * thus we must not redraw here!
8458 */
8459 ++RedrawingDisabled;
8460
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461 win_new_shellsize(); /* fit the windows in the new sized shell */
8462
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463 comp_col(); /* recompute columns for shown command and ruler */
8464
8465 /*
8466 * We're changing the size of the screen.
8467 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8468 * - Move lines from the old arrays into the new arrays, clear extra
8469 * lines (unless the screen is going to be cleared).
8470 * - Free the old arrays.
8471 *
8472 * If anything fails, make ScreenLines NULL, so we don't do anything!
8473 * Continuing with the old ScreenLines may result in a crash, because the
8474 * size is wrong.
8475 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008476 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008478 if (aucmd_win != NULL)
8479 win_free_lsize(aucmd_win);
Bram Moolenaar8caaf822019-06-01 18:11:22 +02008480#ifdef FEAT_TEXT_PROP
8481 // global popup windows
8482 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
8483 win_free_lsize(wp);
8484 // tab-local popup windows
8485 FOR_ALL_TABPAGES(tp)
8486 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
8487 win_free_lsize(wp);
8488#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008490 new_ScreenLines = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
Bram Moolenaar216b7102010-03-23 13:56:59 +01008491 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492 if (enc_utf8)
8493 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008494 new_ScreenLinesUC = LALLOC_MULT(u8char_T, (Rows + 1) * Columns);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008495 for (i = 0; i < p_mco; ++i)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008496 new_ScreenLinesC[i] = LALLOC_CLEAR_MULT(u8char_T,
8497 (Rows + 1) * Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498 }
8499 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008500 new_ScreenLines2 = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
8501 new_ScreenAttrs = LALLOC_MULT(sattr_T, (Rows + 1) * Columns);
8502 new_LineOffset = LALLOC_MULT(unsigned, Rows);
8503 new_LineWraps = LALLOC_MULT(char_u, Rows);
8504 new_TabPageIdxs = LALLOC_MULT(short, Columns);
Bram Moolenaar33796b32019-06-08 16:01:13 +02008505#ifdef FEAT_TEXT_PROP
8506 new_popup_mask = LALLOC_MULT(short, Rows * Columns);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02008507 new_popup_mask_next = LALLOC_MULT(short, Rows * Columns);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008508 new_popup_transparent = LALLOC_MULT(char, Rows * Columns);
Bram Moolenaar33796b32019-06-08 16:01:13 +02008509#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008511 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512 {
8513 if (win_alloc_lines(wp) == FAIL)
8514 {
8515 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008516 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 }
8518 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008519 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8520 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008521 outofmem = TRUE;
Bram Moolenaar8caaf822019-06-01 18:11:22 +02008522#ifdef FEAT_TEXT_PROP
8523 // global popup windows
8524 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
8525 if (win_alloc_lines(wp) == FAIL)
8526 {
8527 outofmem = TRUE;
8528 goto give_up;
8529 }
8530 // tab-local popup windows
8531 FOR_ALL_TABPAGES(tp)
8532 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
8533 if (win_alloc_lines(wp) == FAIL)
8534 {
8535 outofmem = TRUE;
8536 goto give_up;
8537 }
8538#endif
8539
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008540give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008542 for (i = 0; i < p_mco; ++i)
8543 if (new_ScreenLinesC[i] == NULL)
8544 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545 if (new_ScreenLines == NULL
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008546 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548 || new_ScreenAttrs == NULL
8549 || new_LineOffset == NULL
8550 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008551 || new_TabPageIdxs == NULL
Bram Moolenaar33796b32019-06-08 16:01:13 +02008552#ifdef FEAT_TEXT_PROP
8553 || new_popup_mask == NULL
Bram Moolenaar4c063a02019-06-10 21:24:12 +02008554 || new_popup_mask_next == NULL
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008555 || new_popup_transparent == NULL
Bram Moolenaar33796b32019-06-08 16:01:13 +02008556#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557 || outofmem)
8558 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008559 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008560 {
8561 /* guess the size */
8562 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8563
8564 /* Remember we did this to avoid getting outofmem messages over
8565 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008566 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008567 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01008568 VIM_CLEAR(new_ScreenLines);
Bram Moolenaard23a8232018-02-10 18:45:26 +01008569 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008570 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01008571 VIM_CLEAR(new_ScreenLinesC[i]);
8572 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaard23a8232018-02-10 18:45:26 +01008573 VIM_CLEAR(new_ScreenAttrs);
8574 VIM_CLEAR(new_LineOffset);
8575 VIM_CLEAR(new_LineWraps);
8576 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar33796b32019-06-08 16:01:13 +02008577#ifdef FEAT_TEXT_PROP
8578 VIM_CLEAR(new_popup_mask);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02008579 VIM_CLEAR(new_popup_mask_next);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008580 VIM_CLEAR(new_popup_transparent);
Bram Moolenaar33796b32019-06-08 16:01:13 +02008581#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582 }
8583 else
8584 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008585 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008586
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587 for (new_row = 0; new_row < Rows; ++new_row)
8588 {
8589 new_LineOffset[new_row] = new_row * Columns;
8590 new_LineWraps[new_row] = FALSE;
8591
8592 /*
8593 * If the screen is not going to be cleared, copy as much as
8594 * possible from the old screen to the new one and clear the rest
8595 * (used when resizing the window at the "--more--" prompt or when
8596 * executing an external command, for the GUI).
8597 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008598 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599 {
8600 (void)vim_memset(new_ScreenLines + new_row * Columns,
8601 ' ', (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602 if (enc_utf8)
8603 {
8604 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8605 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008606 for (i = 0; i < p_mco; ++i)
8607 (void)vim_memset(new_ScreenLinesC[i]
8608 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609 0, (size_t)Columns * sizeof(u8char_T));
8610 }
8611 if (enc_dbcs == DBCS_JPNU)
8612 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8613 0, (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8615 0, (size_t)Columns * sizeof(sattr_T));
8616 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008617 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618 {
8619 if (screen_Columns < Columns)
8620 len = screen_Columns;
8621 else
8622 len = Columns;
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008623 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008624 * may be invalid now. Also when p_mco changes. */
8625 if (!(enc_utf8 && ScreenLinesUC == NULL)
8626 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008627 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8628 ScreenLines + LineOffset[old_row],
8629 (size_t)len * sizeof(schar_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008630 if (enc_utf8 && ScreenLinesUC != NULL
8631 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632 {
8633 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8634 ScreenLinesUC + LineOffset[old_row],
8635 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008636 for (i = 0; i < p_mco; ++i)
8637 mch_memmove(new_ScreenLinesC[i]
8638 + new_LineOffset[new_row],
8639 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640 (size_t)len * sizeof(u8char_T));
8641 }
8642 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8643 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8644 ScreenLines2 + LineOffset[old_row],
8645 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8647 ScreenAttrs + LineOffset[old_row],
8648 (size_t)len * sizeof(sattr_T));
8649 }
8650 }
8651 }
8652 /* Use the last line of the screen for the current line. */
8653 current_ScreenLine = new_ScreenLines + Rows * Columns;
Bram Moolenaar6ace95e2019-08-13 23:09:49 +02008654
8655#ifdef FEAT_TEXT_PROP
8656 vim_memset(new_popup_mask, 0, Rows * Columns * sizeof(short));
8657 vim_memset(new_popup_transparent, 0, Rows * Columns * sizeof(char));
8658#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008659 }
8660
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008661 free_screenlines();
8662
Bram Moolenaar6ace95e2019-08-13 23:09:49 +02008663 // NOTE: this may result in all pointers to become NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008664 ScreenLines = new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008665 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008666 for (i = 0; i < p_mco; ++i)
8667 ScreenLinesC[i] = new_ScreenLinesC[i];
8668 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008669 ScreenLines2 = new_ScreenLines2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008670 ScreenAttrs = new_ScreenAttrs;
8671 LineOffset = new_LineOffset;
8672 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008673 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar33796b32019-06-08 16:01:13 +02008674#ifdef FEAT_TEXT_PROP
8675 popup_mask = new_popup_mask;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008676 popup_mask_next = new_popup_mask_next;
8677 popup_transparent = new_popup_transparent;
Bram Moolenaar33796b32019-06-08 16:01:13 +02008678 popup_mask_refresh = TRUE;
8679#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680
8681 /* It's important that screen_Rows and screen_Columns reflect the actual
8682 * size of ScreenLines[]. Set them before calling anything. */
8683#ifdef FEAT_GUI
8684 old_Rows = screen_Rows;
8685#endif
8686 screen_Rows = Rows;
8687 screen_Columns = Columns;
8688
8689 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008690 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691 screenclear2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692#ifdef FEAT_GUI
8693 else if (gui.in_use
8694 && !gui.starting
8695 && ScreenLines != NULL
8696 && old_Rows != Rows)
8697 {
8698 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8699 /*
8700 * Adjust the position of the cursor, for when executing an external
8701 * command.
8702 */
8703 if (msg_row >= Rows) /* Rows got smaller */
8704 msg_row = Rows - 1; /* put cursor at last row */
8705 else if (Rows > old_Rows) /* Rows got bigger */
8706 msg_row += Rows - old_Rows; /* put cursor in same place */
8707 if (msg_col >= Columns) /* Columns got smaller */
8708 msg_col = Columns - 1; /* put cursor at last column */
8709 }
8710#endif
Bram Moolenaarca57ab52019-04-13 14:53:16 +02008711 clear_TabPageIdxs();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712
Bram Moolenaar071d4272004-06-13 20:20:40 +00008713 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008714 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008715
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008716 /*
8717 * Do not apply autocommands more than 3 times to avoid an endless loop
8718 * in case applying autocommands always changes Rows or Columns.
8719 */
8720 if (starting == 0 && ++retry_count <= 3)
8721 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008722 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008723 /* In rare cases, autocommands may have altered Rows or Columns,
8724 * jump back to check if we need to allocate the screen again. */
8725 goto retry;
8726 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727}
8728
8729 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008730free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008731{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008732 int i;
8733
Bram Moolenaar33796b32019-06-08 16:01:13 +02008734 VIM_CLEAR(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008735 for (i = 0; i < Screen_mco; ++i)
Bram Moolenaar33796b32019-06-08 16:01:13 +02008736 VIM_CLEAR(ScreenLinesC[i]);
8737 VIM_CLEAR(ScreenLines2);
8738 VIM_CLEAR(ScreenLines);
8739 VIM_CLEAR(ScreenAttrs);
8740 VIM_CLEAR(LineOffset);
8741 VIM_CLEAR(LineWraps);
8742 VIM_CLEAR(TabPageIdxs);
8743#ifdef FEAT_TEXT_PROP
8744 VIM_CLEAR(popup_mask);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02008745 VIM_CLEAR(popup_mask_next);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008746 VIM_CLEAR(popup_transparent);
Bram Moolenaar33796b32019-06-08 16:01:13 +02008747#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008748}
8749
8750 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008751screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752{
8753 check_for_delay(FALSE);
8754 screenalloc(FALSE); /* allocate screen buffers if size changed */
8755 screenclear2(); /* clear the screen */
8756}
8757
8758 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008759screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760{
8761 int i;
8762
8763 if (starting == NO_SCREEN || ScreenLines == NULL
8764#ifdef FEAT_GUI
8765 || (gui.in_use && gui.starting)
8766#endif
8767 )
8768 return;
8769
8770#ifdef FEAT_GUI
8771 if (!gui.in_use)
8772#endif
8773 screen_attr = -1; /* force setting the Normal colors */
8774 screen_stop_highlight(); /* don't want highlighting here */
8775
8776#ifdef FEAT_CLIPBOARD
8777 /* disable selection without redrawing it */
8778 clip_scroll_selection(9999);
8779#endif
8780
8781 /* blank out ScreenLines */
8782 for (i = 0; i < Rows; ++i)
8783 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02008784 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785 LineWraps[i] = FALSE;
8786 }
8787
8788 if (can_clear(T_CL))
8789 {
8790 out_str(T_CL); /* clear the display */
8791 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008792 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793 }
8794 else
8795 {
8796 /* can't clear the screen, mark all chars with invalid attributes */
8797 for (i = 0; i < Rows; ++i)
8798 lineinvalid(LineOffset[i], (int)Columns);
8799 clear_cmdline = TRUE;
8800 }
8801
8802 screen_cleared = TRUE; /* can use contents of ScreenLines now */
8803
8804 win_rest_invalid(firstwin);
8805 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008806 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807 if (must_redraw == CLEAR) /* no need to clear again */
8808 must_redraw = NOT_VALID;
8809 compute_cmdrow();
8810 msg_row = cmdline_row; /* put cursor on last line for messages */
8811 msg_col = 0;
8812 screen_start(); /* don't know where cursor is now */
8813 msg_scrolled = 0; /* can't scroll back */
8814 msg_didany = FALSE;
8815 msg_didout = FALSE;
8816}
8817
8818/*
8819 * Clear one line in ScreenLines.
8820 */
8821 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02008822lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008823{
8824 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825 if (enc_utf8)
8826 (void)vim_memset(ScreenLinesUC + off, 0,
8827 (size_t)width * sizeof(u8char_T));
Bram Moolenaarcfce7172017-08-17 20:31:48 +02008828 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829}
8830
8831/*
8832 * Mark one line in ScreenLines invalid by setting the attributes to an
8833 * invalid value.
8834 */
8835 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008836lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837{
8838 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
8839}
8840
Bram Moolenaar071d4272004-06-13 20:20:40 +00008841/*
8842 * Copy part of a Screenline for vertically split window "wp".
8843 */
8844 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008845linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846{
8847 unsigned off_to = LineOffset[to] + wp->w_wincol;
8848 unsigned off_from = LineOffset[from] + wp->w_wincol;
8849
8850 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
8851 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852 if (enc_utf8)
8853 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008854 int i;
8855
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
8857 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008858 for (i = 0; i < p_mco; ++i)
8859 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
8860 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861 }
8862 if (enc_dbcs == DBCS_JPNU)
8863 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
8864 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
8866 wp->w_width * sizeof(sattr_T));
8867}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868
8869/*
8870 * Return TRUE if clearing with term string "p" would work.
8871 * It can't work when the string is empty or it won't set the right background.
Bram Moolenaar33796b32019-06-08 16:01:13 +02008872 * Don't clear to end-of-line when there are popups, it may cause flicker.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008873 */
8874 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008875can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876{
8877 return (*p != NUL && (t_colors <= 1
8878#ifdef FEAT_GUI
8879 || gui.in_use
8880#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008881#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008882 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02008883 || (!p_tgc && cterm_normal_bg_color == 0)
8884#else
8885 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008886#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02008887 || *T_UT != NUL)
8888#ifdef FEAT_TEXT_PROP
8889 && !(p == T_CE && popup_visible)
8890#endif
8891 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892}
8893
8894/*
8895 * Reset cursor position. Use whenever cursor was moved because of outputting
8896 * something directly to the screen (shell commands) or a terminal control
8897 * code.
8898 */
8899 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008900screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901{
8902 screen_cur_row = screen_cur_col = 9999;
8903}
8904
8905/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906 * Move the cursor to position "row","col" in the screen.
8907 * This tries to find the most efficient way to move, minimizing the number of
8908 * characters sent to the terminal.
8909 */
8910 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008911windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008913 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914 int i;
8915 int plan;
8916 int cost;
8917 int wouldbe_col;
8918 int noinvcurs;
8919 char_u *bs;
8920 int goto_cost;
8921 int attr;
8922
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008923#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
8925
8926#define PLAN_LE 1
8927#define PLAN_CR 2
8928#define PLAN_NL 3
8929#define PLAN_WRITE 4
8930 /* Can't use ScreenLines unless initialized */
8931 if (ScreenLines == NULL)
8932 return;
8933
8934 if (col != screen_cur_col || row != screen_cur_row)
8935 {
8936 /* Check for valid position. */
8937 if (row < 0) /* window without text lines? */
8938 row = 0;
8939 if (row >= screen_Rows)
8940 row = screen_Rows - 1;
8941 if (col >= screen_Columns)
8942 col = screen_Columns - 1;
8943
8944 /* check if no cursor movement is allowed in highlight mode */
8945 if (screen_attr && *T_MS == NUL)
8946 noinvcurs = HIGHL_COST;
8947 else
8948 noinvcurs = 0;
8949 goto_cost = GOTO_COST + noinvcurs;
8950
8951 /*
8952 * Plan how to do the positioning:
8953 * 1. Use CR to move it to column 0, same row.
8954 * 2. Use T_LE to move it a few columns to the left.
8955 * 3. Use NL to move a few lines down, column 0.
8956 * 4. Move a few columns to the right with T_ND or by writing chars.
8957 *
8958 * Don't do this if the cursor went beyond the last column, the cursor
8959 * position is unknown then (some terminals wrap, some don't )
8960 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008961 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962 * characters to move the cursor to the right.
8963 */
8964 if (row >= screen_cur_row && screen_cur_col < Columns)
8965 {
8966 /*
8967 * If the cursor is in the same row, bigger col, we can use CR
8968 * or T_LE.
8969 */
8970 bs = NULL; /* init for GCC */
8971 attr = screen_attr;
8972 if (row == screen_cur_row && col < screen_cur_col)
8973 {
8974 /* "le" is preferred over "bc", because "bc" is obsolete */
8975 if (*T_LE)
8976 bs = T_LE; /* "cursor left" */
8977 else
8978 bs = T_BC; /* "backspace character (old) */
8979 if (*bs)
8980 cost = (screen_cur_col - col) * (int)STRLEN(bs);
8981 else
8982 cost = 999;
8983 if (col + 1 < cost) /* using CR is less characters */
8984 {
8985 plan = PLAN_CR;
8986 wouldbe_col = 0;
8987 cost = 1; /* CR is just one character */
8988 }
8989 else
8990 {
8991 plan = PLAN_LE;
8992 wouldbe_col = col;
8993 }
8994 if (noinvcurs) /* will stop highlighting */
8995 {
8996 cost += noinvcurs;
8997 attr = 0;
8998 }
8999 }
9000
9001 /*
9002 * If the cursor is above where we want to be, we can use CR LF.
9003 */
9004 else if (row > screen_cur_row)
9005 {
9006 plan = PLAN_NL;
9007 wouldbe_col = 0;
9008 cost = (row - screen_cur_row) * 2; /* CR LF */
9009 if (noinvcurs) /* will stop highlighting */
9010 {
9011 cost += noinvcurs;
9012 attr = 0;
9013 }
9014 }
9015
9016 /*
9017 * If the cursor is in the same row, smaller col, just use write.
9018 */
9019 else
9020 {
9021 plan = PLAN_WRITE;
9022 wouldbe_col = screen_cur_col;
9023 cost = 0;
9024 }
9025
9026 /*
9027 * Check if any characters that need to be written have the
9028 * correct attributes. Also avoid UTF-8 characters.
9029 */
9030 i = col - wouldbe_col;
9031 if (i > 0)
9032 cost += i;
9033 if (cost < goto_cost && i > 0)
9034 {
9035 /*
9036 * Check if the attributes are correct without additionally
9037 * stopping highlighting.
9038 */
9039 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9040 while (i && *p++ == attr)
9041 --i;
9042 if (i != 0)
9043 {
9044 /*
9045 * Try if it works when highlighting is stopped here.
9046 */
9047 if (*--p == 0)
9048 {
9049 cost += noinvcurs;
9050 while (i && *p++ == 0)
9051 --i;
9052 }
9053 if (i != 0)
9054 cost = 999; /* different attributes, don't do it */
9055 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009056 if (enc_utf8)
9057 {
9058 /* Don't use an UTF-8 char for positioning, it's slow. */
9059 for (i = wouldbe_col; i < col; ++i)
9060 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9061 {
9062 cost = 999;
9063 break;
9064 }
9065 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009066 }
9067
9068 /*
9069 * We can do it without term_windgoto()!
9070 */
9071 if (cost < goto_cost)
9072 {
9073 if (plan == PLAN_LE)
9074 {
9075 if (noinvcurs)
9076 screen_stop_highlight();
9077 while (screen_cur_col > col)
9078 {
9079 out_str(bs);
9080 --screen_cur_col;
9081 }
9082 }
9083 else if (plan == PLAN_CR)
9084 {
9085 if (noinvcurs)
9086 screen_stop_highlight();
9087 out_char('\r');
9088 screen_cur_col = 0;
9089 }
9090 else if (plan == PLAN_NL)
9091 {
9092 if (noinvcurs)
9093 screen_stop_highlight();
9094 while (screen_cur_row < row)
9095 {
9096 out_char('\n');
9097 ++screen_cur_row;
9098 }
9099 screen_cur_col = 0;
9100 }
9101
9102 i = col - screen_cur_col;
9103 if (i > 0)
9104 {
9105 /*
9106 * Use cursor-right if it's one character only. Avoids
9107 * removing a line of pixels from the last bold char, when
9108 * using the bold trick in the GUI.
9109 */
9110 if (T_ND[0] != NUL && T_ND[1] == NUL)
9111 {
9112 while (i-- > 0)
9113 out_char(*T_ND);
9114 }
9115 else
9116 {
9117 int off;
9118
9119 off = LineOffset[row] + screen_cur_col;
9120 while (i-- > 0)
9121 {
9122 if (ScreenAttrs[off] != screen_attr)
9123 screen_stop_highlight();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009124 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009125 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009126 if (enc_dbcs == DBCS_JPNU
9127 && ScreenLines[off] == 0x8e)
9128 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009129 ++off;
9130 }
9131 }
9132 }
9133 }
9134 }
9135 else
9136 cost = 999;
9137
9138 if (cost >= goto_cost)
9139 {
9140 if (noinvcurs)
9141 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009142 if (row == screen_cur_row && (col > screen_cur_col)
9143 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009144 term_cursor_right(col - screen_cur_col);
9145 else
9146 term_windgoto(row, col);
9147 }
9148 screen_cur_row = row;
9149 screen_cur_col = col;
9150 }
9151}
9152
9153/*
9154 * Set cursor to its position in the current window.
9155 */
9156 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009157setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158{
Bram Moolenaar987723e2018-03-06 11:43:04 +01009159 setcursor_mayforce(FALSE);
9160}
9161
9162/*
9163 * Set cursor to its position in the current window.
9164 * When "force" is TRUE also when not redrawing.
9165 */
9166 void
9167setcursor_mayforce(int force)
9168{
9169 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009170 {
9171 validate_cursor();
9172 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009173 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009174#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009175 /* With 'rightleft' set and the cursor on a double-wide
9176 * character, position it on the leftmost column. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01009177 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol
9178 - ((has_mbyte
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009179 && (*mb_ptr2cells)(ml_get_cursor()) == 2
Bram Moolenaara12a1612019-01-24 16:39:02 +01009180 && vim_isprintc(gchar_cursor())) ? 2 : 1)) :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009181#endif
9182 curwin->w_wcol));
9183 }
9184}
9185
9186
9187/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009188 * Insert 'line_count' lines at 'row' in window 'wp'.
9189 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9190 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191 * scrolling.
9192 * Returns FAIL if the lines are not inserted, OK for success.
9193 */
9194 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009195win_ins_lines(
9196 win_T *wp,
9197 int row,
9198 int line_count,
9199 int invalid,
9200 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201{
9202 int did_delete;
9203 int nextrow;
9204 int lastrow;
9205 int retval;
9206
9207 if (invalid)
9208 wp->w_lines_valid = 0;
9209
9210 if (wp->w_height < 5)
9211 return FAIL;
9212
9213 if (line_count > wp->w_height - row)
9214 line_count = wp->w_height - row;
9215
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009216 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009217 if (retval != MAYBE)
9218 return retval;
9219
9220 /*
9221 * If there is a next window or a status line, we first try to delete the
9222 * lines at the bottom to avoid messing what is after the window.
Bram Moolenaarc363fe12019-08-04 18:13:46 +02009223 * If this fails and there are following windows, don't do anything to
9224 * avoid messing up those windows, better just redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009225 */
9226 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009227 if (wp->w_next != NULL || wp->w_status_height)
9228 {
9229 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009230 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009231 did_delete = TRUE;
9232 else if (wp->w_next)
9233 return FAIL;
9234 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009235 /*
9236 * if no lines deleted, blank the lines that will end up below the window
9237 */
9238 if (!did_delete)
9239 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009240 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009241 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02009242 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009243 lastrow = nextrow + line_count;
9244 if (lastrow > Rows)
9245 lastrow = Rows;
9246 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009247 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009248 ' ', ' ', 0);
9249 }
9250
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009251 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009252 == FAIL)
9253 {
Bram Moolenaarc363fe12019-08-04 18:13:46 +02009254 // deletion will have messed up other windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00009255 if (did_delete)
9256 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009257 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009258 win_rest_invalid(W_NEXT(wp));
9259 }
9260 return FAIL;
9261 }
9262
9263 return OK;
9264}
9265
9266/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009267 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009268 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9269 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9270 * scrolling
9271 * Return OK for success, FAIL if the lines are not deleted.
9272 */
9273 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009274win_del_lines(
9275 win_T *wp,
9276 int row,
9277 int line_count,
9278 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009279 int mayclear,
9280 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009281{
9282 int retval;
9283
9284 if (invalid)
9285 wp->w_lines_valid = 0;
9286
9287 if (line_count > wp->w_height - row)
9288 line_count = wp->w_height - row;
9289
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009290 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009291 if (retval != MAYBE)
9292 return retval;
9293
9294 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009295 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009296 return FAIL;
9297
Bram Moolenaar071d4272004-06-13 20:20:40 +00009298 /*
9299 * If there are windows or status lines below, try to put them at the
9300 * correct place. If we can't do that, they have to be redrawn.
9301 */
9302 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9303 {
9304 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009305 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009306 {
9307 wp->w_redr_status = TRUE;
9308 win_rest_invalid(wp->w_next);
9309 }
9310 }
9311 /*
9312 * If this is the last window and there is no status line, redraw the
9313 * command line later.
9314 */
9315 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009316 redraw_cmdline = TRUE;
9317 return OK;
9318}
9319
9320/*
9321 * Common code for win_ins_lines() and win_del_lines().
9322 * Returns OK or FAIL when the work has been done.
9323 * Returns MAYBE when not finished yet.
9324 */
9325 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009326win_do_lines(
9327 win_T *wp,
9328 int row,
9329 int line_count,
9330 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009331 int del,
9332 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009333{
9334 int retval;
9335
9336 if (!redrawing() || line_count <= 0)
9337 return FAIL;
9338
Bram Moolenaar33796b32019-06-08 16:01:13 +02009339 // When inserting lines would result in loss of command output, just redraw
9340 // the lines.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009341 if (no_win_do_lines_ins && !del)
9342 return FAIL;
9343
Bram Moolenaar33796b32019-06-08 16:01:13 +02009344 // only a few lines left: redraw is faster
Bram Moolenaar4033c552017-09-16 20:54:51 +02009345 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009346 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009347 if (!no_win_do_lines_ins)
Bram Moolenaar33796b32019-06-08 16:01:13 +02009348 screenclear(); // will set wp->w_lines_valid to 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00009349 return FAIL;
9350 }
9351
Bram Moolenaar33796b32019-06-08 16:01:13 +02009352#ifdef FEAT_TEXT_PROP
Bram Moolenaar4c063a02019-06-10 21:24:12 +02009353 // this doesn't work when there are popups visible
Bram Moolenaar33796b32019-06-08 16:01:13 +02009354 if (popup_visible)
9355 return FAIL;
9356#endif
9357
9358 // Delete all remaining lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00009359 if (row + line_count >= wp->w_height)
9360 {
9361 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009362 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009363 ' ', ' ', 0);
9364 return OK;
9365 }
9366
9367 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009368 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009370 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009371 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009372 if (!no_win_do_lines_ins)
9373 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009374
9375 /*
9376 * If the terminal can set a scroll region, use that.
9377 * Always do this in a vertically split window. This will redraw from
9378 * ScreenLines[] when t_CV isn't defined. That's faster than using
9379 * win_line().
9380 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009381 * a character in the lower right corner of the scroll region may cause a
9382 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009383 */
Bram Moolenaar02631462017-09-22 15:20:32 +02009384 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009385 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009386 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009387 scroll_region_set(wp, row);
9388 if (del)
9389 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009390 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009391 else
9392 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009393 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009394 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009395 scroll_region_reset();
9396 return retval;
9397 }
9398
Bram Moolenaar071d4272004-06-13 20:20:40 +00009399 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9400 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009401
9402 return MAYBE;
9403}
9404
9405/*
9406 * window 'wp' and everything after it is messed up, mark it for redraw
9407 */
9408 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009409win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009410{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009411 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009412 {
9413 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009414 wp->w_redr_status = TRUE;
9415 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009416 }
9417 redraw_cmdline = TRUE;
9418}
9419
9420/*
9421 * The rest of the routines in this file perform screen manipulations. The
9422 * given operation is performed physically on the screen. The corresponding
9423 * change is also made to the internal screen image. In this way, the editor
9424 * anticipates the effect of editing changes on the appearance of the screen.
9425 * That way, when we call screenupdate a complete redraw isn't usually
9426 * necessary. Another advantage is that we can keep adding code to anticipate
9427 * screen changes, and in the meantime, everything still works.
9428 */
9429
9430/*
9431 * types for inserting or deleting lines
9432 */
9433#define USE_T_CAL 1
9434#define USE_T_CDL 2
9435#define USE_T_AL 3
9436#define USE_T_CE 4
9437#define USE_T_DL 5
9438#define USE_T_SR 6
9439#define USE_NL 7
9440#define USE_T_CD 8
9441#define USE_REDRAW 9
9442
9443/*
9444 * insert lines on the screen and update ScreenLines[]
9445 * 'end' is the line after the scrolled part. Normally it is Rows.
9446 * When scrolling region used 'off' is the offset from the top for the region.
9447 * 'row' and 'end' are relative to the start of the region.
9448 *
9449 * return FAIL for failure, OK for success.
9450 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009451 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009452screen_ins_lines(
9453 int off,
9454 int row,
9455 int line_count,
9456 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009457 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01009458 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009459{
9460 int i;
9461 int j;
9462 unsigned temp;
9463 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009464 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009465 int type;
9466 int result_empty;
9467 int can_ce = can_clear(T_CE);
9468
9469 /*
9470 * FAIL if
9471 * - there is no valid screen
9472 * - the screen has to be redrawn completely
9473 * - the line count is less than one
9474 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009475 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar33796b32019-06-08 16:01:13 +02009476 * - there is a popup window
Bram Moolenaar071d4272004-06-13 20:20:40 +00009477 */
Bram Moolenaar33796b32019-06-08 16:01:13 +02009478 if (!screen_valid(TRUE)
9479 || line_count <= 0 || line_count > p_ttyscroll
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009480#ifdef FEAT_CLIPBOARD
9481 || (clip_star.state != SELECT_CLEARED
9482 && redrawing_for_callback > 0)
9483#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02009484#ifdef FEAT_TEXT_PROP
9485 || popup_visible
9486#endif
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009487 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009488 return FAIL;
9489
9490 /*
9491 * There are seven ways to insert lines:
9492 * 0. When in a vertically split window and t_CV isn't set, redraw the
9493 * characters from ScreenLines[].
9494 * 1. Use T_CD (clear to end of display) if it exists and the result of
9495 * the insert is just empty lines
9496 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9497 * present or line_count > 1. It looks better if we do all the inserts
9498 * at once.
9499 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9500 * insert is just empty lines and T_CE is not present or line_count >
9501 * 1.
9502 * 4. Use T_AL (insert line) if it exists.
9503 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9504 * just empty lines.
9505 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9506 * just empty lines.
9507 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9508 * the 'da' flag is not set or we have clear line capability.
9509 * 8. redraw the characters from ScreenLines[].
9510 *
9511 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9512 * the scrollbar for the window. It does have insert line, use that if it
9513 * exists.
9514 */
9515 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009516 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9517 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009518 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009519 type = USE_T_CD;
9520 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9521 type = USE_T_CAL;
9522 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9523 type = USE_T_CDL;
9524 else if (*T_AL != NUL)
9525 type = USE_T_AL;
9526 else if (can_ce && result_empty)
9527 type = USE_T_CE;
9528 else if (*T_DL != NUL && result_empty)
9529 type = USE_T_DL;
9530 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9531 type = USE_T_SR;
9532 else
9533 return FAIL;
9534
9535 /*
9536 * For clearing the lines screen_del_lines() is used. This will also take
9537 * care of t_db if necessary.
9538 */
9539 if (type == USE_T_CD || type == USE_T_CDL ||
9540 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009541 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009542
9543 /*
9544 * If text is retained below the screen, first clear or delete as many
9545 * lines at the bottom of the window as are about to be inserted so that
9546 * the deleted lines won't later surface during a screen_del_lines.
9547 */
9548 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009549 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009550
9551#ifdef FEAT_CLIPBOARD
9552 /* Remove a modeless selection when inserting lines halfway the screen
9553 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009554 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009555 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009556 else
9557 clip_scroll_selection(-line_count);
9558#endif
9559
Bram Moolenaar071d4272004-06-13 20:20:40 +00009560#ifdef FEAT_GUI
9561 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9562 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009563 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009564#endif
9565
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009566 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
9567 cursor_col = wp->w_wincol;
9568
Bram Moolenaar071d4272004-06-13 20:20:40 +00009569 if (*T_CCS != NUL) /* cursor relative to region */
9570 cursor_row = row;
9571 else
9572 cursor_row = row + off;
9573
9574 /*
9575 * Shift LineOffset[] line_count down to reflect the inserted lines.
9576 * Clear the inserted lines in ScreenLines[].
9577 */
9578 row += off;
9579 end += off;
9580 for (i = 0; i < line_count; ++i)
9581 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009582 if (wp != NULL && wp->w_width != Columns)
9583 {
9584 /* need to copy part of a line */
9585 j = end - 1 - i;
9586 while ((j -= line_count) >= row)
9587 linecopy(j + line_count, j, wp);
9588 j += line_count;
9589 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009590 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
9591 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009592 else
9593 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9594 LineWraps[j] = FALSE;
9595 }
9596 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009597 {
9598 j = end - 1 - i;
9599 temp = LineOffset[j];
9600 while ((j -= line_count) >= row)
9601 {
9602 LineOffset[j + line_count] = LineOffset[j];
9603 LineWraps[j + line_count] = LineWraps[j];
9604 }
9605 LineOffset[j + line_count] = temp;
9606 LineWraps[j + line_count] = FALSE;
9607 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009608 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009609 else
9610 lineinvalid(temp, (int)Columns);
9611 }
9612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613
9614 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009615 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009616 if (clear_attr != 0)
9617 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009618
Bram Moolenaar071d4272004-06-13 20:20:40 +00009619 /* redraw the characters */
9620 if (type == USE_REDRAW)
9621 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02009622 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623 {
9624 term_append_lines(line_count);
9625 screen_start(); /* don't know where cursor is now */
9626 }
9627 else
9628 {
9629 for (i = 0; i < line_count; i++)
9630 {
9631 if (type == USE_T_AL)
9632 {
9633 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009634 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009635 out_str(T_AL);
9636 }
9637 else /* type == USE_T_SR */
9638 out_str(T_SR);
9639 screen_start(); /* don't know where cursor is now */
9640 }
9641 }
9642
9643 /*
9644 * With scroll-reverse and 'da' flag set we need to clear the lines that
9645 * have been scrolled down into the region.
9646 */
9647 if (type == USE_T_SR && *T_DA)
9648 {
9649 for (i = 0; i < line_count; ++i)
9650 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009651 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009652 out_str(T_CE);
9653 screen_start(); /* don't know where cursor is now */
9654 }
9655 }
9656
9657#ifdef FEAT_GUI
9658 gui_can_update_cursor();
9659 if (gui.in_use)
9660 out_flush(); /* always flush after a scroll */
9661#endif
9662 return OK;
9663}
9664
9665/*
Bram Moolenaar107abd22016-08-12 14:08:25 +02009666 * Delete lines on the screen and update ScreenLines[].
9667 * "end" is the line after the scrolled part. Normally it is Rows.
9668 * When scrolling region used "off" is the offset from the top for the region.
9669 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009670 *
9671 * Return OK for success, FAIL if the lines are not deleted.
9672 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009673 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009674screen_del_lines(
9675 int off,
9676 int row,
9677 int line_count,
9678 int end,
9679 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009680 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +01009681 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009682{
9683 int j;
9684 int i;
9685 unsigned temp;
9686 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009687 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009688 int cursor_end;
9689 int result_empty; /* result is empty until end of region */
9690 int can_delete; /* deleting line codes can be used */
9691 int type;
9692
9693 /*
9694 * FAIL if
9695 * - there is no valid screen
9696 * - the screen has to be redrawn completely
9697 * - the line count is less than one
9698 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009699 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009700 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009701 if (!screen_valid(TRUE) || line_count <= 0
9702 || (!force && line_count > p_ttyscroll)
9703#ifdef FEAT_CLIPBOARD
9704 || (clip_star.state != SELECT_CLEARED
9705 && redrawing_for_callback > 0)
9706#endif
9707 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009708 return FAIL;
9709
9710 /*
9711 * Check if the rest of the current region will become empty.
9712 */
9713 result_empty = row + line_count >= end;
9714
9715 /*
9716 * We can delete lines only when 'db' flag not set or when 'ce' option
9717 * available.
9718 */
9719 can_delete = (*T_DB == NUL || can_clear(T_CE));
9720
9721 /*
9722 * There are six ways to delete lines:
9723 * 0. When in a vertically split window and t_CV isn't set, redraw the
9724 * characters from ScreenLines[].
9725 * 1. Use T_CD if it exists and the result is empty.
9726 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9727 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9728 * none of the other ways work.
9729 * 4. Use T_CE (erase line) if the result is empty.
9730 * 5. Use T_DL (delete line) if it exists.
9731 * 6. redraw the characters from ScreenLines[].
9732 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009733 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9734 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009735 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009736 type = USE_T_CD;
9737#if defined(__BEOS__) && defined(BEOS_DR8)
9738 /*
9739 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9740 * its internal termcap... this works okay for tests which test *T_DB !=
9741 * NUL. It has the disadvantage that the user cannot use any :set t_*
9742 * command to get T_DB (back) to empty_option, only :set term=... will do
9743 * the trick...
9744 * Anyway, this hack will hopefully go away with the next OS release.
9745 * (Olaf Seibert)
9746 */
9747 else if (row == 0 && T_DB == empty_option
9748 && (line_count == 1 || *T_CDL == NUL))
9749#else
9750 else if (row == 0 && (
9751#ifndef AMIGA
9752 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9753 * up, so use delete-line command */
9754 line_count == 1 ||
9755#endif
9756 *T_CDL == NUL))
9757#endif
9758 type = USE_NL;
9759 else if (*T_CDL != NUL && line_count > 1 && can_delete)
9760 type = USE_T_CDL;
9761 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +02009762 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763 type = USE_T_CE;
9764 else if (*T_DL != NUL && can_delete)
9765 type = USE_T_DL;
9766 else if (*T_CDL != NUL && can_delete)
9767 type = USE_T_CDL;
9768 else
9769 return FAIL;
9770
9771#ifdef FEAT_CLIPBOARD
9772 /* Remove a modeless selection when deleting lines halfway the screen or
9773 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009774 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009775 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009776 else
9777 clip_scroll_selection(line_count);
9778#endif
9779
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780#ifdef FEAT_GUI
9781 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9782 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009783 gui_dont_update_cursor(gui.cursor_row >= row + off
9784 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009785#endif
9786
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009787 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
9788 cursor_col = wp->w_wincol;
9789
Bram Moolenaar071d4272004-06-13 20:20:40 +00009790 if (*T_CCS != NUL) /* cursor relative to region */
9791 {
9792 cursor_row = row;
9793 cursor_end = end;
9794 }
9795 else
9796 {
9797 cursor_row = row + off;
9798 cursor_end = end + off;
9799 }
9800
9801 /*
9802 * Now shift LineOffset[] line_count up to reflect the deleted lines.
9803 * Clear the inserted lines in ScreenLines[].
9804 */
9805 row += off;
9806 end += off;
9807 for (i = 0; i < line_count; ++i)
9808 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009809 if (wp != NULL && wp->w_width != Columns)
9810 {
9811 /* need to copy part of a line */
9812 j = row + i;
9813 while ((j += line_count) <= end - 1)
9814 linecopy(j - line_count, j, wp);
9815 j -= line_count;
9816 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009817 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
9818 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009819 else
9820 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9821 LineWraps[j] = FALSE;
9822 }
9823 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009824 {
9825 /* whole width, moving the line pointers is faster */
9826 j = row + i;
9827 temp = LineOffset[j];
9828 while ((j += line_count) <= end - 1)
9829 {
9830 LineOffset[j - line_count] = LineOffset[j];
9831 LineWraps[j - line_count] = LineWraps[j];
9832 }
9833 LineOffset[j - line_count] = temp;
9834 LineWraps[j - line_count] = FALSE;
9835 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009836 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009837 else
9838 lineinvalid(temp, (int)Columns);
9839 }
9840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009841
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009842 if (screen_attr != clear_attr)
9843 screen_stop_highlight();
9844 if (clear_attr != 0)
9845 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009846
Bram Moolenaar071d4272004-06-13 20:20:40 +00009847 /* redraw the characters */
9848 if (type == USE_REDRAW)
9849 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02009850 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009851 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009852 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009853 out_str(T_CD);
9854 screen_start(); /* don't know where cursor is now */
9855 }
9856 else if (type == USE_T_CDL)
9857 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009858 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009859 term_delete_lines(line_count);
9860 screen_start(); /* don't know where cursor is now */
9861 }
9862 /*
9863 * Deleting lines at top of the screen or scroll region: Just scroll
9864 * the whole screen (scroll region) up by outputting newlines on the
9865 * last line.
9866 */
9867 else if (type == USE_NL)
9868 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009869 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009870 for (i = line_count; --i >= 0; )
9871 out_char('\n'); /* cursor will remain on same line */
9872 }
9873 else
9874 {
9875 for (i = line_count; --i >= 0; )
9876 {
9877 if (type == USE_T_DL)
9878 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009879 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009880 out_str(T_DL); /* delete a line */
9881 }
9882 else /* type == USE_T_CE */
9883 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009884 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009885 out_str(T_CE); /* erase a line */
9886 }
9887 screen_start(); /* don't know where cursor is now */
9888 }
9889 }
9890
9891 /*
9892 * If the 'db' flag is set, we need to clear the lines that have been
9893 * scrolled up at the bottom of the region.
9894 */
9895 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
9896 {
9897 for (i = line_count; i > 0; --i)
9898 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009899 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009900 out_str(T_CE); /* erase a line */
9901 screen_start(); /* don't know where cursor is now */
9902 }
9903 }
9904
9905#ifdef FEAT_GUI
9906 gui_can_update_cursor();
9907 if (gui.in_use)
9908 out_flush(); /* always flush after a scroll */
9909#endif
9910
9911 return OK;
9912}
9913
9914/*
Bram Moolenaarcb574f42019-01-25 22:29:57 +01009915 * Return TRUE when postponing displaying the mode message: when not redrawing
9916 * or inside a mapping.
9917 */
9918 int
9919skip_showmode()
9920{
9921 // Call char_avail() only when we are going to show something, because it
9922 // takes a bit of time. redrawing() may also call char_avail_avail().
9923 if (global_busy
9924 || msg_silent != 0
9925 || !redrawing()
9926 || (char_avail() && !KeyTyped))
9927 {
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02009928 redraw_mode = TRUE; // show mode later
Bram Moolenaarcb574f42019-01-25 22:29:57 +01009929 return TRUE;
9930 }
9931 return FALSE;
9932}
9933
9934/*
Bram Moolenaar81226e02018-02-20 21:44:45 +01009935 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009936 *
9937 * If clear_cmdline is TRUE, clear the rest of the cmdline.
9938 * If clear_cmdline is FALSE there may be a message there that needs to be
9939 * cleared only if a mode is shown.
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02009940 * If redraw_mode is TRUE show or clear the mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009941 * Return the length of the message (0 if no message).
9942 */
9943 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009944showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009945{
9946 int need_clear;
9947 int length = 0;
9948 int do_mode;
9949 int attr;
9950 int nwr_save;
9951#ifdef FEAT_INS_EXPAND
9952 int sub_attr;
9953#endif
9954
Bram Moolenaar7df351e2006-01-23 22:30:28 +00009955 do_mode = ((p_smd && msg_silent == 0)
9956 && ((State & INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +02009957 || restart_edit != NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009958 || VIsual_active));
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009959 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009960 {
Bram Moolenaarcb574f42019-01-25 22:29:57 +01009961 if (skip_showmode())
9962 return 0; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00009963
9964 nwr_save = need_wait_return;
9965
9966 /* wait a bit before overwriting an important message */
9967 check_for_delay(FALSE);
9968
9969 /* if the cmdline is more than one line high, erase top lines */
9970 need_clear = clear_cmdline;
9971 if (clear_cmdline && cmdline_row < Rows - 1)
9972 msg_clr_cmdline(); /* will reset clear_cmdline */
9973
9974 /* Position on the last line in the window, column 0 */
9975 msg_pos_mode();
9976 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +01009977 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009978 if (do_mode)
9979 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01009980 msg_puts_attr("--", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009981#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00009982 if (
Bram Moolenaar09092152010-08-08 16:38:42 +02009983# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +00009984 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +02009985# else
Bram Moolenaarc236c162008-07-13 17:41:49 +00009986 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +00009987# endif
Bram Moolenaar09092152010-08-08 16:38:42 +02009988 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02009989# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar32526b32019-01-19 17:43:09 +01009990 msg_puts_attr(" IM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009991# else
Bram Moolenaar32526b32019-01-19 17:43:09 +01009992 msg_puts_attr(" XIM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009993# endif
9994#endif
9995#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
9996 if (gui.in_use)
9997 {
9998 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +01009999 {
10000 /* HANGUL */
10001 if (enc_utf8)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010002 msg_puts_attr(" \355\225\234\352\270\200", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010003 else
Bram Moolenaar32526b32019-01-19 17:43:09 +010010004 msg_puts_attr(" \307\321\261\333", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010005 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010006 }
10007#endif
10008#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010009 /* CTRL-X in Insert mode */
10010 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010011 {
10012 /* These messages can get long, avoid a wrap in a narrow
10013 * window. Prefer showing edit_submode_extra. */
10014 length = (Rows - msg_row) * Columns - 3;
10015 if (edit_submode_extra != NULL)
10016 length -= vim_strsize(edit_submode_extra);
10017 if (length > 0)
10018 {
10019 if (edit_submode_pre != NULL)
10020 length -= vim_strsize(edit_submode_pre);
10021 if (length - vim_strsize(edit_submode) > 0)
10022 {
10023 if (edit_submode_pre != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010024 msg_puts_attr((char *)edit_submode_pre, attr);
10025 msg_puts_attr((char *)edit_submode, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010026 }
10027 if (edit_submode_extra != NULL)
10028 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010029 msg_puts_attr(" ", attr); /* add a space in between */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010030 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010031 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010032 else
10033 sub_attr = attr;
Bram Moolenaar32526b32019-01-19 17:43:09 +010010034 msg_puts_attr((char *)edit_submode_extra, sub_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010035 }
10036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010037 }
10038 else
10039#endif
10040 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010041 if (State & VREPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010042 msg_puts_attr(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010043 else if (State & REPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010044 msg_puts_attr(_(" REPLACE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010045 else if (State & INSERT)
10046 {
10047#ifdef FEAT_RIGHTLEFT
10048 if (p_ri)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010049 msg_puts_attr(_(" REVERSE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010050#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +010010051 msg_puts_attr(_(" INSERT"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010052 }
Bram Moolenaar942b4542018-06-17 16:23:34 +020010053 else if (restart_edit == 'I' || restart_edit == 'A')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010054 msg_puts_attr(_(" (insert)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010055 else if (restart_edit == 'R')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010056 msg_puts_attr(_(" (replace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057 else if (restart_edit == 'V')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010058 msg_puts_attr(_(" (vreplace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010059#ifdef FEAT_RIGHTLEFT
10060 if (p_hkmap)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010061 msg_puts_attr(_(" Hebrew"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010062#endif
10063#ifdef FEAT_KEYMAP
10064 if (State & LANGMAP)
10065 {
10066# ifdef FEAT_ARABIC
10067 if (curwin->w_p_arab)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010068 msg_puts_attr(_(" Arabic"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010069 else
10070# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010071 if (get_keymap_str(curwin, (char_u *)" (%s)",
10072 NameBuff, MAXPATHL))
Bram Moolenaar32526b32019-01-19 17:43:09 +010010073 msg_puts_attr((char *)NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010074 }
10075#endif
10076 if ((State & INSERT) && p_paste)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010077 msg_puts_attr(_(" (paste)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010078
Bram Moolenaar071d4272004-06-13 20:20:40 +000010079 if (VIsual_active)
10080 {
10081 char *p;
10082
10083 /* Don't concatenate separate words to avoid translation
10084 * problems. */
10085 switch ((VIsual_select ? 4 : 0)
10086 + (VIsual_mode == Ctrl_V) * 2
10087 + (VIsual_mode == 'V'))
10088 {
10089 case 0: p = N_(" VISUAL"); break;
10090 case 1: p = N_(" VISUAL LINE"); break;
10091 case 2: p = N_(" VISUAL BLOCK"); break;
10092 case 4: p = N_(" SELECT"); break;
10093 case 5: p = N_(" SELECT LINE"); break;
10094 default: p = N_(" SELECT BLOCK"); break;
10095 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010096 msg_puts_attr(_(p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010097 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010098 msg_puts_attr(" --", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010099 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010100
Bram Moolenaar071d4272004-06-13 20:20:40 +000010101 need_clear = TRUE;
10102 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010103 if (reg_recording != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010104#ifdef FEAT_INS_EXPAND
10105 && edit_submode == NULL /* otherwise it gets too long */
10106#endif
10107 )
10108 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010109 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010110 need_clear = TRUE;
10111 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010112
10113 mode_displayed = TRUE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010114 if (need_clear || clear_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010115 msg_clr_eos();
10116 msg_didout = FALSE; /* overwrite this message */
10117 length = msg_col;
10118 msg_col = 0;
10119 need_wait_return = nwr_save; /* never ask for hit-return for this */
10120 }
10121 else if (clear_cmdline && msg_silent == 0)
10122 /* Clear the whole command line. Will reset "clear_cmdline". */
10123 msg_clr_cmdline();
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010124 else if (redraw_mode)
10125 {
10126 msg_pos_mode();
10127 msg_clr_eos();
10128 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010129
10130#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010131 /* In Visual mode the size of the selected area must be redrawn. */
10132 if (VIsual_active)
10133 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010134
10135 /* If the last window has no status line, the ruler is after the mode
10136 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010137 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +020010138 win_redr_ruler(lastwin, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010139#endif
10140 redraw_cmdline = FALSE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010141 redraw_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010142 clear_cmdline = FALSE;
10143
10144 return length;
10145}
10146
10147/*
10148 * Position for a mode message.
10149 */
10150 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010151msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010152{
10153 msg_col = 0;
10154 msg_row = Rows - 1;
10155}
10156
10157/*
10158 * Delete mode message. Used when ESC is typed which is expected to end
10159 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010160 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010161 */
10162 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010163unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010164{
10165 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010166 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010167 */
10168 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10169 redraw_cmdline = TRUE; /* delete mode later */
10170 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010171 clearmode();
10172}
10173
10174/*
10175 * Clear the mode message.
10176 */
10177 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010178clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010179{
Bram Moolenaar2abad542018-05-19 14:43:45 +020010180 int save_msg_row = msg_row;
10181 int save_msg_col = msg_col;
10182
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010183 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010184 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010185 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010186 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +020010187
10188 msg_col = save_msg_col;
10189 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010190}
10191
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010192 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010193recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010194{
Bram Moolenaar32526b32019-01-19 17:43:09 +010010195 msg_puts_attr(_("recording"), attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010196 if (!shortmess(SHM_RECORDING))
10197 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010198 char s[4];
10199
10200 sprintf(s, " @%c", reg_recording);
10201 msg_puts_attr(s, attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010202 }
10203}
10204
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010205/*
10206 * Draw the tab pages line at the top of the Vim window.
10207 */
Bram Moolenaare12bab32019-01-08 22:02:56 +010010208 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010209draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010210{
10211 int tabcount = 0;
10212 tabpage_T *tp;
10213 int tabwidth;
10214 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010215 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010216 int attr;
10217 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010218 win_T *cwp;
10219 int wincount;
10220 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010221 int c;
10222 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010223 int attr_sel = HL_ATTR(HLF_TPS);
10224 int attr_nosel = HL_ATTR(HLF_TP);
10225 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010226 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010227 int room;
10228 int use_sep_chars = (t_colors < 8
10229#ifdef FEAT_GUI
10230 && !gui.in_use
10231#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010232#ifdef FEAT_TERMGUICOLORS
10233 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010234#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010235 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010236
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010237 if (ScreenLines == NULL)
10238 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010239 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010240
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010241#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010242 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010243 if (gui_use_tabline())
10244 {
10245 gui_update_tabline();
10246 return;
10247 }
10248#endif
10249
10250 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010251 return;
10252
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010253#if defined(FEAT_STL_OPT)
Bram Moolenaarca57ab52019-04-13 14:53:16 +020010254 clear_TabPageIdxs();
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010255
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010256 /* Use the 'tabline' option if it's set. */
10257 if (*p_tal != NUL)
10258 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010259 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010260
10261 /* Check for an error. If there is one we would loop in redrawing the
10262 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010263 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010264 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010265 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010266 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010267 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010268 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010269 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010270 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010271#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010272 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010273 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010274 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010275
Bram Moolenaar238a5642006-02-21 22:12:05 +000010276 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10277 if (tabwidth < 6)
10278 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010279
Bram Moolenaar238a5642006-02-21 22:12:05 +000010280 attr = attr_nosel;
10281 tabcount = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010282 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10283 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010284 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010285 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010286
Bram Moolenaar238a5642006-02-21 22:12:05 +000010287 if (tp->tp_topframe == topframe)
10288 attr = attr_sel;
10289 if (use_sep_chars && col > 0)
10290 screen_putchar('|', 0, col++, attr);
10291
10292 if (tp->tp_topframe != topframe)
10293 attr = attr_nosel;
10294
10295 screen_putchar(' ', 0, col++, attr);
10296
10297 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010298 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010299 cwp = curwin;
10300 wp = firstwin;
10301 }
10302 else
10303 {
10304 cwp = tp->tp_curwin;
10305 wp = tp->tp_firstwin;
10306 }
10307
10308 modified = FALSE;
10309 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10310 if (bufIsChanged(wp->w_buffer))
10311 modified = TRUE;
10312 if (modified || wincount > 1)
10313 {
10314 if (wincount > 1)
10315 {
10316 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010317 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010318 if (col + len >= Columns - 3)
10319 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010320 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010321#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010322 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010323#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010324 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010325#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010326 );
10327 col += len;
10328 }
10329 if (modified)
10330 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10331 screen_putchar(' ', 0, col++, attr);
10332 }
10333
10334 room = scol - col + tabwidth - 1;
10335 if (room > 0)
10336 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010337 /* Get buffer name in NameBuff[] */
10338 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010339 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010340 len = vim_strsize(NameBuff);
10341 p = NameBuff;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010342 if (has_mbyte)
10343 while (len > room)
10344 {
10345 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010346 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010347 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010010348 else if (len > room)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010349 {
10350 p += len - room;
10351 len = room;
10352 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010353 if (len > Columns - col - 1)
10354 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010355
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010356 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010357 col += len;
10358 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010359 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010360
10361 /* Store the tab page number in TabPageIdxs[], so that
10362 * jump_to_mouse() knows where each one is. */
10363 ++tabcount;
10364 while (scol < col)
10365 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010366 }
10367
Bram Moolenaar238a5642006-02-21 22:12:05 +000010368 if (use_sep_chars)
10369 c = '_';
10370 else
10371 c = ' ';
10372 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010373
10374 /* Put an "X" for closing the current tab if there are several. */
10375 if (first_tabpage->tp_next != NULL)
10376 {
10377 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10378 TabPageIdxs[Columns - 1] = -999;
10379 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010380 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010381
10382 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10383 * set. */
10384 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010385}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010386
10387/*
10388 * Get buffer name for "buf" into NameBuff[].
10389 * Takes care of special buffer names and translates special characters.
10390 */
10391 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010392get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010393{
10394 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010395 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010396 else
10397 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10398 trans_characters(NameBuff, MAXPATHL);
10399}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010400
Bram Moolenaar071d4272004-06-13 20:20:40 +000010401/*
10402 * Get the character to use in a status line. Get its attributes in "*attr".
10403 */
10404 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010405fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010406{
10407 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010408
10409#ifdef FEAT_TERMINAL
10410 if (bt_terminal(wp->w_buffer))
10411 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010412 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010413 {
10414 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010415 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010416 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010417 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010418 {
10419 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010420 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010421 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010422 }
10423 else
10424#endif
10425 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010426 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010427 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010428 fill = fill_stl;
10429 }
10430 else
10431 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010432 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010433 fill = fill_stlnc;
10434 }
10435 /* Use fill when there is highlighting, and highlighting of current
10436 * window differs, or the fillchars differ, or this is not the
10437 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010438 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010439 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010440 || (fill_stl != fill_stlnc)))
10441 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010442 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010443 return '^';
10444 return '=';
10445}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010446
Bram Moolenaar071d4272004-06-13 20:20:40 +000010447/*
10448 * Get the character to use in a separator between vertically split windows.
10449 * Get its attributes in "*attr".
10450 */
10451 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010452fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010453{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010454 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010455 if (*attr == 0 && fill_vert == ' ')
10456 return '|';
10457 else
10458 return fill_vert;
10459}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010460
10461/*
10462 * Return TRUE if redrawing should currently be done.
10463 */
10464 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010465redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010466{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010467#ifdef FEAT_EVAL
10468 if (disable_redraw_for_testing)
10469 return 0;
10470 else
10471#endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +020010472 return ((!RedrawingDisabled
10473#ifdef FEAT_EVAL
10474 || ignore_redraw_flag_for_testing
10475#endif
10476 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010477}
10478
10479/*
10480 * Return TRUE if printing messages should currently be done.
10481 */
10482 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010483messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010484{
10485 return (!(p_lz && char_avail() && !KeyTyped));
10486}
10487
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010488#ifdef FEAT_MENU
10489/*
10490 * Draw the window toolbar.
10491 */
10492 static void
10493redraw_win_toolbar(win_T *wp)
10494{
10495 vimmenu_T *menu;
10496 int item_idx = 0;
10497 int item_count = 0;
10498 int col = 0;
10499 int next_col;
10500 int off = (int)(current_ScreenLine - ScreenLines);
10501 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
10502 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
10503
10504 vim_free(wp->w_winbar_items);
10505 for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
10506 ++item_count;
Bram Moolenaarc799fe22019-05-28 23:08:19 +020010507 wp->w_winbar_items = ALLOC_CLEAR_MULT(winbar_item_T, item_count + 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010508
10509 /* TODO: use fewer spaces if there is not enough room */
10510 for (menu = wp->w_winbar->children;
Bram Moolenaar02631462017-09-22 15:20:32 +020010511 menu != NULL && col < wp->w_width; menu = menu->next)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010512 {
10513 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010514 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010515 break;
10516 if (col > 1)
10517 {
10518 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010519 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010520 break;
10521 }
10522
10523 wp->w_winbar_items[item_idx].wb_startcol = col;
10524 space_to_screenline(off + col, button_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010525 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010526 break;
10527
10528 next_col = text_to_screenline(wp, menu->name, col);
10529 while (col < next_col)
10530 {
10531 ScreenAttrs[off + col] = button_attr;
10532 ++col;
10533 }
10534 wp->w_winbar_items[item_idx].wb_endcol = col;
10535 wp->w_winbar_items[item_idx].wb_menu = menu;
10536 ++item_idx;
10537
Bram Moolenaar02631462017-09-22 15:20:32 +020010538 if (col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010539 break;
10540 space_to_screenline(off + col, button_attr);
10541 ++col;
10542 }
Bram Moolenaar02631462017-09-22 15:20:32 +020010543 while (col < wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010544 {
10545 space_to_screenline(off + col, fill_attr);
10546 ++col;
10547 }
10548 wp->w_winbar_items[item_idx].wb_menu = NULL; /* end marker */
10549
Bram Moolenaar02631462017-09-22 15:20:32 +020010550 screen_line(wp->w_winrow, wp->w_wincol, (int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +020010551 (int)wp->w_width, 0);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010552}
10553#endif
Bram Moolenaar491ac282018-06-17 14:47:55 +020010554
Bram Moolenaar071d4272004-06-13 20:20:40 +000010555/*
10556 * Show current status info in ruler and various other places
10557 * If always is FALSE, only show ruler if position has changed.
10558 */
10559 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010560showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010561{
10562 if (!always && !redrawing())
10563 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010564#ifdef FEAT_INS_EXPAND
10565 if (pum_visible())
10566 {
10567 /* Don't redraw right now, do it later. */
10568 curwin->w_redr_status = TRUE;
10569 return;
10570 }
10571#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020010572#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010573 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar362f3562009-11-03 16:20:34 +000010574 redraw_custom_statusline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010575 else
10576#endif
10577#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +020010578 win_redr_ruler(curwin, always, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010579#endif
10580
10581#ifdef FEAT_TITLE
10582 if (need_maketitle
10583# ifdef FEAT_STL_OPT
10584 || (p_icon && (stl_syntax & STL_IN_ICON))
10585 || (p_title && (stl_syntax & STL_IN_TITLE))
10586# endif
10587 )
10588 maketitle();
10589#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010590 /* Redraw the tab pages line if needed. */
10591 if (redraw_tabline)
10592 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010593}
10594
10595#ifdef FEAT_CMDL_INFO
10596 static void
Bram Moolenaar491ac282018-06-17 14:47:55 +020010597win_redr_ruler(win_T *wp, int always, int ignore_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010598{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010599#define RULER_BUF_LEN 70
10600 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010601 int row;
10602 int fillchar;
10603 int attr;
10604 int empty_line = FALSE;
10605 colnr_T virtcol;
10606 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010607 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010608 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010609 int this_ru_col;
10610 int off = 0;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +010010611 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010612
10613 /* If 'ruler' off or redrawing disabled, don't do anything */
10614 if (!p_ru)
10615 return;
10616
10617 /*
10618 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10619 * after deleting lines, before cursor.lnum is corrected.
10620 */
10621 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10622 return;
10623
10624#ifdef FEAT_INS_EXPAND
10625 /* Don't draw the ruler while doing insert-completion, it might overwrite
10626 * the (long) mode message. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010627 if (wp == lastwin && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010628 if (edit_submode != NULL)
10629 return;
Bram Moolenaar491ac282018-06-17 14:47:55 +020010630 // Don't draw the ruler when the popup menu is visible, it may overlap.
10631 // Except when the popup menu will be redrawn anyway.
10632 if (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010633 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010634#endif
10635
10636#ifdef FEAT_STL_OPT
10637 if (*p_ruf)
10638 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010639 int save_called_emsg = called_emsg;
10640
10641 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010642 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010643 if (called_emsg)
10644 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010645 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010646 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010647 return;
10648 }
10649#endif
10650
10651 /*
10652 * Check if not in Insert mode and the line is empty (will show "0-1").
10653 */
10654 if (!(State & INSERT)
10655 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10656 empty_line = TRUE;
10657
10658 /*
10659 * Only draw the ruler when something changed.
10660 */
10661 validate_virtcol_win(wp);
10662 if ( redraw_cmdline
10663 || always
10664 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10665 || wp->w_cursor.col != wp->w_ru_cursor.col
10666 || wp->w_virtcol != wp->w_ru_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +000010667 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
Bram Moolenaar071d4272004-06-13 20:20:40 +000010668 || wp->w_topline != wp->w_ru_topline
10669 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10670#ifdef FEAT_DIFF
10671 || wp->w_topfill != wp->w_ru_topfill
10672#endif
10673 || empty_line != wp->w_ru_empty)
10674 {
10675 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010676 if (wp->w_status_height)
10677 {
10678 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010679 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar53f81742017-09-22 14:35:51 +020010680 off = wp->w_wincol;
Bram Moolenaar02631462017-09-22 15:20:32 +020010681 width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010682 }
10683 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010684 {
10685 row = Rows - 1;
10686 fillchar = ' ';
10687 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010688 width = Columns;
10689 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010690 }
10691
10692 /* In list mode virtcol needs to be recomputed */
10693 virtcol = wp->w_virtcol;
10694 if (wp->w_p_list && lcs_tab1 == NUL)
10695 {
10696 wp->w_p_list = FALSE;
10697 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10698 wp->w_p_list = TRUE;
10699 }
10700
10701 /*
10702 * Some sprintfs return the length, some return a pointer.
10703 * To avoid portability problems we use strlen() here.
10704 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010705 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010706 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10707 ? 0L
10708 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010709 len = STRLEN(buffer);
10710 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010711 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10712 (int)virtcol + 1);
10713
10714 /*
10715 * Add a "50%" if there is room for it.
10716 * On the last line, don't print in the last column (scrolls the
10717 * screen up on some terminals).
10718 */
10719 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010720 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010721 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010722 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010723 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010724 this_ru_col = ru_col - (Columns - width);
10725 if (this_ru_col < 0)
10726 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010727 /* Never use more than half the window/screen width, leave the other
10728 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010729 if (this_ru_col < (width + 1) / 2)
10730 this_ru_col = (width + 1) / 2;
10731 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010732 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010010733 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010734 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010735 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010736 if (has_mbyte)
10737 i += (*mb_char2bytes)(fillchar, buffer + i);
10738 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010739 buffer[i++] = fillchar;
10740 ++o;
10741 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010742 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010743 }
10744 /* Truncate at window boundary. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010745 if (has_mbyte)
10746 {
10747 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010748 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010749 {
10750 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010751 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010752 {
10753 buffer[i] = NUL;
10754 break;
10755 }
10756 }
10757 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010010758 else if (this_ru_col + (int)STRLEN(buffer) > width)
Bram Moolenaar4033c552017-09-16 20:54:51 +020010759 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010760
Bram Moolenaar4033c552017-09-16 20:54:51 +020010761 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010762 i = redraw_cmdline;
10763 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020010764 this_ru_col + off + (int)STRLEN(buffer),
10765 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010766 fillchar, fillchar, attr);
10767 /* don't redraw the cmdline because of showing the ruler */
10768 redraw_cmdline = i;
10769 wp->w_ru_cursor = wp->w_cursor;
10770 wp->w_ru_virtcol = wp->w_virtcol;
10771 wp->w_ru_empty = empty_line;
10772 wp->w_ru_topline = wp->w_topline;
10773 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10774#ifdef FEAT_DIFF
10775 wp->w_ru_topfill = wp->w_topfill;
10776#endif
10777 }
10778}
10779#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010780
10781#if defined(FEAT_LINEBREAK) || defined(PROTO)
10782/*
Bram Moolenaar64486672010-05-16 15:46:46 +020010783 * Return the width of the 'number' and 'relativenumber' column.
10784 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010785 * Otherwise it depends on 'numberwidth' and the line count.
10786 */
10787 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010788number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010789{
10790 int n;
10791 linenr_T lnum;
10792
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020010793 if (wp->w_p_rnu && !wp->w_p_nu)
10794 /* cursor line shows "0" */
10795 lnum = wp->w_height;
10796 else
10797 /* cursor line shows absolute line number */
10798 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020010799
Bram Moolenaar6b314672015-03-20 15:42:10 +010010800 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010801 return wp->w_nrwidth_width;
10802 wp->w_nrwidth_line_count = lnum;
10803
10804 n = 0;
10805 do
10806 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010807 lnum /= 10;
10808 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010809 } while (lnum > 0);
10810
10811 /* 'numberwidth' gives the minimal width plus one */
10812 if (n < wp->w_p_nuw - 1)
10813 n = wp->w_p_nuw - 1;
10814
Bram Moolenaare4b407f2019-07-04 11:59:28 +020010815# ifdef FEAT_SIGNS
10816 // If 'signcolumn' is set to 'number' and there is a sign to display, then
10817 // the minimal width for the number column is 2.
10818 if (n < 2 && (wp->w_buffer->b_signlist != NULL)
10819 && (*wp->w_p_scl == 'n' && *(wp->w_p_scl + 1) == 'u'))
10820 n = 2;
10821# endif
10822
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010823 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010010824 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010825 return n;
10826}
10827#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010828
Bram Moolenaar113e1072019-01-20 15:30:40 +010010829#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010830/*
10831 * Return the current cursor column. This is the actual position on the
10832 * screen. First column is 0.
10833 */
10834 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010835screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010836{
10837 return screen_cur_col;
10838}
10839
10840/*
10841 * Return the current cursor row. This is the actual position on the screen.
10842 * First row is 0.
10843 */
10844 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010845screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010846{
10847 return screen_cur_row;
10848}
Bram Moolenaar113e1072019-01-20 15:30:40 +010010849#endif