blob: 72b346d96d30a4bad9919df1bcb5587984cfe05d [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 Moolenaar071d4272004-06-13 20:20:40 +00004894 /* 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
4907 /* tab amount depends on current column */
4908 n_extra = tab_len;
4909#ifdef FEAT_LINEBREAK
4910 else
4911 {
4912 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 Moolenaar49f9dd72014-08-29 12:08:43 +02004919 /* there are characters to conceal */
4920 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004921 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
4922 */
4923 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
4924 && n_extra > tab_len)
4925 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004926#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004927
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004928 /* if n_extra > 0, it gives the number of chars, to
4929 * use for a tab, else we need to calculate the width
4930 * for a tab */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004931 len = (tab_len * mb_char2len(lcs_tab2));
4932 if (n_extra > 0)
4933 len += n_extra - tab_len;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004934 c = lcs_tab1;
Bram Moolenaar964b3742019-05-24 18:54:09 +02004935 p = alloc(len + 1);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004936 vim_memset(p, ' ', len);
4937 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004938 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004939 p_extra_free = p;
4940 for (i = 0; i < tab_len; i++)
4941 {
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004942 if (*p == NUL)
4943 {
4944 tab_len = i;
4945 break;
4946 }
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004947 mb_char2bytes(lcs_tab2, p);
4948 p += mb_char2len(lcs_tab2);
4949 n_extra += mb_char2len(lcs_tab2)
4950 - (saved_nextra > 0 ? 1 : 0);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004951 }
4952 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004953#ifdef FEAT_CONCEAL
4954 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
4955 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004956 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004957 n_extra -= vcol_off;
4958#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004959 }
4960#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004961#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004962 {
4963 int vc_saved = vcol_off;
4964
4965 /* Tab alignment should be identical regardless of
4966 * 'conceallevel' value. So tab compensates of all
4967 * previous concealed characters, and thus resets
4968 * vcol_off and boguscols accumulated so far in the
4969 * line. Note that the tab can be longer than
4970 * 'tabstop' when there are concealed characters. */
4971 FIX_FOR_BOGUSCOLS;
4972
4973 /* Make sure, the highlighting for the tab char will be
4974 * correctly set further below (effectively reverts the
4975 * FIX_FOR_BOGSUCOLS macro */
4976 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004977 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004978 tab_len += vc_saved;
4979 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004980#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 if (wp->w_p_list)
4983 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004984 c = (n_extra == 0 && lcs_tab3) ? lcs_tab3 : lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004985#ifdef FEAT_LINEBREAK
4986 if (wp->w_p_lbr)
4987 c_extra = NUL; /* using p_extra from above */
4988 else
4989#endif
4990 c_extra = lcs_tab2;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004991 c_final = lcs_tab3;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004992 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004993 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 saved_attr2 = char_attr; /* save current attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004996 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 {
4998 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004999 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005000 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 }
5003 else
5004 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01005005 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 c_extra = ' ';
5007 c = ' ';
5008 }
5009 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005010 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02005011 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005012 || ((fromcol >= 0 || fromcol_prev >= 0)
5013 && tocol > vcol
5014 && VIsual_mode != Ctrl_V
5015 && (
5016# ifdef FEAT_RIGHTLEFT
5017 wp->w_p_rl ? (col >= 0) :
5018# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005019 (col < wp->w_width))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005020 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005021 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005022 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02005023 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005025 /* Display a '$' after the line or highlight an extra
5026 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027#if defined(FEAT_DIFF) || defined(LINE_ATTR)
5028 /* For a diff line the highlighting continues after the
5029 * "$". */
5030 if (
5031# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005032 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033# ifdef LINE_ATTR
5034 &&
5035# endif
5036# endif
5037# ifdef LINE_ATTR
5038 line_attr == 0
5039# endif
5040 )
5041#endif
5042 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 /* In virtualedit, visual selections may extend
5044 * beyond end of line. */
5045 if (area_highlighting && virtual_active()
5046 && tocol != MAXCOL && vcol < tocol)
5047 n_extra = 0;
5048 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 {
5050 p_extra = at_end_str;
5051 n_extra = 1;
5052 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005053 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 }
5055 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02005056 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005057 c = lcs_eol;
5058 else
5059 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 lcs_eol_one = -1;
5061 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005062 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005064 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 n_attr = 1;
5066 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005068 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 {
5070 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005071 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005072 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 }
5074 else
5075 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 }
5077 else if (c != NUL)
5078 {
5079 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02005080 if (n_extra == 0)
5081 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082#ifdef FEAT_RIGHTLEFT
5083 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
5084 rl_mirror(p_extra); /* reverse "<12>" */
5085#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005087 c_final = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005088#ifdef FEAT_LINEBREAK
5089 if (wp->w_p_lbr)
5090 {
5091 char_u *p;
5092
5093 c = *p_extra;
Bram Moolenaar964b3742019-05-24 18:54:09 +02005094 p = alloc(n_extra + 1);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005095 vim_memset(p, ' ', n_extra);
5096 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5097 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005098 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005099 p_extra_free = p_extra = p;
5100 }
5101 else
5102#endif
5103 {
5104 n_extra = byte2cells(c) - 1;
5105 c = *p_extra++;
5106 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005107 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 {
5109 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005110 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 saved_attr2 = char_attr; /* save current attr */
5112 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 else if (VIsual_active
5116 && (VIsual_mode == Ctrl_V
5117 || VIsual_mode == 'v')
5118 && virtual_active()
5119 && tocol != MAXCOL
5120 && vcol < tocol
5121 && (
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005122#ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 wp->w_p_rl ? (col >= 0) :
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005124#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005125 (col < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 {
5127 c = ' ';
5128 --ptr; /* put it back at the NUL */
5129 }
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005130#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 else if ((
5132# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005133 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005135# ifdef FEAT_TERMINAL
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005136 win_attr != 0 ||
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005137# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 ) && (
5140# ifdef FEAT_RIGHTLEFT
5141 wp->w_p_rl ? (col >= 0) :
5142# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005143 (col
5144# ifdef FEAT_CONCEAL
5145 - boguscols
5146# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005147 < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 {
5149 /* Highlight until the right side of the window */
5150 c = ' ';
5151 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005152
5153 /* Remember we do the char for line highlighting. */
5154 ++did_line_attr;
5155
5156 /* don't do search HL for the rest of the line */
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005157 if (line_attr != 0 && char_attr == search_attr
5158 && (did_line_attr > 1
5159 || (wp->w_p_list && lcs_eol > 0)))
Bram Moolenaar91170f82006-05-05 21:15:17 +00005160 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161# ifdef FEAT_DIFF
5162 if (diff_hlf == HLF_TXD)
5163 {
5164 diff_hlf = HLF_CHD;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005165 if (vi_attr == 0 || char_attr != vi_attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005166 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005167 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005168 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5169 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005170 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 }
5173# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005174# ifdef FEAT_TERMINAL
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005175 if (win_attr != 0)
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005176 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005177 char_attr = win_attr;
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005178 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5179 char_attr = hl_combine_attr(char_attr,
5180 HL_ATTR(HLF_CUL));
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02005181 else if (line_attr)
5182 char_attr = hl_combine_attr(char_attr, line_attr);
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005183 }
5184# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 }
5186#endif
5187 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005188
5189#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005190 if ( wp->w_p_cole > 0
5191 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaarab62c192019-03-30 16:39:05 +01005192 conceal_cursor_line(wp))
5193 && ((syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005194 && !(lnum_in_visual_area
5195 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005196 {
5197 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005198 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005199 && (syn_get_sub_char() != NUL || match_conc
5200 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005201 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005202 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005203 /* First time at this concealed item: display one
5204 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005205 if (match_conc)
5206 c = match_conc;
5207 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005208 c = syn_get_sub_char();
5209 else if (lcs_conceal != NUL)
5210 c = lcs_conceal;
5211 else
5212 c = ' ';
5213
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005214 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005215
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005216 if (n_extra > 0)
5217 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005218 vcol += n_extra;
5219 if (wp->w_p_wrap && n_extra > 0)
5220 {
5221# ifdef FEAT_RIGHTLEFT
5222 if (wp->w_p_rl)
5223 {
5224 col -= n_extra;
5225 boguscols -= n_extra;
5226 }
5227 else
5228# endif
5229 {
5230 boguscols += n_extra;
5231 col += n_extra;
5232 }
5233 }
5234 n_extra = 0;
5235 n_attr = 0;
5236 }
5237 else if (n_skip == 0)
5238 {
5239 is_concealing = TRUE;
5240 n_skip = 1;
5241 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005242 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005243 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005244 {
5245 mb_utf8 = TRUE;
5246 u8cc[0] = 0;
5247 c = 0xc0;
5248 }
5249 else
5250 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005251 }
5252 else
5253 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005254 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005255 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005256 }
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02005257
5258 if (n_skip > 0 && did_decrement_ptr)
5259 // not showing the '>', put pointer back to avoid getting stuck
5260 ++ptr;
5261
5262#endif // FEAT_CONCEAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 }
5264
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005265#ifdef FEAT_CONCEAL
5266 /* In the cursor line and we may be concealing characters: correct
5267 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005268 if (!did_wcol && draw_state == WL_LINE
5269 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005270 && conceal_cursor_line(wp)
5271 && (int)wp->w_virtcol <= vcol + n_skip)
5272 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005273# ifdef FEAT_RIGHTLEFT
5274 if (wp->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02005275 wp->w_wcol = wp->w_width - col + boguscols - 1;
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005276 else
5277# endif
5278 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005279 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005280 did_wcol = TRUE;
5281 }
5282#endif
5283
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 /* Don't override visual selection highlighting. */
5285 if (n_attr > 0
5286 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005287 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 char_attr = extra_attr;
5289
Bram Moolenaar81695252004-12-29 20:58:21 +00005290#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 /* XIM don't send preedit_start and preedit_end, but they send
5292 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5293 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005294 if (p_imst == IM_ON_THE_SPOT
5295 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005296 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 && (State & INSERT)
5298 && !p_imdisable
5299 && im_is_preediting()
5300 && draw_state == WL_LINE)
5301 {
5302 colnr_T tcol;
5303
5304 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005305 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 else
5307 tcol = preedit_end_col;
5308 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5309 {
5310 if (feedback_old_attr < 0)
5311 {
5312 feedback_col = 0;
5313 feedback_old_attr = char_attr;
5314 }
5315 char_attr = im_get_feedback_attr(feedback_col);
5316 if (char_attr < 0)
5317 char_attr = feedback_old_attr;
5318 feedback_col++;
5319 }
5320 else if (feedback_old_attr >= 0)
5321 {
5322 char_attr = feedback_old_attr;
5323 feedback_old_attr = -1;
5324 feedback_col = 0;
5325 }
5326 }
5327#endif
5328 /*
5329 * Handle the case where we are in column 0 but not on the first
5330 * character of the line and the user wants us to show us a
5331 * special character (via 'listchars' option "precedes:<char>".
5332 */
5333 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005334 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5336#ifdef FEAT_DIFF
5337 && filler_todo <= 0
5338#endif
5339 && draw_state > WL_NR
5340 && c != NUL)
5341 {
5342 c = lcs_prec;
5343 lcs_prec_todo = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005344 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5345 {
5346 /* Double-width character being overwritten by the "precedes"
5347 * character, need to fill up half the character. */
5348 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005349 c_final = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005350 n_extra = 1;
5351 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005352 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005355 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 {
5357 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005358 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005359 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 }
5361 else
5362 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005363 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 {
5365 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005366 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 n_attr3 = 1;
5368 }
5369 }
5370
5371 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005372 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 */
Bram Moolenaarf914a332019-07-20 15:09:56 +02005374 if ((c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005375#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005376 || did_line_attr == 1
5377#endif
Bram Moolenaarf914a332019-07-20 15:09:56 +02005378 ) && eol_hl_off == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005380#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarbbca7732019-07-24 18:13:16 +02005381 // flag to indicate whether prevcol equals startcol of search_hl or
5382 // one of the matches
5383 int prevcol_hl_flag = get_prevcol_hl_flag(wp, &search_hl,
5384 (long)(ptr - line) - (c == NUL));
Bram Moolenaar91170f82006-05-05 21:15:17 +00005385#endif
Bram Moolenaarbbca7732019-07-24 18:13:16 +02005386 // Invert at least one char, used for Visual and empty line or
5387 // highlight match at end of line. If it's beyond the last
5388 // char on the screen, just overwrite that one (tricky!) Not
5389 // needed when a '$' was displayed for 'list'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005391 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005392 && (VIsual_mode != Ctrl_V
5393 || lnum == VIsual.lnum
5394 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005395 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarbbca7732019-07-24 18:13:16 +02005397 // highlight 'hlsearch' match at end of line
5398 || (prevcol_hl_flag
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005399# ifdef FEAT_SYN_HL
5400 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
5401 && !(wp == curwin && VIsual_active))
5402# endif
5403# ifdef FEAT_DIFF
5404 && diff_hlf == (hlf_T)0
5405# endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005406# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005407 && did_line_attr <= 1
5408# endif
5409 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410#endif
5411 ))
5412 {
5413 int n = 0;
5414
5415#ifdef FEAT_RIGHTLEFT
5416 if (wp->w_p_rl)
5417 {
5418 if (col < 0)
5419 n = 1;
5420 }
5421 else
5422#endif
5423 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005424 if (col >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 n = -1;
5426 }
5427 if (n != 0)
5428 {
5429 /* At the window boundary, highlight the last character
5430 * instead (better than nothing). */
5431 off += n;
5432 col += n;
5433 }
5434 else
5435 {
5436 /* Add a blank character to highlight. */
5437 ScreenLines[off] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 if (enc_utf8)
5439 ScreenLinesUC[off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 }
5441#ifdef FEAT_SEARCH_EXTRA
5442 if (area_attr == 0)
5443 {
Bram Moolenaarbbca7732019-07-24 18:13:16 +02005444 // Use attributes from match with highest priority among
5445 // 'search_hl' and the match list.
5446 get_search_match_hl(wp, &search_hl,
5447 (long)(ptr - line), &char_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 }
5449#endif
5450 ScreenAttrs[off] = char_attr;
5451#ifdef FEAT_RIGHTLEFT
5452 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005453 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005455 --off;
5456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 else
5458#endif
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 Moolenaar600dddc2006-03-12 22:05:10 +00005463 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005464 eol_hl_off = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005466 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467
Bram Moolenaar91170f82006-05-05 21:15:17 +00005468 /*
5469 * At end of the text line.
5470 */
5471 if (c == NUL)
5472 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005473#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005474 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005475 if (wp->w_p_wrap)
5476 v = wp->w_skipcol;
5477 else
5478 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005479
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005480 /* check if line ends before left margin */
5481 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005482 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005483#ifdef FEAT_CONCEAL
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01005484 // Get rid of the boguscols now, we want to draw until the right
5485 // edge for 'cursorcolumn'.
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005486 col -= boguscols;
5487 boguscols = 0;
5488#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005489
5490 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005491 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005492
5493 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005494 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5495 && (int)wp->w_virtcol <
Bram Moolenaar02631462017-09-22 15:20:32 +02005496 wp->w_width * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005497 && lnum != wp->w_cursor.lnum)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005498 || draw_color_col
5499 || win_attr != 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005500# ifdef FEAT_RIGHTLEFT
5501 && !wp->w_p_rl
5502# endif
5503 )
5504 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005505 int rightmost_vcol = 0;
5506 int i;
5507
5508 if (wp->w_p_cuc)
5509 rightmost_vcol = wp->w_virtcol;
5510 if (draw_color_col)
5511 /* determine rightmost colorcolumn to possibly draw */
5512 for (i = 0; color_cols[i] >= 0; ++i)
5513 if (rightmost_vcol < color_cols[i])
5514 rightmost_vcol = color_cols[i];
5515
Bram Moolenaar02631462017-09-22 15:20:32 +02005516 while (col < wp->w_width)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005517 {
5518 ScreenLines[off] = ' ';
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005519 if (enc_utf8)
5520 ScreenLinesUC[off] = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005521 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005522 if (draw_color_col)
5523 draw_color_col = advance_color_col(VCOL_HLC,
5524 &color_cols);
5525
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005526 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005527 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005528 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005529 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005530 else
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005531 ScreenAttrs[off++] = win_attr;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005532
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005533 if (VCOL_HLC >= rightmost_vcol && win_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005534 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005535
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005536 ++vcol;
5537 }
5538 }
5539#endif
5540
Bram Moolenaar53f81742017-09-22 14:35:51 +02005541 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005542 (int)wp->w_width, screen_line_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 row++;
5544
5545 /*
5546 * Update w_cline_height and w_cline_folded if the cursor line was
5547 * updated (saves a call to plines() later).
5548 */
5549 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5550 {
5551 curwin->w_cline_row = startrow;
5552 curwin->w_cline_height = row - startrow;
5553#ifdef FEAT_FOLDING
5554 curwin->w_cline_folded = FALSE;
5555#endif
5556 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5557 }
5558
5559 break;
5560 }
5561
Bram Moolenaara5c6a0b2019-05-08 20:20:46 +02005562 // Show "extends" character from 'listchars' if beyond the line end and
5563 // 'list' is set.
5564 if (lcs_ext != NUL
5565 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 && !wp->w_p_wrap
5567#ifdef FEAT_DIFF
5568 && filler_todo <= 0
5569#endif
5570 && (
5571#ifdef FEAT_RIGHTLEFT
5572 wp->w_p_rl ? col == 0 :
5573#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005574 col == wp->w_width - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005576 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5578 {
5579 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005580 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005582 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 {
5584 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005585 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005586 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 }
5588 else
5589 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 }
5591
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005592#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005593 /* advance to the next 'colorcolumn' */
5594 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005595 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005596
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005597 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005598 * highlight the cursor position itself.
5599 * Also highlight the 'colorcolumn' if it is different than
5600 * 'cursorcolumn' */
5601 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005602 if (draw_state == WL_LINE && !lnum_in_visual_area
5603 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005604 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005605 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005606 && lnum != wp->w_cursor.lnum)
5607 {
5608 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005609 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005610 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005611 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005612 {
5613 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005614 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005615 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005616 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005617#endif
5618
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 /*
5620 * Store character to be displayed.
5621 * Skip characters that are left of the screen for 'nowrap'.
5622 */
5623 vcol_prev = vcol;
5624 if (draw_state < WL_LINE || n_skip <= 0)
5625 {
5626 /*
5627 * Store the character.
5628 */
Bram Moolenaara12a1612019-01-24 16:39:02 +01005629#if defined(FEAT_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5631 {
5632 /* A double-wide character is: put first halve in left cell. */
5633 --off;
5634 --col;
5635 }
5636#endif
5637 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005639 {
5640 if ((mb_c & 0xff00) == 0x8e00)
5641 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 else if (enc_utf8)
5645 {
5646 if (mb_utf8)
5647 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005648 int i;
5649
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005651 if ((c & 0xff) == 0)
5652 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005653 for (i = 0; i < Screen_mco; ++i)
5654 {
5655 ScreenLinesC[i][off] = u8cc[i];
5656 if (u8cc[i] == 0)
5657 break;
5658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 }
5660 else
5661 ScreenLinesUC[off] = 0;
5662 }
5663 if (multi_attr)
5664 {
5665 ScreenAttrs[off] = multi_attr;
5666 multi_attr = 0;
5667 }
5668 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 ScreenAttrs[off] = char_attr;
5670
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5672 {
5673 /* Need to fill two screen columns. */
5674 ++off;
5675 ++col;
5676 if (enc_utf8)
5677 /* UTF-8: Put a 0 in the second screen char. */
5678 ScreenLines[off] = 0;
5679 else
5680 /* DBCS: Put second byte in the second screen char. */
5681 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005682 if (draw_state > WL_NR
5683#ifdef FEAT_DIFF
5684 && filler_todo <= 0
5685#endif
5686 )
5687 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 /* When "tocol" is halfway a character, set it to the end of
5689 * the character, otherwise highlighting won't stop. */
5690 if (tocol == vcol)
5691 ++tocol;
5692#ifdef FEAT_RIGHTLEFT
5693 if (wp->w_p_rl)
5694 {
5695 /* now it's time to backup one cell */
5696 --off;
5697 --col;
5698 }
5699#endif
5700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701#ifdef FEAT_RIGHTLEFT
5702 if (wp->w_p_rl)
5703 {
5704 --off;
5705 --col;
5706 }
5707 else
5708#endif
5709 {
5710 ++off;
5711 ++col;
5712 }
5713 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005714#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005715 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005716 {
5717 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005718 ++vcol_off;
5719 if (n_extra > 0)
5720 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005721 if (wp->w_p_wrap)
5722 {
5723 /*
5724 * Special voodoo required if 'wrap' is on.
5725 *
5726 * Advance the column indicator to force the line
5727 * drawing to wrap early. This will make the line
5728 * take up the same screen space when parts are concealed,
5729 * so that cursor line computations aren't messed up.
5730 *
5731 * To avoid the fictitious advance of 'col' causing
5732 * trailing junk to be written out of the screen line
5733 * we are building, 'boguscols' keeps track of the number
5734 * of bad columns we have advanced.
5735 */
5736 if (n_extra > 0)
5737 {
5738 vcol += n_extra;
5739# ifdef FEAT_RIGHTLEFT
5740 if (wp->w_p_rl)
5741 {
5742 col -= n_extra;
5743 boguscols -= n_extra;
5744 }
5745 else
5746# endif
5747 {
5748 col += n_extra;
5749 boguscols += n_extra;
5750 }
5751 n_extra = 0;
5752 n_attr = 0;
5753 }
5754
5755
Bram Moolenaar860cae12010-06-05 23:22:07 +02005756 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5757 {
5758 /* Need to fill two screen columns. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01005759# ifdef FEAT_RIGHTLEFT
Bram Moolenaar860cae12010-06-05 23:22:07 +02005760 if (wp->w_p_rl)
5761 {
5762 --boguscols;
5763 --col;
5764 }
5765 else
Bram Moolenaara12a1612019-01-24 16:39:02 +01005766# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02005767 {
5768 ++boguscols;
5769 ++col;
5770 }
5771 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005772
5773# ifdef FEAT_RIGHTLEFT
5774 if (wp->w_p_rl)
5775 {
5776 --boguscols;
5777 --col;
5778 }
5779 else
5780# endif
5781 {
5782 ++boguscols;
5783 ++col;
5784 }
5785 }
5786 else
5787 {
5788 if (n_extra > 0)
5789 {
5790 vcol += n_extra;
5791 n_extra = 0;
5792 n_attr = 0;
5793 }
5794 }
5795
5796 }
5797#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 else
5799 --n_skip;
5800
Bram Moolenaar64486672010-05-16 15:46:46 +02005801 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5802 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005803 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804#ifdef FEAT_DIFF
5805 && filler_todo <= 0
5806#endif
5807 )
5808 ++vcol;
5809
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005810#ifdef FEAT_SYN_HL
5811 if (vcol_save_attr >= 0)
5812 char_attr = vcol_save_attr;
5813#endif
5814
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815 /* restore attributes after "predeces" in 'listchars' */
5816 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5817 char_attr = saved_attr3;
5818
5819 /* restore attributes after last 'listchars' or 'number' char */
5820 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5821 char_attr = saved_attr2;
5822
5823 /*
5824 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005825 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 */
5827 if ((
5828#ifdef FEAT_RIGHTLEFT
5829 wp->w_p_rl ? (col < 0) :
5830#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005831 (col >= wp->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 && (*ptr != NUL
5833#ifdef FEAT_DIFF
5834 || filler_todo > 0
5835#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005836 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5838 )
5839 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005840#ifdef FEAT_CONCEAL
Bram Moolenaar53f81742017-09-22 14:35:51 +02005841 screen_line(screen_row, wp->w_wincol, col - boguscols,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005842 (int)wp->w_width, screen_line_flags);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005843 boguscols = 0;
5844#else
Bram Moolenaar53f81742017-09-22 14:35:51 +02005845 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005846 (int)wp->w_width, screen_line_flags);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005847#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 ++row;
5849 ++screen_row;
5850
5851 /* When not wrapping and finished diff lines, or when displayed
5852 * '$' and highlighting until last column, break here. */
5853 if ((!wp->w_p_wrap
5854#ifdef FEAT_DIFF
5855 && filler_todo <= 0
5856#endif
5857 ) || lcs_eol_one == -1)
5858 break;
5859
5860 /* When the window is too narrow draw all "@" lines. */
5861 if (draw_state != WL_LINE
5862#ifdef FEAT_DIFF
5863 && filler_todo <= 0
5864#endif
5865 )
5866 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01005867 win_draw_end(wp, '@', ' ', TRUE, row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 row = endrow;
5870 }
5871
5872 /* When line got too long for screen break here. */
5873 if (row == endrow)
5874 {
5875 ++row;
5876 break;
5877 }
5878
5879 if (screen_cur_row == screen_row - 1
5880#ifdef FEAT_DIFF
5881 && filler_todo <= 0
5882#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005883 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884 {
5885 /* Remember that the line wraps, used for modeless copy. */
5886 LineWraps[screen_row - 1] = TRUE;
5887
5888 /*
5889 * Special trick to make copy/paste of wrapped lines work with
5890 * xterm/screen: write an extra character beyond the end of
5891 * the line. This will work with all terminal types
5892 * (regardless of the xn,am settings).
5893 * Only do this on a fast tty.
5894 * Only do this if the cursor is on the current line
5895 * (something has been written in it).
5896 * Don't do this for the GUI.
5897 * Don't do this for double-width characters.
5898 * Don't do this for a window not at the right screen border.
5899 */
5900 if (p_tf
5901#ifdef FEAT_GUI
5902 && !gui.in_use
5903#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005905 && ((*mb_off2cells)(LineOffset[screen_row],
5906 LineOffset[screen_row] + screen_Columns)
5907 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005909 + (int)Columns - 2,
5910 LineOffset[screen_row] + screen_Columns)
Bram Moolenaara12a1612019-01-24 16:39:02 +01005911 == 2)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912 {
5913 /* First make sure we are at the end of the screen line,
5914 * then output the same character again to let the
5915 * terminal know about the wrap. If the terminal doesn't
5916 * auto-wrap, we overwrite the character. */
Bram Moolenaar02631462017-09-22 15:20:32 +02005917 if (screen_cur_col != wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918 screen_char(LineOffset[screen_row - 1]
5919 + (unsigned)Columns - 1,
5920 screen_row - 1, (int)(Columns - 1));
5921
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 /* When there is a multi-byte character, just output a
5923 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005924 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5925 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926 out_char(' ');
5927 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 out_char(ScreenLines[LineOffset[screen_row - 1]
5929 + (Columns - 1)]);
5930 /* force a redraw of the first char on the next line */
5931 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5932 screen_start(); /* don't know where cursor is now */
5933 }
5934 }
5935
5936 col = 0;
5937 off = (unsigned)(current_ScreenLine - ScreenLines);
5938#ifdef FEAT_RIGHTLEFT
5939 if (wp->w_p_rl)
5940 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005941 col = wp->w_width - 1; /* col is not used if breaking! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 off += col;
5943 }
5944#endif
5945
5946 /* reset the drawing state for the start of a wrapped line */
5947 draw_state = WL_START;
5948 saved_n_extra = n_extra;
5949 saved_p_extra = p_extra;
5950 saved_c_extra = c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005951 saved_c_final = c_final;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 saved_char_attr = char_attr;
5953 n_extra = 0;
5954 lcs_prec_todo = lcs_prec;
5955#ifdef FEAT_LINEBREAK
5956# ifdef FEAT_DIFF
5957 if (filler_todo <= 0)
5958# endif
5959 need_showbreak = TRUE;
5960#endif
5961#ifdef FEAT_DIFF
5962 --filler_todo;
5963 /* When the filler lines are actually below the last line of the
5964 * file, don't draw the line itself, break here. */
5965 if (filler_todo == 0 && wp->w_botfill)
5966 break;
5967#endif
5968 }
5969
5970 } /* for every character in the line */
5971
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005972#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005973 /* After an empty line check first word for capital. */
5974 if (*skipwhite(line) == NUL)
5975 {
5976 capcol_lnum = lnum + 1;
5977 cap_col = 0;
5978 }
5979#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01005980#ifdef FEAT_TEXT_PROP
5981 vim_free(text_props);
5982 vim_free(text_prop_idxs);
5983#endif
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005984
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005985 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 return row;
5987}
5988
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005989/*
5990 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005991 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005992 */
5993 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005994comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005995{
5996 int i;
5997
5998 for (i = 0; i < Screen_mco; ++i)
5999 {
6000 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
6001 return TRUE;
6002 if (ScreenLinesC[i][off_from] == 0)
6003 break;
6004 }
6005 return FALSE;
6006}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006007
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008/*
6009 * Check whether the given character needs redrawing:
6010 * - the (first byte of the) character is different
6011 * - the attributes are different
6012 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006013 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014 */
6015 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006016char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017{
6018 if (cols > 0
6019 && ((ScreenLines[off_from] != ScreenLines[off_to]
6020 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021 || (enc_dbcs != 0
6022 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
6023 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
6024 ? ScreenLines2[off_from] != ScreenLines2[off_to]
6025 : (cols > 1 && ScreenLines[off_from + 1]
6026 != ScreenLines[off_to + 1])))
6027 || (enc_utf8
6028 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6029 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006030 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006031 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6032 && ScreenLines[off_from + 1]
Bram Moolenaara12a1612019-01-24 16:39:02 +01006033 != ScreenLines[off_to + 1])))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 return TRUE;
6035 return FALSE;
6036}
6037
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006038#if defined(FEAT_TERMINAL) || defined(PROTO)
6039/*
6040 * Return the index in ScreenLines[] for the current screen line.
6041 */
6042 int
6043screen_get_current_line_off()
6044{
6045 return (int)(current_ScreenLine - ScreenLines);
6046}
6047#endif
6048
Bram Moolenaarc662ec92019-06-23 00:15:57 +02006049#ifdef FEAT_TEXT_PROP
6050/*
6051 * Return TRUE if this position has a higher level popup or this cell is
6052 * transparent in the current popup.
6053 */
6054 static int
6055blocked_by_popup(int row, int col)
6056{
6057 int off;
6058
6059 if (!popup_visible)
6060 return FALSE;
6061 off = row * screen_Columns + col;
6062 return popup_mask[off] > screen_zindex || popup_transparent[off];
6063}
6064#endif
6065
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066/*
6067 * Move one "cooked" screen line to the screen, but only the characters that
6068 * have actually changed. Handle insert/delete character.
6069 * "coloff" gives the first column on the screen for this line.
6070 * "endcol" gives the columns where valid characters are.
6071 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6072 * needs to be cleared, negative otherwise.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006073 * "flags" can have bits:
6074 * SLF_POPUP popup window
6075 * SLF_RIGHTLEFT rightleft window:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6077 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6078 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006079 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006080screen_line(
6081 int row,
6082 int coloff,
6083 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006084 int clear_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006085 int flags UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086{
6087 unsigned off_from;
6088 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006089 unsigned max_off_from;
6090 unsigned max_off_to;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093 int force = FALSE; /* force update rest of the line */
6094 int redraw_this /* bool: does character need redraw? */
6095#ifdef FEAT_GUI
6096 = TRUE /* For GUI when while-loop empty */
6097#endif
6098 ;
6099 int redraw_next; /* redraw_this for next character */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 int clear_next = FALSE;
6101 int char_cells; /* 1: normal char */
6102 /* 2: occupies two display cells */
6103# define CHAR_CELLS char_cells
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006105 /* Check for illegal row and col, just in case. */
6106 if (row >= Rows)
6107 row = Rows - 1;
6108 if (endcol > Columns)
6109 endcol = Columns;
6110
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111# ifdef FEAT_CLIPBOARD
6112 clip_may_clear_selection(row, row);
6113# endif
6114
6115 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6116 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006117 max_off_from = off_from + screen_Columns;
6118 max_off_to = LineOffset[row] + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119
6120#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006121 if (flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122 {
6123 /* Clear rest first, because it's left of the text. */
6124 if (clear_width > 0)
6125 {
6126 while (col <= endcol && ScreenLines[off_to] == ' '
6127 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006128 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129 {
6130 ++off_to;
6131 ++col;
6132 }
6133 if (col <= endcol)
6134 screen_fill(row, row + 1, col + coloff,
6135 endcol + coloff + 1, ' ', ' ', 0);
6136 }
6137 col = endcol + 1;
6138 off_to = LineOffset[row] + col + coloff;
6139 off_from += col;
6140 endcol = (clear_width > 0 ? clear_width : -clear_width);
6141 }
6142#endif /* FEAT_RIGHTLEFT */
6143
6144 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6145
6146 while (col < endcol)
6147 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006149 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150 else
6151 char_cells = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152
6153 redraw_this = redraw_next;
6154 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6155 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6156
6157#ifdef FEAT_GUI
6158 /* If the next character was bold, then redraw the current character to
6159 * remove any pixels that might have spilt over into us. This only
6160 * happens in the GUI.
6161 */
6162 if (redraw_next && gui.in_use)
6163 {
6164 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006165 if (hl > HL_ALL)
6166 hl = syn_attr2attr(hl);
6167 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 redraw_this = TRUE;
6169 }
6170#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02006171#ifdef FEAT_TEXT_PROP
Bram Moolenaarc662ec92019-06-23 00:15:57 +02006172 if (blocked_by_popup(row, col + coloff))
Bram Moolenaar33796b32019-06-08 16:01:13 +02006173 redraw_this = FALSE;
6174#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175 if (redraw_this)
6176 {
6177 /*
6178 * Special handling when 'xs' termcap flag set (hpterm):
6179 * Attributes for characters are stored at the position where the
6180 * cursor is when writing the highlighting code. The
6181 * start-highlighting code must be written with the cursor on the
6182 * first highlighted character. The stop-highlighting code must
6183 * be written with the cursor just after the last highlighted
6184 * character.
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01006185 * Overwriting a character doesn't remove its highlighting. Need
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 * to clear the rest of the line, and force redrawing it
6187 * completely.
6188 */
6189 if ( p_wiv
6190 && !force
6191#ifdef FEAT_GUI
6192 && !gui.in_use
6193#endif
6194 && ScreenAttrs[off_to] != 0
6195 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6196 {
6197 /*
6198 * Need to remove highlighting attributes here.
6199 */
6200 windgoto(row, col + coloff);
6201 out_str(T_CE); /* clear rest of this screen line */
6202 screen_start(); /* don't know where cursor is now */
6203 force = TRUE; /* force redraw of rest of the line */
6204 redraw_next = TRUE; /* or else next char would miss out */
6205
6206 /*
6207 * If the previous character was highlighted, need to stop
6208 * highlighting at this character.
6209 */
6210 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6211 {
6212 screen_attr = ScreenAttrs[off_to - 1];
6213 term_windgoto(row, col + coloff);
6214 screen_stop_highlight();
6215 }
6216 else
6217 screen_attr = 0; /* highlighting has stopped */
6218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 if (enc_dbcs != 0)
6220 {
6221 /* Check if overwriting a double-byte with a single-byte or
6222 * the other way around requires another character to be
6223 * redrawn. For UTF-8 this isn't needed, because comparing
6224 * ScreenLinesUC[] is sufficient. */
6225 if (char_cells == 1
6226 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006227 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228 {
6229 /* Writing a single-cell character over a double-cell
6230 * character: need to redraw the next cell. */
6231 ScreenLines[off_to + 1] = 0;
6232 redraw_next = TRUE;
6233 }
6234 else if (char_cells == 2
6235 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006236 && (*mb_off2cells)(off_to, max_off_to) == 1
6237 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 {
6239 /* Writing the second half of a double-cell character over
6240 * a double-cell character: need to redraw the second
6241 * cell. */
6242 ScreenLines[off_to + 2] = 0;
6243 redraw_next = TRUE;
6244 }
6245
6246 if (enc_dbcs == DBCS_JPNU)
6247 ScreenLines2[off_to] = ScreenLines2[off_from];
6248 }
6249 /* When writing a single-width character over a double-width
6250 * character and at the end of the redrawn text, need to clear out
6251 * the right halve of the old character.
6252 * Also required when writing the right halve of a double-width
6253 * char over the left halve of an existing one. */
6254 if (has_mbyte && col + char_cells == endcol
6255 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006256 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006258 && (*mb_off2cells)(off_to, max_off_to) == 1
6259 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 clear_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261
6262 ScreenLines[off_to] = ScreenLines[off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 if (enc_utf8)
6264 {
6265 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6266 if (ScreenLinesUC[off_from] != 0)
6267 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006268 int i;
6269
6270 for (i = 0; i < Screen_mco; ++i)
6271 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 }
6273 }
6274 if (char_cells == 2)
6275 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276
6277#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006278 /* The bold trick makes a single column of pixels appear in the
6279 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 * character should be redrawn too. This happens for our own GUI
6281 * and for some xterms. */
6282 if (
6283# ifdef FEAT_GUI
6284 gui.in_use
6285# endif
6286# if defined(FEAT_GUI) && defined(UNIX)
6287 ||
6288# endif
6289# ifdef UNIX
6290 term_is_xterm
6291# endif
6292 )
6293 {
6294 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006295 if (hl > HL_ALL)
6296 hl = syn_attr2attr(hl);
6297 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298 redraw_next = TRUE;
6299 }
6300#endif
6301 ScreenAttrs[off_to] = ScreenAttrs[off_from];
Bram Moolenaara12a1612019-01-24 16:39:02 +01006302
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006303 /* For simplicity set the attributes of second half of a
6304 * double-wide character equal to the first half. */
6305 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006307
6308 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 screen_char(off_to, row, col + coloff);
6312 }
6313 else if ( p_wiv
6314#ifdef FEAT_GUI
6315 && !gui.in_use
6316#endif
6317 && col + coloff > 0)
6318 {
6319 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6320 {
6321 /*
6322 * Don't output stop-highlight when moving the cursor, it will
6323 * stop the highlighting when it should continue.
6324 */
6325 screen_attr = 0;
6326 }
6327 else if (screen_attr != 0)
6328 screen_stop_highlight();
6329 }
6330
6331 off_to += CHAR_CELLS;
6332 off_from += CHAR_CELLS;
6333 col += CHAR_CELLS;
6334 }
6335
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336 if (clear_next)
6337 {
6338 /* Clear the second half of a double-wide character of which the left
6339 * half was overwritten with a single-wide character. */
6340 ScreenLines[off_to] = ' ';
6341 if (enc_utf8)
6342 ScreenLinesUC[off_to] = 0;
6343 screen_char(off_to, row, col + coloff);
6344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345
6346 if (clear_width > 0
6347#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006348 && !(flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349#endif
6350 )
6351 {
6352#ifdef FEAT_GUI
6353 int startCol = col;
6354#endif
6355
6356 /* blank out the rest of the line */
6357 while (col < clear_width && ScreenLines[off_to] == ' '
6358 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006359 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360 {
6361 ++off_to;
6362 ++col;
6363 }
6364 if (col < clear_width)
6365 {
6366#ifdef FEAT_GUI
6367 /*
6368 * In the GUI, clearing the rest of the line may leave pixels
6369 * behind if the first character cleared was bold. Some bold
6370 * fonts spill over the left. In this case we redraw the previous
6371 * character too. If we didn't skip any blanks above, then we
6372 * only redraw if the character wasn't already redrawn anyway.
6373 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006374 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 {
6376 hl = ScreenAttrs[off_to];
6377 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006378 {
6379 int prev_cells = 1;
Bram Moolenaara12a1612019-01-24 16:39:02 +01006380
Bram Moolenaar9c697322006-10-09 20:11:17 +00006381 if (enc_utf8)
6382 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6383 * that its width is 2. */
6384 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6385 else if (enc_dbcs != 0)
6386 {
6387 /* find previous character by counting from first
6388 * column and get its width. */
6389 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006390 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006391
6392 while (off < off_to)
6393 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006394 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006395 off += prev_cells;
6396 }
6397 }
6398
6399 if (enc_dbcs != 0 && prev_cells > 1)
6400 screen_char_2(off_to - prev_cells, row,
6401 col + coloff - prev_cells);
6402 else
Bram Moolenaar9c697322006-10-09 20:11:17 +00006403 screen_char(off_to - prev_cells, row,
6404 col + coloff - prev_cells);
6405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406 }
6407#endif
6408 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6409 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410 off_to += clear_width - col;
6411 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412 }
6413 }
6414
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006415 if (clear_width > 0
6416#ifdef FEAT_TEXT_PROP
6417 && !(flags & SLF_POPUP) // no separator for popup window
6418#endif
6419 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006421 // For a window that has a right neighbor, draw the separator char
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006422 // right of the window contents. But not on top of a popup window.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006423 if (coloff + col < Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006425#ifdef FEAT_TEXT_PROP
Bram Moolenaarc662ec92019-06-23 00:15:57 +02006426 if (!blocked_by_popup(row, col + coloff))
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006427#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006429 int c;
6430
6431 c = fillchar_vsep(&hl);
6432 if (ScreenLines[off_to] != (schar_T)c
6433 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6434 != (c >= 0x80 ? c : 0))
6435 || ScreenAttrs[off_to] != hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006437 ScreenLines[off_to] = c;
6438 ScreenAttrs[off_to] = hl;
6439 if (enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006441 if (c >= 0x80)
6442 {
6443 ScreenLinesUC[off_to] = c;
6444 ScreenLinesC[0][off_to] = 0;
6445 }
6446 else
6447 ScreenLinesUC[off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448 }
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006449 screen_char(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451 }
6452 }
6453 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 LineWraps[row] = FALSE;
6455 }
6456}
6457
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006458#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006460 * Mirror text "str" for right-left displaying.
6461 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006463 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006464rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465{
6466 char_u *p1, *p2;
6467 int t;
6468
6469 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6470 {
6471 t = *p1;
6472 *p1 = *p2;
6473 *p2 = t;
6474 }
6475}
6476#endif
6477
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478/*
6479 * mark all status lines for redraw; used after first :cd
6480 */
6481 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006482status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483{
6484 win_T *wp;
6485
Bram Moolenaar29323592016-07-24 22:04:11 +02006486 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487 if (wp->w_status_height)
6488 {
6489 wp->w_redr_status = TRUE;
6490 redraw_later(VALID);
6491 }
6492}
6493
6494/*
6495 * mark all status lines of the current buffer for redraw
6496 */
6497 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006498status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499{
6500 win_T *wp;
6501
Bram Moolenaar29323592016-07-24 22:04:11 +02006502 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6504 {
6505 wp->w_redr_status = TRUE;
6506 redraw_later(VALID);
6507 }
6508}
6509
6510/*
6511 * Redraw all status lines that need to be redrawn.
6512 */
6513 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006514redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515{
6516 win_T *wp;
6517
Bram Moolenaar29323592016-07-24 22:04:11 +02006518 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006520 win_redr_status(wp, FALSE);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006521 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006522 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524
Bram Moolenaar4033c552017-09-16 20:54:51 +02006525#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526/*
6527 * Redraw all status lines at the bottom of frame "frp".
6528 */
6529 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006530win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531{
6532 if (frp->fr_layout == FR_LEAF)
6533 frp->fr_win->w_redr_status = TRUE;
6534 else if (frp->fr_layout == FR_ROW)
6535 {
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006536 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537 win_redraw_last_status(frp);
6538 }
6539 else /* frp->fr_layout == FR_COL */
6540 {
6541 frp = frp->fr_child;
6542 while (frp->fr_next != NULL)
6543 frp = frp->fr_next;
6544 win_redraw_last_status(frp);
6545 }
6546}
6547#endif
6548
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549/*
6550 * Draw the verticap separator right of window "wp" starting with line "row".
6551 */
6552 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006553draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554{
6555 int hl;
6556 int c;
6557
6558 if (wp->w_vsep_width)
6559 {
6560 /* draw the vertical separator right of this window */
6561 c = fillchar_vsep(&hl);
6562 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6563 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6564 c, ' ', hl);
6565 }
6566}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567
6568#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006569static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570
6571/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006572 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573 */
6574 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006575status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576{
6577 int len = 0;
6578
6579#ifdef FEAT_MENU
6580 int emenu = (xp->xp_context == EXPAND_MENUS
6581 || xp->xp_context == EXPAND_MENUNAMES);
6582
6583 /* Check for menu separators - replace with '|'. */
6584 if (emenu && menu_is_separator(s))
6585 return 1;
6586#endif
6587
6588 while (*s != NUL)
6589 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006590 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006591 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006592 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593 }
6594
6595 return len;
6596}
6597
6598/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006599 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006600 * These are backslashes used for escaping. Do show backslashes in help tags.
6601 */
6602 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006603skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006604{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006605 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006606#ifdef FEAT_MENU
6607 || ((xp->xp_context == EXPAND_MENUS
6608 || xp->xp_context == EXPAND_MENUNAMES)
6609 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6610#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006611 )
6612 {
6613#ifndef BACKSLASH_IN_FILENAME
6614 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6615 return 2;
6616#endif
6617 return 1;
6618 }
6619 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006620}
6621
6622/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623 * Show wildchar matches in the status line.
6624 * Show at least the "match" item.
6625 * We start at item 'first_match' in the list and show all matches that fit.
6626 *
6627 * If inversion is possible we use it. Else '=' characters are used.
6628 */
6629 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006630win_redr_status_matches(
6631 expand_T *xp,
6632 int num_matches,
6633 char_u **matches, /* list of matches */
6634 int match,
6635 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636{
6637#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6638 int row;
6639 char_u *buf;
6640 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006641 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 int fillchar;
6643 int attr;
6644 int i;
6645 int highlight = TRUE;
6646 char_u *selstart = NULL;
6647 int selstart_col = 0;
6648 char_u *selend = NULL;
6649 static int first_match = 0;
6650 int add_left = FALSE;
6651 char_u *s;
6652#ifdef FEAT_MENU
6653 int emenu;
6654#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656
6657 if (matches == NULL) /* interrupted completion? */
6658 return;
6659
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006660 if (has_mbyte)
Bram Moolenaar964b3742019-05-24 18:54:09 +02006661 buf = alloc(Columns * MB_MAXBYTES + 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006662 else
Bram Moolenaar964b3742019-05-24 18:54:09 +02006663 buf = alloc(Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664 if (buf == NULL)
6665 return;
6666
6667 if (match == -1) /* don't show match but original text */
6668 {
6669 match = 0;
6670 highlight = FALSE;
6671 }
6672 /* count 1 for the ending ">" */
6673 clen = status_match_len(xp, L_MATCH(match)) + 3;
6674 if (match == 0)
6675 first_match = 0;
6676 else if (match < first_match)
6677 {
6678 /* jumping left, as far as we can go */
6679 first_match = match;
6680 add_left = TRUE;
6681 }
6682 else
6683 {
6684 /* check if match fits on the screen */
6685 for (i = first_match; i < match; ++i)
6686 clen += status_match_len(xp, L_MATCH(i)) + 2;
6687 if (first_match > 0)
6688 clen += 2;
6689 /* jumping right, put match at the left */
6690 if ((long)clen > Columns)
6691 {
6692 first_match = match;
6693 /* if showing the last match, we can add some on the left */
6694 clen = 2;
6695 for (i = match; i < num_matches; ++i)
6696 {
6697 clen += status_match_len(xp, L_MATCH(i)) + 2;
6698 if ((long)clen >= Columns)
6699 break;
6700 }
6701 if (i == num_matches)
6702 add_left = TRUE;
6703 }
6704 }
6705 if (add_left)
6706 while (first_match > 0)
6707 {
6708 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6709 if ((long)clen >= Columns)
6710 break;
6711 --first_match;
6712 }
6713
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006714 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715
6716 if (first_match == 0)
6717 {
6718 *buf = NUL;
6719 len = 0;
6720 }
6721 else
6722 {
6723 STRCPY(buf, "< ");
6724 len = 2;
6725 }
6726 clen = len;
6727
6728 i = first_match;
6729 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6730 {
6731 if (i == match)
6732 {
6733 selstart = buf + len;
6734 selstart_col = clen;
6735 }
6736
6737 s = L_MATCH(i);
6738 /* Check for menu separators - replace with '|' */
6739#ifdef FEAT_MENU
6740 emenu = (xp->xp_context == EXPAND_MENUS
6741 || xp->xp_context == EXPAND_MENUNAMES);
6742 if (emenu && menu_is_separator(s))
6743 {
6744 STRCPY(buf + len, transchar('|'));
6745 l = (int)STRLEN(buf + len);
6746 len += l;
6747 clen += l;
6748 }
6749 else
6750#endif
6751 for ( ; *s != NUL; ++s)
6752 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006753 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754 clen += ptr2cells(s);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006755 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756 {
6757 STRNCPY(buf + len, s, l);
6758 s += l - 1;
6759 len += l;
6760 }
6761 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 {
6763 STRCPY(buf + len, transchar_byte(*s));
6764 len += (int)STRLEN(buf + len);
6765 }
6766 }
6767 if (i == match)
6768 selend = buf + len;
6769
6770 *(buf + len++) = ' ';
6771 *(buf + len++) = ' ';
6772 clen += 2;
6773 if (++i == num_matches)
6774 break;
6775 }
6776
6777 if (i != num_matches)
6778 {
6779 *(buf + len++) = '>';
6780 ++clen;
6781 }
6782
6783 buf[len] = NUL;
6784
6785 row = cmdline_row - 1;
6786 if (row >= 0)
6787 {
6788 if (wild_menu_showing == 0)
6789 {
6790 if (msg_scrolled > 0)
6791 {
6792 /* Put the wildmenu just above the command line. If there is
6793 * no room, scroll the screen one line up. */
6794 if (cmdline_row == Rows - 1)
6795 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02006796 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 ++msg_scrolled;
6798 }
6799 else
6800 {
6801 ++cmdline_row;
6802 ++row;
6803 }
6804 wild_menu_showing = WM_SCROLLED;
6805 }
6806 else
6807 {
6808 /* Create status line if needed by setting 'laststatus' to 2.
6809 * Set 'winminheight' to zero to avoid that the window is
6810 * resized. */
6811 if (lastwin->w_status_height == 0)
6812 {
6813 save_p_ls = p_ls;
6814 save_p_wmh = p_wmh;
6815 p_ls = 2;
6816 p_wmh = 0;
6817 last_status(FALSE);
6818 }
6819 wild_menu_showing = WM_SHOWN;
6820 }
6821 }
6822
6823 screen_puts(buf, row, 0, attr);
6824 if (selstart != NULL && highlight)
6825 {
6826 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006827 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 }
6829
6830 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6831 }
6832
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 vim_free(buf);
6835}
6836#endif
6837
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838/*
6839 * Redraw the status line of window wp.
6840 *
6841 * If inversion is possible we use it. Else '=' characters are used.
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006842 * If "ignore_pum" is TRUE, also redraw statusline when the popup menu is
6843 * displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844 */
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006845 static void
Bram Moolenaar829adb72018-06-24 19:24:03 +02006846win_redr_status(win_T *wp, int ignore_pum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847{
6848 int row;
6849 char_u *p;
6850 int len;
6851 int fillchar;
6852 int attr;
6853 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006854 static int busy = FALSE;
6855
6856 /* It's possible to get here recursively when 'statusline' (indirectly)
6857 * invokes ":redrawstatus". Simply ignore the call then. */
6858 if (busy)
6859 return;
6860 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861
6862 wp->w_redr_status = FALSE;
6863 if (wp->w_status_height == 0)
6864 {
6865 /* no status line, can only be last window */
6866 redraw_cmdline = TRUE;
6867 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006868 else if (!redrawing()
6869#ifdef FEAT_INS_EXPAND
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006870 // don't update status line when popup menu is visible and may be
6871 // drawn over it, unless it will be redrawn later
6872 || (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006873#endif
6874 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875 {
6876 /* Don't redraw right now, do it later. */
6877 wp->w_redr_status = TRUE;
6878 }
6879#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006880 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881 {
6882 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006883 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884 }
6885#endif
6886 else
6887 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006888 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006890 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 p = NameBuff;
6892 len = (int)STRLEN(p);
6893
Bram Moolenaard85f2712017-07-28 21:51:57 +02006894 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895#ifdef FEAT_QUICKFIX
6896 || wp->w_p_pvw
6897#endif
6898 || bufIsChanged(wp->w_buffer)
6899 || wp->w_buffer->b_p_ro)
6900 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02006901 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006903 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 len += (int)STRLEN(p + len);
6905 }
6906#ifdef FEAT_QUICKFIX
6907 if (wp->w_p_pvw)
6908 {
6909 STRCPY(p + len, _("[Preview]"));
6910 len += (int)STRLEN(p + len);
6911 }
6912#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02006913 if (bufIsChanged(wp->w_buffer)
6914#ifdef FEAT_TERMINAL
6915 && !bt_terminal(wp->w_buffer)
6916#endif
6917 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918 {
6919 STRCPY(p + len, "[+]");
6920 len += 3;
6921 }
6922 if (wp->w_buffer->b_p_ro)
6923 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006924 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01006925 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 }
6927
Bram Moolenaar02631462017-09-22 15:20:32 +02006928 this_ru_col = ru_col - (Columns - wp->w_width);
6929 if (this_ru_col < (wp->w_width + 1) / 2)
6930 this_ru_col = (wp->w_width + 1) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 if (this_ru_col <= 1)
6932 {
6933 p = (char_u *)"<"; /* No room for file name! */
6934 len = 1;
6935 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01006936 else if (has_mbyte)
6937 {
6938 int clen = 0, i;
6939
6940 /* Count total number of display cells. */
6941 clen = mb_string2cells(p, -1);
6942
6943 /* Find first character that will fit.
6944 * Going from start to end is much faster for DBCS. */
6945 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
6946 i += (*mb_ptr2len)(p + i))
6947 clen -= (*mb_ptr2cells)(p + i);
6948 len = clen;
6949 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950 {
Bram Moolenaara12a1612019-01-24 16:39:02 +01006951 p = p + i - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952 *p = '<';
Bram Moolenaara12a1612019-01-24 16:39:02 +01006953 ++len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 }
6955
Bram Moolenaara12a1612019-01-24 16:39:02 +01006956 }
6957 else if (len > this_ru_col - 1)
6958 {
6959 p += len - (this_ru_col - 1);
6960 *p = '<';
6961 len = this_ru_col - 1;
6962 }
6963
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar53f81742017-09-22 14:35:51 +02006965 screen_puts(p, row, wp->w_wincol, attr);
6966 screen_fill(row, row + 1, len + wp->w_wincol,
6967 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02006969 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6971 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
Bram Moolenaar53f81742017-09-22 14:35:51 +02006972 - 1 + wp->w_wincol), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973
6974#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +02006975 win_redr_ruler(wp, TRUE, ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976#endif
6977 }
6978
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 /*
6980 * May need to draw the character below the vertical separator.
6981 */
6982 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6983 {
6984 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006985 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986 else
6987 fillchar = fillchar_vsep(&attr);
6988 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6989 attr);
6990 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006991 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992}
6993
Bram Moolenaar238a5642006-02-21 22:12:05 +00006994#ifdef FEAT_STL_OPT
6995/*
6996 * Redraw the status line according to 'statusline' and take care of any
6997 * errors encountered.
6998 */
6999 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007000redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00007001{
Bram Moolenaar362f3562009-11-03 16:20:34 +00007002 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02007003 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007004
7005 /* When called recursively return. This can happen when the statusline
7006 * contains an expression that triggers a redraw. */
7007 if (entered)
7008 return;
7009 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007010
Bram Moolenaara742e082016-04-05 21:10:38 +02007011 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007012 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02007013 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007014 {
7015 /* When there is an error disable the statusline, otherwise the
7016 * display is messed up with errors and a redraw triggers the problem
7017 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007018 set_string_option_direct((char_u *)"statusline", -1,
7019 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007020 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007021 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007022 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007023 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007024}
7025#endif
7026
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027/*
7028 * Return TRUE if the status line of window "wp" is connected to the status
7029 * line of the window right of it. If not, then it's a vertical separator.
7030 * Only call if (wp->w_vsep_width != 0).
7031 */
7032 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007033stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034{
7035 frame_T *fr;
7036
7037 fr = wp->w_frame;
7038 while (fr->fr_parent != NULL)
7039 {
7040 if (fr->fr_parent->fr_layout == FR_COL)
7041 {
7042 if (fr->fr_next != NULL)
7043 break;
7044 }
7045 else
7046 {
7047 if (fr->fr_next != NULL)
7048 return TRUE;
7049 }
7050 fr = fr->fr_parent;
7051 }
7052 return FALSE;
7053}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056/*
7057 * Get the value to show for the language mappings, active 'keymap'.
7058 */
7059 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007060get_keymap_str(
7061 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007062 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007063 char_u *buf, /* buffer for the result */
7064 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065{
7066 char_u *p;
7067
7068 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7069 return FALSE;
7070
7071 {
7072#ifdef FEAT_EVAL
7073 buf_T *old_curbuf = curbuf;
7074 win_T *old_curwin = curwin;
7075 char_u *s;
7076
7077 curbuf = wp->w_buffer;
7078 curwin = wp;
7079 STRCPY(buf, "b:keymap_name"); /* must be writable */
7080 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007081 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082 --emsg_skip;
7083 curbuf = old_curbuf;
7084 curwin = old_curwin;
7085 if (p == NULL || *p == NUL)
7086#endif
7087 {
7088#ifdef FEAT_KEYMAP
7089 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7090 p = wp->w_buffer->b_p_keymap;
7091 else
7092#endif
7093 p = (char_u *)"lang";
7094 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007095 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 buf[0] = NUL;
7097#ifdef FEAT_EVAL
7098 vim_free(s);
7099#endif
7100 }
7101 return buf[0] != NUL;
7102}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103
7104#if defined(FEAT_STL_OPT) || defined(PROTO)
7105/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007106 * Redraw the status line or ruler of window "wp".
7107 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108 */
7109 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007110win_redr_custom(
7111 win_T *wp,
7112 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007114 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 int attr;
7116 int curattr;
7117 int row;
7118 int col = 0;
7119 int maxwidth;
7120 int width;
7121 int n;
7122 int len;
7123 int fillchar;
7124 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007125 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007127 struct stl_hlrec hltab[STL_MAX_ITEM];
7128 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007129 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007130 win_T *ewp;
7131 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132
Bram Moolenaar1d633412013-12-11 15:52:01 +01007133 /* There is a tiny chance that this gets called recursively: When
7134 * redrawing a status line triggers redrawing the ruler or tabline.
7135 * Avoid trouble by not allowing recursion. */
7136 if (entered)
7137 return;
7138 entered = TRUE;
7139
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007141 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007143 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007144 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007145 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007146 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007147 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007148 maxwidth = Columns;
7149# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007150 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007151# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007153 else
7154 {
7155 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007156 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02007157 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007158
7159 if (draw_ruler)
7160 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007161 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007162 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007163 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007164 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007165 if (*++stl == '-')
7166 stl++;
7167 if (atoi((char *)stl))
7168 while (VIM_ISDIGIT(*stl))
7169 stl++;
7170 if (*stl++ != '(')
7171 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007172 }
Bram Moolenaar02631462017-09-22 15:20:32 +02007173 col = ru_col - (Columns - wp->w_width);
7174 if (col < (wp->w_width + 1) / 2)
7175 col = (wp->w_width + 1) / 2;
7176 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007177 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007178 {
7179 row = Rows - 1;
7180 --maxwidth; /* writing in last column may cause scrolling */
7181 fillchar = ' ';
7182 attr = 0;
7183 }
7184
7185# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007186 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007187# endif
7188 }
7189 else
7190 {
7191 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007192 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007193 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007194 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007195# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007196 use_sandbox = was_set_insecurely((char_u *)"statusline",
7197 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007198# endif
7199 }
7200
Bram Moolenaar53f81742017-09-22 14:35:51 +02007201 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007202 }
7203
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007205 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206
Bram Moolenaar61452852011-02-01 18:01:11 +01007207 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7208 * the cursor away and back. */
7209 ewp = wp == NULL ? curwin : wp;
7210 p_crb_save = ewp->w_p_crb;
7211 ewp->w_p_crb = FALSE;
7212
Bram Moolenaar362f3562009-11-03 16:20:34 +00007213 /* Make a copy, because the statusline may include a function call that
7214 * might change the option value and free the memory. */
7215 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007216 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007217 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007218 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007219 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007220 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007222 /* Make all characters printable. */
7223 p = transstr(buf);
7224 if (p != NULL)
7225 {
7226 vim_strncpy(buf, p, sizeof(buf) - 1);
7227 vim_free(p);
7228 }
7229
7230 /* fill up with "fillchar" */
7231 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007232 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234 len += (*mb_char2bytes)(fillchar, buf + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 ++width;
7236 }
7237 buf[len] = NUL;
7238
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007239 /*
7240 * Draw each snippet with the specified highlighting.
7241 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242 curattr = attr;
7243 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007244 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007246 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247 screen_puts_len(p, len, row, col, curattr);
7248 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007249 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007251 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007253 else if (hltab[n].userhl < 0)
7254 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007255#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007256 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7257 && wp->w_status_height != 0)
7258 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007259 else if (wp != NULL && bt_terminal(wp->w_buffer)
7260 && wp->w_status_height != 0)
7261 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02007262#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007263 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007264 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007266 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 }
7268 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007269
7270 if (wp == NULL)
7271 {
7272 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7273 col = 0;
7274 len = 0;
7275 p = buf;
7276 fillchar = 0;
7277 for (n = 0; tabtab[n].start != NULL; n++)
7278 {
7279 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7280 while (col < len)
7281 TabPageIdxs[col++] = fillchar;
7282 p = tabtab[n].start;
7283 fillchar = tabtab[n].userhl;
7284 }
7285 while (col < Columns)
7286 TabPageIdxs[col++] = fillchar;
7287 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007288
7289theend:
7290 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291}
7292
7293#endif /* FEAT_STL_OPT */
7294
7295/*
7296 * Output a single character directly to the screen and update ScreenLines.
7297 */
7298 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007299screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301 char_u buf[MB_MAXBYTES + 1];
7302
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007303 if (has_mbyte)
7304 buf[(*mb_char2bytes)(c, buf)] = NUL;
7305 else
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007306 {
7307 buf[0] = c;
7308 buf[1] = NUL;
7309 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310 screen_puts(buf, row, col, attr);
7311}
7312
7313/*
7314 * Get a single character directly from ScreenLines into "bytes[]".
7315 * Also return its attribute in *attrp;
7316 */
7317 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007318screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319{
7320 unsigned off;
7321
7322 /* safety check */
7323 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7324 {
7325 off = LineOffset[row] + col;
7326 *attrp = ScreenAttrs[off];
7327 bytes[0] = ScreenLines[off];
7328 bytes[1] = NUL;
7329
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330 if (enc_utf8 && ScreenLinesUC[off] != 0)
7331 bytes[utfc_char2bytes(off, bytes)] = NUL;
7332 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7333 {
7334 bytes[0] = ScreenLines[off];
7335 bytes[1] = ScreenLines2[off];
7336 bytes[2] = NUL;
7337 }
7338 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7339 {
7340 bytes[1] = ScreenLines[off + 1];
7341 bytes[2] = NUL;
7342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343 }
7344}
7345
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007346/*
7347 * Return TRUE if composing characters for screen posn "off" differs from
7348 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007349 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007350 */
7351 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007352screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007353{
7354 int i;
7355
7356 for (i = 0; i < Screen_mco; ++i)
7357 {
7358 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7359 return TRUE;
7360 if (u8cc[i] == 0)
7361 break;
7362 }
7363 return FALSE;
7364}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007365
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366/*
7367 * Put string '*text' on the screen at position 'row' and 'col', with
7368 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7369 * Note: only outputs within one row, message is truncated at screen boundary!
7370 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7371 */
7372 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007373screen_puts(
7374 char_u *text,
7375 int row,
7376 int col,
7377 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378{
7379 screen_puts_len(text, -1, row, col, attr);
7380}
7381
7382/*
7383 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7384 * a NUL.
7385 */
7386 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007387screen_puts_len(
7388 char_u *text,
7389 int textlen,
7390 int row,
7391 int col,
7392 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393{
7394 unsigned off;
7395 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007396 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397 int c;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007398 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399 int mbyte_blen = 1;
7400 int mbyte_cells = 1;
7401 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007402 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403 int clear_next_cell = FALSE;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007404#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007406 int pc, nc, nc1;
7407 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007409 int force_redraw_this;
7410 int force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007411 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412
Bram Moolenaar0b4c9ed2019-06-03 22:04:23 +02007413 // Safety check. The check for negative row and column is to fix issue
7414 // #4102. TODO: find out why row/col could be negative.
7415 if (ScreenLines == NULL
7416 || row >= screen_Rows || row < 0
7417 || col >= screen_Columns || col < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007419 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420
Bram Moolenaarc236c162008-07-13 17:41:49 +00007421 /* When drawing over the right halve of a double-wide char clear out the
7422 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007423 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaara12a1612019-01-24 16:39:02 +01007424#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00007425 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01007426#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007427 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007428 {
7429 ScreenLines[off - 1] = ' ';
7430 ScreenAttrs[off - 1] = 0;
7431 if (enc_utf8)
7432 {
7433 ScreenLinesUC[off - 1] = 0;
7434 ScreenLinesC[0][off - 1] = 0;
7435 }
7436 /* redraw the previous cell, make it empty */
7437 screen_char(off - 1, row, col - 1);
7438 /* force the cell at "col" to be redrawn */
7439 force_redraw_next = TRUE;
7440 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007441
Bram Moolenaar367329b2007-08-30 11:53:22 +00007442 max_off = LineOffset[row] + screen_Columns;
Bram Moolenaara064ac82007-08-05 18:10:54 +00007443 while (col < screen_Columns
7444 && (len < 0 || (int)(ptr - text) < len)
7445 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446 {
7447 c = *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448 /* check if this is the first byte of a multibyte */
7449 if (has_mbyte)
7450 {
7451 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007452 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007454 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7456 mbyte_cells = 1;
7457 else if (enc_dbcs != 0)
7458 mbyte_cells = mbyte_blen;
7459 else /* enc_utf8 */
7460 {
7461 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007462 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 (int)((text + len) - ptr));
7464 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007465 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007467#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7469 {
7470 /* Do Arabic shaping. */
7471 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7472 {
7473 /* Past end of string to be displayed. */
7474 nc = NUL;
7475 nc1 = NUL;
7476 }
7477 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007478 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007479 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7480 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007481 nc1 = pcc[0];
7482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 pc = prev_c;
7484 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007485 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486 }
7487 else
7488 prev_c = u8c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007489#endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007490 if (col + mbyte_cells > screen_Columns)
7491 {
7492 /* Only 1 cell left, but character requires 2 cells:
7493 * display a '>' in the last column to avoid wrapping. */
7494 c = '>';
7495 mbyte_cells = 1;
7496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497 }
7498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007500 force_redraw_this = force_redraw_next;
7501 force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007502
7503 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504 || (mbyte_cells == 2
7505 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7506 || (enc_dbcs == DBCS_JPNU
7507 && c == 0x8e
7508 && ScreenLines2[off] != ptr[1])
7509 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007510 && (ScreenLinesUC[off] !=
7511 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7512 || (ScreenLinesUC[off] != 0
7513 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007515 || exmode_active;
7516
Bram Moolenaar24a5ac52019-06-08 19:01:18 +02007517 if ((need_redraw || force_redraw_this)
7518#ifdef FEAT_TEXT_PROP
Bram Moolenaarc662ec92019-06-23 00:15:57 +02007519 && !blocked_by_popup(row, col)
Bram Moolenaar24a5ac52019-06-08 19:01:18 +02007520#endif
7521 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522 {
7523#if defined(FEAT_GUI) || defined(UNIX)
7524 /* The bold trick makes a single row of pixels appear in the next
7525 * character. When a bold character is removed, the next
7526 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007527 * and for some xterms. */
7528 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529# ifdef FEAT_GUI
7530 gui.in_use
7531# endif
7532# if defined(FEAT_GUI) && defined(UNIX)
7533 ||
7534# endif
7535# ifdef UNIX
7536 term_is_xterm
7537# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007538 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007540 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007542 if (n > HL_ALL)
7543 n = syn_attr2attr(n);
7544 if (n & HL_BOLD)
7545 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546 }
7547#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548 /* When at the end of the text and overwriting a two-cell
7549 * character with a one-cell character, need to clear the next
7550 * cell. Also when overwriting the left halve of a two-cell char
7551 * with the right halve of a two-cell char. Do this only once
7552 * (mb_off2cells() may return 2 on the right halve). */
7553 if (clear_next_cell)
7554 clear_next_cell = FALSE;
7555 else if (has_mbyte
7556 && (len < 0 ? ptr[mbyte_blen] == NUL
7557 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007558 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007560 && (*mb_off2cells)(off, max_off) == 1
7561 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562 clear_next_cell = TRUE;
7563
7564 /* Make sure we never leave a second byte of a double-byte behind,
7565 * it confuses mb_off2cells(). */
7566 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007567 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007569 && (*mb_off2cells)(off, max_off) == 1
7570 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571 ScreenLines[off + mbyte_blen] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572 ScreenLines[off] = c;
7573 ScreenAttrs[off] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 if (enc_utf8)
7575 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007576 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007577 ScreenLinesUC[off] = 0;
7578 else
7579 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007580 int i;
7581
Bram Moolenaar071d4272004-06-13 20:20:40 +00007582 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007583 for (i = 0; i < Screen_mco; ++i)
7584 {
7585 ScreenLinesC[i][off] = u8cc[i];
7586 if (u8cc[i] == 0)
7587 break;
7588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589 }
7590 if (mbyte_cells == 2)
7591 {
7592 ScreenLines[off + 1] = 0;
7593 ScreenAttrs[off + 1] = attr;
7594 }
7595 screen_char(off, row, col);
7596 }
7597 else if (mbyte_cells == 2)
7598 {
7599 ScreenLines[off + 1] = ptr[1];
7600 ScreenAttrs[off + 1] = attr;
7601 screen_char_2(off, row, col);
7602 }
7603 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7604 {
7605 ScreenLines2[off] = ptr[1];
7606 screen_char(off, row, col);
7607 }
7608 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609 screen_char(off, row, col);
7610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611 if (has_mbyte)
7612 {
7613 off += mbyte_cells;
7614 col += mbyte_cells;
7615 ptr += mbyte_blen;
7616 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007617 {
7618 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007620 len = -1;
7621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622 }
7623 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624 {
7625 ++off;
7626 ++col;
7627 ++ptr;
7628 }
7629 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007630
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007631 /* If we detected the next character needs to be redrawn, but the text
7632 * doesn't extend up to there, update the character here. */
7633 if (force_redraw_next && col < screen_Columns)
7634 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007635 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7636 screen_char_2(off, row, col);
7637 else
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007638 screen_char(off, row, col);
7639 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640}
7641
7642#ifdef FEAT_SEARCH_EXTRA
7643/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007644 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645 */
7646 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007647start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648{
7649 if (p_hls && !no_hlsearch)
7650 {
7651 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007652 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007653# ifdef FEAT_RELTIME
7654 /* Set the time limit to 'redrawtime'. */
7655 profile_setlimit(p_rdt, &search_hl.tm);
7656# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657 }
7658}
7659
7660/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007661 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662 */
7663 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007664end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665{
7666 if (search_hl.rm.regprog != NULL)
7667 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007668 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669 search_hl.rm.regprog = NULL;
7670 }
7671}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02007672#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02007673
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007675screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676{
7677 attrentry_T *aep = NULL;
7678
7679 screen_attr = attr;
7680 if (full_screen
Bram Moolenaar4f974752019-02-17 17:44:42 +01007681#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682 && termcap_active
7683#endif
7684 )
7685 {
7686#ifdef FEAT_GUI
7687 if (gui.in_use)
7688 {
7689 char buf[20];
7690
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007691 /* The GUI handles this internally. */
7692 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693 OUT_STR(buf);
7694 }
7695 else
7696#endif
7697 {
7698 if (attr > HL_ALL) /* special HL attr. */
7699 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007700 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701 aep = syn_cterm_attr2entry(attr);
7702 else
7703 aep = syn_term_attr2entry(attr);
7704 if (aep == NULL) /* did ":syntax clear" */
7705 attr = 0;
7706 else
7707 attr = aep->ae_attr;
7708 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01007709 if ((attr & HL_BOLD) && *T_MD != NUL) /* bold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007711 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007712#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007713 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
7714 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
7715 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007716#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007717 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007718 /* If the Normal FG color has BOLD attribute and the new HL
7719 * has a FG color defined, clear BOLD. */
7720 out_str(T_ME);
Bram Moolenaar45a00002017-12-22 21:12:34 +01007721 if ((attr & HL_STANDOUT) && *T_SO != NUL) /* standout */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722 out_str(T_SO);
Bram Moolenaar45a00002017-12-22 21:12:34 +01007723 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) /* undercurl */
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01007724 out_str(T_UCS);
7725 if (((attr & HL_UNDERLINE) /* underline or undercurl */
Bram Moolenaar45a00002017-12-22 21:12:34 +01007726 || ((attr & HL_UNDERCURL) && *T_UCS == NUL))
7727 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728 out_str(T_US);
Bram Moolenaar45a00002017-12-22 21:12:34 +01007729 if ((attr & HL_ITALIC) && *T_CZH != NUL) /* italic */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730 out_str(T_CZH);
Bram Moolenaar45a00002017-12-22 21:12:34 +01007731 if ((attr & HL_INVERSE) && *T_MR != NUL) /* inverse (reverse) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732 out_str(T_MR);
Bram Moolenaar45a00002017-12-22 21:12:34 +01007733 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) /* strike */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02007734 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735
7736 /*
7737 * Output the color or start string after bold etc., in case the
7738 * bold etc. override the color setting.
7739 */
7740 if (aep != NULL)
7741 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007742#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007743 /* When 'termguicolors' is set but fg or bg is unset,
7744 * fall back to the cterm colors. This helps for SpellBad,
7745 * where the GUI uses a red undercurl. */
7746 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007748 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007749 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007750 }
7751 else
7752#endif
7753 if (t_colors > 1)
7754 {
7755 if (aep->ae_u.cterm.fg_color)
7756 term_fg_color(aep->ae_u.cterm.fg_color - 1);
7757 }
7758#ifdef FEAT_TERMGUICOLORS
7759 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
7760 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007761 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007762 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763 }
7764 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007765#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007766 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007768 if (aep->ae_u.cterm.bg_color)
7769 term_bg_color(aep->ae_u.cterm.bg_color - 1);
7770 }
7771
Bram Moolenaarf708ac52018-03-12 21:48:32 +01007772 if (!IS_CTERM)
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007773 {
7774 if (aep->ae_u.term.start != NULL)
7775 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776 }
7777 }
7778 }
7779 }
7780}
7781
7782 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007783screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784{
7785 int do_ME = FALSE; /* output T_ME code */
7786
7787 if (screen_attr != 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01007788#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789 && termcap_active
7790#endif
7791 )
7792 {
7793#ifdef FEAT_GUI
7794 if (gui.in_use)
7795 {
7796 char buf[20];
7797
7798 /* use internal GUI code */
7799 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
7800 OUT_STR(buf);
7801 }
7802 else
7803#endif
7804 {
7805 if (screen_attr > HL_ALL) /* special HL attr. */
7806 {
7807 attrentry_T *aep;
7808
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007809 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810 {
7811 /*
7812 * Assume that t_me restores the original colors!
7813 */
7814 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007815 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007816#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007817 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
7818 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
7819 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007820#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007821 aep->ae_u.cterm.fg_color) || (
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.bg_rgb != CTERMCOLOR
7824 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
7825 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007826#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007827 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828 do_ME = TRUE;
7829 }
7830 else
7831 {
7832 aep = syn_term_attr2entry(screen_attr);
7833 if (aep != NULL && aep->ae_u.term.stop != NULL)
7834 {
7835 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
7836 do_ME = TRUE;
7837 else
7838 out_str(aep->ae_u.term.stop);
7839 }
7840 }
7841 if (aep == NULL) /* did ":syntax clear" */
7842 screen_attr = 0;
7843 else
7844 screen_attr = aep->ae_attr;
7845 }
7846
7847 /*
7848 * Often all ending-codes are equal to T_ME. Avoid outputting the
7849 * same sequence several times.
7850 */
7851 if (screen_attr & HL_STANDOUT)
7852 {
7853 if (STRCMP(T_SE, T_ME) == 0)
7854 do_ME = TRUE;
7855 else
7856 out_str(T_SE);
7857 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01007858 if ((screen_attr & HL_UNDERCURL) && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01007859 {
7860 if (STRCMP(T_UCE, T_ME) == 0)
7861 do_ME = TRUE;
7862 else
7863 out_str(T_UCE);
7864 }
7865 if ((screen_attr & HL_UNDERLINE)
Bram Moolenaar45a00002017-12-22 21:12:34 +01007866 || ((screen_attr & HL_UNDERCURL) && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867 {
7868 if (STRCMP(T_UE, T_ME) == 0)
7869 do_ME = TRUE;
7870 else
7871 out_str(T_UE);
7872 }
7873 if (screen_attr & HL_ITALIC)
7874 {
7875 if (STRCMP(T_CZR, T_ME) == 0)
7876 do_ME = TRUE;
7877 else
7878 out_str(T_CZR);
7879 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02007880 if (screen_attr & HL_STRIKETHROUGH)
7881 {
7882 if (STRCMP(T_STE, T_ME) == 0)
7883 do_ME = TRUE;
7884 else
7885 out_str(T_STE);
7886 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
7888 out_str(T_ME);
7889
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007890#ifdef FEAT_TERMGUICOLORS
7891 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007893 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007894 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007895 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007896 term_bg_rgb_color(cterm_normal_bg_gui_color);
7897 }
7898 else
7899#endif
7900 {
7901 if (t_colors > 1)
7902 {
7903 /* set Normal cterm colors */
7904 if (cterm_normal_fg_color != 0)
7905 term_fg_color(cterm_normal_fg_color - 1);
7906 if (cterm_normal_bg_color != 0)
7907 term_bg_color(cterm_normal_bg_color - 1);
7908 if (cterm_normal_fg_bold)
7909 out_str(T_MD);
7910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911 }
7912 }
7913 }
7914 screen_attr = 0;
7915}
7916
7917/*
7918 * Reset the colors for a cterm. Used when leaving Vim.
7919 * The machine specific code may override this again.
7920 */
7921 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007922reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007924 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 {
7926 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007927#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007928 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
7929 || cterm_normal_bg_gui_color != INVALCOLOR)
7930 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007931#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007933#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934 {
7935 out_str(T_OP);
7936 screen_attr = -1;
7937 }
7938 if (cterm_normal_fg_bold)
7939 {
7940 out_str(T_ME);
7941 screen_attr = -1;
7942 }
7943 }
7944}
7945
7946/*
7947 * Put character ScreenLines["off"] on the screen at position "row" and "col",
7948 * using the attributes from ScreenAttrs["off"].
7949 */
7950 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007951screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952{
7953 int attr;
7954
7955 /* Check for illegal values, just in case (could happen just after
7956 * resizing). */
7957 if (row >= screen_Rows || col >= screen_Columns)
7958 return;
7959
Bram Moolenaarae654382019-01-17 21:09:05 +01007960#ifdef FEAT_INS_EXPAND
Bram Moolenaar33796b32019-06-08 16:01:13 +02007961 // Skip if under the popup menu.
7962 // Popup windows with zindex higher than POPUPMENU_ZINDEX go on top.
7963 if (pum_under_menu(row, col)
7964# ifdef FEAT_TEXT_PROP
7965 && screen_zindex <= POPUPMENU_ZINDEX
7966# endif
7967 )
Bram Moolenaarae654382019-01-17 21:09:05 +01007968 return;
7969#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02007970#ifdef FEAT_TEXT_PROP
Bram Moolenaarc662ec92019-06-23 00:15:57 +02007971 if (blocked_by_popup(row, col))
Bram Moolenaar33796b32019-06-08 16:01:13 +02007972 return;
7973#endif
7974
Bram Moolenaar494838a2015-02-10 19:20:37 +01007975 /* Outputting a character in the last cell on the screen may scroll the
7976 * screen up. Only do it when the "xn" termcap property is set, otherwise
7977 * mark the character invalid (update it when scrolled up). */
7978 if (*T_XN == NUL
7979 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980#ifdef FEAT_RIGHTLEFT
7981 /* account for first command-line character in rightleft mode */
7982 && !cmdmsg_rl
7983#endif
7984 )
7985 {
7986 ScreenAttrs[off] = (sattr_T)-1;
7987 return;
7988 }
7989
7990 /*
7991 * Stop highlighting first, so it's easier to move the cursor.
7992 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993 if (screen_char_attr != 0)
7994 attr = screen_char_attr;
7995 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996 attr = ScreenAttrs[off];
7997 if (screen_attr != attr)
7998 screen_stop_highlight();
7999
8000 windgoto(row, col);
8001
8002 if (screen_attr != attr)
8003 screen_start_highlight(attr);
8004
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005 if (enc_utf8 && ScreenLinesUC[off] != 0)
8006 {
8007 char_u buf[MB_MAXBYTES + 1];
8008
Bram Moolenaarcb070082016-04-02 22:14:51 +02008009 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008010 {
8011 if (*p_ambw == 'd'
Bram Moolenaara12a1612019-01-24 16:39:02 +01008012#ifdef FEAT_GUI
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008013 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008014#endif
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008015 )
8016 {
8017 /* Clear the two screen cells. If the character is actually
8018 * single width it won't change the second cell. */
8019 out_str((char_u *)" ");
8020 term_windgoto(row, col);
8021 }
8022 /* not sure where the cursor is after drawing the ambiguous width
8023 * character */
Bram Moolenaarcb070082016-04-02 22:14:51 +02008024 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008025 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02008026 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008028
8029 /* Convert the UTF-8 character to bytes and write it. */
8030 buf[utfc_char2bytes(off, buf)] = NUL;
8031 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032 }
8033 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037 /* double-byte character in single-width cell */
8038 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8039 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040 }
8041
8042 screen_cur_col++;
8043}
8044
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045/*
8046 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8047 * on the screen at position 'row' and 'col'.
8048 * The attributes of the first byte is used for all. This is required to
8049 * output the two bytes of a double-byte character with nothing in between.
8050 */
8051 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008052screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053{
8054 /* Check for illegal values (could be wrong when screen was resized). */
8055 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8056 return;
8057
8058 /* Outputting the last character on the screen may scrollup the screen.
8059 * Don't to it! Mark the character invalid (update it when scrolled up) */
8060 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8061 {
8062 ScreenAttrs[off] = (sattr_T)-1;
8063 return;
8064 }
8065
8066 /* Output the first byte normally (positions the cursor), then write the
8067 * second byte directly. */
8068 screen_char(off, row, col);
8069 out_char(ScreenLines[off + 1]);
8070 ++screen_cur_col;
8071}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073/*
8074 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8075 * This uses the contents of ScreenLines[] and doesn't change it.
8076 */
8077 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008078screen_draw_rectangle(
8079 int row,
8080 int col,
8081 int height,
8082 int width,
8083 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084{
8085 int r, c;
8086 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008087 int max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008089 /* Can't use ScreenLines unless initialized */
8090 if (ScreenLines == NULL)
8091 return;
8092
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093 if (invert)
8094 screen_char_attr = HL_INVERSE;
8095 for (r = row; r < row + height; ++r)
8096 {
8097 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008098 max_off = off + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 for (c = col; c < col + width; ++c)
8100 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00008101 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102 {
8103 screen_char_2(off + c, r, c);
8104 ++c;
8105 }
8106 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107 {
8108 screen_char(off + c, r, c);
Bram Moolenaar367329b2007-08-30 11:53:22 +00008109 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110 ++c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111 }
8112 }
8113 }
8114 screen_char_attr = 0;
8115}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117/*
8118 * Redraw the characters for a vertically split window.
8119 */
8120 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008121redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122{
8123 int col;
8124 int width;
8125
8126# ifdef FEAT_CLIPBOARD
8127 clip_may_clear_selection(row, end - 1);
8128# endif
8129
8130 if (wp == NULL)
8131 {
8132 col = 0;
8133 width = Columns;
8134 }
8135 else
8136 {
8137 col = wp->w_wincol;
8138 width = wp->w_width;
8139 }
8140 screen_draw_rectangle(row, col, end - row, width, FALSE);
8141}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008143 static void
8144space_to_screenline(int off, int attr)
8145{
8146 ScreenLines[off] = ' ';
8147 ScreenAttrs[off] = attr;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008148 if (enc_utf8)
8149 ScreenLinesUC[off] = 0;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008150}
8151
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152/*
8153 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8154 * with character 'c1' in first column followed by 'c2' in the other columns.
8155 * Use attributes 'attr'.
8156 */
8157 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008158screen_fill(
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008159 int start_row,
8160 int end_row,
8161 int start_col,
8162 int end_col,
8163 int c1,
8164 int c2,
8165 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008167 int row;
8168 int col;
8169 int off;
8170 int end_off;
8171 int did_delete;
8172 int c;
8173 int norm_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008175 int force_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176#endif
8177
8178 if (end_row > screen_Rows) /* safety check */
8179 end_row = screen_Rows;
8180 if (end_col > screen_Columns) /* safety check */
8181 end_col = screen_Columns;
8182 if (ScreenLines == NULL
8183 || start_row >= end_row
8184 || start_col >= end_col) /* nothing to do */
8185 return;
8186
8187 /* it's a "normal" terminal when not in a GUI or cterm */
8188 norm_term = (
8189#ifdef FEAT_GUI
8190 !gui.in_use &&
8191#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008192 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193 for (row = start_row; row < end_row; ++row)
8194 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008195 if (has_mbyte
Bram Moolenaara12a1612019-01-24 16:39:02 +01008196#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00008197 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008198#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00008199 )
8200 {
8201 /* When drawing over the right halve of a double-wide char clear
8202 * out the left halve. When drawing over the left halve of a
8203 * double wide-char clear out the right halve. Only needed in a
8204 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008205 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008206 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008207 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008208 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008209 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210 /*
8211 * Try to use delete-line termcap code, when no attributes or in a
8212 * "normal" terminal, where a bold/italic space is just a
8213 * space.
8214 */
8215 did_delete = FALSE;
8216 if (c2 == ' '
8217 && end_col == Columns
8218 && can_clear(T_CE)
8219 && (attr == 0
8220 || (norm_term
8221 && attr <= HL_ALL
8222 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8223 {
8224 /*
8225 * check if we really need to clear something
8226 */
8227 col = start_col;
8228 if (c1 != ' ') /* don't clear first char */
8229 ++col;
8230
8231 off = LineOffset[row] + col;
8232 end_off = LineOffset[row] + end_col;
8233
8234 /* skip blanks (used often, keep it fast!) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235 if (enc_utf8)
8236 while (off < end_off && ScreenLines[off] == ' '
8237 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8238 ++off;
8239 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240 while (off < end_off && ScreenLines[off] == ' '
8241 && ScreenAttrs[off] == 0)
8242 ++off;
8243 if (off < end_off) /* something to be cleared */
8244 {
8245 col = off - LineOffset[row];
8246 screen_stop_highlight();
8247 term_windgoto(row, col);/* clear rest of this screen line */
8248 out_str(T_CE);
8249 screen_start(); /* don't know where cursor is now */
8250 col = end_col - col;
8251 while (col--) /* clear chars in ScreenLines */
8252 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008253 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254 ++off;
8255 }
8256 }
8257 did_delete = TRUE; /* the chars are cleared now */
8258 }
8259
8260 off = LineOffset[row] + start_col;
8261 c = c1;
8262 for (col = start_col; col < end_col; ++col)
8263 {
Bram Moolenaar33796b32019-06-08 16:01:13 +02008264 if ((ScreenLines[off] != c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008265 || (enc_utf8 && (int)ScreenLinesUC[off]
8266 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267 || ScreenAttrs[off] != attr
8268#if defined(FEAT_GUI) || defined(UNIX)
8269 || force_next
8270#endif
8271 )
Bram Moolenaar33796b32019-06-08 16:01:13 +02008272#ifdef FEAT_TEXT_PROP
8273 // Skip if under a(nother) popup.
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008274 && !blocked_by_popup(row, col)
Bram Moolenaar33796b32019-06-08 16:01:13 +02008275#endif
8276 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277 {
8278#if defined(FEAT_GUI) || defined(UNIX)
8279 /* The bold trick may make a single row of pixels appear in
8280 * the next character. When a bold character is removed, the
8281 * next character should be redrawn too. This happens for our
8282 * own GUI and for some xterms. */
8283 if (
8284# ifdef FEAT_GUI
8285 gui.in_use
8286# endif
8287# if defined(FEAT_GUI) && defined(UNIX)
8288 ||
8289# endif
8290# ifdef UNIX
8291 term_is_xterm
8292# endif
8293 )
8294 {
8295 if (ScreenLines[off] != ' '
8296 && (ScreenAttrs[off] > HL_ALL
8297 || ScreenAttrs[off] & HL_BOLD))
8298 force_next = TRUE;
8299 else
8300 force_next = FALSE;
8301 }
8302#endif
8303 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 if (enc_utf8)
8305 {
8306 if (c >= 0x80)
8307 {
8308 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008309 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310 }
8311 else
8312 ScreenLinesUC[off] = 0;
8313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314 ScreenAttrs[off] = attr;
8315 if (!did_delete || c != ' ')
8316 screen_char(off, row, col);
8317 }
8318 ++off;
8319 if (col == start_col)
8320 {
8321 if (did_delete)
8322 break;
8323 c = c2;
8324 }
8325 }
8326 if (end_col == Columns)
8327 LineWraps[row] = FALSE;
8328 if (row == Rows - 1) /* overwritten the command line */
8329 {
8330 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02008331 if (start_col == 0 && end_col == Columns
8332 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008334 if (start_col == 0)
8335 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 }
8337 }
8338}
8339
8340/*
8341 * Check if there should be a delay. Used before clearing or redrawing the
8342 * screen or the command line.
8343 */
8344 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008345check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346{
8347 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8348 && !did_wait_return
8349 && emsg_silent == 0)
8350 {
8351 out_flush();
8352 ui_delay(1000L, TRUE);
8353 emsg_on_display = FALSE;
8354 if (check_msg_scroll)
8355 msg_scroll = FALSE;
8356 }
8357}
8358
8359/*
Bram Moolenaarca57ab52019-04-13 14:53:16 +02008360 * Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect.
8361 */
8362 static void
8363clear_TabPageIdxs(void)
8364{
8365 int scol;
8366
8367 for (scol = 0; scol < Columns; ++scol)
8368 TabPageIdxs[scol] = 0;
8369}
8370
8371/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008373 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008374 * Returns TRUE if there is a valid screen to write to.
8375 * Returns FALSE when starting up and screen not initialized yet.
8376 */
8377 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008378screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008380 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381 return (ScreenLines != NULL);
8382}
8383
8384/*
8385 * Resize the shell to Rows and Columns.
8386 * Allocate ScreenLines[] and associated items.
8387 *
8388 * There may be some time between setting Rows and Columns and (re)allocating
8389 * ScreenLines[]. This happens when starting up and when (manually) changing
8390 * the shell size. Always use screen_Rows and screen_Columns to access items
8391 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8392 * final size of the shell is needed.
8393 */
8394 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008395screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396{
8397 int new_row, old_row;
8398#ifdef FEAT_GUI
8399 int old_Rows;
8400#endif
8401 win_T *wp;
8402 int outofmem = FALSE;
8403 int len;
8404 schar_T *new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008406 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008408 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409 sattr_T *new_ScreenAttrs;
8410 unsigned *new_LineOffset;
8411 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008412 short *new_TabPageIdxs;
Bram Moolenaar33796b32019-06-08 16:01:13 +02008413#ifdef FEAT_TEXT_PROP
8414 short *new_popup_mask;
Bram Moolenaar4c063a02019-06-10 21:24:12 +02008415 short *new_popup_mask_next;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008416 char *new_popup_transparent;
Bram Moolenaar33796b32019-06-08 16:01:13 +02008417#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00008418 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008420 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008421 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008423retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424 /*
8425 * Allocation of the screen buffers is done only when the size changes and
8426 * when Rows and Columns have been set and we have started doing full
8427 * screen stuff.
8428 */
8429 if ((ScreenLines != NULL
8430 && Rows == screen_Rows
8431 && Columns == screen_Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 && enc_utf8 == (ScreenLinesUC != NULL)
8433 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaara12a1612019-01-24 16:39:02 +01008434 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435 || Rows == 0
8436 || Columns == 0
8437 || (!full_screen && ScreenLines == NULL))
8438 return;
8439
8440 /*
8441 * It's possible that we produce an out-of-memory message below, which
8442 * will cause this function to be called again. To break the loop, just
8443 * return here.
8444 */
8445 if (entered)
8446 return;
8447 entered = TRUE;
8448
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008449 /*
8450 * Note that the window sizes are updated before reallocating the arrays,
8451 * thus we must not redraw here!
8452 */
8453 ++RedrawingDisabled;
8454
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455 win_new_shellsize(); /* fit the windows in the new sized shell */
8456
Bram Moolenaar071d4272004-06-13 20:20:40 +00008457 comp_col(); /* recompute columns for shown command and ruler */
8458
8459 /*
8460 * We're changing the size of the screen.
8461 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8462 * - Move lines from the old arrays into the new arrays, clear extra
8463 * lines (unless the screen is going to be cleared).
8464 * - Free the old arrays.
8465 *
8466 * If anything fails, make ScreenLines NULL, so we don't do anything!
8467 * Continuing with the old ScreenLines may result in a crash, because the
8468 * size is wrong.
8469 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008470 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008472 if (aucmd_win != NULL)
8473 win_free_lsize(aucmd_win);
Bram Moolenaar8caaf822019-06-01 18:11:22 +02008474#ifdef FEAT_TEXT_PROP
8475 // global popup windows
8476 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
8477 win_free_lsize(wp);
8478 // tab-local popup windows
8479 FOR_ALL_TABPAGES(tp)
8480 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
8481 win_free_lsize(wp);
8482#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008484 new_ScreenLines = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
Bram Moolenaar216b7102010-03-23 13:56:59 +01008485 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486 if (enc_utf8)
8487 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008488 new_ScreenLinesUC = LALLOC_MULT(u8char_T, (Rows + 1) * Columns);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008489 for (i = 0; i < p_mco; ++i)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008490 new_ScreenLinesC[i] = LALLOC_CLEAR_MULT(u8char_T,
8491 (Rows + 1) * Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492 }
8493 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008494 new_ScreenLines2 = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
8495 new_ScreenAttrs = LALLOC_MULT(sattr_T, (Rows + 1) * Columns);
8496 new_LineOffset = LALLOC_MULT(unsigned, Rows);
8497 new_LineWraps = LALLOC_MULT(char_u, Rows);
8498 new_TabPageIdxs = LALLOC_MULT(short, Columns);
Bram Moolenaar33796b32019-06-08 16:01:13 +02008499#ifdef FEAT_TEXT_PROP
8500 new_popup_mask = LALLOC_MULT(short, Rows * Columns);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02008501 new_popup_mask_next = LALLOC_MULT(short, Rows * Columns);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008502 new_popup_transparent = LALLOC_MULT(char, Rows * Columns);
Bram Moolenaar33796b32019-06-08 16:01:13 +02008503#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008505 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506 {
8507 if (win_alloc_lines(wp) == FAIL)
8508 {
8509 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008510 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511 }
8512 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008513 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8514 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008515 outofmem = TRUE;
Bram Moolenaar8caaf822019-06-01 18:11:22 +02008516#ifdef FEAT_TEXT_PROP
8517 // global popup windows
8518 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
8519 if (win_alloc_lines(wp) == FAIL)
8520 {
8521 outofmem = TRUE;
8522 goto give_up;
8523 }
8524 // tab-local popup windows
8525 FOR_ALL_TABPAGES(tp)
8526 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
8527 if (win_alloc_lines(wp) == FAIL)
8528 {
8529 outofmem = TRUE;
8530 goto give_up;
8531 }
8532#endif
8533
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008534give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008536 for (i = 0; i < p_mco; ++i)
8537 if (new_ScreenLinesC[i] == NULL)
8538 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539 if (new_ScreenLines == NULL
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008540 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542 || new_ScreenAttrs == NULL
8543 || new_LineOffset == NULL
8544 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008545 || new_TabPageIdxs == NULL
Bram Moolenaar33796b32019-06-08 16:01:13 +02008546#ifdef FEAT_TEXT_PROP
8547 || new_popup_mask == NULL
Bram Moolenaar4c063a02019-06-10 21:24:12 +02008548 || new_popup_mask_next == NULL
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008549 || new_popup_transparent == NULL
Bram Moolenaar33796b32019-06-08 16:01:13 +02008550#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551 || outofmem)
8552 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008553 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008554 {
8555 /* guess the size */
8556 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8557
8558 /* Remember we did this to avoid getting outofmem messages over
8559 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008560 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008561 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01008562 VIM_CLEAR(new_ScreenLines);
Bram Moolenaard23a8232018-02-10 18:45:26 +01008563 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008564 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01008565 VIM_CLEAR(new_ScreenLinesC[i]);
8566 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaard23a8232018-02-10 18:45:26 +01008567 VIM_CLEAR(new_ScreenAttrs);
8568 VIM_CLEAR(new_LineOffset);
8569 VIM_CLEAR(new_LineWraps);
8570 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar33796b32019-06-08 16:01:13 +02008571#ifdef FEAT_TEXT_PROP
8572 VIM_CLEAR(new_popup_mask);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02008573 VIM_CLEAR(new_popup_mask_next);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008574 VIM_CLEAR(new_popup_transparent);
Bram Moolenaar33796b32019-06-08 16:01:13 +02008575#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576 }
8577 else
8578 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008579 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008580
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581 for (new_row = 0; new_row < Rows; ++new_row)
8582 {
8583 new_LineOffset[new_row] = new_row * Columns;
8584 new_LineWraps[new_row] = FALSE;
8585
8586 /*
8587 * If the screen is not going to be cleared, copy as much as
8588 * possible from the old screen to the new one and clear the rest
8589 * (used when resizing the window at the "--more--" prompt or when
8590 * executing an external command, for the GUI).
8591 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008592 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593 {
8594 (void)vim_memset(new_ScreenLines + new_row * Columns,
8595 ' ', (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596 if (enc_utf8)
8597 {
8598 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8599 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008600 for (i = 0; i < p_mco; ++i)
8601 (void)vim_memset(new_ScreenLinesC[i]
8602 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008603 0, (size_t)Columns * sizeof(u8char_T));
8604 }
8605 if (enc_dbcs == DBCS_JPNU)
8606 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8607 0, (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8609 0, (size_t)Columns * sizeof(sattr_T));
8610 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008611 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612 {
8613 if (screen_Columns < Columns)
8614 len = screen_Columns;
8615 else
8616 len = Columns;
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008617 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008618 * may be invalid now. Also when p_mco changes. */
8619 if (!(enc_utf8 && ScreenLinesUC == NULL)
8620 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008621 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8622 ScreenLines + LineOffset[old_row],
8623 (size_t)len * sizeof(schar_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008624 if (enc_utf8 && ScreenLinesUC != NULL
8625 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626 {
8627 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8628 ScreenLinesUC + LineOffset[old_row],
8629 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008630 for (i = 0; i < p_mco; ++i)
8631 mch_memmove(new_ScreenLinesC[i]
8632 + new_LineOffset[new_row],
8633 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634 (size_t)len * sizeof(u8char_T));
8635 }
8636 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8637 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8638 ScreenLines2 + LineOffset[old_row],
8639 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8641 ScreenAttrs + LineOffset[old_row],
8642 (size_t)len * sizeof(sattr_T));
8643 }
8644 }
8645 }
8646 /* Use the last line of the screen for the current line. */
8647 current_ScreenLine = new_ScreenLines + Rows * Columns;
Bram Moolenaar6ace95e2019-08-13 23:09:49 +02008648
8649#ifdef FEAT_TEXT_PROP
8650 vim_memset(new_popup_mask, 0, Rows * Columns * sizeof(short));
8651 vim_memset(new_popup_transparent, 0, Rows * Columns * sizeof(char));
8652#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653 }
8654
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008655 free_screenlines();
8656
Bram Moolenaar6ace95e2019-08-13 23:09:49 +02008657 // NOTE: this may result in all pointers to become NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008658 ScreenLines = new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008659 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008660 for (i = 0; i < p_mco; ++i)
8661 ScreenLinesC[i] = new_ScreenLinesC[i];
8662 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663 ScreenLines2 = new_ScreenLines2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008664 ScreenAttrs = new_ScreenAttrs;
8665 LineOffset = new_LineOffset;
8666 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008667 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar33796b32019-06-08 16:01:13 +02008668#ifdef FEAT_TEXT_PROP
8669 popup_mask = new_popup_mask;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008670 popup_mask_next = new_popup_mask_next;
8671 popup_transparent = new_popup_transparent;
Bram Moolenaar33796b32019-06-08 16:01:13 +02008672 popup_mask_refresh = TRUE;
8673#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674
8675 /* It's important that screen_Rows and screen_Columns reflect the actual
8676 * size of ScreenLines[]. Set them before calling anything. */
8677#ifdef FEAT_GUI
8678 old_Rows = screen_Rows;
8679#endif
8680 screen_Rows = Rows;
8681 screen_Columns = Columns;
8682
8683 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008684 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685 screenclear2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686#ifdef FEAT_GUI
8687 else if (gui.in_use
8688 && !gui.starting
8689 && ScreenLines != NULL
8690 && old_Rows != Rows)
8691 {
8692 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8693 /*
8694 * Adjust the position of the cursor, for when executing an external
8695 * command.
8696 */
8697 if (msg_row >= Rows) /* Rows got smaller */
8698 msg_row = Rows - 1; /* put cursor at last row */
8699 else if (Rows > old_Rows) /* Rows got bigger */
8700 msg_row += Rows - old_Rows; /* put cursor in same place */
8701 if (msg_col >= Columns) /* Columns got smaller */
8702 msg_col = Columns - 1; /* put cursor at last column */
8703 }
8704#endif
Bram Moolenaarca57ab52019-04-13 14:53:16 +02008705 clear_TabPageIdxs();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008706
Bram Moolenaar071d4272004-06-13 20:20:40 +00008707 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008708 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008709
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008710 /*
8711 * Do not apply autocommands more than 3 times to avoid an endless loop
8712 * in case applying autocommands always changes Rows or Columns.
8713 */
8714 if (starting == 0 && ++retry_count <= 3)
8715 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008716 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008717 /* In rare cases, autocommands may have altered Rows or Columns,
8718 * jump back to check if we need to allocate the screen again. */
8719 goto retry;
8720 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721}
8722
8723 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008724free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008725{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008726 int i;
8727
Bram Moolenaar33796b32019-06-08 16:01:13 +02008728 VIM_CLEAR(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008729 for (i = 0; i < Screen_mco; ++i)
Bram Moolenaar33796b32019-06-08 16:01:13 +02008730 VIM_CLEAR(ScreenLinesC[i]);
8731 VIM_CLEAR(ScreenLines2);
8732 VIM_CLEAR(ScreenLines);
8733 VIM_CLEAR(ScreenAttrs);
8734 VIM_CLEAR(LineOffset);
8735 VIM_CLEAR(LineWraps);
8736 VIM_CLEAR(TabPageIdxs);
8737#ifdef FEAT_TEXT_PROP
8738 VIM_CLEAR(popup_mask);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02008739 VIM_CLEAR(popup_mask_next);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008740 VIM_CLEAR(popup_transparent);
Bram Moolenaar33796b32019-06-08 16:01:13 +02008741#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008742}
8743
8744 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008745screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746{
8747 check_for_delay(FALSE);
8748 screenalloc(FALSE); /* allocate screen buffers if size changed */
8749 screenclear2(); /* clear the screen */
8750}
8751
8752 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008753screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754{
8755 int i;
8756
8757 if (starting == NO_SCREEN || ScreenLines == NULL
8758#ifdef FEAT_GUI
8759 || (gui.in_use && gui.starting)
8760#endif
8761 )
8762 return;
8763
8764#ifdef FEAT_GUI
8765 if (!gui.in_use)
8766#endif
8767 screen_attr = -1; /* force setting the Normal colors */
8768 screen_stop_highlight(); /* don't want highlighting here */
8769
8770#ifdef FEAT_CLIPBOARD
8771 /* disable selection without redrawing it */
8772 clip_scroll_selection(9999);
8773#endif
8774
8775 /* blank out ScreenLines */
8776 for (i = 0; i < Rows; ++i)
8777 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02008778 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779 LineWraps[i] = FALSE;
8780 }
8781
8782 if (can_clear(T_CL))
8783 {
8784 out_str(T_CL); /* clear the display */
8785 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008786 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787 }
8788 else
8789 {
8790 /* can't clear the screen, mark all chars with invalid attributes */
8791 for (i = 0; i < Rows; ++i)
8792 lineinvalid(LineOffset[i], (int)Columns);
8793 clear_cmdline = TRUE;
8794 }
8795
8796 screen_cleared = TRUE; /* can use contents of ScreenLines now */
8797
8798 win_rest_invalid(firstwin);
8799 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008800 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801 if (must_redraw == CLEAR) /* no need to clear again */
8802 must_redraw = NOT_VALID;
8803 compute_cmdrow();
8804 msg_row = cmdline_row; /* put cursor on last line for messages */
8805 msg_col = 0;
8806 screen_start(); /* don't know where cursor is now */
8807 msg_scrolled = 0; /* can't scroll back */
8808 msg_didany = FALSE;
8809 msg_didout = FALSE;
8810}
8811
8812/*
8813 * Clear one line in ScreenLines.
8814 */
8815 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02008816lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008817{
8818 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008819 if (enc_utf8)
8820 (void)vim_memset(ScreenLinesUC + off, 0,
8821 (size_t)width * sizeof(u8char_T));
Bram Moolenaarcfce7172017-08-17 20:31:48 +02008822 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008823}
8824
8825/*
8826 * Mark one line in ScreenLines invalid by setting the attributes to an
8827 * invalid value.
8828 */
8829 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008830lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831{
8832 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
8833}
8834
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835/*
8836 * Copy part of a Screenline for vertically split window "wp".
8837 */
8838 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008839linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840{
8841 unsigned off_to = LineOffset[to] + wp->w_wincol;
8842 unsigned off_from = LineOffset[from] + wp->w_wincol;
8843
8844 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
8845 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846 if (enc_utf8)
8847 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008848 int i;
8849
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
8851 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008852 for (i = 0; i < p_mco; ++i)
8853 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
8854 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855 }
8856 if (enc_dbcs == DBCS_JPNU)
8857 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
8858 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
8860 wp->w_width * sizeof(sattr_T));
8861}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862
8863/*
8864 * Return TRUE if clearing with term string "p" would work.
8865 * It can't work when the string is empty or it won't set the right background.
Bram Moolenaar33796b32019-06-08 16:01:13 +02008866 * Don't clear to end-of-line when there are popups, it may cause flicker.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867 */
8868 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008869can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870{
8871 return (*p != NUL && (t_colors <= 1
8872#ifdef FEAT_GUI
8873 || gui.in_use
8874#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008875#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008876 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02008877 || (!p_tgc && cterm_normal_bg_color == 0)
8878#else
8879 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008880#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02008881 || *T_UT != NUL)
8882#ifdef FEAT_TEXT_PROP
8883 && !(p == T_CE && popup_visible)
8884#endif
8885 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00008886}
8887
8888/*
8889 * Reset cursor position. Use whenever cursor was moved because of outputting
8890 * something directly to the screen (shell commands) or a terminal control
8891 * code.
8892 */
8893 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008894screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895{
8896 screen_cur_row = screen_cur_col = 9999;
8897}
8898
8899/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900 * Move the cursor to position "row","col" in the screen.
8901 * This tries to find the most efficient way to move, minimizing the number of
8902 * characters sent to the terminal.
8903 */
8904 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008905windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008907 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908 int i;
8909 int plan;
8910 int cost;
8911 int wouldbe_col;
8912 int noinvcurs;
8913 char_u *bs;
8914 int goto_cost;
8915 int attr;
8916
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008917#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
8919
8920#define PLAN_LE 1
8921#define PLAN_CR 2
8922#define PLAN_NL 3
8923#define PLAN_WRITE 4
8924 /* Can't use ScreenLines unless initialized */
8925 if (ScreenLines == NULL)
8926 return;
8927
8928 if (col != screen_cur_col || row != screen_cur_row)
8929 {
8930 /* Check for valid position. */
8931 if (row < 0) /* window without text lines? */
8932 row = 0;
8933 if (row >= screen_Rows)
8934 row = screen_Rows - 1;
8935 if (col >= screen_Columns)
8936 col = screen_Columns - 1;
8937
8938 /* check if no cursor movement is allowed in highlight mode */
8939 if (screen_attr && *T_MS == NUL)
8940 noinvcurs = HIGHL_COST;
8941 else
8942 noinvcurs = 0;
8943 goto_cost = GOTO_COST + noinvcurs;
8944
8945 /*
8946 * Plan how to do the positioning:
8947 * 1. Use CR to move it to column 0, same row.
8948 * 2. Use T_LE to move it a few columns to the left.
8949 * 3. Use NL to move a few lines down, column 0.
8950 * 4. Move a few columns to the right with T_ND or by writing chars.
8951 *
8952 * Don't do this if the cursor went beyond the last column, the cursor
8953 * position is unknown then (some terminals wrap, some don't )
8954 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008955 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00008956 * characters to move the cursor to the right.
8957 */
8958 if (row >= screen_cur_row && screen_cur_col < Columns)
8959 {
8960 /*
8961 * If the cursor is in the same row, bigger col, we can use CR
8962 * or T_LE.
8963 */
8964 bs = NULL; /* init for GCC */
8965 attr = screen_attr;
8966 if (row == screen_cur_row && col < screen_cur_col)
8967 {
8968 /* "le" is preferred over "bc", because "bc" is obsolete */
8969 if (*T_LE)
8970 bs = T_LE; /* "cursor left" */
8971 else
8972 bs = T_BC; /* "backspace character (old) */
8973 if (*bs)
8974 cost = (screen_cur_col - col) * (int)STRLEN(bs);
8975 else
8976 cost = 999;
8977 if (col + 1 < cost) /* using CR is less characters */
8978 {
8979 plan = PLAN_CR;
8980 wouldbe_col = 0;
8981 cost = 1; /* CR is just one character */
8982 }
8983 else
8984 {
8985 plan = PLAN_LE;
8986 wouldbe_col = col;
8987 }
8988 if (noinvcurs) /* will stop highlighting */
8989 {
8990 cost += noinvcurs;
8991 attr = 0;
8992 }
8993 }
8994
8995 /*
8996 * If the cursor is above where we want to be, we can use CR LF.
8997 */
8998 else if (row > screen_cur_row)
8999 {
9000 plan = PLAN_NL;
9001 wouldbe_col = 0;
9002 cost = (row - screen_cur_row) * 2; /* CR LF */
9003 if (noinvcurs) /* will stop highlighting */
9004 {
9005 cost += noinvcurs;
9006 attr = 0;
9007 }
9008 }
9009
9010 /*
9011 * If the cursor is in the same row, smaller col, just use write.
9012 */
9013 else
9014 {
9015 plan = PLAN_WRITE;
9016 wouldbe_col = screen_cur_col;
9017 cost = 0;
9018 }
9019
9020 /*
9021 * Check if any characters that need to be written have the
9022 * correct attributes. Also avoid UTF-8 characters.
9023 */
9024 i = col - wouldbe_col;
9025 if (i > 0)
9026 cost += i;
9027 if (cost < goto_cost && i > 0)
9028 {
9029 /*
9030 * Check if the attributes are correct without additionally
9031 * stopping highlighting.
9032 */
9033 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9034 while (i && *p++ == attr)
9035 --i;
9036 if (i != 0)
9037 {
9038 /*
9039 * Try if it works when highlighting is stopped here.
9040 */
9041 if (*--p == 0)
9042 {
9043 cost += noinvcurs;
9044 while (i && *p++ == 0)
9045 --i;
9046 }
9047 if (i != 0)
9048 cost = 999; /* different attributes, don't do it */
9049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050 if (enc_utf8)
9051 {
9052 /* Don't use an UTF-8 char for positioning, it's slow. */
9053 for (i = wouldbe_col; i < col; ++i)
9054 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9055 {
9056 cost = 999;
9057 break;
9058 }
9059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009060 }
9061
9062 /*
9063 * We can do it without term_windgoto()!
9064 */
9065 if (cost < goto_cost)
9066 {
9067 if (plan == PLAN_LE)
9068 {
9069 if (noinvcurs)
9070 screen_stop_highlight();
9071 while (screen_cur_col > col)
9072 {
9073 out_str(bs);
9074 --screen_cur_col;
9075 }
9076 }
9077 else if (plan == PLAN_CR)
9078 {
9079 if (noinvcurs)
9080 screen_stop_highlight();
9081 out_char('\r');
9082 screen_cur_col = 0;
9083 }
9084 else if (plan == PLAN_NL)
9085 {
9086 if (noinvcurs)
9087 screen_stop_highlight();
9088 while (screen_cur_row < row)
9089 {
9090 out_char('\n');
9091 ++screen_cur_row;
9092 }
9093 screen_cur_col = 0;
9094 }
9095
9096 i = col - screen_cur_col;
9097 if (i > 0)
9098 {
9099 /*
9100 * Use cursor-right if it's one character only. Avoids
9101 * removing a line of pixels from the last bold char, when
9102 * using the bold trick in the GUI.
9103 */
9104 if (T_ND[0] != NUL && T_ND[1] == NUL)
9105 {
9106 while (i-- > 0)
9107 out_char(*T_ND);
9108 }
9109 else
9110 {
9111 int off;
9112
9113 off = LineOffset[row] + screen_cur_col;
9114 while (i-- > 0)
9115 {
9116 if (ScreenAttrs[off] != screen_attr)
9117 screen_stop_highlight();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009118 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009119 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009120 if (enc_dbcs == DBCS_JPNU
9121 && ScreenLines[off] == 0x8e)
9122 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009123 ++off;
9124 }
9125 }
9126 }
9127 }
9128 }
9129 else
9130 cost = 999;
9131
9132 if (cost >= goto_cost)
9133 {
9134 if (noinvcurs)
9135 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009136 if (row == screen_cur_row && (col > screen_cur_col)
9137 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009138 term_cursor_right(col - screen_cur_col);
9139 else
9140 term_windgoto(row, col);
9141 }
9142 screen_cur_row = row;
9143 screen_cur_col = col;
9144 }
9145}
9146
9147/*
9148 * Set cursor to its position in the current window.
9149 */
9150 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009151setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009152{
Bram Moolenaar987723e2018-03-06 11:43:04 +01009153 setcursor_mayforce(FALSE);
9154}
9155
9156/*
9157 * Set cursor to its position in the current window.
9158 * When "force" is TRUE also when not redrawing.
9159 */
9160 void
9161setcursor_mayforce(int force)
9162{
9163 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009164 {
9165 validate_cursor();
9166 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009167 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009169 /* With 'rightleft' set and the cursor on a double-wide
9170 * character, position it on the leftmost column. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01009171 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol
9172 - ((has_mbyte
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009173 && (*mb_ptr2cells)(ml_get_cursor()) == 2
Bram Moolenaara12a1612019-01-24 16:39:02 +01009174 && vim_isprintc(gchar_cursor())) ? 2 : 1)) :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009175#endif
9176 curwin->w_wcol));
9177 }
9178}
9179
9180
9181/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009182 * Insert 'line_count' lines at 'row' in window 'wp'.
9183 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9184 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185 * scrolling.
9186 * Returns FAIL if the lines are not inserted, OK for success.
9187 */
9188 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009189win_ins_lines(
9190 win_T *wp,
9191 int row,
9192 int line_count,
9193 int invalid,
9194 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195{
9196 int did_delete;
9197 int nextrow;
9198 int lastrow;
9199 int retval;
9200
9201 if (invalid)
9202 wp->w_lines_valid = 0;
9203
9204 if (wp->w_height < 5)
9205 return FAIL;
9206
9207 if (line_count > wp->w_height - row)
9208 line_count = wp->w_height - row;
9209
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009210 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009211 if (retval != MAYBE)
9212 return retval;
9213
9214 /*
9215 * If there is a next window or a status line, we first try to delete the
9216 * lines at the bottom to avoid messing what is after the window.
Bram Moolenaarc363fe12019-08-04 18:13:46 +02009217 * If this fails and there are following windows, don't do anything to
9218 * avoid messing up those windows, better just redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009219 */
9220 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009221 if (wp->w_next != NULL || wp->w_status_height)
9222 {
9223 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009224 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009225 did_delete = TRUE;
9226 else if (wp->w_next)
9227 return FAIL;
9228 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009229 /*
9230 * if no lines deleted, blank the lines that will end up below the window
9231 */
9232 if (!did_delete)
9233 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009234 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009235 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02009236 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009237 lastrow = nextrow + line_count;
9238 if (lastrow > Rows)
9239 lastrow = Rows;
9240 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009241 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009242 ' ', ' ', 0);
9243 }
9244
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009245 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246 == FAIL)
9247 {
Bram Moolenaarc363fe12019-08-04 18:13:46 +02009248 // deletion will have messed up other windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00009249 if (did_delete)
9250 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009251 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009252 win_rest_invalid(W_NEXT(wp));
9253 }
9254 return FAIL;
9255 }
9256
9257 return OK;
9258}
9259
9260/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009261 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009262 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9263 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9264 * scrolling
9265 * Return OK for success, FAIL if the lines are not deleted.
9266 */
9267 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009268win_del_lines(
9269 win_T *wp,
9270 int row,
9271 int line_count,
9272 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009273 int mayclear,
9274 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009275{
9276 int retval;
9277
9278 if (invalid)
9279 wp->w_lines_valid = 0;
9280
9281 if (line_count > wp->w_height - row)
9282 line_count = wp->w_height - row;
9283
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009284 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009285 if (retval != MAYBE)
9286 return retval;
9287
9288 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009289 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009290 return FAIL;
9291
Bram Moolenaar071d4272004-06-13 20:20:40 +00009292 /*
9293 * If there are windows or status lines below, try to put them at the
9294 * correct place. If we can't do that, they have to be redrawn.
9295 */
9296 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9297 {
9298 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009299 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009300 {
9301 wp->w_redr_status = TRUE;
9302 win_rest_invalid(wp->w_next);
9303 }
9304 }
9305 /*
9306 * If this is the last window and there is no status line, redraw the
9307 * command line later.
9308 */
9309 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009310 redraw_cmdline = TRUE;
9311 return OK;
9312}
9313
9314/*
9315 * Common code for win_ins_lines() and win_del_lines().
9316 * Returns OK or FAIL when the work has been done.
9317 * Returns MAYBE when not finished yet.
9318 */
9319 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009320win_do_lines(
9321 win_T *wp,
9322 int row,
9323 int line_count,
9324 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009325 int del,
9326 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009327{
9328 int retval;
9329
9330 if (!redrawing() || line_count <= 0)
9331 return FAIL;
9332
Bram Moolenaar33796b32019-06-08 16:01:13 +02009333 // When inserting lines would result in loss of command output, just redraw
9334 // the lines.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009335 if (no_win_do_lines_ins && !del)
9336 return FAIL;
9337
Bram Moolenaar33796b32019-06-08 16:01:13 +02009338 // only a few lines left: redraw is faster
Bram Moolenaar4033c552017-09-16 20:54:51 +02009339 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009340 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009341 if (!no_win_do_lines_ins)
Bram Moolenaar33796b32019-06-08 16:01:13 +02009342 screenclear(); // will set wp->w_lines_valid to 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00009343 return FAIL;
9344 }
9345
Bram Moolenaar33796b32019-06-08 16:01:13 +02009346#ifdef FEAT_TEXT_PROP
Bram Moolenaar4c063a02019-06-10 21:24:12 +02009347 // this doesn't work when there are popups visible
Bram Moolenaar33796b32019-06-08 16:01:13 +02009348 if (popup_visible)
9349 return FAIL;
9350#endif
9351
9352 // Delete all remaining lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00009353 if (row + line_count >= wp->w_height)
9354 {
9355 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009356 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009357 ' ', ' ', 0);
9358 return OK;
9359 }
9360
9361 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009362 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009363 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009364 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009365 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009366 if (!no_win_do_lines_ins)
9367 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009368
9369 /*
9370 * If the terminal can set a scroll region, use that.
9371 * Always do this in a vertically split window. This will redraw from
9372 * ScreenLines[] when t_CV isn't defined. That's faster than using
9373 * win_line().
9374 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009375 * a character in the lower right corner of the scroll region may cause a
9376 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009377 */
Bram Moolenaar02631462017-09-22 15:20:32 +02009378 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009379 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009380 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009381 scroll_region_set(wp, row);
9382 if (del)
9383 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009384 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009385 else
9386 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009387 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009389 scroll_region_reset();
9390 return retval;
9391 }
9392
Bram Moolenaar071d4272004-06-13 20:20:40 +00009393 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9394 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009395
9396 return MAYBE;
9397}
9398
9399/*
9400 * window 'wp' and everything after it is messed up, mark it for redraw
9401 */
9402 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009403win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009404{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009405 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009406 {
9407 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009408 wp->w_redr_status = TRUE;
9409 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009410 }
9411 redraw_cmdline = TRUE;
9412}
9413
9414/*
9415 * The rest of the routines in this file perform screen manipulations. The
9416 * given operation is performed physically on the screen. The corresponding
9417 * change is also made to the internal screen image. In this way, the editor
9418 * anticipates the effect of editing changes on the appearance of the screen.
9419 * That way, when we call screenupdate a complete redraw isn't usually
9420 * necessary. Another advantage is that we can keep adding code to anticipate
9421 * screen changes, and in the meantime, everything still works.
9422 */
9423
9424/*
9425 * types for inserting or deleting lines
9426 */
9427#define USE_T_CAL 1
9428#define USE_T_CDL 2
9429#define USE_T_AL 3
9430#define USE_T_CE 4
9431#define USE_T_DL 5
9432#define USE_T_SR 6
9433#define USE_NL 7
9434#define USE_T_CD 8
9435#define USE_REDRAW 9
9436
9437/*
9438 * insert lines on the screen and update ScreenLines[]
9439 * 'end' is the line after the scrolled part. Normally it is Rows.
9440 * When scrolling region used 'off' is the offset from the top for the region.
9441 * 'row' and 'end' are relative to the start of the region.
9442 *
9443 * return FAIL for failure, OK for success.
9444 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009445 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009446screen_ins_lines(
9447 int off,
9448 int row,
9449 int line_count,
9450 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009451 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01009452 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453{
9454 int i;
9455 int j;
9456 unsigned temp;
9457 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009458 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009459 int type;
9460 int result_empty;
9461 int can_ce = can_clear(T_CE);
9462
9463 /*
9464 * FAIL if
9465 * - there is no valid screen
9466 * - the screen has to be redrawn completely
9467 * - the line count is less than one
9468 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009469 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar33796b32019-06-08 16:01:13 +02009470 * - there is a popup window
Bram Moolenaar071d4272004-06-13 20:20:40 +00009471 */
Bram Moolenaar33796b32019-06-08 16:01:13 +02009472 if (!screen_valid(TRUE)
9473 || line_count <= 0 || line_count > p_ttyscroll
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009474#ifdef FEAT_CLIPBOARD
9475 || (clip_star.state != SELECT_CLEARED
9476 && redrawing_for_callback > 0)
9477#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02009478#ifdef FEAT_TEXT_PROP
9479 || popup_visible
9480#endif
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009481 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009482 return FAIL;
9483
9484 /*
9485 * There are seven ways to insert lines:
9486 * 0. When in a vertically split window and t_CV isn't set, redraw the
9487 * characters from ScreenLines[].
9488 * 1. Use T_CD (clear to end of display) if it exists and the result of
9489 * the insert is just empty lines
9490 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9491 * present or line_count > 1. It looks better if we do all the inserts
9492 * at once.
9493 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9494 * insert is just empty lines and T_CE is not present or line_count >
9495 * 1.
9496 * 4. Use T_AL (insert line) if it exists.
9497 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9498 * just empty lines.
9499 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9500 * just empty lines.
9501 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9502 * the 'da' flag is not set or we have clear line capability.
9503 * 8. redraw the characters from ScreenLines[].
9504 *
9505 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9506 * the scrollbar for the window. It does have insert line, use that if it
9507 * exists.
9508 */
9509 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009510 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9511 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009512 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009513 type = USE_T_CD;
9514 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9515 type = USE_T_CAL;
9516 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9517 type = USE_T_CDL;
9518 else if (*T_AL != NUL)
9519 type = USE_T_AL;
9520 else if (can_ce && result_empty)
9521 type = USE_T_CE;
9522 else if (*T_DL != NUL && result_empty)
9523 type = USE_T_DL;
9524 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9525 type = USE_T_SR;
9526 else
9527 return FAIL;
9528
9529 /*
9530 * For clearing the lines screen_del_lines() is used. This will also take
9531 * care of t_db if necessary.
9532 */
9533 if (type == USE_T_CD || type == USE_T_CDL ||
9534 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009535 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009536
9537 /*
9538 * If text is retained below the screen, first clear or delete as many
9539 * lines at the bottom of the window as are about to be inserted so that
9540 * the deleted lines won't later surface during a screen_del_lines.
9541 */
9542 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009543 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009544
9545#ifdef FEAT_CLIPBOARD
9546 /* Remove a modeless selection when inserting lines halfway the screen
9547 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009548 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009549 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009550 else
9551 clip_scroll_selection(-line_count);
9552#endif
9553
Bram Moolenaar071d4272004-06-13 20:20:40 +00009554#ifdef FEAT_GUI
9555 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9556 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009557 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558#endif
9559
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009560 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
9561 cursor_col = wp->w_wincol;
9562
Bram Moolenaar071d4272004-06-13 20:20:40 +00009563 if (*T_CCS != NUL) /* cursor relative to region */
9564 cursor_row = row;
9565 else
9566 cursor_row = row + off;
9567
9568 /*
9569 * Shift LineOffset[] line_count down to reflect the inserted lines.
9570 * Clear the inserted lines in ScreenLines[].
9571 */
9572 row += off;
9573 end += off;
9574 for (i = 0; i < line_count; ++i)
9575 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009576 if (wp != NULL && wp->w_width != Columns)
9577 {
9578 /* need to copy part of a line */
9579 j = end - 1 - i;
9580 while ((j -= line_count) >= row)
9581 linecopy(j + line_count, j, wp);
9582 j += line_count;
9583 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009584 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
9585 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009586 else
9587 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9588 LineWraps[j] = FALSE;
9589 }
9590 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009591 {
9592 j = end - 1 - i;
9593 temp = LineOffset[j];
9594 while ((j -= line_count) >= row)
9595 {
9596 LineOffset[j + line_count] = LineOffset[j];
9597 LineWraps[j + line_count] = LineWraps[j];
9598 }
9599 LineOffset[j + line_count] = temp;
9600 LineWraps[j + line_count] = FALSE;
9601 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009602 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603 else
9604 lineinvalid(temp, (int)Columns);
9605 }
9606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607
9608 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009609 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009610 if (clear_attr != 0)
9611 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009612
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613 /* redraw the characters */
9614 if (type == USE_REDRAW)
9615 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02009616 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009617 {
9618 term_append_lines(line_count);
9619 screen_start(); /* don't know where cursor is now */
9620 }
9621 else
9622 {
9623 for (i = 0; i < line_count; i++)
9624 {
9625 if (type == USE_T_AL)
9626 {
9627 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009628 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009629 out_str(T_AL);
9630 }
9631 else /* type == USE_T_SR */
9632 out_str(T_SR);
9633 screen_start(); /* don't know where cursor is now */
9634 }
9635 }
9636
9637 /*
9638 * With scroll-reverse and 'da' flag set we need to clear the lines that
9639 * have been scrolled down into the region.
9640 */
9641 if (type == USE_T_SR && *T_DA)
9642 {
9643 for (i = 0; i < line_count; ++i)
9644 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009645 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009646 out_str(T_CE);
9647 screen_start(); /* don't know where cursor is now */
9648 }
9649 }
9650
9651#ifdef FEAT_GUI
9652 gui_can_update_cursor();
9653 if (gui.in_use)
9654 out_flush(); /* always flush after a scroll */
9655#endif
9656 return OK;
9657}
9658
9659/*
Bram Moolenaar107abd22016-08-12 14:08:25 +02009660 * Delete lines on the screen and update ScreenLines[].
9661 * "end" is the line after the scrolled part. Normally it is Rows.
9662 * When scrolling region used "off" is the offset from the top for the region.
9663 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664 *
9665 * Return OK for success, FAIL if the lines are not deleted.
9666 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009667 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009668screen_del_lines(
9669 int off,
9670 int row,
9671 int line_count,
9672 int end,
9673 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009674 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +01009675 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009676{
9677 int j;
9678 int i;
9679 unsigned temp;
9680 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009681 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009682 int cursor_end;
9683 int result_empty; /* result is empty until end of region */
9684 int can_delete; /* deleting line codes can be used */
9685 int type;
9686
9687 /*
9688 * FAIL if
9689 * - there is no valid screen
9690 * - the screen has to be redrawn completely
9691 * - the line count is less than one
9692 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009693 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009694 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009695 if (!screen_valid(TRUE) || line_count <= 0
9696 || (!force && line_count > p_ttyscroll)
9697#ifdef FEAT_CLIPBOARD
9698 || (clip_star.state != SELECT_CLEARED
9699 && redrawing_for_callback > 0)
9700#endif
9701 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009702 return FAIL;
9703
9704 /*
9705 * Check if the rest of the current region will become empty.
9706 */
9707 result_empty = row + line_count >= end;
9708
9709 /*
9710 * We can delete lines only when 'db' flag not set or when 'ce' option
9711 * available.
9712 */
9713 can_delete = (*T_DB == NUL || can_clear(T_CE));
9714
9715 /*
9716 * There are six ways to delete lines:
9717 * 0. When in a vertically split window and t_CV isn't set, redraw the
9718 * characters from ScreenLines[].
9719 * 1. Use T_CD if it exists and the result is empty.
9720 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9721 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9722 * none of the other ways work.
9723 * 4. Use T_CE (erase line) if the result is empty.
9724 * 5. Use T_DL (delete line) if it exists.
9725 * 6. redraw the characters from ScreenLines[].
9726 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009727 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9728 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009729 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009730 type = USE_T_CD;
9731#if defined(__BEOS__) && defined(BEOS_DR8)
9732 /*
9733 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9734 * its internal termcap... this works okay for tests which test *T_DB !=
9735 * NUL. It has the disadvantage that the user cannot use any :set t_*
9736 * command to get T_DB (back) to empty_option, only :set term=... will do
9737 * the trick...
9738 * Anyway, this hack will hopefully go away with the next OS release.
9739 * (Olaf Seibert)
9740 */
9741 else if (row == 0 && T_DB == empty_option
9742 && (line_count == 1 || *T_CDL == NUL))
9743#else
9744 else if (row == 0 && (
9745#ifndef AMIGA
9746 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9747 * up, so use delete-line command */
9748 line_count == 1 ||
9749#endif
9750 *T_CDL == NUL))
9751#endif
9752 type = USE_NL;
9753 else if (*T_CDL != NUL && line_count > 1 && can_delete)
9754 type = USE_T_CDL;
9755 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +02009756 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757 type = USE_T_CE;
9758 else if (*T_DL != NUL && can_delete)
9759 type = USE_T_DL;
9760 else if (*T_CDL != NUL && can_delete)
9761 type = USE_T_CDL;
9762 else
9763 return FAIL;
9764
9765#ifdef FEAT_CLIPBOARD
9766 /* Remove a modeless selection when deleting lines halfway the screen or
9767 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009768 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009769 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009770 else
9771 clip_scroll_selection(line_count);
9772#endif
9773
Bram Moolenaar071d4272004-06-13 20:20:40 +00009774#ifdef FEAT_GUI
9775 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9776 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009777 gui_dont_update_cursor(gui.cursor_row >= row + off
9778 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009779#endif
9780
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009781 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
9782 cursor_col = wp->w_wincol;
9783
Bram Moolenaar071d4272004-06-13 20:20:40 +00009784 if (*T_CCS != NUL) /* cursor relative to region */
9785 {
9786 cursor_row = row;
9787 cursor_end = end;
9788 }
9789 else
9790 {
9791 cursor_row = row + off;
9792 cursor_end = end + off;
9793 }
9794
9795 /*
9796 * Now shift LineOffset[] line_count up to reflect the deleted lines.
9797 * Clear the inserted lines in ScreenLines[].
9798 */
9799 row += off;
9800 end += off;
9801 for (i = 0; i < line_count; ++i)
9802 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009803 if (wp != NULL && wp->w_width != Columns)
9804 {
9805 /* need to copy part of a line */
9806 j = row + i;
9807 while ((j += line_count) <= end - 1)
9808 linecopy(j - line_count, j, wp);
9809 j -= line_count;
9810 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009811 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
9812 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009813 else
9814 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9815 LineWraps[j] = FALSE;
9816 }
9817 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009818 {
9819 /* whole width, moving the line pointers is faster */
9820 j = row + i;
9821 temp = LineOffset[j];
9822 while ((j += line_count) <= end - 1)
9823 {
9824 LineOffset[j - line_count] = LineOffset[j];
9825 LineWraps[j - line_count] = LineWraps[j];
9826 }
9827 LineOffset[j - line_count] = temp;
9828 LineWraps[j - line_count] = FALSE;
9829 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009830 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009831 else
9832 lineinvalid(temp, (int)Columns);
9833 }
9834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009835
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009836 if (screen_attr != clear_attr)
9837 screen_stop_highlight();
9838 if (clear_attr != 0)
9839 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009840
Bram Moolenaar071d4272004-06-13 20:20:40 +00009841 /* redraw the characters */
9842 if (type == USE_REDRAW)
9843 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02009844 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009845 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009846 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009847 out_str(T_CD);
9848 screen_start(); /* don't know where cursor is now */
9849 }
9850 else if (type == USE_T_CDL)
9851 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009852 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009853 term_delete_lines(line_count);
9854 screen_start(); /* don't know where cursor is now */
9855 }
9856 /*
9857 * Deleting lines at top of the screen or scroll region: Just scroll
9858 * the whole screen (scroll region) up by outputting newlines on the
9859 * last line.
9860 */
9861 else if (type == USE_NL)
9862 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009863 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009864 for (i = line_count; --i >= 0; )
9865 out_char('\n'); /* cursor will remain on same line */
9866 }
9867 else
9868 {
9869 for (i = line_count; --i >= 0; )
9870 {
9871 if (type == USE_T_DL)
9872 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009873 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009874 out_str(T_DL); /* delete a line */
9875 }
9876 else /* type == USE_T_CE */
9877 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009878 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009879 out_str(T_CE); /* erase a line */
9880 }
9881 screen_start(); /* don't know where cursor is now */
9882 }
9883 }
9884
9885 /*
9886 * If the 'db' flag is set, we need to clear the lines that have been
9887 * scrolled up at the bottom of the region.
9888 */
9889 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
9890 {
9891 for (i = line_count; i > 0; --i)
9892 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009893 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009894 out_str(T_CE); /* erase a line */
9895 screen_start(); /* don't know where cursor is now */
9896 }
9897 }
9898
9899#ifdef FEAT_GUI
9900 gui_can_update_cursor();
9901 if (gui.in_use)
9902 out_flush(); /* always flush after a scroll */
9903#endif
9904
9905 return OK;
9906}
9907
9908/*
Bram Moolenaarcb574f42019-01-25 22:29:57 +01009909 * Return TRUE when postponing displaying the mode message: when not redrawing
9910 * or inside a mapping.
9911 */
9912 int
9913skip_showmode()
9914{
9915 // Call char_avail() only when we are going to show something, because it
9916 // takes a bit of time. redrawing() may also call char_avail_avail().
9917 if (global_busy
9918 || msg_silent != 0
9919 || !redrawing()
9920 || (char_avail() && !KeyTyped))
9921 {
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02009922 redraw_mode = TRUE; // show mode later
Bram Moolenaarcb574f42019-01-25 22:29:57 +01009923 return TRUE;
9924 }
9925 return FALSE;
9926}
9927
9928/*
Bram Moolenaar81226e02018-02-20 21:44:45 +01009929 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009930 *
9931 * If clear_cmdline is TRUE, clear the rest of the cmdline.
9932 * If clear_cmdline is FALSE there may be a message there that needs to be
9933 * cleared only if a mode is shown.
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02009934 * If redraw_mode is TRUE show or clear the mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009935 * Return the length of the message (0 if no message).
9936 */
9937 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009938showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009939{
9940 int need_clear;
9941 int length = 0;
9942 int do_mode;
9943 int attr;
9944 int nwr_save;
9945#ifdef FEAT_INS_EXPAND
9946 int sub_attr;
9947#endif
9948
Bram Moolenaar7df351e2006-01-23 22:30:28 +00009949 do_mode = ((p_smd && msg_silent == 0)
9950 && ((State & INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +02009951 || restart_edit != NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009952 || VIsual_active));
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009953 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009954 {
Bram Moolenaarcb574f42019-01-25 22:29:57 +01009955 if (skip_showmode())
9956 return 0; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00009957
9958 nwr_save = need_wait_return;
9959
9960 /* wait a bit before overwriting an important message */
9961 check_for_delay(FALSE);
9962
9963 /* if the cmdline is more than one line high, erase top lines */
9964 need_clear = clear_cmdline;
9965 if (clear_cmdline && cmdline_row < Rows - 1)
9966 msg_clr_cmdline(); /* will reset clear_cmdline */
9967
9968 /* Position on the last line in the window, column 0 */
9969 msg_pos_mode();
9970 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +01009971 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009972 if (do_mode)
9973 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01009974 msg_puts_attr("--", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009975#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00009976 if (
Bram Moolenaar09092152010-08-08 16:38:42 +02009977# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +00009978 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +02009979# else
Bram Moolenaarc236c162008-07-13 17:41:49 +00009980 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +00009981# endif
Bram Moolenaar09092152010-08-08 16:38:42 +02009982 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02009983# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar32526b32019-01-19 17:43:09 +01009984 msg_puts_attr(" IM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009985# else
Bram Moolenaar32526b32019-01-19 17:43:09 +01009986 msg_puts_attr(" XIM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009987# endif
9988#endif
9989#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
9990 if (gui.in_use)
9991 {
9992 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +01009993 {
9994 /* HANGUL */
9995 if (enc_utf8)
Bram Moolenaar32526b32019-01-19 17:43:09 +01009996 msg_puts_attr(" \355\225\234\352\270\200", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +01009997 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01009998 msg_puts_attr(" \307\321\261\333", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +01009999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010000 }
10001#endif
10002#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010003 /* CTRL-X in Insert mode */
10004 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010005 {
10006 /* These messages can get long, avoid a wrap in a narrow
10007 * window. Prefer showing edit_submode_extra. */
10008 length = (Rows - msg_row) * Columns - 3;
10009 if (edit_submode_extra != NULL)
10010 length -= vim_strsize(edit_submode_extra);
10011 if (length > 0)
10012 {
10013 if (edit_submode_pre != NULL)
10014 length -= vim_strsize(edit_submode_pre);
10015 if (length - vim_strsize(edit_submode) > 0)
10016 {
10017 if (edit_submode_pre != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010018 msg_puts_attr((char *)edit_submode_pre, attr);
10019 msg_puts_attr((char *)edit_submode, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010020 }
10021 if (edit_submode_extra != NULL)
10022 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010023 msg_puts_attr(" ", attr); /* add a space in between */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010024 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010025 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010026 else
10027 sub_attr = attr;
Bram Moolenaar32526b32019-01-19 17:43:09 +010010028 msg_puts_attr((char *)edit_submode_extra, sub_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010029 }
10030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010031 }
10032 else
10033#endif
10034 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010035 if (State & VREPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010036 msg_puts_attr(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010037 else if (State & REPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010038 msg_puts_attr(_(" REPLACE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010039 else if (State & INSERT)
10040 {
10041#ifdef FEAT_RIGHTLEFT
10042 if (p_ri)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010043 msg_puts_attr(_(" REVERSE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010044#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +010010045 msg_puts_attr(_(" INSERT"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010046 }
Bram Moolenaar942b4542018-06-17 16:23:34 +020010047 else if (restart_edit == 'I' || restart_edit == 'A')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010048 msg_puts_attr(_(" (insert)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010049 else if (restart_edit == 'R')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010050 msg_puts_attr(_(" (replace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010051 else if (restart_edit == 'V')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010052 msg_puts_attr(_(" (vreplace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010053#ifdef FEAT_RIGHTLEFT
10054 if (p_hkmap)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010055 msg_puts_attr(_(" Hebrew"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010056#endif
10057#ifdef FEAT_KEYMAP
10058 if (State & LANGMAP)
10059 {
10060# ifdef FEAT_ARABIC
10061 if (curwin->w_p_arab)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010062 msg_puts_attr(_(" Arabic"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010063 else
10064# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010065 if (get_keymap_str(curwin, (char_u *)" (%s)",
10066 NameBuff, MAXPATHL))
Bram Moolenaar32526b32019-01-19 17:43:09 +010010067 msg_puts_attr((char *)NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010068 }
10069#endif
10070 if ((State & INSERT) && p_paste)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010071 msg_puts_attr(_(" (paste)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010072
Bram Moolenaar071d4272004-06-13 20:20:40 +000010073 if (VIsual_active)
10074 {
10075 char *p;
10076
10077 /* Don't concatenate separate words to avoid translation
10078 * problems. */
10079 switch ((VIsual_select ? 4 : 0)
10080 + (VIsual_mode == Ctrl_V) * 2
10081 + (VIsual_mode == 'V'))
10082 {
10083 case 0: p = N_(" VISUAL"); break;
10084 case 1: p = N_(" VISUAL LINE"); break;
10085 case 2: p = N_(" VISUAL BLOCK"); break;
10086 case 4: p = N_(" SELECT"); break;
10087 case 5: p = N_(" SELECT LINE"); break;
10088 default: p = N_(" SELECT BLOCK"); break;
10089 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010090 msg_puts_attr(_(p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010091 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010092 msg_puts_attr(" --", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010093 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010094
Bram Moolenaar071d4272004-06-13 20:20:40 +000010095 need_clear = TRUE;
10096 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010097 if (reg_recording != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010098#ifdef FEAT_INS_EXPAND
10099 && edit_submode == NULL /* otherwise it gets too long */
10100#endif
10101 )
10102 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010103 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010104 need_clear = TRUE;
10105 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010106
10107 mode_displayed = TRUE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010108 if (need_clear || clear_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010109 msg_clr_eos();
10110 msg_didout = FALSE; /* overwrite this message */
10111 length = msg_col;
10112 msg_col = 0;
10113 need_wait_return = nwr_save; /* never ask for hit-return for this */
10114 }
10115 else if (clear_cmdline && msg_silent == 0)
10116 /* Clear the whole command line. Will reset "clear_cmdline". */
10117 msg_clr_cmdline();
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010118 else if (redraw_mode)
10119 {
10120 msg_pos_mode();
10121 msg_clr_eos();
10122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010123
10124#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010125 /* In Visual mode the size of the selected area must be redrawn. */
10126 if (VIsual_active)
10127 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010128
10129 /* If the last window has no status line, the ruler is after the mode
10130 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010131 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +020010132 win_redr_ruler(lastwin, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010133#endif
10134 redraw_cmdline = FALSE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010135 redraw_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010136 clear_cmdline = FALSE;
10137
10138 return length;
10139}
10140
10141/*
10142 * Position for a mode message.
10143 */
10144 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010145msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010146{
10147 msg_col = 0;
10148 msg_row = Rows - 1;
10149}
10150
10151/*
10152 * Delete mode message. Used when ESC is typed which is expected to end
10153 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010154 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010155 */
10156 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010157unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010158{
10159 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010160 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010161 */
10162 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10163 redraw_cmdline = TRUE; /* delete mode later */
10164 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010165 clearmode();
10166}
10167
10168/*
10169 * Clear the mode message.
10170 */
10171 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010172clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010173{
Bram Moolenaar2abad542018-05-19 14:43:45 +020010174 int save_msg_row = msg_row;
10175 int save_msg_col = msg_col;
10176
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010177 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010178 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010179 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010180 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +020010181
10182 msg_col = save_msg_col;
10183 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010184}
10185
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010186 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010187recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010188{
Bram Moolenaar32526b32019-01-19 17:43:09 +010010189 msg_puts_attr(_("recording"), attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010190 if (!shortmess(SHM_RECORDING))
10191 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010192 char s[4];
10193
10194 sprintf(s, " @%c", reg_recording);
10195 msg_puts_attr(s, attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010196 }
10197}
10198
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010199/*
10200 * Draw the tab pages line at the top of the Vim window.
10201 */
Bram Moolenaare12bab32019-01-08 22:02:56 +010010202 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010203draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010204{
10205 int tabcount = 0;
10206 tabpage_T *tp;
10207 int tabwidth;
10208 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010209 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010210 int attr;
10211 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010212 win_T *cwp;
10213 int wincount;
10214 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010215 int c;
10216 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010217 int attr_sel = HL_ATTR(HLF_TPS);
10218 int attr_nosel = HL_ATTR(HLF_TP);
10219 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010220 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010221 int room;
10222 int use_sep_chars = (t_colors < 8
10223#ifdef FEAT_GUI
10224 && !gui.in_use
10225#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010226#ifdef FEAT_TERMGUICOLORS
10227 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010228#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010229 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010230
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010231 if (ScreenLines == NULL)
10232 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010233 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010234
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010235#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010236 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010237 if (gui_use_tabline())
10238 {
10239 gui_update_tabline();
10240 return;
10241 }
10242#endif
10243
10244 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010245 return;
10246
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010247#if defined(FEAT_STL_OPT)
Bram Moolenaarca57ab52019-04-13 14:53:16 +020010248 clear_TabPageIdxs();
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010249
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010250 /* Use the 'tabline' option if it's set. */
10251 if (*p_tal != NUL)
10252 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010253 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010254
10255 /* Check for an error. If there is one we would loop in redrawing the
10256 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010257 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010258 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010259 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010260 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010261 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010262 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010263 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010264 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010265#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010266 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010267 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010268 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010269
Bram Moolenaar238a5642006-02-21 22:12:05 +000010270 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10271 if (tabwidth < 6)
10272 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010273
Bram Moolenaar238a5642006-02-21 22:12:05 +000010274 attr = attr_nosel;
10275 tabcount = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010276 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10277 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010278 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010279 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010280
Bram Moolenaar238a5642006-02-21 22:12:05 +000010281 if (tp->tp_topframe == topframe)
10282 attr = attr_sel;
10283 if (use_sep_chars && col > 0)
10284 screen_putchar('|', 0, col++, attr);
10285
10286 if (tp->tp_topframe != topframe)
10287 attr = attr_nosel;
10288
10289 screen_putchar(' ', 0, col++, attr);
10290
10291 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010292 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010293 cwp = curwin;
10294 wp = firstwin;
10295 }
10296 else
10297 {
10298 cwp = tp->tp_curwin;
10299 wp = tp->tp_firstwin;
10300 }
10301
10302 modified = FALSE;
10303 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10304 if (bufIsChanged(wp->w_buffer))
10305 modified = TRUE;
10306 if (modified || wincount > 1)
10307 {
10308 if (wincount > 1)
10309 {
10310 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010311 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010312 if (col + len >= Columns - 3)
10313 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010314 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010315#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010316 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010317#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010318 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010319#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010320 );
10321 col += len;
10322 }
10323 if (modified)
10324 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10325 screen_putchar(' ', 0, col++, attr);
10326 }
10327
10328 room = scol - col + tabwidth - 1;
10329 if (room > 0)
10330 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010331 /* Get buffer name in NameBuff[] */
10332 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010333 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010334 len = vim_strsize(NameBuff);
10335 p = NameBuff;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010336 if (has_mbyte)
10337 while (len > room)
10338 {
10339 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010340 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010341 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010010342 else if (len > room)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010343 {
10344 p += len - room;
10345 len = room;
10346 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010347 if (len > Columns - col - 1)
10348 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010349
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010350 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010351 col += len;
10352 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010353 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010354
10355 /* Store the tab page number in TabPageIdxs[], so that
10356 * jump_to_mouse() knows where each one is. */
10357 ++tabcount;
10358 while (scol < col)
10359 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010360 }
10361
Bram Moolenaar238a5642006-02-21 22:12:05 +000010362 if (use_sep_chars)
10363 c = '_';
10364 else
10365 c = ' ';
10366 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010367
10368 /* Put an "X" for closing the current tab if there are several. */
10369 if (first_tabpage->tp_next != NULL)
10370 {
10371 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10372 TabPageIdxs[Columns - 1] = -999;
10373 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010374 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010375
10376 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10377 * set. */
10378 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010379}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010380
10381/*
10382 * Get buffer name for "buf" into NameBuff[].
10383 * Takes care of special buffer names and translates special characters.
10384 */
10385 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010386get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010387{
10388 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010389 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010390 else
10391 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10392 trans_characters(NameBuff, MAXPATHL);
10393}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010394
Bram Moolenaar071d4272004-06-13 20:20:40 +000010395/*
10396 * Get the character to use in a status line. Get its attributes in "*attr".
10397 */
10398 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010399fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010400{
10401 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010402
10403#ifdef FEAT_TERMINAL
10404 if (bt_terminal(wp->w_buffer))
10405 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010406 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010407 {
10408 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010409 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010410 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010411 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010412 {
10413 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010414 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010415 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010416 }
10417 else
10418#endif
10419 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010420 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010421 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010422 fill = fill_stl;
10423 }
10424 else
10425 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010426 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010427 fill = fill_stlnc;
10428 }
10429 /* Use fill when there is highlighting, and highlighting of current
10430 * window differs, or the fillchars differ, or this is not the
10431 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010432 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010433 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010434 || (fill_stl != fill_stlnc)))
10435 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010436 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010437 return '^';
10438 return '=';
10439}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010440
Bram Moolenaar071d4272004-06-13 20:20:40 +000010441/*
10442 * Get the character to use in a separator between vertically split windows.
10443 * Get its attributes in "*attr".
10444 */
10445 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010446fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010447{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010448 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010449 if (*attr == 0 && fill_vert == ' ')
10450 return '|';
10451 else
10452 return fill_vert;
10453}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010454
10455/*
10456 * Return TRUE if redrawing should currently be done.
10457 */
10458 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010459redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010460{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010461#ifdef FEAT_EVAL
10462 if (disable_redraw_for_testing)
10463 return 0;
10464 else
10465#endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +020010466 return ((!RedrawingDisabled
10467#ifdef FEAT_EVAL
10468 || ignore_redraw_flag_for_testing
10469#endif
10470 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010471}
10472
10473/*
10474 * Return TRUE if printing messages should currently be done.
10475 */
10476 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010477messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010478{
10479 return (!(p_lz && char_avail() && !KeyTyped));
10480}
10481
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010482#ifdef FEAT_MENU
10483/*
10484 * Draw the window toolbar.
10485 */
10486 static void
10487redraw_win_toolbar(win_T *wp)
10488{
10489 vimmenu_T *menu;
10490 int item_idx = 0;
10491 int item_count = 0;
10492 int col = 0;
10493 int next_col;
10494 int off = (int)(current_ScreenLine - ScreenLines);
10495 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
10496 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
10497
10498 vim_free(wp->w_winbar_items);
10499 for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
10500 ++item_count;
Bram Moolenaarc799fe22019-05-28 23:08:19 +020010501 wp->w_winbar_items = ALLOC_CLEAR_MULT(winbar_item_T, item_count + 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010502
10503 /* TODO: use fewer spaces if there is not enough room */
10504 for (menu = wp->w_winbar->children;
Bram Moolenaar02631462017-09-22 15:20:32 +020010505 menu != NULL && col < wp->w_width; menu = menu->next)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010506 {
10507 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010508 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010509 break;
10510 if (col > 1)
10511 {
10512 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010513 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010514 break;
10515 }
10516
10517 wp->w_winbar_items[item_idx].wb_startcol = col;
10518 space_to_screenline(off + col, button_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 next_col = text_to_screenline(wp, menu->name, col);
10523 while (col < next_col)
10524 {
10525 ScreenAttrs[off + col] = button_attr;
10526 ++col;
10527 }
10528 wp->w_winbar_items[item_idx].wb_endcol = col;
10529 wp->w_winbar_items[item_idx].wb_menu = menu;
10530 ++item_idx;
10531
Bram Moolenaar02631462017-09-22 15:20:32 +020010532 if (col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010533 break;
10534 space_to_screenline(off + col, button_attr);
10535 ++col;
10536 }
Bram Moolenaar02631462017-09-22 15:20:32 +020010537 while (col < wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010538 {
10539 space_to_screenline(off + col, fill_attr);
10540 ++col;
10541 }
10542 wp->w_winbar_items[item_idx].wb_menu = NULL; /* end marker */
10543
Bram Moolenaar02631462017-09-22 15:20:32 +020010544 screen_line(wp->w_winrow, wp->w_wincol, (int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +020010545 (int)wp->w_width, 0);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010546}
10547#endif
Bram Moolenaar491ac282018-06-17 14:47:55 +020010548
Bram Moolenaar071d4272004-06-13 20:20:40 +000010549/*
10550 * Show current status info in ruler and various other places
10551 * If always is FALSE, only show ruler if position has changed.
10552 */
10553 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010554showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010555{
10556 if (!always && !redrawing())
10557 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010558#ifdef FEAT_INS_EXPAND
10559 if (pum_visible())
10560 {
10561 /* Don't redraw right now, do it later. */
10562 curwin->w_redr_status = TRUE;
10563 return;
10564 }
10565#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020010566#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010567 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar362f3562009-11-03 16:20:34 +000010568 redraw_custom_statusline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010569 else
10570#endif
10571#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +020010572 win_redr_ruler(curwin, always, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010573#endif
10574
10575#ifdef FEAT_TITLE
10576 if (need_maketitle
10577# ifdef FEAT_STL_OPT
10578 || (p_icon && (stl_syntax & STL_IN_ICON))
10579 || (p_title && (stl_syntax & STL_IN_TITLE))
10580# endif
10581 )
10582 maketitle();
10583#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010584 /* Redraw the tab pages line if needed. */
10585 if (redraw_tabline)
10586 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010587}
10588
10589#ifdef FEAT_CMDL_INFO
10590 static void
Bram Moolenaar491ac282018-06-17 14:47:55 +020010591win_redr_ruler(win_T *wp, int always, int ignore_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010592{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010593#define RULER_BUF_LEN 70
10594 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010595 int row;
10596 int fillchar;
10597 int attr;
10598 int empty_line = FALSE;
10599 colnr_T virtcol;
10600 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010601 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010602 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010603 int this_ru_col;
10604 int off = 0;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +010010605 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010606
10607 /* If 'ruler' off or redrawing disabled, don't do anything */
10608 if (!p_ru)
10609 return;
10610
10611 /*
10612 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10613 * after deleting lines, before cursor.lnum is corrected.
10614 */
10615 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10616 return;
10617
10618#ifdef FEAT_INS_EXPAND
10619 /* Don't draw the ruler while doing insert-completion, it might overwrite
10620 * the (long) mode message. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010621 if (wp == lastwin && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010622 if (edit_submode != NULL)
10623 return;
Bram Moolenaar491ac282018-06-17 14:47:55 +020010624 // Don't draw the ruler when the popup menu is visible, it may overlap.
10625 // Except when the popup menu will be redrawn anyway.
10626 if (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010627 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010628#endif
10629
10630#ifdef FEAT_STL_OPT
10631 if (*p_ruf)
10632 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010633 int save_called_emsg = called_emsg;
10634
10635 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010636 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010637 if (called_emsg)
10638 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010639 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010640 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010641 return;
10642 }
10643#endif
10644
10645 /*
10646 * Check if not in Insert mode and the line is empty (will show "0-1").
10647 */
10648 if (!(State & INSERT)
10649 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10650 empty_line = TRUE;
10651
10652 /*
10653 * Only draw the ruler when something changed.
10654 */
10655 validate_virtcol_win(wp);
10656 if ( redraw_cmdline
10657 || always
10658 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10659 || wp->w_cursor.col != wp->w_ru_cursor.col
10660 || wp->w_virtcol != wp->w_ru_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +000010661 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
Bram Moolenaar071d4272004-06-13 20:20:40 +000010662 || wp->w_topline != wp->w_ru_topline
10663 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10664#ifdef FEAT_DIFF
10665 || wp->w_topfill != wp->w_ru_topfill
10666#endif
10667 || empty_line != wp->w_ru_empty)
10668 {
10669 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010670 if (wp->w_status_height)
10671 {
10672 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010673 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar53f81742017-09-22 14:35:51 +020010674 off = wp->w_wincol;
Bram Moolenaar02631462017-09-22 15:20:32 +020010675 width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010676 }
10677 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010678 {
10679 row = Rows - 1;
10680 fillchar = ' ';
10681 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010682 width = Columns;
10683 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010684 }
10685
10686 /* In list mode virtcol needs to be recomputed */
10687 virtcol = wp->w_virtcol;
10688 if (wp->w_p_list && lcs_tab1 == NUL)
10689 {
10690 wp->w_p_list = FALSE;
10691 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10692 wp->w_p_list = TRUE;
10693 }
10694
10695 /*
10696 * Some sprintfs return the length, some return a pointer.
10697 * To avoid portability problems we use strlen() here.
10698 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010699 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010700 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10701 ? 0L
10702 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010703 len = STRLEN(buffer);
10704 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010705 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10706 (int)virtcol + 1);
10707
10708 /*
10709 * Add a "50%" if there is room for it.
10710 * On the last line, don't print in the last column (scrolls the
10711 * screen up on some terminals).
10712 */
10713 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010714 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010715 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010716 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010717 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010718 this_ru_col = ru_col - (Columns - width);
10719 if (this_ru_col < 0)
10720 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010721 /* Never use more than half the window/screen width, leave the other
10722 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010723 if (this_ru_col < (width + 1) / 2)
10724 this_ru_col = (width + 1) / 2;
10725 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010726 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010010727 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010728 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010729 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010730 if (has_mbyte)
10731 i += (*mb_char2bytes)(fillchar, buffer + i);
10732 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010733 buffer[i++] = fillchar;
10734 ++o;
10735 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010736 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010737 }
10738 /* Truncate at window boundary. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010739 if (has_mbyte)
10740 {
10741 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010742 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010743 {
10744 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010745 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010746 {
10747 buffer[i] = NUL;
10748 break;
10749 }
10750 }
10751 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010010752 else if (this_ru_col + (int)STRLEN(buffer) > width)
Bram Moolenaar4033c552017-09-16 20:54:51 +020010753 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010754
Bram Moolenaar4033c552017-09-16 20:54:51 +020010755 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010756 i = redraw_cmdline;
10757 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020010758 this_ru_col + off + (int)STRLEN(buffer),
10759 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010760 fillchar, fillchar, attr);
10761 /* don't redraw the cmdline because of showing the ruler */
10762 redraw_cmdline = i;
10763 wp->w_ru_cursor = wp->w_cursor;
10764 wp->w_ru_virtcol = wp->w_virtcol;
10765 wp->w_ru_empty = empty_line;
10766 wp->w_ru_topline = wp->w_topline;
10767 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10768#ifdef FEAT_DIFF
10769 wp->w_ru_topfill = wp->w_topfill;
10770#endif
10771 }
10772}
10773#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010774
10775#if defined(FEAT_LINEBREAK) || defined(PROTO)
10776/*
Bram Moolenaar64486672010-05-16 15:46:46 +020010777 * Return the width of the 'number' and 'relativenumber' column.
10778 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010779 * Otherwise it depends on 'numberwidth' and the line count.
10780 */
10781 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010782number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010783{
10784 int n;
10785 linenr_T lnum;
10786
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020010787 if (wp->w_p_rnu && !wp->w_p_nu)
10788 /* cursor line shows "0" */
10789 lnum = wp->w_height;
10790 else
10791 /* cursor line shows absolute line number */
10792 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020010793
Bram Moolenaar6b314672015-03-20 15:42:10 +010010794 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010795 return wp->w_nrwidth_width;
10796 wp->w_nrwidth_line_count = lnum;
10797
10798 n = 0;
10799 do
10800 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010801 lnum /= 10;
10802 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010803 } while (lnum > 0);
10804
10805 /* 'numberwidth' gives the minimal width plus one */
10806 if (n < wp->w_p_nuw - 1)
10807 n = wp->w_p_nuw - 1;
10808
Bram Moolenaare4b407f2019-07-04 11:59:28 +020010809# ifdef FEAT_SIGNS
10810 // If 'signcolumn' is set to 'number' and there is a sign to display, then
10811 // the minimal width for the number column is 2.
10812 if (n < 2 && (wp->w_buffer->b_signlist != NULL)
10813 && (*wp->w_p_scl == 'n' && *(wp->w_p_scl + 1) == 'u'))
10814 n = 2;
10815# endif
10816
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010817 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010010818 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010819 return n;
10820}
10821#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010822
Bram Moolenaar113e1072019-01-20 15:30:40 +010010823#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010824/*
10825 * Return the current cursor column. This is the actual position on the
10826 * screen. First column is 0.
10827 */
10828 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010829screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010830{
10831 return screen_cur_col;
10832}
10833
10834/*
10835 * Return the current cursor row. This is the actual position on the screen.
10836 * First row is 0.
10837 */
10838 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010839screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010840{
10841 return screen_cur_row;
10842}
Bram Moolenaar113e1072019-01-20 15:30:40 +010010843#endif