blob: 1681e1c57d5037a3b5912dd3c05eebfb7f9cf6aa [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * screen.c: code for displaying on the screen
12 *
13 * Output to the screen (console, terminal emulator or GUI window) is minimized
14 * by remembering what is already on the screen, and only updating the parts
15 * that changed.
16 *
17 * ScreenLines[off] Contains a copy of the whole screen, as it is currently
18 * displayed (excluding text written by external commands).
19 * ScreenAttrs[off] Contains the associated attributes.
20 * LineOffset[row] Contains the offset into ScreenLines*[] and ScreenAttrs[]
21 * for each line.
22 * LineWraps[row] Flag for each line whether it wraps to the next line.
23 *
24 * For double-byte characters, two consecutive bytes in ScreenLines[] can form
25 * one character which occupies two display cells.
26 * For UTF-8 a multi-byte character is converted to Unicode and stored in
27 * ScreenLinesUC[]. ScreenLines[] contains the first byte only. For an ASCII
Bram Moolenaar70c49c12010-03-23 15:36:35 +010028 * character without composing chars ScreenLinesUC[] will be 0 and
29 * ScreenLinesC[][] is not used. When the character occupies two display
30 * cells the next byte in ScreenLines[] is 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +000031 * ScreenLinesC[][] contain up to 'maxcombine' composing characters
Bram Moolenaar70c49c12010-03-23 15:36:35 +010032 * (drawn on top of the first character). There is 0 after the last one used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000033 * ScreenLines2[] is only used for euc-jp to store the second byte if the
34 * first byte is 0x8e (single-width character).
35 *
36 * The screen_*() functions write to the screen and handle updating
37 * ScreenLines[].
38 *
39 * update_screen() is the function that updates all windows and status lines.
40 * It is called form the main loop when must_redraw is non-zero. It may be
Bram Moolenaar2c7a7632007-05-10 18:19:11 +000041 * called from other places when an immediate screen update is needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +000042 *
43 * The part of the buffer that is displayed in a window is set with:
44 * - w_topline (first buffer line in window)
Bram Moolenaarea389e92014-05-28 21:40:52 +020045 * - w_topfill (filler lines above the first line)
Bram Moolenaar071d4272004-06-13 20:20:40 +000046 * - w_leftcol (leftmost window cell in window),
47 * - w_skipcol (skipped window cells of first line)
48 *
49 * Commands that only move the cursor around in a window, do not need to take
50 * action to update the display. The main loop will check if w_topline is
51 * valid and update it (scroll the window) when needed.
52 *
53 * Commands that scroll a window change w_topline and must call
54 * check_cursor() to move the cursor into the visible part of the window, and
55 * call redraw_later(VALID) to have the window displayed by update_screen()
56 * later.
57 *
58 * Commands that change text in the buffer must call changed_bytes() or
59 * changed_lines() to mark the area that changed and will require updating
60 * later. The main loop will call update_screen(), which will update each
61 * window that shows the changed buffer. This assumes text above the change
62 * can remain displayed as it is. Text after the change may need updating for
63 * scrolling, folding and syntax highlighting.
64 *
65 * Commands that change how a window is displayed (e.g., setting 'list') or
66 * invalidate the contents of a window in another way (e.g., change fold
67 * settings), must call redraw_later(NOT_VALID) to have the whole window
68 * redisplayed by update_screen() later.
69 *
70 * Commands that change how a buffer is displayed (e.g., setting 'tabstop')
71 * must call redraw_curbuf_later(NOT_VALID) to have all the windows for the
72 * buffer redisplayed by update_screen() later.
73 *
Bram Moolenaar600dddc2006-03-12 22:05:10 +000074 * Commands that change highlighting and possibly cause a scroll too must call
75 * redraw_later(SOME_VALID) to update the whole window but still use scrolling
76 * to avoid redrawing everything. But the length of displayed lines must not
77 * change, use NOT_VALID then.
78 *
Bram Moolenaar071d4272004-06-13 20:20:40 +000079 * Commands that move the window position must call redraw_later(NOT_VALID).
80 * TODO: should minimize redrawing by scrolling when possible.
81 *
82 * Commands that change everything (e.g., resizing the screen) must call
83 * redraw_all_later(NOT_VALID) or redraw_all_later(CLEAR).
84 *
85 * Things that are handled indirectly:
86 * - When messages scroll the screen up, msg_scrolled will be set and
87 * update_screen() called to redraw.
88 */
89
90#include "vim.h"
91
Bram Moolenaar5641f382012-06-13 18:06:36 +020092#define MB_FILLER_CHAR '<' /* character used when a double-width character
93 * doesn't fit. */
94
Bram Moolenaar071d4272004-06-13 20:20:40 +000095/*
96 * The attributes that are actually active for writing to the screen.
97 */
98static int screen_attr = 0;
99
100/*
101 * Positioning the cursor is reduced by remembering the last position.
102 * Mostly used by windgoto() and screen_char().
103 */
104static int screen_cur_row, screen_cur_col; /* last known cursor position */
105
106#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarbbca7732019-07-24 18:13:16 +0200107static match_T search_hl; // used for 'hlsearch' highlight matching
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108#endif
109
110#ifdef FEAT_FOLDING
111static foldinfo_T win_foldinfo; /* info for 'foldcolumn' */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100112static int compute_foldcolumn(win_T *wp, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113#endif
114
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200115/* Flag that is set when drawing for a callback, not from the main command
116 * loop. */
117static int redrawing_for_callback = 0;
118
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119/*
120 * Buffer for one screen line (characters and attributes).
121 */
122static schar_T *current_ScreenLine;
123
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100124static void win_update(win_T *wp);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200125static void win_redr_status(win_T *wp, int ignore_pum);
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100126static void win_draw_end(win_T *wp, int c1, int c2, int draw_margin, int row, int endrow, hlf_T hl);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127#ifdef FEAT_FOLDING
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100128static void fold_line(win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row);
129static void fill_foldcolumn(char_u *p, win_T *wp, int closed, linenr_T lnum);
130static void copy_text_attr(int off, char_u *buf, int len, int attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +0200132static int win_line(win_T *, linenr_T, int, int, int nochange, int number_only);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100133static void draw_vsep_win(win_T *wp, int row);
Bram Moolenaar238a5642006-02-21 22:12:05 +0000134#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100135static void redraw_custom_statusline(win_T *wp);
Bram Moolenaar238a5642006-02-21 22:12:05 +0000136#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100138static void start_search_hl(void);
139static void end_search_hl(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100141static void screen_char(unsigned off, int row, int col);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100142static void screen_char_2(unsigned off, int row, int col);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100143static void screenclear2(void);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200144static void lineclear(unsigned off, int width, int attr);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100145static void lineinvalid(unsigned off, int width);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200146static int win_do_lines(win_T *wp, int row, int line_count, int mayclear, int del, int clear_attr);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100147static void win_rest_invalid(win_T *wp);
148static void msg_pos_mode(void);
149static void recording_mode(int attr);
Bram Moolenaar3633cf52017-07-31 22:29:35 +0200150static int fillchar_status(int *attr, win_T *wp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100151static int fillchar_vsep(int *attr);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200152#ifdef FEAT_MENU
153static void redraw_win_toolbar(win_T *wp);
154#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100156static void win_redr_custom(win_T *wp, int draw_ruler);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157#endif
158#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +0200159static void win_redr_ruler(win_T *wp, int always, int ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160#endif
161
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162/* Ugly global: overrule attribute used by screen_char() */
163static int screen_char_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164
Bram Moolenaarc0aa4822017-07-16 14:04:29 +0200165#ifdef FEAT_RIGHTLEFT
166# define HAS_RIGHTLEFT(x) x
167#else
168# define HAS_RIGHTLEFT(x) FALSE
169#endif
170
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200171// flags for screen_line()
172#define SLF_RIGHTLEFT 1
173#define SLF_POPUP 2
174
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175/*
176 * Redraw the current window later, with update_screen(type).
177 * Set must_redraw only if not already set to a higher value.
Bram Moolenaarae654382019-01-17 21:09:05 +0100178 * E.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179 */
180 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100181redraw_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182{
183 redraw_win_later(curwin, type);
184}
185
186 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100187redraw_win_later(
188 win_T *wp,
189 int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190{
Bram Moolenaar4f198282017-10-23 21:53:30 +0200191 if (!exiting && wp->w_redr_type < type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 {
193 wp->w_redr_type = type;
194 if (type >= NOT_VALID)
195 wp->w_lines_valid = 0;
196 if (must_redraw < type) /* must_redraw is the maximum of all windows */
197 must_redraw = type;
198 }
199}
200
201/*
202 * Force a complete redraw later. Also resets the highlighting. To be used
203 * after executing a shell command that messes up the screen.
204 */
205 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100206redraw_later_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207{
208 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000209#ifdef FEAT_GUI
210 if (gui.in_use)
211 /* Use a code that will reset gui.highlight_mask in
212 * gui_stop_highlight(). */
213 screen_attr = HL_ALL + 1;
214 else
215#endif
216 /* Use attributes that is very unlikely to appear in text. */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +0200217 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE | HL_STRIKETHROUGH;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218}
219
220/*
221 * Mark all windows to be redrawn later.
222 */
223 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100224redraw_all_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225{
226 win_T *wp;
227
228 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229 redraw_win_later(wp, type);
Bram Moolenaar04b4e1a2019-01-06 22:22:07 +0100230 // This may be needed when switching tabs.
231 if (must_redraw < type)
232 must_redraw = type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233}
234
235/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000236 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237 */
238 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100239redraw_curbuf_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240{
241 redraw_buf_later(curbuf, type);
242}
243
244 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100245redraw_buf_later(buf_T *buf, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246{
247 win_T *wp;
248
249 FOR_ALL_WINDOWS(wp)
250 {
251 if (wp->w_buffer == buf)
252 redraw_win_later(wp, type);
253 }
254}
255
Bram Moolenaar113e1072019-01-20 15:30:40 +0100256#if defined(FEAT_SIGNS) || defined(PROTO)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200257 void
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100258redraw_buf_line_later(buf_T *buf, linenr_T lnum)
259{
260 win_T *wp;
261
262 FOR_ALL_WINDOWS(wp)
263 if (wp->w_buffer == buf && lnum >= wp->w_topline
264 && lnum < wp->w_botline)
265 redrawWinline(wp, lnum);
266}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100267#endif
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100268
Bram Moolenaar113e1072019-01-20 15:30:40 +0100269#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100270 void
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200271redraw_buf_and_status_later(buf_T *buf, int type)
272{
273 win_T *wp;
274
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200275#ifdef FEAT_WILDMENU
Bram Moolenaar86033562017-07-12 20:24:41 +0200276 if (wild_menu_showing != 0)
277 /* Don't redraw while the command line completion is displayed, it
278 * would disappear. */
279 return;
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200280#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200281 FOR_ALL_WINDOWS(wp)
282 {
283 if (wp->w_buffer == buf)
284 {
285 redraw_win_later(wp, type);
286 wp->w_redr_status = TRUE;
287 }
288 }
289}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100290#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200291
Bram Moolenaar113e1072019-01-20 15:30:40 +0100292#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000293/*
Bram Moolenaar2951b772013-07-03 12:45:31 +0200294 * Redraw as soon as possible. When the command line is not scrolled redraw
295 * right away and restore what was on the command line.
296 * Return a code indicating what happened.
297 */
298 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100299redraw_asap(int type)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200300{
301 int rows;
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200302 int cols = screen_Columns;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200303 int r;
304 int ret = 0;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200305 schar_T *screenline; /* copy from ScreenLines[] */
306 sattr_T *screenattr; /* copy from ScreenAttrs[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200307 int i;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200308 u8char_T *screenlineUC = NULL; /* copy from ScreenLinesUC[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200309 u8char_T *screenlineC[MAX_MCO]; /* copy from ScreenLinesC[][] */
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200310 schar_T *screenline2 = NULL; /* copy from ScreenLines2[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200311
312 redraw_later(type);
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200313 if (msg_scrolled || (State != NORMAL && State != NORMAL_BUSY) || exiting)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200314 return ret;
315
316 /* Allocate space to save the text displayed in the command line area. */
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200317 rows = screen_Rows - cmdline_row;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200318 screenline = LALLOC_MULT(schar_T, rows * cols);
319 screenattr = LALLOC_MULT(sattr_T, rows * cols);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200320 if (screenline == NULL || screenattr == NULL)
321 ret = 2;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200322 if (enc_utf8)
323 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200324 screenlineUC = LALLOC_MULT(u8char_T, rows * cols);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200325 if (screenlineUC == NULL)
326 ret = 2;
327 for (i = 0; i < p_mco; ++i)
328 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200329 screenlineC[i] = LALLOC_MULT(u8char_T, rows * cols);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200330 if (screenlineC[i] == NULL)
331 ret = 2;
332 }
333 }
334 if (enc_dbcs == DBCS_JPNU)
335 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200336 screenline2 = LALLOC_MULT(schar_T, rows * cols);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200337 if (screenline2 == NULL)
338 ret = 2;
339 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200340
341 if (ret != 2)
342 {
343 /* Save the text displayed in the command line area. */
344 for (r = 0; r < rows; ++r)
345 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200346 mch_memmove(screenline + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200347 ScreenLines + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200348 (size_t)cols * sizeof(schar_T));
349 mch_memmove(screenattr + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200350 ScreenAttrs + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200351 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200352 if (enc_utf8)
353 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200354 mch_memmove(screenlineUC + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200355 ScreenLinesUC + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200356 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200357 for (i = 0; i < p_mco; ++i)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200358 mch_memmove(screenlineC[i] + r * cols,
359 ScreenLinesC[i] + LineOffset[cmdline_row + r],
360 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200361 }
362 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200363 mch_memmove(screenline2 + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200364 ScreenLines2 + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200365 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200366 }
367
368 update_screen(0);
369 ret = 3;
370
371 if (must_redraw == 0)
372 {
373 int off = (int)(current_ScreenLine - ScreenLines);
374
375 /* Restore the text displayed in the command line area. */
376 for (r = 0; r < rows; ++r)
377 {
378 mch_memmove(current_ScreenLine,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200379 screenline + r * cols,
380 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200381 mch_memmove(ScreenAttrs + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200382 screenattr + r * cols,
383 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200384 if (enc_utf8)
385 {
386 mch_memmove(ScreenLinesUC + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200387 screenlineUC + r * cols,
388 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200389 for (i = 0; i < p_mco; ++i)
390 mch_memmove(ScreenLinesC[i] + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200391 screenlineC[i] + r * cols,
392 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200393 }
394 if (enc_dbcs == DBCS_JPNU)
395 mch_memmove(ScreenLines2 + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200396 screenline2 + r * cols,
397 (size_t)cols * sizeof(schar_T));
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200398 screen_line(cmdline_row + r, 0, cols, cols, 0);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200399 }
400 ret = 4;
401 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200402 }
403
404 vim_free(screenline);
405 vim_free(screenattr);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200406 if (enc_utf8)
407 {
408 vim_free(screenlineUC);
409 for (i = 0; i < p_mco; ++i)
410 vim_free(screenlineC[i]);
411 }
412 if (enc_dbcs == DBCS_JPNU)
413 vim_free(screenline2);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200414
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200415 /* Show the intro message when appropriate. */
416 maybe_intro_message();
417
418 setcursor();
419
Bram Moolenaar2951b772013-07-03 12:45:31 +0200420 return ret;
421}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100422#endif
Bram Moolenaar2951b772013-07-03 12:45:31 +0200423
424/*
Bram Moolenaar975b5272016-03-15 23:10:59 +0100425 * Invoked after an asynchronous callback is called.
426 * If an echo command was used the cursor needs to be put back where
427 * it belongs. If highlighting was changed a redraw is needed.
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200428 * If "call_update_screen" is FALSE don't call update_screen() when at the
429 * command line.
Bram Moolenaar975b5272016-03-15 23:10:59 +0100430 */
431 void
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200432redraw_after_callback(int call_update_screen)
Bram Moolenaar975b5272016-03-15 23:10:59 +0100433{
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200434 ++redrawing_for_callback;
435
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100436 if (State == HITRETURN || State == ASKMORE)
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200437 ; // do nothing
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100438 else if (State & CMDLINE)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200439 {
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200440 // Don't redraw when in prompt_for_number().
441 if (cmdline_row > 0)
442 {
443 // Redrawing only works when the screen didn't scroll. Don't clear
444 // wildmenu entries.
445 if (msg_scrolled == 0
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200446#ifdef FEAT_WILDMENU
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200447 && wild_menu_showing == 0
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200448#endif
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200449 && call_update_screen)
450 update_screen(0);
451
452 // Redraw in the same position, so that the user can continue
453 // editing the command.
454 redrawcmdline_ex(FALSE);
455 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200456 }
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200457 else if (State & (NORMAL | INSERT | TERMINAL))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100458 {
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200459 // keep the command line if possible
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200460 update_screen(VALID_NO_UPDATE);
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100461 setcursor();
462 }
Bram Moolenaar975b5272016-03-15 23:10:59 +0100463 cursor_on();
Bram Moolenaar975b5272016-03-15 23:10:59 +0100464#ifdef FEAT_GUI
Bram Moolenaara338adc2018-01-31 20:51:47 +0100465 if (gui.in_use && !gui_mch_is_blink_off())
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200466 // Don't update the cursor when it is blinking and off to avoid
467 // flicker.
Bram Moolenaara338adc2018-01-31 20:51:47 +0100468 out_flush_cursor(FALSE, FALSE);
469 else
Bram Moolenaar975b5272016-03-15 23:10:59 +0100470#endif
Bram Moolenaaracda04f2018-02-08 09:57:28 +0100471 out_flush();
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200472
473 --redrawing_for_callback;
Bram Moolenaar975b5272016-03-15 23:10:59 +0100474}
475
476/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477 * Changed something in the current window, at buffer line "lnum", that
478 * requires that line and possibly other lines to be redrawn.
479 * Used when entering/leaving Insert mode with the cursor on a folded line.
480 * Used to remove the "$" from a change command.
481 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
482 * may become invalid and the whole window will have to be redrawn.
483 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100485redrawWinline(
Bram Moolenaar90a99792018-09-12 21:52:18 +0200486 win_T *wp,
Bram Moolenaarae12f4b2019-01-09 20:51:04 +0100487 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488{
Bram Moolenaar90a99792018-09-12 21:52:18 +0200489 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
490 wp->w_redraw_top = lnum;
491 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
492 wp->w_redraw_bot = lnum;
493 redraw_win_later(wp, VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494}
495
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200496/*
497 * To be called when "updating_screen" was set before and now the postponed
498 * side effects may take place.
499 */
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200500 void
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200501after_updating_screen(int may_resize_shell UNUSED)
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200502{
503 updating_screen = FALSE;
504#ifdef FEAT_GUI
505 if (may_resize_shell)
506 gui_may_resize_shell();
507#endif
508#ifdef FEAT_TERMINAL
509 term_check_channel_closed_recently();
510#endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +0200511
512#ifdef HAVE_DROP_FILE
513 // If handle_drop() was called while updating_screen was TRUE need to
514 // handle the drop now.
515 handle_any_postponed_drop();
516#endif
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200517}
518
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519/*
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200520 * Update all windows that are editing the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521 */
522 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100523update_curbuf(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524{
525 redraw_curbuf_later(type);
526 update_screen(type);
527}
528
529/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 * Based on the current value of curwin->w_topline, transfer a screenfull
531 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
Bram Moolenaar072412e2017-09-13 22:11:35 +0200532 * Return OK when the screen was updated, FAIL if it was not done.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533 */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200534 int
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200535update_screen(int type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536{
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200537 int type = type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538 win_T *wp;
539 static int did_intro = FALSE;
540#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
541 int did_one;
542#endif
Bram Moolenaar144445d2016-07-08 21:41:54 +0200543#ifdef FEAT_GUI
Bram Moolenaar107abd22016-08-12 14:08:25 +0200544 int did_undraw = FALSE;
Bram Moolenaar1f3601e2019-04-26 20:33:00 +0200545 int gui_cursor_col = 0;
546 int gui_cursor_row = 0;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200547#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200548 int no_update = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000550 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 if (!screen_valid(TRUE))
Bram Moolenaar072412e2017-09-13 22:11:35 +0200552 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200554 if (type == VALID_NO_UPDATE)
555 {
556 no_update = TRUE;
557 type = 0;
558 }
559
Bram Moolenaara3347722019-05-11 21:14:24 +0200560#ifdef FEAT_EVAL
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200561 {
562 buf_T *buf;
563
564 // Before updating the screen, notify any listeners of changed text.
565 FOR_ALL_BUFFERS(buf)
566 invoke_listeners(buf);
567 }
Bram Moolenaara3347722019-05-11 21:14:24 +0200568#endif
569
Bram Moolenaar4f57eef2019-08-24 20:54:19 +0200570#ifdef FEAT_DIFF
571 // May have postponed updating diffs.
572 if (need_diff_redraw)
573 diff_redraw(TRUE);
574#endif
575
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576 if (must_redraw)
577 {
578 if (type < must_redraw) /* use maximal type */
579 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000580
581 /* must_redraw is reset here, so that when we run into some weird
582 * reason to redraw while busy redrawing (e.g., asynchronous
583 * scrolling), or update_topline() in win_update() will cause a
584 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585 must_redraw = 0;
586 }
587
Bram Moolenaar6eddadf2018-05-06 16:40:16 +0200588 /* May need to update w_lines[]. */
589 if (curwin->w_lines_valid == 0 && type < NOT_VALID
590#ifdef FEAT_TERMINAL
591 && !term_do_update_window(curwin)
592#endif
593 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 type = NOT_VALID;
595
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000596 /* Postpone the redrawing when it's not needed and when being called
597 * recursively. */
598 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 {
600 redraw_later(type); /* remember type for next time */
601 must_redraw = type;
602 if (type > INVERTED_ALL)
603 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200604 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605 }
Bram Moolenaarec572ad2019-07-07 14:26:59 +0200606 updating_screen = TRUE;
Bram Moolenaar33796b32019-06-08 16:01:13 +0200607
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200608#ifdef FEAT_TEXT_PROP
Bram Moolenaar4c063a02019-06-10 21:24:12 +0200609 // Update popup_mask if needed. This may set w_redraw_top and w_redraw_bot
610 // in some windows.
611 may_update_popup_mask(type);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200612#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614#ifdef FEAT_SYN_HL
615 ++display_tick; /* let syntax code know we're in a next round of
616 * display updating */
617#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200618 if (no_update)
619 ++no_win_do_lines_ins;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620
621 /*
622 * if the screen was scrolled up when displaying a message, scroll it down
623 */
624 if (msg_scrolled)
625 {
626 clear_cmdline = TRUE;
627 if (msg_scrolled > Rows - 5) /* clearing is faster */
628 type = CLEAR;
629 else if (type != CLEAR)
630 {
631 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200632 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, 0, NULL)
633 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 type = CLEAR;
635 FOR_ALL_WINDOWS(wp)
636 {
Bram Moolenaar98fb65c2019-06-02 20:33:32 +0200637 if (wp->w_winrow < msg_scrolled)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 {
639 if (W_WINROW(wp) + wp->w_height > msg_scrolled
640 && wp->w_redr_type < REDRAW_TOP
641 && wp->w_lines_valid > 0
642 && wp->w_topline == wp->w_lines[0].wl_lnum)
643 {
644 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
645 wp->w_redr_type = REDRAW_TOP;
646 }
647 else
648 {
649 wp->w_redr_type = NOT_VALID;
Bram Moolenaare0de17d2017-09-24 16:24:34 +0200650 if (W_WINROW(wp) + wp->w_height + wp->w_status_height
651 <= msg_scrolled)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 }
654 }
655 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200656 if (!no_update)
657 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000658 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 }
660 msg_scrolled = 0;
661 need_wait_return = FALSE;
662 }
663
664 /* reset cmdline_row now (may have been changed temporarily) */
665 compute_cmdrow();
666
667 /* Check for changed highlighting */
668 if (need_highlight_changed)
669 highlight_changed();
670
671 if (type == CLEAR) /* first clear screen */
672 {
673 screenclear(); /* will reset clear_cmdline */
674 type = NOT_VALID;
Bram Moolenaar9f5f7bf2017-06-28 20:45:26 +0200675 /* must_redraw may be set indirectly, avoid another redraw later */
676 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 }
678
679 if (clear_cmdline) /* going to clear cmdline (done below) */
680 check_for_delay(FALSE);
681
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000682#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200683 /* Force redraw when width of 'number' or 'relativenumber' column
684 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000685 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200686 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
687 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000688 curwin->w_redr_type = NOT_VALID;
689#endif
690
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 /*
692 * Only start redrawing if there is really something to do.
693 */
694 if (type == INVERTED)
695 update_curswant();
696 if (curwin->w_redr_type < type
697 && !((type == VALID
698 && curwin->w_lines[0].wl_valid
699#ifdef FEAT_DIFF
700 && curwin->w_topfill == curwin->w_old_topfill
701 && curwin->w_botfill == curwin->w_old_botfill
702#endif
703 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000705 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
707 && curwin->w_old_visual_mode == VIsual_mode
708 && (curwin->w_valid & VALID_VIRTCOL)
709 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 ))
711 curwin->w_redr_type = type;
712
Bram Moolenaar5a305422006-04-28 22:38:25 +0000713 /* Redraw the tab pages line if needed. */
714 if (redraw_tabline || type >= NOT_VALID)
715 draw_tabline();
Bram Moolenaar5a305422006-04-28 22:38:25 +0000716
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717#ifdef FEAT_SYN_HL
718 /*
719 * Correct stored syntax highlighting info for changes in each displayed
720 * buffer. Each buffer must only be done once.
721 */
722 FOR_ALL_WINDOWS(wp)
723 {
724 if (wp->w_buffer->b_mod_set)
725 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 win_T *wwp;
727
728 /* Check if we already did this buffer. */
729 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
730 if (wwp->w_buffer == wp->w_buffer)
731 break;
Bram Moolenaar4033c552017-09-16 20:54:51 +0200732 if (wwp == wp && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 syn_stack_apply_changes(wp->w_buffer);
734 }
735 }
736#endif
737
738 /*
739 * Go from top to bottom through the windows, redrawing the ones that need
740 * it.
741 */
742#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
743 did_one = FALSE;
744#endif
745#ifdef FEAT_SEARCH_EXTRA
746 search_hl.rm.regprog = NULL;
747#endif
748 FOR_ALL_WINDOWS(wp)
749 {
750 if (wp->w_redr_type != 0)
751 {
752 cursor_off();
753#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
754 if (!did_one)
755 {
756 did_one = TRUE;
757# ifdef FEAT_SEARCH_EXTRA
758 start_search_hl();
759# endif
760# ifdef FEAT_CLIPBOARD
761 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200762 if (clip_star.available && clip_isautosel_star())
763 clip_update_selection(&clip_star);
764 if (clip_plus.available && clip_isautosel_plus())
765 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766# endif
767#ifdef FEAT_GUI
768 /* Remove the cursor before starting to do anything, because
769 * scrolling may make it difficult to redraw the text under
770 * it. */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200771 if (gui.in_use && wp == curwin)
Bram Moolenaar144445d2016-07-08 21:41:54 +0200772 {
773 gui_cursor_col = gui.cursor_col;
774 gui_cursor_row = gui.cursor_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 gui_undraw_cursor();
Bram Moolenaar107abd22016-08-12 14:08:25 +0200776 did_undraw = TRUE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200777 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778#endif
779 }
780#endif
781 win_update(wp);
782 }
783
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 /* redraw status line after the window to minimize cursor movement */
785 if (wp->w_redr_status)
786 {
787 cursor_off();
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200788 win_redr_status(wp, TRUE); // any popup menu will be redrawn below
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 }
791#if defined(FEAT_SEARCH_EXTRA)
792 end_search_hl();
793#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100794 /* May need to redraw the popup menu. */
Bram Moolenaar491ac282018-06-17 14:47:55 +0200795 pum_may_redraw();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 /* Reset b_mod_set flags. Going through all windows is probably faster
798 * than going through all buffers (there could be many buffers). */
Bram Moolenaar29323592016-07-24 22:04:11 +0200799 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 wp->w_buffer->b_mod_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +0200802#ifdef FEAT_TEXT_PROP
803 // Display popup windows on top of the windows and command line.
804 update_popups(win_update);
805#endif
806
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200807 after_updating_screen(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808
809 /* Clear or redraw the command line. Done last, because scrolling may
810 * mess up the command line. */
Bram Moolenaar4c25bd72019-04-20 23:38:07 +0200811 if (clear_cmdline || redraw_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 showmode();
813
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200814 if (no_update)
815 --no_win_do_lines_ins;
816
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200818 if (!did_intro)
819 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 did_intro = TRUE;
821
822#ifdef FEAT_GUI
823 /* Redraw the cursor and update the scrollbars when all screen updating is
824 * done. */
825 if (gui.in_use)
826 {
Bram Moolenaar107abd22016-08-12 14:08:25 +0200827 if (did_undraw && !gui_mch_is_blink_off())
Bram Moolenaar144445d2016-07-08 21:41:54 +0200828 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100829 mch_disable_flush();
830 out_flush(); /* required before updating the cursor */
831 mch_enable_flush();
832
Bram Moolenaar144445d2016-07-08 21:41:54 +0200833 /* Put the GUI position where the cursor was, gui_update_cursor()
834 * uses that. */
835 gui.col = gui_cursor_col;
836 gui.row = gui_cursor_row;
Bram Moolenaar84dbd492016-10-02 23:09:31 +0200837 gui.col = mb_fix_col(gui.col, gui.row);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara338adc2018-01-31 20:51:47 +0100839 gui_may_flush();
Bram Moolenaar65549bd2016-07-08 22:52:37 +0200840 screen_cur_col = gui.col;
841 screen_cur_row = gui.row;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200842 }
Bram Moolenaara338adc2018-01-31 20:51:47 +0100843 else
844 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 gui_update_scrollbars(FALSE);
846 }
847#endif
Bram Moolenaar072412e2017-09-13 22:11:35 +0200848 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849}
850
Bram Moolenaar1fa8fdd2019-02-25 05:41:15 +0100851#if defined(FEAT_NETBEANS_INTG) || defined(FEAT_GUI)
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100852/*
853 * Prepare for updating one or more windows.
854 * Caller must check for "updating_screen" already set to avoid recursiveness.
855 */
856 static void
857update_prepare(void)
858{
859 cursor_off();
860 updating_screen = TRUE;
861#ifdef FEAT_GUI
862 /* Remove the cursor before starting to do anything, because scrolling may
863 * make it difficult to redraw the text under it. */
864 if (gui.in_use)
865 gui_undraw_cursor();
866#endif
867#ifdef FEAT_SEARCH_EXTRA
868 start_search_hl();
869#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +0200870#ifdef FEAT_TEXT_PROP
871 // Update popup_mask if needed.
Bram Moolenaar68acb412019-06-26 03:40:36 +0200872 may_update_popup_mask(must_redraw);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200873#endif
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100874}
875
876/*
877 * Finish updating one or more windows.
878 */
879 static void
880update_finish(void)
881{
Bram Moolenaar4c25bd72019-04-20 23:38:07 +0200882 if (redraw_cmdline || redraw_mode)
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100883 showmode();
884
885# ifdef FEAT_SEARCH_EXTRA
886 end_search_hl();
887# endif
888
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200889 after_updating_screen(TRUE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100890
891# ifdef FEAT_GUI
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100892 /* Redraw the cursor and update the scrollbars when all screen updating is
893 * done. */
894 if (gui.in_use)
895 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100896 out_flush_cursor(FALSE, FALSE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100897 gui_update_scrollbars(FALSE);
898 }
899# endif
900}
901#endif
902
Bram Moolenaar860cae12010-06-05 23:22:07 +0200903#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200904/*
905 * Return TRUE if the cursor line in window "wp" may be concealed, according
906 * to the 'concealcursor' option.
907 */
908 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100909conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200910{
911 int c;
912
913 if (*wp->w_p_cocu == NUL)
914 return FALSE;
915 if (get_real_state() & VISUAL)
916 c = 'v';
917 else if (State & INSERT)
918 c = 'i';
919 else if (State & NORMAL)
920 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200921 else if (State & CMDLINE)
922 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200923 else
924 return FALSE;
925 return vim_strchr(wp->w_p_cocu, c) != NULL;
926}
927
928/*
929 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
930 */
931 void
Bram Moolenaarb9464822018-05-10 15:09:49 +0200932conceal_check_cursor_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200933{
934 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
935 {
936 need_cursor_line_redraw = TRUE;
937 /* Need to recompute cursor column, e.g., when starting Visual mode
938 * without concealing. */
939 curs_columns(TRUE);
940 }
941}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942#endif
943
Bram Moolenaar113e1072019-01-20 15:30:40 +0100944#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100946update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947{
948 win_T *wp;
949 int doit = FALSE;
950
951# ifdef FEAT_FOLDING
952 win_foldinfo.fi_level = 0;
953# endif
954
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100955 // update/delete a specific sign
956 redraw_buf_line_later(buf, lnum);
957
958 // check if it resulted in the need to redraw a window
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 if (wp->w_redr_type != 0)
961 doit = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100963 /* Return when there is nothing to do, screen updating is already
Bram Moolenaar07920482017-08-01 20:53:30 +0200964 * happening (recursive call), messages on the screen or still starting up.
965 */
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100966 if (!doit || updating_screen
Bram Moolenaar07920482017-08-01 20:53:30 +0200967 || State == ASKMORE || State == HITRETURN
968 || msg_scrolled
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100969#ifdef FEAT_GUI
970 || gui.starting
971#endif
972 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 return;
974
975 /* update all windows that need updating */
976 update_prepare();
977
Bram Moolenaar29323592016-07-24 22:04:11 +0200978 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 {
980 if (wp->w_redr_type != 0)
981 win_update(wp);
982 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200983 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985
986 update_finish();
987}
988#endif
989
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200990/*
991 * Get 'wincolor' attribute for window "wp". If not set and "wp" is a popup
992 * window then get the "Pmenu" highlight attribute.
993 */
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200994 int
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200995get_wcr_attr(win_T *wp)
996{
997 int wcr_attr = 0;
998
999 if (*wp->w_p_wcr != NUL)
1000 wcr_attr = syn_name2attr(wp->w_p_wcr);
1001#ifdef FEAT_TEXT_PROP
Bram Moolenaarc363fe12019-08-04 18:13:46 +02001002 else if (WIN_IS_POPUP(wp))
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001003 {
1004 if (wp->w_popup_flags & POPF_INFO)
1005 wcr_attr = HL_ATTR(HLF_PSI); // PmenuSel
1006 else
1007 wcr_attr = HL_ATTR(HLF_PNI); // Pmenu
1008 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001009#endif
1010 return wcr_attr;
1011}
1012
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013#if defined(FEAT_GUI) || defined(PROTO)
1014/*
1015 * Update a single window, its status line and maybe the command line msg.
1016 * Used for the GUI scrollbar.
1017 */
1018 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001019updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020{
Bram Moolenaar19f990e2009-11-25 12:08:03 +00001021 /* return if already busy updating */
1022 if (updating_screen)
1023 return;
1024
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 update_prepare();
1026
1027#ifdef FEAT_CLIPBOARD
1028 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001029 if (clip_star.available && clip_isautosel_star())
1030 clip_update_selection(&clip_star);
1031 if (clip_plus.available && clip_isautosel_plus())
1032 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001034
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001036
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001037 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001038 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001039 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001040
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 if (wp->w_redr_status
1042# ifdef FEAT_CMDL_INFO
1043 || p_ru
1044# endif
1045# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001046 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047# endif
1048 )
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001049 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050
Bram Moolenaar988c4332019-06-02 14:12:11 +02001051#ifdef FEAT_TEXT_PROP
1052 // Display popup windows on top of everything.
Bram Moolenaara540f8a2019-06-14 19:23:57 +02001053 update_popups(win_update);
Bram Moolenaar988c4332019-06-02 14:12:11 +02001054#endif
1055
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 update_finish();
1057}
1058#endif
1059
1060/*
1061 * Update a single window.
1062 *
1063 * This may cause the windows below it also to be redrawn (when clearing the
1064 * screen or scrolling lines).
1065 *
1066 * How the window is redrawn depends on wp->w_redr_type. Each type also
1067 * implies the one below it.
1068 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001069 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1071 * INVERTED redraw the changed part of the Visual area
1072 * INVERTED_ALL redraw the whole Visual area
1073 * VALID 1. scroll up/down to adjust for a changed w_topline
1074 * 2. update lines at the top when scrolled down
1075 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001076 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 * b_mod_top and b_mod_bot.
1078 * - if wp->w_redraw_top non-zero, redraw lines between
1079 * wp->w_redraw_top and wp->w_redr_bot.
1080 * - continue redrawing when syntax status is invalid.
1081 * 4. if scrolled up, update lines at the bottom.
1082 * This results in three areas that may need updating:
1083 * top: from first row to top_end (when scrolled down)
1084 * mid: from mid_start to mid_end (update inversion or changed text)
1085 * bot: from bot_start to last row (when scrolled up)
1086 */
1087 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001088win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089{
1090 buf_T *buf = wp->w_buffer;
1091 int type;
1092 int top_end = 0; /* Below last row of the top area that needs
1093 updating. 0 when no top area updating. */
1094 int mid_start = 999;/* first row of the mid area that needs
1095 updating. 999 when no mid area updating. */
1096 int mid_end = 0; /* Below last row of the mid area that needs
1097 updating. 0 when no mid area updating. */
1098 int bot_start = 999;/* first row of the bot area that needs
1099 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 int scrolled_down = FALSE; /* TRUE when scrolled down when
1101 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarfbfb7572019-07-25 20:53:03 +02001103 int top_to_mod = FALSE; // redraw above mod_top
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104#endif
1105
1106 int row; /* current window row to display */
1107 linenr_T lnum; /* current buffer lnum to display */
1108 int idx; /* current index in w_lines[] */
1109 int srow; /* starting row of the current line */
1110
1111 int eof = FALSE; /* if TRUE, we hit the end of the file */
1112 int didline = FALSE; /* if TRUE, we finished the last line */
1113 int i;
1114 long j;
1115 static int recursive = FALSE; /* being called recursively */
1116 int old_botline = wp->w_botline;
1117#ifdef FEAT_FOLDING
1118 long fold_count;
1119#endif
1120#ifdef FEAT_SYN_HL
1121 /* remember what happened to the previous line, to know if
1122 * check_visual_highlight() can be used */
1123#define DID_NONE 1 /* didn't update a line */
1124#define DID_LINE 2 /* updated a normal line */
1125#define DID_FOLD 3 /* updated a folded line */
1126 int did_update = DID_NONE;
1127 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1128#endif
1129 linenr_T mod_top = 0;
1130 linenr_T mod_bot = 0;
1131#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1132 int save_got_int;
1133#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001134#ifdef SYN_TIME_LIMIT
1135 proftime_T syntax_tm;
1136#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137
1138 type = wp->w_redr_type;
1139
1140 if (type == NOT_VALID)
1141 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 wp->w_lines_valid = 0;
1144 }
1145
1146 /* Window is zero-height: nothing to draw. */
Bram Moolenaar415a6932017-12-05 20:31:07 +01001147 if (wp->w_height + WINBAR_HEIGHT(wp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 {
1149 wp->w_redr_type = 0;
1150 return;
1151 }
1152
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 /* Window is zero-width: Only need to draw the separator. */
1154 if (wp->w_width == 0)
1155 {
1156 /* draw the vertical separator right of this window */
1157 draw_vsep_win(wp, 0);
1158 wp->w_redr_type = 0;
1159 return;
1160 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001162#ifdef FEAT_TERMINAL
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001163 // If this window contains a terminal, redraw works completely differently.
1164 if (term_do_update_window(wp))
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001165 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001166 term_update_window(wp);
Bram Moolenaar181ca992018-02-13 21:19:21 +01001167# ifdef FEAT_MENU
1168 /* Draw the window toolbar, if there is one. */
1169 if (winbar_height(wp) > 0)
1170 redraw_win_toolbar(wp);
1171# endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001172 wp->w_redr_type = 0;
1173 return;
1174 }
1175#endif
1176
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarbbca7732019-07-24 18:13:16 +02001178 init_search_hl(wp, &search_hl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179#endif
1180
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001181#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001182 /* Force redraw when width of 'number' or 'relativenumber' column
1183 * changes. */
1184 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001185 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001186 {
1187 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001188 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001189 }
1190 else
1191#endif
1192
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1194 {
1195 /*
1196 * When there are both inserted/deleted lines and specific lines to be
1197 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1198 * everything (only happens when redrawing is off for while).
1199 */
1200 type = NOT_VALID;
1201 }
1202 else
1203 {
1204 /*
1205 * Set mod_top to the first line that needs displaying because of
1206 * changes. Set mod_bot to the first line after the changes.
1207 */
1208 mod_top = wp->w_redraw_top;
1209 if (wp->w_redraw_bot != 0)
1210 mod_bot = wp->w_redraw_bot + 1;
1211 else
1212 mod_bot = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 if (buf->b_mod_set)
1214 {
1215 if (mod_top == 0 || mod_top > buf->b_mod_top)
1216 {
1217 mod_top = buf->b_mod_top;
1218#ifdef FEAT_SYN_HL
1219 /* Need to redraw lines above the change that may be included
1220 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001221 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001223 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 if (mod_top < 1)
1225 mod_top = 1;
1226 }
1227#endif
1228 }
1229 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1230 mod_bot = buf->b_mod_bot;
1231
1232#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarfbfb7572019-07-25 20:53:03 +02001233 // When 'hlsearch' is on and using a multi-line search pattern, a
1234 // change in one line may make the Search highlighting in a
1235 // previous line invalid. Simple solution: redraw all visible
1236 // lines above the change.
1237 // Same for a match pattern.
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001238 if (search_hl.rm.regprog != NULL
1239 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001241 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001242 {
Bram Moolenaarfbfb7572019-07-25 20:53:03 +02001243 matchitem_T *cur = wp->w_match_head;
1244
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001245 while (cur != NULL)
1246 {
1247 if (cur->match.regprog != NULL
1248 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001249 {
1250 top_to_mod = TRUE;
1251 break;
1252 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001253 cur = cur->next;
1254 }
1255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256#endif
1257 }
1258#ifdef FEAT_FOLDING
1259 if (mod_top != 0 && hasAnyFolding(wp))
1260 {
1261 linenr_T lnumt, lnumb;
1262
1263 /*
1264 * A change in a line can cause lines above it to become folded or
1265 * unfolded. Find the top most buffer line that may be affected.
1266 * If the line was previously folded and displayed, get the first
1267 * line of that fold. If the line is folded now, get the first
1268 * folded line. Use the minimum of these two.
1269 */
1270
1271 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1272 * the line below it. If there is no valid entry, use w_topline.
1273 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1274 * to this line. If there is no valid entry, use MAXLNUM. */
1275 lnumt = wp->w_topline;
1276 lnumb = MAXLNUM;
1277 for (i = 0; i < wp->w_lines_valid; ++i)
1278 if (wp->w_lines[i].wl_valid)
1279 {
1280 if (wp->w_lines[i].wl_lastlnum < mod_top)
1281 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1282 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1283 {
1284 lnumb = wp->w_lines[i].wl_lnum;
1285 /* When there is a fold column it might need updating
1286 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001287 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 ++lnumb;
1289 }
1290 }
1291
1292 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1293 if (mod_top > lnumt)
1294 mod_top = lnumt;
1295
1296 /* Now do the same for the bottom line (one above mod_bot). */
1297 --mod_bot;
1298 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1299 ++mod_bot;
1300 if (mod_bot < lnumb)
1301 mod_bot = lnumb;
1302 }
1303#endif
1304
1305 /* When a change starts above w_topline and the end is below
1306 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001307 * If the end of the change is above w_topline: do like no change was
1308 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 if (mod_top != 0 && mod_top < wp->w_topline)
1310 {
1311 if (mod_bot > wp->w_topline)
1312 mod_top = wp->w_topline;
1313#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001314 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 top_end = 1;
1316#endif
1317 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001318
1319 /* When line numbers are displayed need to redraw all lines below
1320 * inserted/deleted lines. */
1321 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1322 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 }
Bram Moolenaar895d9662019-01-31 21:57:21 +01001324 wp->w_redraw_top = 0; // reset for next time
1325 wp->w_redraw_bot = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326
1327 /*
1328 * When only displaying the lines at the top, set top_end. Used when
1329 * window has scrolled down for msg_scrolled.
1330 */
1331 if (type == REDRAW_TOP)
1332 {
1333 j = 0;
1334 for (i = 0; i < wp->w_lines_valid; ++i)
1335 {
1336 j += wp->w_lines[i].wl_size;
1337 if (j >= wp->w_upd_rows)
1338 {
1339 top_end = j;
1340 break;
1341 }
1342 }
1343 if (top_end == 0)
1344 /* not found (cannot happen?): redraw everything */
1345 type = NOT_VALID;
1346 else
1347 /* top area defined, the rest is VALID */
1348 type = VALID;
1349 }
1350
Bram Moolenaar367329b2007-08-30 11:53:22 +00001351 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001352 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1353 * non-zero and thus not FALSE) will indicate that screenclear() was not
1354 * called. */
1355 if (screen_cleared)
1356 screen_cleared = MAYBE;
1357
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 /*
1359 * If there are no changes on the screen that require a complete redraw,
1360 * handle three cases:
1361 * 1: we are off the top of the screen by a few lines: scroll down
1362 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1363 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1364 * w_lines[] that needs updating.
1365 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001366 if ((type == VALID || type == SOME_VALID
1367 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368#ifdef FEAT_DIFF
1369 && !wp->w_botfill && !wp->w_old_botfill
1370#endif
1371 )
1372 {
1373 if (mod_top != 0 && wp->w_topline == mod_top)
1374 {
1375 /*
1376 * w_topline is the first changed line, the scrolling will be done
1377 * further down.
1378 */
1379 }
1380 else if (wp->w_lines[0].wl_valid
1381 && (wp->w_topline < wp->w_lines[0].wl_lnum
1382#ifdef FEAT_DIFF
1383 || (wp->w_topline == wp->w_lines[0].wl_lnum
1384 && wp->w_topfill > wp->w_old_topfill)
1385#endif
1386 ))
1387 {
1388 /*
1389 * New topline is above old topline: May scroll down.
1390 */
1391#ifdef FEAT_FOLDING
1392 if (hasAnyFolding(wp))
1393 {
1394 linenr_T ln;
1395
1396 /* count the number of lines we are off, counting a sequence
1397 * of folded lines as one */
1398 j = 0;
1399 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1400 {
1401 ++j;
1402 if (j >= wp->w_height - 2)
1403 break;
1404 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1405 }
1406 }
1407 else
1408#endif
1409 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1410 if (j < wp->w_height - 2) /* not too far off */
1411 {
1412 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1413#ifdef FEAT_DIFF
1414 /* insert extra lines for previously invisible filler lines */
1415 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1416 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1417 - wp->w_old_topfill;
1418#endif
1419 if (i < wp->w_height - 2) /* less than a screen off */
1420 {
1421 /*
1422 * Try to insert the correct number of lines.
1423 * If not the last window, delete the lines at the bottom.
1424 * win_ins_lines may fail when the terminal can't do it.
1425 */
1426 if (i > 0)
1427 check_for_delay(FALSE);
1428 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1429 {
1430 if (wp->w_lines_valid != 0)
1431 {
1432 /* Need to update rows that are new, stop at the
1433 * first one that scrolled down. */
1434 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436
1437 /* Move the entries that were scrolled, disable
1438 * the entries for the lines to be redrawn. */
1439 if ((wp->w_lines_valid += j) > wp->w_height)
1440 wp->w_lines_valid = wp->w_height;
1441 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1442 wp->w_lines[idx] = wp->w_lines[idx - j];
1443 while (idx >= 0)
1444 wp->w_lines[idx--].wl_valid = FALSE;
1445 }
1446 }
1447 else
1448 mid_start = 0; /* redraw all lines */
1449 }
1450 else
1451 mid_start = 0; /* redraw all lines */
1452 }
1453 else
1454 mid_start = 0; /* redraw all lines */
1455 }
1456 else
1457 {
1458 /*
1459 * New topline is at or below old topline: May scroll up.
1460 * When topline didn't change, find first entry in w_lines[] that
1461 * needs updating.
1462 */
1463
1464 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1465 j = -1;
1466 row = 0;
1467 for (i = 0; i < wp->w_lines_valid; i++)
1468 {
1469 if (wp->w_lines[i].wl_valid
1470 && wp->w_lines[i].wl_lnum == wp->w_topline)
1471 {
1472 j = i;
1473 break;
1474 }
1475 row += wp->w_lines[i].wl_size;
1476 }
1477 if (j == -1)
1478 {
1479 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1480 * lines */
1481 mid_start = 0;
1482 }
1483 else
1484 {
1485 /*
1486 * Try to delete the correct number of lines.
1487 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1488 */
1489#ifdef FEAT_DIFF
1490 /* If the topline didn't change, delete old filler lines,
1491 * otherwise delete filler lines of the new topline... */
1492 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1493 row += wp->w_old_topfill;
1494 else
1495 row += diff_check_fill(wp, wp->w_topline);
1496 /* ... but don't delete new filler lines. */
1497 row -= wp->w_topfill;
1498#endif
1499 if (row > 0)
1500 {
1501 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001502 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1503 == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 bot_start = wp->w_height - row;
1505 else
1506 mid_start = 0; /* redraw all lines */
1507 }
1508 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1509 {
1510 /*
1511 * Skip the lines (below the deleted lines) that are still
1512 * valid and don't need redrawing. Copy their info
1513 * upwards, to compensate for the deleted lines. Set
1514 * bot_start to the first row that needs redrawing.
1515 */
1516 bot_start = 0;
1517 idx = 0;
1518 for (;;)
1519 {
1520 wp->w_lines[idx] = wp->w_lines[j];
1521 /* stop at line that didn't fit, unless it is still
1522 * valid (no lines deleted) */
1523 if (row > 0 && bot_start + row
1524 + (int)wp->w_lines[j].wl_size > wp->w_height)
1525 {
1526 wp->w_lines_valid = idx + 1;
1527 break;
1528 }
1529 bot_start += wp->w_lines[idx++].wl_size;
1530
1531 /* stop at the last valid entry in w_lines[].wl_size */
1532 if (++j >= wp->w_lines_valid)
1533 {
1534 wp->w_lines_valid = idx;
1535 break;
1536 }
1537 }
1538#ifdef FEAT_DIFF
1539 /* Correct the first entry for filler lines at the top
1540 * when it won't get updated below. */
1541 if (wp->w_p_diff && bot_start > 0)
1542 wp->w_lines[0].wl_size =
1543 plines_win_nofill(wp, wp->w_topline, TRUE)
1544 + wp->w_topfill;
1545#endif
1546 }
1547 }
1548 }
1549
1550 /* When starting redraw in the first line, redraw all lines. When
1551 * there is only one window it's probably faster to clear the screen
1552 * first. */
1553 if (mid_start == 0)
1554 {
1555 mid_end = wp->w_height;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001556 if (ONE_WINDOW && !WIN_IS_POPUP(wp))
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001557 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001558 /* Clear the screen when it was not done by win_del_lines() or
1559 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1560 * then. */
1561 if (screen_cleared != TRUE)
1562 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001563 /* The screen was cleared, redraw the tab pages line. */
1564 if (redraw_tabline)
1565 draw_tabline();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001568
1569 /* When win_del_lines() or win_ins_lines() caused the screen to be
1570 * cleared (only happens for the first window) or when screenclear()
1571 * was called directly above, "must_redraw" will have been set to
1572 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1573 if (screen_cleared == TRUE)
1574 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 }
1576 else
1577 {
1578 /* Not VALID or INVERTED: redraw all lines. */
1579 mid_start = 0;
1580 mid_end = wp->w_height;
1581 }
1582
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001583 if (type == SOME_VALID)
1584 {
1585 /* SOME_VALID: redraw all lines. */
1586 mid_start = 0;
1587 mid_end = wp->w_height;
1588 type = NOT_VALID;
1589 }
1590
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 /* check if we are updating or removing the inverted part */
1592 if ((VIsual_active && buf == curwin->w_buffer)
1593 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1594 {
1595 linenr_T from, to;
1596
1597 if (VIsual_active)
1598 {
1599 if (VIsual_active
1600 && (VIsual_mode != wp->w_old_visual_mode
1601 || type == INVERTED_ALL))
1602 {
1603 /*
1604 * If the type of Visual selection changed, redraw the whole
1605 * selection. Also when the ownership of the X selection is
1606 * gained or lost.
1607 */
1608 if (curwin->w_cursor.lnum < VIsual.lnum)
1609 {
1610 from = curwin->w_cursor.lnum;
1611 to = VIsual.lnum;
1612 }
1613 else
1614 {
1615 from = VIsual.lnum;
1616 to = curwin->w_cursor.lnum;
1617 }
1618 /* redraw more when the cursor moved as well */
1619 if (wp->w_old_cursor_lnum < from)
1620 from = wp->w_old_cursor_lnum;
1621 if (wp->w_old_cursor_lnum > to)
1622 to = wp->w_old_cursor_lnum;
1623 if (wp->w_old_visual_lnum < from)
1624 from = wp->w_old_visual_lnum;
1625 if (wp->w_old_visual_lnum > to)
1626 to = wp->w_old_visual_lnum;
1627 }
1628 else
1629 {
1630 /*
1631 * Find the line numbers that need to be updated: The lines
1632 * between the old cursor position and the current cursor
1633 * position. Also check if the Visual position changed.
1634 */
1635 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1636 {
1637 from = curwin->w_cursor.lnum;
1638 to = wp->w_old_cursor_lnum;
1639 }
1640 else
1641 {
1642 from = wp->w_old_cursor_lnum;
1643 to = curwin->w_cursor.lnum;
1644 if (from == 0) /* Visual mode just started */
1645 from = to;
1646 }
1647
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001648 if (VIsual.lnum != wp->w_old_visual_lnum
1649 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 {
1651 if (wp->w_old_visual_lnum < from
1652 && wp->w_old_visual_lnum != 0)
1653 from = wp->w_old_visual_lnum;
1654 if (wp->w_old_visual_lnum > to)
1655 to = wp->w_old_visual_lnum;
1656 if (VIsual.lnum < from)
1657 from = VIsual.lnum;
1658 if (VIsual.lnum > to)
1659 to = VIsual.lnum;
1660 }
1661 }
1662
1663 /*
1664 * If in block mode and changed column or curwin->w_curswant:
1665 * update all lines.
1666 * First compute the actual start and end column.
1667 */
1668 if (VIsual_mode == Ctrl_V)
1669 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001670 colnr_T fromc, toc;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001671#if defined(FEAT_LINEBREAK)
Bram Moolenaar404406a2014-10-09 13:24:43 +02001672 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673
Bram Moolenaar404406a2014-10-09 13:24:43 +02001674 if (curwin->w_p_lbr)
1675 ve_flags = VE_ALL;
1676#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001678#if defined(FEAT_LINEBREAK)
Bram Moolenaar404406a2014-10-09 13:24:43 +02001679 ve_flags = save_ve_flags;
1680#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 ++toc;
1682 if (curwin->w_curswant == MAXCOL)
1683 toc = MAXCOL;
1684
1685 if (fromc != wp->w_old_cursor_fcol
1686 || toc != wp->w_old_cursor_lcol)
1687 {
1688 if (from > VIsual.lnum)
1689 from = VIsual.lnum;
1690 if (to < VIsual.lnum)
1691 to = VIsual.lnum;
1692 }
1693 wp->w_old_cursor_fcol = fromc;
1694 wp->w_old_cursor_lcol = toc;
1695 }
1696 }
1697 else
1698 {
1699 /* Use the line numbers of the old Visual area. */
1700 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1701 {
1702 from = wp->w_old_cursor_lnum;
1703 to = wp->w_old_visual_lnum;
1704 }
1705 else
1706 {
1707 from = wp->w_old_visual_lnum;
1708 to = wp->w_old_cursor_lnum;
1709 }
1710 }
1711
1712 /*
1713 * There is no need to update lines above the top of the window.
1714 */
1715 if (from < wp->w_topline)
1716 from = wp->w_topline;
1717
1718 /*
1719 * If we know the value of w_botline, use it to restrict the update to
1720 * the lines that are visible in the window.
1721 */
1722 if (wp->w_valid & VALID_BOTLINE)
1723 {
1724 if (from >= wp->w_botline)
1725 from = wp->w_botline - 1;
1726 if (to >= wp->w_botline)
1727 to = wp->w_botline - 1;
1728 }
1729
1730 /*
1731 * Find the minimal part to be updated.
1732 * Watch out for scrolling that made entries in w_lines[] invalid.
1733 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1734 * top_end; need to redraw from top_end to the "to" line.
1735 * A middle mouse click with a Visual selection may change the text
1736 * above the Visual area and reset wl_valid, do count these for
1737 * mid_end (in srow).
1738 */
1739 if (mid_start > 0)
1740 {
1741 lnum = wp->w_topline;
1742 idx = 0;
1743 srow = 0;
1744 if (scrolled_down)
1745 mid_start = top_end;
1746 else
1747 mid_start = 0;
1748 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1749 {
1750 if (wp->w_lines[idx].wl_valid)
1751 mid_start += wp->w_lines[idx].wl_size;
1752 else if (!scrolled_down)
1753 srow += wp->w_lines[idx].wl_size;
1754 ++idx;
1755# ifdef FEAT_FOLDING
1756 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1757 lnum = wp->w_lines[idx].wl_lnum;
1758 else
1759# endif
1760 ++lnum;
1761 }
1762 srow += mid_start;
1763 mid_end = wp->w_height;
1764 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1765 {
1766 if (wp->w_lines[idx].wl_valid
1767 && wp->w_lines[idx].wl_lnum >= to + 1)
1768 {
1769 /* Only update until first row of this line */
1770 mid_end = srow;
1771 break;
1772 }
1773 srow += wp->w_lines[idx].wl_size;
1774 }
1775 }
1776 }
1777
1778 if (VIsual_active && buf == curwin->w_buffer)
1779 {
1780 wp->w_old_visual_mode = VIsual_mode;
1781 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1782 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001783 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 wp->w_old_curswant = curwin->w_curswant;
1785 }
1786 else
1787 {
1788 wp->w_old_visual_mode = 0;
1789 wp->w_old_cursor_lnum = 0;
1790 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001791 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793
1794#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1795 /* reset got_int, otherwise regexp won't work */
1796 save_got_int = got_int;
1797 got_int = 0;
1798#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001799#ifdef SYN_TIME_LIMIT
1800 /* Set the time limit to 'redrawtime'. */
1801 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02001802 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001803#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804#ifdef FEAT_FOLDING
1805 win_foldinfo.fi_level = 0;
1806#endif
1807
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001808#ifdef FEAT_MENU
1809 /*
1810 * Draw the window toolbar, if there is one.
1811 * TODO: only when needed.
1812 */
1813 if (winbar_height(wp) > 0)
1814 redraw_win_toolbar(wp);
1815#endif
1816
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 /*
1818 * Update all the window rows.
1819 */
1820 idx = 0; /* first entry in w_lines[].wl_size */
1821 row = 0;
1822 srow = 0;
1823 lnum = wp->w_topline; /* first line shown in window */
1824 for (;;)
1825 {
1826 /* stop updating when reached the end of the window (check for _past_
1827 * the end of the window is at the end of the loop) */
1828 if (row == wp->w_height)
1829 {
1830 didline = TRUE;
1831 break;
1832 }
1833
1834 /* stop updating when hit the end of the file */
1835 if (lnum > buf->b_ml.ml_line_count)
1836 {
1837 eof = TRUE;
1838 break;
1839 }
1840
1841 /* Remember the starting row of the line that is going to be dealt
1842 * with. It is used further down when the line doesn't fit. */
1843 srow = row;
1844
1845 /*
1846 * Update a line when it is in an area that needs updating, when it
1847 * has changes or w_lines[idx] is invalid.
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02001848 * "bot_start" may be halfway a wrapped line after using
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 * win_del_lines(), check if the current line includes it.
1850 * When syntax folding is being used, the saved syntax states will
1851 * already have been updated, we can't see where the syntax state is
1852 * the same again, just update until the end of the window.
1853 */
1854 if (row < top_end
1855 || (row >= mid_start && row < mid_end)
1856#ifdef FEAT_SEARCH_EXTRA
1857 || top_to_mod
1858#endif
1859 || idx >= wp->w_lines_valid
1860 || (row + wp->w_lines[idx].wl_size > bot_start)
1861 || (mod_top != 0
1862 && (lnum == mod_top
1863 || (lnum >= mod_top
1864 && (lnum < mod_bot
1865#ifdef FEAT_SYN_HL
1866 || did_update == DID_FOLD
1867 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001868 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 && (
1870# ifdef FEAT_FOLDING
1871 (foldmethodIsSyntax(wp)
1872 && hasAnyFolding(wp)) ||
1873# endif
1874 syntax_check_changed(lnum)))
1875#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001876#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001877 /* match in fixed position might need redraw
1878 * if lines were inserted or deleted */
1879 || (wp->w_match_head != NULL
1880 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001881#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 )))))
1883 {
1884#ifdef FEAT_SEARCH_EXTRA
1885 if (lnum == mod_top)
1886 top_to_mod = FALSE;
1887#endif
1888
1889 /*
1890 * When at start of changed lines: May scroll following lines
1891 * up or down to minimize redrawing.
1892 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001893 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 */
1895 if (lnum == mod_top
1896 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001897 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 {
1899 int old_rows = 0;
1900 int new_rows = 0;
1901 int xtra_rows;
1902 linenr_T l;
1903
1904 /* Count the old number of window rows, using w_lines[], which
1905 * should still contain the sizes for the lines as they are
1906 * currently displayed. */
1907 for (i = idx; i < wp->w_lines_valid; ++i)
1908 {
1909 /* Only valid lines have a meaningful wl_lnum. Invalid
1910 * lines are part of the changed area. */
1911 if (wp->w_lines[i].wl_valid
1912 && wp->w_lines[i].wl_lnum == mod_bot)
1913 break;
1914 old_rows += wp->w_lines[i].wl_size;
1915#ifdef FEAT_FOLDING
1916 if (wp->w_lines[i].wl_valid
1917 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1918 {
1919 /* Must have found the last valid entry above mod_bot.
1920 * Add following invalid entries. */
1921 ++i;
1922 while (i < wp->w_lines_valid
1923 && !wp->w_lines[i].wl_valid)
1924 old_rows += wp->w_lines[i++].wl_size;
1925 break;
1926 }
1927#endif
1928 }
1929
1930 if (i >= wp->w_lines_valid)
1931 {
1932 /* We can't find a valid line below the changed lines,
1933 * need to redraw until the end of the window.
1934 * Inserting/deleting lines has no use. */
1935 bot_start = 0;
1936 }
1937 else
1938 {
1939 /* Able to count old number of rows: Count new window
1940 * rows, and may insert/delete lines */
1941 j = idx;
1942 for (l = lnum; l < mod_bot; ++l)
1943 {
1944#ifdef FEAT_FOLDING
1945 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1946 ++new_rows;
1947 else
1948#endif
1949#ifdef FEAT_DIFF
1950 if (l == wp->w_topline)
1951 new_rows += plines_win_nofill(wp, l, TRUE)
1952 + wp->w_topfill;
1953 else
1954#endif
1955 new_rows += plines_win(wp, l, TRUE);
1956 ++j;
1957 if (new_rows > wp->w_height - row - 2)
1958 {
1959 /* it's getting too much, must redraw the rest */
1960 new_rows = 9999;
1961 break;
1962 }
1963 }
1964 xtra_rows = new_rows - old_rows;
1965 if (xtra_rows < 0)
1966 {
1967 /* May scroll text up. If there is not enough
1968 * remaining text or scrolling fails, must redraw the
1969 * rest. If scrolling works, must redraw the text
1970 * below the scrolled text. */
1971 if (row - xtra_rows >= wp->w_height - 2)
1972 mod_bot = MAXLNUM;
1973 else
1974 {
1975 check_for_delay(FALSE);
1976 if (win_del_lines(wp, row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001977 -xtra_rows, FALSE, FALSE, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 mod_bot = MAXLNUM;
1979 else
1980 bot_start = wp->w_height + xtra_rows;
1981 }
1982 }
1983 else if (xtra_rows > 0)
1984 {
1985 /* May scroll text down. If there is not enough
1986 * remaining text of scrolling fails, must redraw the
1987 * rest. */
1988 if (row + xtra_rows >= wp->w_height - 2)
1989 mod_bot = MAXLNUM;
1990 else
1991 {
1992 check_for_delay(FALSE);
1993 if (win_ins_lines(wp, row + old_rows,
1994 xtra_rows, FALSE, FALSE) == FAIL)
1995 mod_bot = MAXLNUM;
1996 else if (top_end > row + old_rows)
1997 /* Scrolled the part at the top that requires
1998 * updating down. */
1999 top_end += xtra_rows;
2000 }
2001 }
2002
2003 /* When not updating the rest, may need to move w_lines[]
2004 * entries. */
2005 if (mod_bot != MAXLNUM && i != j)
2006 {
2007 if (j < i)
2008 {
2009 int x = row + new_rows;
2010
2011 /* move entries in w_lines[] upwards */
2012 for (;;)
2013 {
2014 /* stop at last valid entry in w_lines[] */
2015 if (i >= wp->w_lines_valid)
2016 {
2017 wp->w_lines_valid = j;
2018 break;
2019 }
2020 wp->w_lines[j] = wp->w_lines[i];
2021 /* stop at a line that won't fit */
2022 if (x + (int)wp->w_lines[j].wl_size
2023 > wp->w_height)
2024 {
2025 wp->w_lines_valid = j + 1;
2026 break;
2027 }
2028 x += wp->w_lines[j++].wl_size;
2029 ++i;
2030 }
2031 if (bot_start > x)
2032 bot_start = x;
2033 }
2034 else /* j > i */
2035 {
2036 /* move entries in w_lines[] downwards */
2037 j -= i;
2038 wp->w_lines_valid += j;
2039 if (wp->w_lines_valid > wp->w_height)
2040 wp->w_lines_valid = wp->w_height;
2041 for (i = wp->w_lines_valid; i - j >= idx; --i)
2042 wp->w_lines[i] = wp->w_lines[i - j];
2043
2044 /* The w_lines[] entries for inserted lines are
2045 * now invalid, but wl_size may be used above.
2046 * Reset to zero. */
2047 while (i >= idx)
2048 {
2049 wp->w_lines[i].wl_size = 0;
2050 wp->w_lines[i--].wl_valid = FALSE;
2051 }
2052 }
2053 }
2054 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 }
2056
2057#ifdef FEAT_FOLDING
2058 /*
2059 * When lines are folded, display one line for all of them.
2060 * Otherwise, display normally (can be several display lines when
2061 * 'wrap' is on).
2062 */
2063 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2064 if (fold_count != 0)
2065 {
2066 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2067 ++row;
2068 --fold_count;
2069 wp->w_lines[idx].wl_folded = TRUE;
2070 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2071# ifdef FEAT_SYN_HL
2072 did_update = DID_FOLD;
2073# endif
2074 }
2075 else
2076#endif
2077 if (idx < wp->w_lines_valid
2078 && wp->w_lines[idx].wl_valid
2079 && wp->w_lines[idx].wl_lnum == lnum
2080 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002081 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002082 && !WIN_IS_POPUP(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 && srow + wp->w_lines[idx].wl_size > wp->w_height
2084#ifdef FEAT_DIFF
2085 && diff_check_fill(wp, lnum) == 0
2086#endif
2087 )
2088 {
2089 /* This line is not going to fit. Don't draw anything here,
2090 * will draw "@ " lines below. */
2091 row = wp->w_height + 1;
2092 }
2093 else
2094 {
2095#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarbbca7732019-07-24 18:13:16 +02002096 prepare_search_hl(wp, &search_hl, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097#endif
2098#ifdef FEAT_SYN_HL
2099 /* Let the syntax stuff know we skipped a few lines. */
2100 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002101 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 syntax_end_parsing(syntax_last_parsed + 1);
2103#endif
2104
2105 /*
2106 * Display one line.
2107 */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002108 row = win_line(wp, lnum, srow, wp->w_height,
2109 mod_top == 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110
2111#ifdef FEAT_FOLDING
2112 wp->w_lines[idx].wl_folded = FALSE;
2113 wp->w_lines[idx].wl_lastlnum = lnum;
2114#endif
2115#ifdef FEAT_SYN_HL
2116 did_update = DID_LINE;
2117 syntax_last_parsed = lnum;
2118#endif
2119 }
2120
2121 wp->w_lines[idx].wl_lnum = lnum;
2122 wp->w_lines[idx].wl_valid = TRUE;
Bram Moolenaar0e19fc02017-10-28 14:45:16 +02002123
2124 /* Past end of the window or end of the screen. Note that after
2125 * resizing wp->w_height may be end up too big. That's a problem
2126 * elsewhere, but prevent a crash here. */
2127 if (row > wp->w_height || row + wp->w_winrow >= Rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 {
2129 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002130 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2132 ++idx;
2133 break;
2134 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002135 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 wp->w_lines[idx].wl_size = row - srow;
2137 ++idx;
2138#ifdef FEAT_FOLDING
2139 lnum += fold_count + 1;
2140#else
2141 ++lnum;
2142#endif
2143 }
2144 else
2145 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002146 if (wp->w_p_rnu)
2147 {
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002148#ifdef FEAT_FOLDING
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002149 // 'relativenumber' set: The text doesn't need to be drawn, but
2150 // the number column nearly always does.
Bram Moolenaar7701f302018-10-02 21:20:32 +02002151 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2152 if (fold_count != 0)
Bram Moolenaar7701f302018-10-02 21:20:32 +02002153 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
Bram Moolenaar7701f302018-10-02 21:20:32 +02002154 else
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002155#endif
Bram Moolenaar7701f302018-10-02 21:20:32 +02002156 (void)win_line(wp, lnum, srow, wp->w_height, TRUE, TRUE);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002157 }
2158
2159 // This line does not need to be drawn, advance to the next one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 row += wp->w_lines[idx++].wl_size;
2161 if (row > wp->w_height) /* past end of screen */
2162 break;
2163#ifdef FEAT_FOLDING
2164 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2165#else
2166 ++lnum;
2167#endif
2168#ifdef FEAT_SYN_HL
2169 did_update = DID_NONE;
2170#endif
2171 }
2172
2173 if (lnum > buf->b_ml.ml_line_count)
2174 {
2175 eof = TRUE;
2176 break;
2177 }
2178 }
2179 /*
2180 * End of loop over all window lines.
2181 */
2182
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002183#ifdef FEAT_VTP
2184 /* Rewrite the character at the end of the screen line. */
2185 if (use_vtp())
2186 {
2187 int i;
2188
2189 for (i = 0; i < Rows; ++i)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002190 if (enc_utf8)
2191 if ((*mb_off2cells)(LineOffset[i] + Columns - 2,
2192 LineOffset[i] + screen_Columns) > 1)
2193 screen_draw_rectangle(i, Columns - 2, 1, 2, FALSE);
2194 else
2195 screen_draw_rectangle(i, Columns - 1, 1, 1, FALSE);
2196 else
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002197 screen_char(LineOffset[i] + Columns - 1, i, Columns - 1);
2198 }
2199#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200
2201 if (idx > wp->w_lines_valid)
2202 wp->w_lines_valid = idx;
2203
2204#ifdef FEAT_SYN_HL
2205 /*
2206 * Let the syntax stuff know we stop parsing here.
2207 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002208 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 syntax_end_parsing(syntax_last_parsed + 1);
2210#endif
2211
2212 /*
2213 * If we didn't hit the end of the file, and we didn't finish the last
2214 * line we were working on, then the line didn't fit.
2215 */
2216 wp->w_empty_rows = 0;
2217#ifdef FEAT_DIFF
2218 wp->w_filler_rows = 0;
2219#endif
2220 if (!eof && !didline)
2221 {
2222 if (lnum == wp->w_topline)
2223 {
2224 /*
2225 * Single line that does not fit!
2226 * Don't overwrite it, it can be edited.
2227 */
2228 wp->w_botline = lnum + 1;
2229 }
2230#ifdef FEAT_DIFF
2231 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2232 {
2233 /* Window ends in filler lines. */
2234 wp->w_botline = lnum;
2235 wp->w_filler_rows = wp->w_height - srow;
2236 }
2237#endif
Bram Moolenaar17146962019-05-30 00:12:11 +02002238#ifdef FEAT_TEXT_PROP
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002239 else if (WIN_IS_POPUP(wp))
Bram Moolenaar17146962019-05-30 00:12:11 +02002240 {
2241 // popup line that doesn't fit is left as-is
2242 wp->w_botline = lnum;
2243 }
2244#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002245 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2246 {
2247 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2248
2249 /*
2250 * Last line isn't finished: Display "@@@" in the last screen line.
2251 */
Bram Moolenaar53f81742017-09-22 14:35:51 +02002252 screen_puts_len((char_u *)"@@", 2, scr_row, wp->w_wincol,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002253 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002254 screen_fill(scr_row, scr_row + 1,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002255 (int)wp->w_wincol + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002256 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002257 set_empty_rows(wp, srow);
2258 wp->w_botline = lnum;
2259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2261 {
2262 /*
2263 * Last line isn't finished: Display "@@@" at the end.
2264 */
2265 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2266 W_WINROW(wp) + wp->w_height,
2267 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002268 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 set_empty_rows(wp, srow);
2270 wp->w_botline = lnum;
2271 }
2272 else
2273 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002274 win_draw_end(wp, '@', ' ', TRUE, srow, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 wp->w_botline = lnum;
2276 }
2277 }
2278 else
2279 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 if (eof) /* we hit the end of the file */
2282 {
2283 wp->w_botline = buf->b_ml.ml_line_count + 1;
2284#ifdef FEAT_DIFF
2285 j = diff_check_fill(wp, wp->w_botline);
2286 if (j > 0 && !wp->w_botfill)
2287 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002288 // Display filler lines at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 if (char2cells(fill_diff) > 1)
2290 i = '-';
2291 else
2292 i = fill_diff;
2293 if (row + j > wp->w_height)
2294 j = wp->w_height - row;
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002295 win_draw_end(wp, i, i, TRUE, row, row + (int)j, HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 row += j;
2297 }
2298#endif
2299 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002300 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 wp->w_botline = lnum;
2302
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002303 // Make sure the rest of the screen is blank
2304 // put '~'s on rows that aren't part of the file.
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002305 win_draw_end(wp, WIN_IS_POPUP(wp) ? ' ' : '~',
2306 ' ', FALSE, row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 }
2308
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002309#ifdef SYN_TIME_LIMIT
2310 syn_set_timeout(NULL);
2311#endif
2312
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 /* Reset the type of redrawing required, the window has been updated. */
2314 wp->w_redr_type = 0;
2315#ifdef FEAT_DIFF
2316 wp->w_old_topfill = wp->w_topfill;
2317 wp->w_old_botfill = wp->w_botfill;
2318#endif
2319
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002320 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 {
2322 /*
2323 * There is a trick with w_botline. If we invalidate it on each
2324 * change that might modify it, this will cause a lot of expensive
2325 * calls to plines() in update_topline() each time. Therefore the
2326 * value of w_botline is often approximated, and this value is used to
2327 * compute the value of w_topline. If the value of w_botline was
2328 * wrong, check that the value of w_topline is correct (cursor is on
2329 * the visible part of the text). If it's not, we need to redraw
2330 * again. Mostly this just means scrolling up a few lines, so it
2331 * doesn't look too bad. Only do this for the current window (where
2332 * changes are relevant).
2333 */
2334 wp->w_valid |= VALID_BOTLINE;
2335 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2336 {
2337 recursive = TRUE;
2338 curwin->w_valid &= ~VALID_TOPLINE;
2339 update_topline(); /* may invalidate w_botline again */
2340 if (must_redraw != 0)
2341 {
2342 /* Don't update for changes in buffer again. */
2343 i = curbuf->b_mod_set;
2344 curbuf->b_mod_set = FALSE;
2345 win_update(curwin);
2346 must_redraw = 0;
2347 curbuf->b_mod_set = i;
2348 }
2349 recursive = FALSE;
2350 }
2351 }
2352
2353#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2354 /* restore got_int, unless CTRL-C was hit while redrawing */
2355 if (!got_int)
2356 got_int = save_got_int;
2357#endif
2358}
2359
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360/*
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002361 * Call screen_fill() with the columns adjusted for 'rightleft' if needed.
2362 * Return the new offset.
2363 */
2364 static int
2365screen_fill_end(
2366 win_T *wp,
2367 int c1,
2368 int c2,
2369 int off,
2370 int width,
2371 int row,
2372 int endrow,
2373 int attr)
2374{
2375 int nn = off + width;
2376
2377 if (nn > wp->w_width)
2378 nn = wp->w_width;
2379#ifdef FEAT_RIGHTLEFT
2380 if (wp->w_p_rl)
2381 {
2382 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2383 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - off,
2384 c1, c2, attr);
2385 }
2386 else
2387#endif
2388 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2389 wp->w_wincol + off, (int)wp->w_wincol + nn,
2390 c1, c2, attr);
2391 return nn;
2392}
2393
2394/*
2395 * Clear lines near the end the window and mark the unused lines with "c1".
2396 * use "c2" as the filler character.
2397 * When "draw_margin" is TRUE then draw the sign, fold and number columns.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 */
2399 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002400win_draw_end(
2401 win_T *wp,
2402 int c1,
2403 int c2,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002404 int draw_margin,
Bram Moolenaar05540972016-01-30 20:31:25 +01002405 int row,
2406 int endrow,
2407 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 int n = 0;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002410 int attr = HL_ATTR(hl);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002411 int wcr_attr = get_wcr_attr(wp);
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002412
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002413 attr = hl_combine_attr(wcr_attr, attr);
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002414
2415 if (draw_margin)
2416 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002417#ifdef FEAT_FOLDING
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002418 int fdc = compute_foldcolumn(wp, 0);
2419
2420 if (fdc > 0)
2421 // draw the fold column
2422 n = screen_fill_end(wp, ' ', ' ', n, fdc,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002423 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_FC)));
Bram Moolenaar1c934292015-01-27 16:39:29 +01002424#endif
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002425#ifdef FEAT_SIGNS
2426 if (signcolumn_on(wp))
2427 // draw the sign column
2428 n = screen_fill_end(wp, ' ', ' ', n, 2,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002429 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_SC)));
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002430#endif
2431 if ((wp->w_p_nu || wp->w_p_rnu)
2432 && vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
2433 // draw the number column
2434 n = screen_fill_end(wp, ' ', ' ', n, number_width(wp) + 1,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002435 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_N)));
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437
2438#ifdef FEAT_RIGHTLEFT
2439 if (wp->w_p_rl)
2440 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002442 wp->w_wincol, W_ENDCOL(wp) - 1 - n,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002443 c2, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002445 W_ENDCOL(wp) - 1 - n, W_ENDCOL(wp) - n,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002446 c1, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 }
2448 else
2449#endif
2450 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002452 wp->w_wincol + n, (int)W_ENDCOL(wp),
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002453 c1, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 }
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002455
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 set_empty_rows(wp, row);
2457}
2458
Bram Moolenaar1a384422010-07-14 19:53:30 +02002459#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02002460/*
2461 * Advance **color_cols and return TRUE when there are columns to draw.
2462 */
2463 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002464advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002465{
2466 while (**color_cols >= 0 && vcol > **color_cols)
2467 ++*color_cols;
2468 return (**color_cols >= 0);
2469}
2470#endif
2471
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002472#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
2473/*
2474 * Copy "text" to ScreenLines using "attr".
2475 * Returns the next screen column.
2476 */
2477 static int
2478text_to_screenline(win_T *wp, char_u *text, int col)
2479{
2480 int off = (int)(current_ScreenLine - ScreenLines);
2481
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002482 if (has_mbyte)
2483 {
2484 int cells;
2485 int u8c, u8cc[MAX_MCO];
2486 int i;
2487 int idx;
2488 int c_len;
2489 char_u *p;
2490# ifdef FEAT_ARABIC
2491 int prev_c = 0; /* previous Arabic character */
2492 int prev_c1 = 0; /* first composing char for prev_c */
2493# endif
2494
2495# ifdef FEAT_RIGHTLEFT
2496 if (wp->w_p_rl)
2497 idx = off;
2498 else
2499# endif
2500 idx = off + col;
2501
2502 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2503 for (p = text; *p != NUL; )
2504 {
2505 cells = (*mb_ptr2cells)(p);
2506 c_len = (*mb_ptr2len)(p);
Bram Moolenaar02631462017-09-22 15:20:32 +02002507 if (col + cells > wp->w_width
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002508# ifdef FEAT_RIGHTLEFT
2509 - (wp->w_p_rl ? col : 0)
2510# endif
2511 )
2512 break;
2513 ScreenLines[idx] = *p;
2514 if (enc_utf8)
2515 {
2516 u8c = utfc_ptr2char(p, u8cc);
2517 if (*p < 0x80 && u8cc[0] == 0)
2518 {
2519 ScreenLinesUC[idx] = 0;
2520#ifdef FEAT_ARABIC
2521 prev_c = u8c;
2522#endif
2523 }
2524 else
2525 {
2526#ifdef FEAT_ARABIC
2527 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2528 {
2529 /* Do Arabic shaping. */
2530 int pc, pc1, nc;
2531 int pcc[MAX_MCO];
2532 int firstbyte = *p;
2533
2534 /* The idea of what is the previous and next
2535 * character depends on 'rightleft'. */
2536 if (wp->w_p_rl)
2537 {
2538 pc = prev_c;
2539 pc1 = prev_c1;
2540 nc = utf_ptr2char(p + c_len);
2541 prev_c1 = u8cc[0];
2542 }
2543 else
2544 {
2545 pc = utfc_ptr2char(p + c_len, pcc);
2546 nc = prev_c;
2547 pc1 = pcc[0];
2548 }
2549 prev_c = u8c;
2550
2551 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
2552 pc, pc1, nc);
2553 ScreenLines[idx] = firstbyte;
2554 }
2555 else
2556 prev_c = u8c;
2557#endif
2558 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar9ba61172019-01-24 18:20:17 +01002559 ScreenLinesUC[idx] = u8c;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002560 for (i = 0; i < Screen_mco; ++i)
2561 {
2562 ScreenLinesC[i][idx] = u8cc[i];
2563 if (u8cc[i] == 0)
2564 break;
2565 }
2566 }
2567 if (cells > 1)
2568 ScreenLines[idx + 1] = 0;
2569 }
2570 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2571 /* double-byte single width character */
2572 ScreenLines2[idx] = p[1];
2573 else if (cells > 1)
2574 /* double-width character */
2575 ScreenLines[idx + 1] = p[1];
2576 col += cells;
2577 idx += cells;
2578 p += c_len;
2579 }
2580 }
2581 else
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002582 {
2583 int len = (int)STRLEN(text);
2584
Bram Moolenaar02631462017-09-22 15:20:32 +02002585 if (len > wp->w_width - col)
2586 len = wp->w_width - col;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002587 if (len > 0)
2588 {
2589#ifdef FEAT_RIGHTLEFT
2590 if (wp->w_p_rl)
Bram Moolenaarc6663882019-02-22 19:14:54 +01002591 mch_memmove(current_ScreenLine, text, len);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002592 else
2593#endif
Bram Moolenaarc6663882019-02-22 19:14:54 +01002594 mch_memmove(current_ScreenLine + col, text, len);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002595 col += len;
2596 }
2597 }
2598 return col;
2599}
2600#endif
2601
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602#ifdef FEAT_FOLDING
2603/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002604 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2605 * space is available for window "wp", minus "col".
2606 */
2607 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002608compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002609{
2610 int fdc = wp->w_p_fdc;
2611 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
Bram Moolenaar02631462017-09-22 15:20:32 +02002612 int wwidth = wp->w_width;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002613
2614 if (fdc > wwidth - (col + wmw))
2615 fdc = wwidth - (col + wmw);
2616 return fdc;
2617}
2618
2619/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 * Display one folded line.
2621 */
2622 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002623fold_line(
2624 win_T *wp,
2625 long fold_count,
2626 foldinfo_T *foldinfo,
2627 linenr_T lnum,
2628 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002630 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 pos_T *top, *bot;
2632 linenr_T lnume = lnum + fold_count - 1;
2633 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002634 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 int col;
2637 int txtcol;
2638 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002639 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640
2641 /* Build the fold line:
2642 * 1. Add the cmdwin_type for the command-line window
2643 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002644 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 * 4. Compose the text
2646 * 5. Add the text
2647 * 6. set highlighting for the Visual area an other text
2648 */
2649 col = 0;
2650
2651 /*
2652 * 1. Add the cmdwin_type for the command-line window
2653 * Ignores 'rightleft', this window is never right-left.
2654 */
2655#ifdef FEAT_CMDWIN
2656 if (cmdwin_type != 0 && wp == curwin)
2657 {
2658 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002659 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 if (enc_utf8)
2661 ScreenLinesUC[off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 ++col;
2663 }
2664#endif
2665
2666 /*
2667 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002668 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002670 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 if (fdc > 0)
2672 {
2673 fill_foldcolumn(buf, wp, TRUE, lnum);
2674#ifdef FEAT_RIGHTLEFT
2675 if (wp->w_p_rl)
2676 {
2677 int i;
2678
Bram Moolenaar02631462017-09-22 15:20:32 +02002679 copy_text_attr(off + wp->w_width - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002680 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 /* reverse the fold column */
2682 for (i = 0; i < fdc; ++i)
Bram Moolenaar02631462017-09-22 15:20:32 +02002683 ScreenLines[off + wp->w_width - i - 1 - col] = buf[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 }
2685 else
2686#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002687 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 col += fdc;
2689 }
2690
2691#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6f470022018-04-10 18:47:20 +02002692# define RL_MEMSET(p, v, l) \
2693 do { \
2694 if (wp->w_p_rl) \
2695 for (ri = 0; ri < l; ++ri) \
2696 ScreenAttrs[off + (wp->w_width - (p) - (l)) + ri] = v; \
2697 else \
2698 for (ri = 0; ri < l; ++ri) \
2699 ScreenAttrs[off + (p) + ri] = v; \
2700 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701#else
Bram Moolenaar6f470022018-04-10 18:47:20 +02002702# define RL_MEMSET(p, v, l) \
2703 do { \
2704 for (ri = 0; ri < l; ++ri) \
2705 ScreenAttrs[off + (p) + ri] = v; \
2706 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707#endif
2708
Bram Moolenaar64486672010-05-16 15:46:46 +02002709 /* Set all attributes of the 'number' or 'relativenumber' column and the
2710 * text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002711 RL_MEMSET(col, HL_ATTR(HLF_FL), wp->w_width - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712
2713#ifdef FEAT_SIGNS
2714 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002715 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002717 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 if (len > 0)
2719 {
2720 if (len > 2)
2721 len = 2;
2722# ifdef FEAT_RIGHTLEFT
2723 if (wp->w_p_rl)
2724 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002725 copy_text_attr(off + wp->w_width - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002726 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 else
2728# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002729 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 col += len;
2731 }
2732 }
2733#endif
2734
2735 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002736 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002738 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002740 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 if (len > 0)
2742 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002743 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002744 long num;
2745 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002746
2747 if (len > w + 1)
2748 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002749
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002750 if (wp->w_p_nu && !wp->w_p_rnu)
2751 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002752 num = (long)lnum;
2753 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002754 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002755 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002756 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002757 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002758 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002759 /* 'number' + 'relativenumber': cursor line shows absolute
2760 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002761 num = lnum;
2762 fmt = "%-*ld ";
2763 }
2764 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002765
Bram Moolenaar700e7342013-01-30 12:31:36 +01002766 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767#ifdef FEAT_RIGHTLEFT
2768 if (wp->w_p_rl)
2769 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002770 copy_text_attr(off + wp->w_width - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002771 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 else
2773#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002774 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 col += len;
2776 }
2777 }
2778
2779 /*
2780 * 4. Compose the folded-line string with 'foldtext', if set.
2781 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002782 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783
2784 txtcol = col; /* remember where text starts */
2785
2786 /*
2787 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2788 * Right-left text is put in columns 0 - number-col, normal text is put
2789 * in columns number-col - window-width.
2790 */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002791 col = text_to_screenline(wp, text, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792
2793 /* Fill the rest of the line with the fold filler */
2794#ifdef FEAT_RIGHTLEFT
2795 if (wp->w_p_rl)
2796 col -= txtcol;
2797#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02002798 while (col < wp->w_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799#ifdef FEAT_RIGHTLEFT
2800 - (wp->w_p_rl ? txtcol : 0)
2801#endif
2802 )
2803 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 if (enc_utf8)
2805 {
2806 if (fill_fold >= 0x80)
2807 {
2808 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002809 ScreenLinesC[0][off + col] = 0;
Bram Moolenaaracda04f2018-02-08 09:57:28 +01002810 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 }
2812 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002813 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002815 ScreenLines[off + col] = fill_fold;
2816 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002817 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002819 else
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002820 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 }
2822
2823 if (text != buf)
2824 vim_free(text);
2825
2826 /*
2827 * 6. set highlighting for the Visual area an other text.
2828 * If all folded lines are in the Visual area, highlight the line.
2829 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2831 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002832 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 {
2834 /* Visual is after curwin->w_cursor */
2835 top = &curwin->w_cursor;
2836 bot = &VIsual;
2837 }
2838 else
2839 {
2840 /* Visual is before curwin->w_cursor */
2841 top = &VIsual;
2842 bot = &curwin->w_cursor;
2843 }
2844 if (lnum >= top->lnum
2845 && lnume <= bot->lnum
2846 && (VIsual_mode != 'v'
2847 || ((lnum > top->lnum
2848 || (lnum == top->lnum
2849 && top->col == 0))
2850 && (lnume < bot->lnum
2851 || (lnume == bot->lnum
2852 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002853 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 {
2855 if (VIsual_mode == Ctrl_V)
2856 {
2857 /* Visual block mode: highlight the chars part of the block */
Bram Moolenaar02631462017-09-22 15:20:32 +02002858 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002860 if (wp->w_old_cursor_lcol != MAXCOL
2861 && wp->w_old_cursor_lcol + txtcol
Bram Moolenaar02631462017-09-22 15:20:32 +02002862 < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 len = wp->w_old_cursor_lcol;
2864 else
Bram Moolenaar02631462017-09-22 15:20:32 +02002865 len = wp->w_width - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002866 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002867 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 }
2869 }
2870 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002871 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 /* Set all attributes of the text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002873 RL_MEMSET(txtcol, HL_ATTR(HLF_V), wp->w_width - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 }
2876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002878#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002879 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2880 if (wp->w_p_cc_cols)
2881 {
2882 int i = 0;
2883 int j = wp->w_p_cc_cols[i];
2884 int old_txtcol = txtcol;
2885
2886 while (j > -1)
2887 {
2888 txtcol += j;
2889 if (wp->w_p_wrap)
2890 txtcol -= wp->w_skipcol;
2891 else
2892 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002893 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002894 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002895 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002896 txtcol = old_txtcol;
2897 j = wp->w_p_cc_cols[++i];
2898 }
2899 }
2900
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002901 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002902 if (wp->w_p_cuc)
2903 {
2904 txtcol += wp->w_virtcol;
2905 if (wp->w_p_wrap)
2906 txtcol -= wp->w_skipcol;
2907 else
2908 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002909 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaar85595c52008-10-02 16:04:05 +00002910 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002911 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00002912 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002913#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914
Bram Moolenaar02631462017-09-22 15:20:32 +02002915 screen_line(row + W_WINROW(wp), wp->w_wincol, (int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002916 (int)wp->w_width, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917
2918 /*
2919 * Update w_cline_height and w_cline_folded if the cursor line was
2920 * updated (saves a call to plines() later).
2921 */
2922 if (wp == curwin
2923 && lnum <= curwin->w_cursor.lnum
2924 && lnume >= curwin->w_cursor.lnum)
2925 {
2926 curwin->w_cline_row = row;
2927 curwin->w_cline_height = 1;
2928 curwin->w_cline_folded = TRUE;
2929 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2930 }
2931}
2932
2933/*
2934 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2935 */
2936 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002937copy_text_attr(
2938 int off,
2939 char_u *buf,
2940 int len,
2941 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002943 int i;
2944
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 mch_memmove(ScreenLines + off, buf, (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 if (enc_utf8)
2947 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002948 for (i = 0; i < len; ++i)
2949 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950}
2951
2952/*
2953 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002954 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 */
2956 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002957fill_foldcolumn(
2958 char_u *p,
2959 win_T *wp,
2960 int closed, /* TRUE of FALSE */
2961 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962{
2963 int i = 0;
2964 int level;
2965 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002966 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002967 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968
2969 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002970 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971
2972 level = win_foldinfo.fi_level;
2973 if (level > 0)
2974 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002975 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002976 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002977
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 /* If the column is too narrow, we start at the lowest level that
2979 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002980 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 if (first_level < 1)
2982 first_level = 1;
2983
Bram Moolenaar1c934292015-01-27 16:39:29 +01002984 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 {
2986 if (win_foldinfo.fi_lnum == lnum
2987 && first_level + i >= win_foldinfo.fi_low_level)
2988 p[i] = '-';
2989 else if (first_level == 1)
2990 p[i] = '|';
2991 else if (first_level + i <= 9)
2992 p[i] = '0' + first_level + i;
2993 else
2994 p[i] = '>';
2995 if (first_level + i == level)
2996 break;
2997 }
2998 }
2999 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01003000 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001}
3002#endif /* FEAT_FOLDING */
3003
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01003004#ifdef FEAT_TEXT_PROP
3005static textprop_T *current_text_props = NULL;
3006static buf_T *current_buf = NULL;
3007
3008 static int
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01003009text_prop_compare(const void *s1, const void *s2)
3010{
3011 int idx1, idx2;
3012 proptype_T *pt1, *pt2;
3013 colnr_T col1, col2;
3014
3015 idx1 = *(int *)s1;
3016 idx2 = *(int *)s2;
3017 pt1 = text_prop_type_by_id(current_buf, current_text_props[idx1].tp_type);
3018 pt2 = text_prop_type_by_id(current_buf, current_text_props[idx2].tp_type);
3019 if (pt1 == pt2)
3020 return 0;
3021 if (pt1 == NULL)
3022 return -1;
3023 if (pt2 == NULL)
3024 return 1;
3025 if (pt1->pt_priority != pt2->pt_priority)
3026 return pt1->pt_priority > pt2->pt_priority ? 1 : -1;
3027 col1 = current_text_props[idx1].tp_col;
3028 col2 = current_text_props[idx2].tp_col;
3029 return col1 == col2 ? 0 : col1 > col2 ? 1 : -1;
3030}
3031#endif
3032
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003033#ifdef FEAT_SIGNS
3034/*
3035 * Get information needed to display the sign in line 'lnum' in window 'wp'.
3036 * If 'nrcol' is TRUE, the sign is going to be displayed in the number column.
3037 * Otherwise the sign is going to be displayed in the sign column.
3038 */
3039 static void
3040get_sign_display_info(
3041 int nrcol,
3042 win_T *wp,
Bram Moolenaar4e038572019-07-04 18:28:35 +02003043 linenr_T lnum UNUSED,
3044 sign_attrs_T *sattr,
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003045 int wcr_attr,
3046 int row,
3047 int startrow,
Bram Moolenaarbf8c3ad2019-06-19 14:28:43 +02003048 int filler_lines UNUSED,
3049 int filler_todo UNUSED,
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003050 int *c_extrap,
3051 int *c_finalp,
3052 char_u *extra,
3053 char_u **pp_extra,
3054 int *n_extrap,
3055 int *char_attrp)
3056{
3057 int text_sign;
3058# ifdef FEAT_SIGN_ICONS
3059 int icon_sign;
3060# endif
3061
3062 // Draw two cells with the sign value or blank.
3063 *c_extrap = ' ';
3064 *c_finalp = NUL;
3065 if (nrcol)
3066 *n_extrap = number_width(wp) + 1;
3067 else
3068 {
3069 *char_attrp = hl_combine_attr(wcr_attr, HL_ATTR(HLF_SC));
3070 *n_extrap = 2;
3071 }
3072
3073 if (row == startrow
3074#ifdef FEAT_DIFF
3075 + filler_lines && filler_todo <= 0
3076#endif
3077 )
3078 {
Bram Moolenaar4e038572019-07-04 18:28:35 +02003079 text_sign = (sattr->text != NULL) ? sattr->typenr : 0;
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003080# ifdef FEAT_SIGN_ICONS
Bram Moolenaar4e038572019-07-04 18:28:35 +02003081 icon_sign = (sattr->icon != NULL) ? sattr->typenr : 0;
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003082 if (gui.in_use && icon_sign != 0)
3083 {
3084 // Use the image in this position.
Bram Moolenaar4dff4ae2019-06-19 16:31:28 +02003085 if (nrcol)
3086 {
3087 *c_extrap = NUL;
3088 sprintf((char *)extra, "%-*c ", number_width(wp), SIGN_BYTE);
3089 *pp_extra = extra;
3090 *n_extrap = (int)STRLEN(*pp_extra);
3091 }
3092 else
3093 *c_extrap = SIGN_BYTE;
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003094# ifdef FEAT_NETBEANS_INTG
Bram Moolenaar4e038572019-07-04 18:28:35 +02003095 if (netbeans_active() && (buf_signcount(wp->w_buffer, lnum) > 1))
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003096 {
Bram Moolenaar4dff4ae2019-06-19 16:31:28 +02003097 if (nrcol)
3098 {
3099 *c_extrap = NUL;
3100 sprintf((char *)extra, "%-*c ", number_width(wp),
3101 MULTISIGN_BYTE);
3102 *pp_extra = extra;
3103 *n_extrap = (int)STRLEN(*pp_extra);
3104 }
3105 else
3106 *c_extrap = MULTISIGN_BYTE;
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003107 }
3108# endif
Bram Moolenaar4dff4ae2019-06-19 16:31:28 +02003109 *c_finalp = NUL;
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003110 *char_attrp = icon_sign;
3111 }
3112 else
3113# endif
3114 if (text_sign != 0)
3115 {
Bram Moolenaar4e038572019-07-04 18:28:35 +02003116 *pp_extra = sattr->text;
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003117 if (*pp_extra != NULL)
3118 {
3119 if (nrcol)
3120 {
Bram Moolenaard6bcff42019-07-18 12:48:16 +02003121 int n, width = number_width(wp) - 2;
3122
3123 for (n = 0; n < width; n++)
3124 extra[n] = ' ';
3125 extra[n] = 0;
3126 STRCAT(extra, *pp_extra);
3127 STRCAT(extra, " ");
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003128 *pp_extra = extra;
3129 }
3130 *c_extrap = NUL;
3131 *c_finalp = NUL;
3132 *n_extrap = (int)STRLEN(*pp_extra);
3133 }
Bram Moolenaar4e038572019-07-04 18:28:35 +02003134 *char_attrp = sattr->texthl;
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003135 }
3136 }
3137}
3138#endif
3139
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140/*
3141 * Display line "lnum" of window 'wp' on the screen.
3142 * Start at row "startrow", stop when "endrow" is reached.
3143 * wp->w_virtcol needs to be valid.
3144 *
3145 * Return the number of last row the line occupies.
3146 */
3147 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003148win_line(
3149 win_T *wp,
3150 linenr_T lnum,
3151 int startrow,
3152 int endrow,
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003153 int nochange UNUSED, // not updating for changed text
3154 int number_only) // only update the number column
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155{
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003156 int col = 0; // visual column on screen
3157 unsigned off; // offset in ScreenLines/ScreenAttrs
3158 int c = 0; // init for GCC
3159 long vcol = 0; // virtual column (for tabs)
Bram Moolenaard574ea22015-01-14 19:35:14 +01003160#ifdef FEAT_LINEBREAK
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003161 long vcol_sbr = -1; // virtual column after showbreak
Bram Moolenaard574ea22015-01-14 19:35:14 +01003162#endif
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003163 long vcol_prev = -1; // "vcol" of previous character
3164 char_u *line; // current line
3165 char_u *ptr; // current position in "line"
3166 int row; // row in the window, excl w_winrow
3167 int screen_row; // row on the screen, incl w_winrow
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003169 char_u extra[21]; // "%ld " and 'fdc' must fit in here
3170 int n_extra = 0; // number of extra chars
3171 char_u *p_extra = NULL; // string of extra chars, plus NUL
3172 char_u *p_extra_free = NULL; // p_extra needs to be freed
3173 int c_extra = NUL; // extra chars, all the same
3174 int c_final = NUL; // final char, mandatory if set
3175 int extra_attr = 0; // attributes when n_extra != 0
3176 static char_u *at_end_str = (char_u *)""; // used for p_extra when
3177 // displaying lcs_eol at end-of-line
3178 int lcs_eol_one = lcs_eol; // lcs_eol until it's been used
3179 int lcs_prec_todo = lcs_prec; // lcs_prec until it's been used
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003181 // saved "extra" items for when draw_state becomes WL_LINE (again)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 int saved_n_extra = 0;
3183 char_u *saved_p_extra = NULL;
3184 int saved_c_extra = 0;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003185 int saved_c_final = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 int saved_char_attr = 0;
3187
3188 int n_attr = 0; /* chars with special attr */
3189 int saved_attr2 = 0; /* char_attr saved for n_attr */
3190 int n_attr3 = 0; /* chars with overruling special attr */
3191 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
3192
3193 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
3194
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003195 int fromcol = -10; // start of inverting
3196 int tocol = MAXCOL; // end of inverting
3197 int fromcol_prev = -2; // start of inverting after cursor
3198 int noinvcur = FALSE; // don't invert the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003200 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 pos_T pos;
3202 long v;
3203
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003204 int char_attr = 0; // attributes for next character
3205 int attr_pri = FALSE; // char_attr has priority
3206 int area_highlighting = FALSE; // Visual or incsearch highlighting
3207 // in this line
3208 int vi_attr = 0; // attributes for Visual and incsearch
3209 // highlighting
3210 int wcr_attr = 0; // attributes from 'wincolor'
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003211 int win_attr = 0; // background for whole window, except
3212 // margins and "~" lines.
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003213 int area_attr = 0; // attributes desired by highlighting
3214 int search_attr = 0; // attributes desired by 'hlsearch'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003216 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 int syntax_attr = 0; /* attributes desired by syntax */
3218 int has_syntax = FALSE; /* this buffer has syntax highl. */
3219 int save_did_emsg;
Bram Moolenaar1a384422010-07-14 19:53:30 +02003220 int draw_color_col = FALSE; /* highlight colorcolumn */
3221 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003222#endif
Bram Moolenaarf914a332019-07-20 15:09:56 +02003223 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003224#ifdef FEAT_TEXT_PROP
3225 int text_prop_count;
3226 int text_prop_next = 0; // next text property to use
3227 textprop_T *text_props = NULL;
3228 int *text_prop_idxs = NULL;
3229 int text_props_active = 0;
3230 proptype_T *text_prop_type = NULL;
3231 int text_prop_attr = 0;
Bram Moolenaarde24a872019-05-05 15:48:00 +02003232 int text_prop_combine = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003233#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003234#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003235 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003236# define SPWORDLEN 150
3237 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003238 int nextlinecol = 0; /* column where nextline[] starts */
3239 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003240 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003241 int spell_attr = 0; /* attributes desired by spelling */
3242 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003243 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3244 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003245 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003246 static int cap_col = -1; /* column to check for Cap word */
3247 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003248 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003250 int extra_check = 0; // has extra highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 int multi_attr = 0; /* attributes desired by multibyte */
3252 int mb_l = 1; /* multi-byte byte length */
3253 int mb_c = 0; /* decoded multi-byte character */
3254 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003255 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaare9726e32019-06-19 18:01:21 +02003256#if defined(FEAT_DIFF) || defined(FEAT_SIGNS)
Bram Moolenaarbf8c3ad2019-06-19 14:28:43 +02003257 int filler_lines = 0; /* nr of filler lines to be drawn */
3258 int filler_todo = 0; /* nr of filler lines still to do + 1 */
Bram Moolenaare9726e32019-06-19 18:01:21 +02003259#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260#ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003261 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 int change_start = MAXCOL; /* first col of changed area */
3263 int change_end = -1; /* last col of changed area */
3264#endif
3265 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3266#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003267 int need_showbreak = FALSE; /* overlong line, skipping first x
3268 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003270#if defined(FEAT_SIGNS) || defined(FEAT_QUICKFIX) \
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003271 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003273 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274#endif
Bram Moolenaarb4d9b892019-07-04 22:59:06 +02003275#ifdef FEAT_SIGNS
3276 int sign_present = FALSE;
3277 sign_attrs_T sattr;
3278#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279#ifdef FEAT_ARABIC
3280 int prev_c = 0; /* previous Arabic character */
3281 int prev_c1 = 0; /* first composing char for prev_c */
3282#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003283#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003284 int did_line_attr = 0;
3285#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003286#ifdef FEAT_TERMINAL
3287 int get_term_attr = FALSE;
3288#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289
3290 /* draw_state: items that are drawn in sequence: */
3291#define WL_START 0 /* nothing done yet */
3292#ifdef FEAT_CMDWIN
3293# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3294#else
3295# define WL_CMDLINE WL_START
3296#endif
3297#ifdef FEAT_FOLDING
3298# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3299#else
3300# define WL_FOLD WL_CMDLINE
3301#endif
3302#ifdef FEAT_SIGNS
3303# define WL_SIGN WL_FOLD + 1 /* column for signs */
3304#else
3305# define WL_SIGN WL_FOLD /* column for signs */
3306#endif
3307#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003308#ifdef FEAT_LINEBREAK
3309# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003311# define WL_BRI WL_NR
3312#endif
3313#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3314# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3315#else
3316# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317#endif
3318#define WL_LINE WL_SBR + 1 /* text in the line */
3319 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003320#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 int feedback_col = 0;
3322 int feedback_old_attr = -1;
3323#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003324 int screen_line_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325
Bram Moolenaarfbfb7572019-07-25 20:53:03 +02003326#if defined(FEAT_CONCEAL) || defined(FEAT_SEARCH_EXTRA)
Bram Moolenaar07d13562019-07-24 18:43:08 +02003327 int match_conc = 0; // cchar for match functions
Bram Moolenaarfbfb7572019-07-25 20:53:03 +02003328#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02003329#ifdef FEAT_CONCEAL
3330 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003331 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003332 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003333 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003334 int is_concealing = FALSE;
Bram Moolenaar07d13562019-07-24 18:43:08 +02003335 int boguscols = 0; // nonexistent columns added to force
3336 // wrapping
3337 int vcol_off = 0; // offset for concealed characters
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003338 int did_wcol = FALSE;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003339 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003340# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003341# define FIX_FOR_BOGUSCOLS \
3342 { \
3343 n_extra += vcol_off; \
3344 vcol -= vcol_off; \
3345 vcol_off = 0; \
3346 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003347 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003348 boguscols = 0; \
3349 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003350#else
3351# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003352#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353
3354 if (startrow > endrow) /* past the end already! */
3355 return startrow;
3356
3357 row = startrow;
3358 screen_row = row + W_WINROW(wp);
3359
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003360 if (!number_only)
3361 {
3362 /*
3363 * To speed up the loop below, set extra_check when there is linebreak,
3364 * trailing white space and/or syntax processing to be done.
3365 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366#ifdef FEAT_LINEBREAK
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003367 extra_check = wp->w_p_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368#endif
3369#ifdef FEAT_SYN_HL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003370 if (syntax_present(wp) && !wp->w_s->b_syn_error
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003371# ifdef SYN_TIME_LIMIT
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003372 && !wp->w_s->b_syn_slow
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003373# endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003374 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003376 /* Prepare for syntax highlighting in this line. When there is an
3377 * error, stop syntax highlighting. */
3378 save_did_emsg = did_emsg;
3379 did_emsg = FALSE;
3380 syntax_start(wp, lnum);
3381 if (did_emsg)
3382 wp->w_s->b_syn_error = TRUE;
3383 else
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003384 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003385 did_emsg = save_did_emsg;
3386#ifdef SYN_TIME_LIMIT
3387 if (!wp->w_s->b_syn_slow)
3388#endif
3389 {
3390 has_syntax = TRUE;
3391 extra_check = TRUE;
3392 }
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003395
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003396 /* Check for columns to display for 'colorcolumn'. */
3397 color_cols = wp->w_p_cc_cols;
3398 if (color_cols != NULL)
3399 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003400#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003401
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003402#ifdef FEAT_TERMINAL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003403 if (term_show_buffer(wp->w_buffer))
3404 {
3405 extra_check = TRUE;
3406 get_term_attr = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003407 win_attr = term_get_attr(wp->w_buffer, lnum, -1);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003408 }
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003409#endif
3410
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003411#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003412 if (wp->w_p_spell
3413 && *wp->w_s->b_p_spl != NUL
3414 && wp->w_s->b_langp.ga_len > 0
3415 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003416 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003417 /* Prepare for spell checking. */
3418 has_spell = TRUE;
3419 extra_check = TRUE;
3420
Bram Moolenaar7701f302018-10-02 21:20:32 +02003421 /* Get the start of the next line, so that words that wrap to the
3422 * next line are found too: "et<line-break>al.".
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003423 * Trick: skip a few chars for C/shell/Vim comments */
3424 nextline[SPWORDLEN] = NUL;
3425 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3426 {
3427 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3428 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3429 }
3430
Bram Moolenaar7701f302018-10-02 21:20:32 +02003431 /* When a word wrapped from the previous line the start of the
3432 * current line is valid. */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003433 if (lnum == checked_lnum)
3434 cur_checked_col = checked_col;
3435 checked_lnum = 0;
3436
3437 /* When there was a sentence end in the previous line may require a
3438 * word starting with capital in this line. In line 1 always check
3439 * the first word. */
3440 if (lnum != capcol_lnum)
3441 cap_col = -1;
3442 if (lnum == 1)
3443 cap_col = 0;
3444 capcol_lnum = 0;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446#endif
3447
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003448 /*
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003449 * handle Visual active in this window
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003450 */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003451 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003453 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 {
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003455 // Visual is after curwin->w_cursor
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003456 top = &curwin->w_cursor;
3457 bot = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 }
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003459 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 {
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003461 // Visual is before curwin->w_cursor
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003462 top = &VIsual;
3463 bot = &curwin->w_cursor;
3464 }
3465 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003466 if (VIsual_mode == Ctrl_V)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003467 {
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003468 // block mode
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003469 if (lnum_in_visual_area)
3470 {
3471 fromcol = wp->w_old_cursor_fcol;
3472 tocol = wp->w_old_cursor_lcol;
3473 }
3474 }
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003475 else
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003476 {
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003477 // non-block mode
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003478 if (lnum > top->lnum && lnum <= bot->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 fromcol = 0;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003480 else if (lnum == top->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 {
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003482 if (VIsual_mode == 'V') // linewise
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003483 fromcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 else
3485 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003486 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3487 if (gchar_pos(top) == NUL)
3488 tocol = fromcol + 1;
3489 }
3490 }
3491 if (VIsual_mode != 'V' && lnum == bot->lnum)
3492 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003493 if (*p_sel == 'e' && bot->col == 0 && bot->coladd == 0)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003494 {
3495 fromcol = -10;
3496 tocol = MAXCOL;
3497 }
3498 else if (bot->col == MAXCOL)
3499 tocol = MAXCOL;
3500 else
3501 {
3502 pos = *bot;
3503 if (*p_sel == 'e')
3504 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3505 else
3506 {
3507 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3508 ++tocol;
3509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 }
3511 }
3512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003514 /* Check if the character under the cursor should not be inverted */
3515 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003516#ifdef FEAT_GUI
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003517 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003518#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003519 )
3520 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003522 /* if inverting in this line set area_highlighting */
3523 if (fromcol >= 0)
3524 {
3525 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003526 vi_attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003528 if ((clip_star.available && !clip_star.owned
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003529 && clip_isautosel_star())
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003530 || (clip_plus.available && !clip_plus.owned
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003531 && clip_isautosel_plus()))
3532 vi_attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003534 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003537 /*
3538 * handle 'incsearch' and ":s///c" highlighting
3539 */
3540 else if (highlight_match
3541 && wp == curwin
3542 && lnum >= curwin->w_cursor.lnum
3543 && lnum <= curwin->w_cursor.lnum + search_match_lines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003545 if (lnum == curwin->w_cursor.lnum)
3546 getvcol(curwin, &(curwin->w_cursor),
Bram Moolenaarc6663882019-02-22 19:14:54 +01003547 (colnr_T *)&fromcol, NULL, NULL);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003548 else
3549 fromcol = 0;
3550 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3551 {
3552 pos.lnum = lnum;
3553 pos.col = search_match_endcol;
3554 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3555 }
3556 else
3557 tocol = MAXCOL;
3558 /* do at least one character; happens when past end of line */
3559 if (fromcol == tocol)
3560 tocol = fromcol + 1;
3561 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003562 vi_attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 }
3565
3566#ifdef FEAT_DIFF
3567 filler_lines = diff_check(wp, lnum);
3568 if (filler_lines < 0)
3569 {
3570 if (filler_lines == -1)
3571 {
3572 if (diff_find_change(wp, lnum, &change_start, &change_end))
3573 diff_hlf = HLF_ADD; /* added line */
3574 else if (change_start == 0)
3575 diff_hlf = HLF_TXD; /* changed text */
3576 else
3577 diff_hlf = HLF_CHD; /* changed line */
3578 }
3579 else
3580 diff_hlf = HLF_ADD; /* added line */
3581 filler_lines = 0;
3582 area_highlighting = TRUE;
3583 }
3584 if (lnum == wp->w_topline)
3585 filler_lines = wp->w_topfill;
3586 filler_todo = filler_lines;
3587#endif
3588
Bram Moolenaar4e038572019-07-04 18:28:35 +02003589#ifdef FEAT_SIGNS
3590 sign_present = buf_get_signattrs(wp->w_buffer, lnum, &sattr);
3591#endif
3592
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593#ifdef LINE_ATTR
3594# ifdef FEAT_SIGNS
3595 /* If this line has a sign with line highlighting set line_attr. */
Bram Moolenaar4e038572019-07-04 18:28:35 +02003596 if (sign_present)
3597 line_attr = sattr.linehl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003599# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003601 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003602 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603# endif
3604 if (line_attr != 0)
3605 area_highlighting = TRUE;
3606#endif
3607
3608 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3609 ptr = line;
3610
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003611#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003612 if (has_spell && !number_only)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003613 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003614 /* For checking first word with a capital skip white space. */
3615 if (cap_col == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003616 cap_col = getwhitecols(line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003617
Bram Moolenaar30abd282005-06-22 22:35:10 +00003618 /* To be able to spell-check over line boundaries copy the end of the
3619 * current line into nextline[]. Above the start of the next line was
3620 * copied to nextline[SPWORDLEN]. */
3621 if (nextline[SPWORDLEN] == NUL)
3622 {
3623 /* No next line or it is empty. */
3624 nextlinecol = MAXCOL;
3625 nextline_idx = 0;
3626 }
3627 else
3628 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003629 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003630 if (v < SPWORDLEN)
3631 {
3632 /* Short line, use it completely and append the start of the
3633 * next line. */
3634 nextlinecol = 0;
3635 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003636 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003637 nextline_idx = v + 1;
3638 }
3639 else
3640 {
3641 /* Long line, use only the last SPWORDLEN bytes. */
3642 nextlinecol = v - SPWORDLEN;
3643 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3644 nextline_idx = SPWORDLEN + 1;
3645 }
3646 }
3647 }
3648#endif
3649
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003650 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 {
Bram Moolenaar895d9662019-01-31 21:57:21 +01003652 if (lcs_space || lcs_trail || lcs_nbsp)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003653 extra_check = TRUE;
3654 /* find start of trailing whitespace */
3655 if (lcs_trail)
3656 {
3657 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003658 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003659 --trailcol;
3660 trailcol += (colnr_T) (ptr - line);
3661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 }
3663
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003664 wcr_attr = get_wcr_attr(wp);
3665 if (wcr_attr != 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003666 {
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003667 win_attr = wcr_attr;
3668 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003669 }
3670#ifdef FEAT_TEXT_PROP
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02003671 if (WIN_IS_POPUP(wp))
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003672 screen_line_flags |= SLF_POPUP;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003673#endif
3674
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 /*
3676 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3677 * first character to be displayed.
3678 */
3679 if (wp->w_p_wrap)
3680 v = wp->w_skipcol;
3681 else
3682 v = wp->w_leftcol;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003683 if (v > 0 && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 char_u *prev_ptr = ptr;
Bram Moolenaara12a1612019-01-24 16:39:02 +01003686
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 while (vcol < v && *ptr != NUL)
3688 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003689 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 vcol += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 prev_ptr = ptr;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003692 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 }
3694
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003695 /* When:
3696 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003697 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003698 * - 'virtualedit' is set, or
3699 * - the visual mode is active,
3700 * the end of the line may be before the start of the displayed part.
3701 */
3702 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003703#ifdef FEAT_SYN_HL
3704 wp->w_p_cuc || draw_color_col ||
3705#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003706 virtual_active() ||
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003707 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 vcol = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709
3710 /* Handle a character that's not completely on the screen: Put ptr at
3711 * that character but skip the first few screen characters. */
3712 if (vcol > v)
3713 {
3714 vcol -= c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 ptr = prev_ptr;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003716 /* If the character fits on the screen, don't need to skip it.
3717 * Except for a TAB. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01003718 if (( (*mb_ptr2cells)(ptr) >= c || *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003719 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 }
3721
3722 /*
3723 * Adjust for when the inverted text is before the screen,
3724 * and when the start of the inverted text is before the screen.
3725 */
3726 if (tocol <= vcol)
3727 fromcol = 0;
3728 else if (fromcol >= 0 && fromcol < vcol)
3729 fromcol = vcol;
3730
3731#ifdef FEAT_LINEBREAK
3732 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3733 if (wp->w_p_wrap)
3734 need_showbreak = TRUE;
3735#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003736#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003737 /* When spell checking a word we need to figure out the start of the
3738 * word and if it's badly spelled or not. */
3739 if (has_spell)
3740 {
3741 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003742 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003743 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003744
3745 pos = wp->w_cursor;
3746 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003747 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003748 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003749
3750 /* spell_move_to() may call ml_get() and make "line" invalid */
3751 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3752 ptr = line + linecol;
3753
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003754 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003755 {
3756 /* no bad word found at line start, don't check until end of a
3757 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003758 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003759 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003760 }
3761 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003762 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003763 /* bad word found, use attributes until end of word */
3764 word_end = wp->w_cursor.col + len + 1;
3765
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003766 /* Turn index into actual attributes. */
3767 if (spell_hlf != HLF_COUNT)
3768 spell_attr = highlight_attr[spell_hlf];
3769 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003770 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003771
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003772# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003773 /* Need to restart syntax highlighting for this line. */
3774 if (has_syntax)
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003775 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003776# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003777 }
3778#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 }
3780
3781 /*
3782 * Correct highlighting for cursor that can't be disabled.
3783 * Avoids having to check this for each character.
3784 */
3785 if (fromcol >= 0)
3786 {
3787 if (noinvcur)
3788 {
3789 if ((colnr_T)fromcol == wp->w_virtcol)
3790 {
3791 /* highlighting starts at cursor, let it start just after the
3792 * cursor */
3793 fromcol_prev = fromcol;
3794 fromcol = -1;
3795 }
3796 else if ((colnr_T)fromcol < wp->w_virtcol)
3797 /* restart highlighting after the cursor */
3798 fromcol_prev = wp->w_virtcol;
3799 }
3800 if (fromcol >= tocol)
3801 fromcol = -1;
3802 }
3803
3804#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarbbca7732019-07-24 18:13:16 +02003805 if (!number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003807 v = (long)(ptr - line);
Bram Moolenaarbbca7732019-07-24 18:13:16 +02003808 area_highlighting |= prepare_search_hl_line(wp, lnum, (colnr_T)v,
3809 &line, &search_hl, &search_attr);
3810 ptr = line + v; // "line" may have been updated
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 }
3812#endif
3813
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003814#ifdef FEAT_SYN_HL
Bram Moolenaar8156ed32019-03-09 11:46:15 +01003815 // Cursor line highlighting for 'cursorline' in the current window.
3816 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003817 {
Bram Moolenaar8156ed32019-03-09 11:46:15 +01003818 // Do not show the cursor line when Visual mode is active, because it's
3819 // not clear what is selected then. Do update w_last_cursorline.
3820 if (!(wp == curwin && VIsual_active))
3821 {
3822 line_attr = HL_ATTR(HLF_CUL);
3823 area_highlighting = TRUE;
3824 }
Bram Moolenaarc07ff5c2019-01-30 21:41:14 +01003825 wp->w_last_cursorline = wp->w_cursor.lnum;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003826 }
3827#endif
3828
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003829#ifdef FEAT_TEXT_PROP
3830 {
3831 char_u *prop_start;
3832
3833 text_prop_count = get_text_props(wp->w_buffer, lnum,
3834 &prop_start, FALSE);
3835 if (text_prop_count > 0)
3836 {
3837 // Make a copy of the properties, so that they are properly
3838 // aligned.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003839 text_props = ALLOC_MULT(textprop_T, text_prop_count);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003840 if (text_props != NULL)
3841 mch_memmove(text_props, prop_start,
3842 text_prop_count * sizeof(textprop_T));
3843
3844 // Allocate an array for the indexes.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003845 text_prop_idxs = ALLOC_MULT(int, text_prop_count);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003846 area_highlighting = TRUE;
3847 extra_check = TRUE;
3848 }
3849 }
3850#endif
3851
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003852 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 col = 0;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003854
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855#ifdef FEAT_RIGHTLEFT
3856 if (wp->w_p_rl)
3857 {
3858 /* Rightleft window: process the text in the normal direction, but put
3859 * it in current_ScreenLine[] from right to left. Start at the
3860 * rightmost column of the window. */
Bram Moolenaar02631462017-09-22 15:20:32 +02003861 col = wp->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 off += col;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003863 screen_line_flags |= SLF_RIGHTLEFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 }
3865#endif
3866
3867 /*
3868 * Repeat for the whole displayed line.
3869 */
3870 for (;;)
3871 {
Bram Moolenaarfbfb7572019-07-25 20:53:03 +02003872#if defined(FEAT_CONCEAL) || defined(FEAT_SEARCH_EXTRA)
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02003873 int has_match_conc = 0; // match wants to conceal
Bram Moolenaarfbfb7572019-07-25 20:53:03 +02003874#endif
Bram Moolenaar07d13562019-07-24 18:43:08 +02003875#ifdef FEAT_CONCEAL
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02003876 int did_decrement_ptr = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003877#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 /* Skip this quickly when working on the text. */
3879 if (draw_state != WL_LINE)
3880 {
3881#ifdef FEAT_CMDWIN
3882 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3883 {
3884 draw_state = WL_CMDLINE;
3885 if (cmdwin_type != 0 && wp == curwin)
3886 {
3887 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003889 c_extra = cmdwin_type;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003890 c_final = NUL;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003891 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 }
3893 }
3894#endif
3895
3896#ifdef FEAT_FOLDING
3897 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3898 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003899 int fdc = compute_foldcolumn(wp, 0);
3900
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003902 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003904 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003905 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003906 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003907 p_extra_free = alloc(12 + 1);
3908
3909 if (p_extra_free != NULL)
3910 {
3911 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3912 n_extra = fdc;
3913 p_extra_free[n_extra] = NUL;
3914 p_extra = p_extra_free;
3915 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003916 c_final = NUL;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003917 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_FC));
Bram Moolenaar62706602016-12-09 19:28:48 +01003918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 }
3920 }
3921#endif
3922
3923#ifdef FEAT_SIGNS
3924 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3925 {
3926 draw_state = WL_SIGN;
3927 /* Show the sign column when there are any signs in this
3928 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003929 if (signcolumn_on(wp))
Bram Moolenaar4e038572019-07-04 18:28:35 +02003930 get_sign_display_info(FALSE, wp, lnum, &sattr, wcr_attr,
3931 row, startrow, filler_lines, filler_todo, &c_extra,
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003932 &c_final, extra, &p_extra, &n_extra, &char_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 }
3934#endif
3935
3936 if (draw_state == WL_NR - 1 && n_extra == 0)
3937 {
3938 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003939 /* Display the absolute or relative line number. After the
3940 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3941 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 && (row == startrow
3943#ifdef FEAT_DIFF
3944 + filler_lines
3945#endif
3946 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3947 {
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003948#ifdef FEAT_SIGNS
3949 // If 'signcolumn' is set to 'number' and a sign is present
3950 // in 'lnum', then display the sign instead of the line
3951 // number.
3952 if ((*wp->w_p_scl == 'n' && *(wp->w_p_scl + 1) == 'u')
Bram Moolenaar4e038572019-07-04 18:28:35 +02003953 && sign_present)
3954 get_sign_display_info(TRUE, wp, lnum, &sattr, wcr_attr,
3955 row, startrow, filler_lines, filler_todo,
3956 &c_extra, &c_final, extra, &p_extra, &n_extra,
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003957 &char_attr);
3958 else
3959#endif
3960 {
3961 /* Draw the line number (empty space after wrapping). */
3962 if (row == startrow
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963#ifdef FEAT_DIFF
3964 + filler_lines
3965#endif
3966 )
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003967 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003968 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003969 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003970
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003971 if (wp->w_p_nu && !wp->w_p_rnu)
3972 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003973 num = (long)lnum;
3974 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003975 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003976 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003977 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003978 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003979 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003980 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003981 num = lnum;
3982 fmt = "%-*ld ";
3983 }
3984 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003985
Bram Moolenaar700e7342013-01-30 12:31:36 +01003986 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003987 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 if (wp->w_skipcol > 0)
3989 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3990 *p_extra = '-';
3991#ifdef FEAT_RIGHTLEFT
3992 if (wp->w_p_rl) /* reverse line numbers */
Bram Moolenaare73f9112019-03-29 18:29:54 +01003993 {
3994 char_u *p1, *p2;
3995 int t;
3996
3997 // like rl_mirror(), but keep the space at the end
3998 p2 = skiptowhite(extra) - 1;
3999 for (p1 = extra; p1 < p2; ++p1, --p2)
4000 {
4001 t = *p1;
4002 *p1 = *p2;
4003 *p2 = t;
4004 }
4005 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006#endif
4007 p_extra = extra;
4008 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004009 c_final = NUL;
Bram Moolenaar394c5d82019-06-17 21:48:05 +02004010 }
4011 else
4012 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004014 c_final = NUL;
Bram Moolenaar394c5d82019-06-17 21:48:05 +02004015 }
4016 n_extra = number_width(wp) + 1;
4017 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_N));
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004018#ifdef FEAT_SYN_HL
Bram Moolenaar394c5d82019-06-17 21:48:05 +02004019 /* When 'cursorline' is set highlight the line number of
4020 * the current line differently.
4021 * TODO: Can we use CursorLine instead of CursorLineNr
4022 * when CursorLineNr isn't set? */
4023 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01004024 && lnum == wp->w_cursor.lnum)
Bram Moolenaar193ffd12019-05-25 22:57:30 +02004025 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_CLN));
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004026#endif
Bram Moolenaar394c5d82019-06-17 21:48:05 +02004027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 }
4029 }
4030
Bram Moolenaar597a4222014-06-25 14:39:50 +02004031#ifdef FEAT_LINEBREAK
4032 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
4033 && n_extra == 0 && *p_sbr != NUL)
4034 /* draw indent after showbreak value */
4035 draw_state = WL_BRI;
4036 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
4037 /* After the showbreak, draw the breakindent */
4038 draw_state = WL_BRI - 1;
4039
4040 /* draw 'breakindent': indent wrapped text accordingly */
4041 if (draw_state == WL_BRI - 1 && n_extra == 0)
4042 {
4043 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01004044 /* if need_showbreak is set, breakindent also applies */
4045 if (wp->w_p_bri && n_extra == 0
4046 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004047# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004048 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004049# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004050 )
4051 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01004052 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004053# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004054 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004055 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004056 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004057# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02004058 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4059 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004060 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004061# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02004062 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004063# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02004064 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02004065 c_extra = ' ';
4066 n_extra = get_breakindent_win(wp,
4067 ml_get_buf(wp->w_buffer, lnum, FALSE));
4068 /* Correct end of highlighted area for 'breakindent',
4069 * required when 'linebreak' is also set. */
4070 if (tocol == vcol)
4071 tocol += n_extra;
4072 }
4073 }
4074#endif
4075
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
4077 if (draw_state == WL_SBR - 1 && n_extra == 0)
4078 {
4079 draw_state = WL_SBR;
4080# ifdef FEAT_DIFF
4081 if (filler_todo > 0)
4082 {
4083 /* Draw "deleted" diff line(s). */
4084 if (char2cells(fill_diff) > 1)
Bram Moolenaar83a52172019-01-16 22:41:54 +01004085 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 c_extra = '-';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004087 c_final = NUL;
4088 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 else
Bram Moolenaar83a52172019-01-16 22:41:54 +01004090 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 c_extra = fill_diff;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004092 c_final = NUL;
4093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094# ifdef FEAT_RIGHTLEFT
4095 if (wp->w_p_rl)
4096 n_extra = col + 1;
4097 else
4098# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004099 n_extra = wp->w_width - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004100 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 }
4102# endif
4103# ifdef FEAT_LINEBREAK
4104 if (*p_sbr != NUL && need_showbreak)
4105 {
4106 /* Draw 'showbreak' at the start of each broken line. */
4107 p_extra = p_sbr;
4108 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004109 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004111 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004113 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 /* Correct end of highlighted area for 'showbreak',
4115 * required when 'linebreak' is also set. */
4116 if (tocol == vcol)
4117 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004118#ifdef FEAT_SYN_HL
4119 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004120 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004121 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004122 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004123#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 }
4125# endif
4126 }
4127#endif
4128
4129 if (draw_state == WL_LINE - 1 && n_extra == 0)
4130 {
4131 draw_state = WL_LINE;
4132 if (saved_n_extra)
4133 {
4134 /* Continue item from end of wrapped line. */
4135 n_extra = saved_n_extra;
4136 c_extra = saved_c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004137 c_final = saved_c_final;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 p_extra = saved_p_extra;
4139 char_attr = saved_char_attr;
4140 }
4141 else
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004142 char_attr = win_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 }
4144 }
4145
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004146 // When still displaying '$' of change command, stop at cursor.
4147 // When only displaying the (relative) line number and that's done,
4148 // stop here.
4149 if ((dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004150 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151#ifdef FEAT_DIFF
4152 && filler_todo <= 0
4153#endif
4154 )
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004155 || (number_only && draw_state > WL_NR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 {
Bram Moolenaar02631462017-09-22 15:20:32 +02004157 screen_line(screen_row, wp->w_wincol, col, -(int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004158 screen_line_flags);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004159 /* Pretend we have finished updating the window. Except when
4160 * 'cursorcolumn' is set. */
4161#ifdef FEAT_SYN_HL
4162 if (wp->w_p_cuc)
4163 row = wp->w_cline_row + wp->w_cline_height;
4164 else
4165#endif
4166 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 break;
4168 }
4169
Bram Moolenaar637532b2019-01-03 21:44:40 +01004170 if (draw_state == WL_LINE && (area_highlighting
4171#ifdef FEAT_SPELL
4172 || has_spell
4173#endif
4174 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 {
4176 /* handle Visual or match highlighting in this line */
4177 if (vcol == fromcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4179 && (*mb_ptr2cells)(ptr) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004181 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 && vcol < tocol))
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004183 area_attr = vi_attr; /* start highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 else if (area_attr != 0
4185 && (vcol == tocol
4186 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188
4189#ifdef FEAT_SEARCH_EXTRA
4190 if (!n_extra)
4191 {
4192 /*
Bram Moolenaarbbca7732019-07-24 18:13:16 +02004193 * Check for start/end of 'hlsearch' and other matches.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 * After end, check for start/end of next match.
4195 * When another match, have to check for start again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004197 v = (long)(ptr - line);
Bram Moolenaarbbca7732019-07-24 18:13:16 +02004198 search_attr = update_search_hl(wp, lnum, (colnr_T)v, &line,
4199 &search_hl, &has_match_conc, &match_conc,
4200 did_line_attr, lcs_eol_one);
4201 ptr = line + v; // "line" may have been changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 }
4203#endif
4204
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004206 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004208 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4209 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004211 if (diff_hlf == HLF_TXD && ptr - line > change_end
4212 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004214 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004215 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004216 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 }
4218#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004219
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004220#ifdef FEAT_TEXT_PROP
4221 if (text_props != NULL)
4222 {
4223 int pi;
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004224 int bcol = (int)(ptr - line);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004225
Bram Moolenaara956bf62019-06-19 17:34:24 +02004226 if (n_extra > 0)
4227 --bcol; // still working on the previous char, e.g. Tab
4228
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004229 // Check if any active property ends.
4230 for (pi = 0; pi < text_props_active; ++pi)
4231 {
4232 int tpi = text_prop_idxs[pi];
4233
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004234 if (bcol >= text_props[tpi].tp_col - 1
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004235 + text_props[tpi].tp_len)
4236 {
4237 if (pi + 1 < text_props_active)
4238 mch_memmove(text_prop_idxs + pi,
4239 text_prop_idxs + pi + 1,
4240 sizeof(int)
4241 * (text_props_active - (pi + 1)));
4242 --text_props_active;
4243 --pi;
4244 }
4245 }
4246
4247 // Add any text property that starts in this column.
4248 while (text_prop_next < text_prop_count
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004249 && bcol >= text_props[text_prop_next].tp_col - 1)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004250 text_prop_idxs[text_props_active++] = text_prop_next++;
4251
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004252 text_prop_attr = 0;
Bram Moolenaarde24a872019-05-05 15:48:00 +02004253 text_prop_combine = FALSE;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004254 if (text_props_active > 0)
4255 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004256 // Sort the properties on priority and/or starting last.
4257 // Then combine the attributes, highest priority last.
4258 current_text_props = text_props;
4259 current_buf = wp->w_buffer;
4260 qsort((void *)text_prop_idxs, (size_t)text_props_active,
4261 sizeof(int), text_prop_compare);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004262
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004263 for (pi = 0; pi < text_props_active; ++pi)
4264 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004265 int tpi = text_prop_idxs[pi];
Bram Moolenaarde24a872019-05-05 15:48:00 +02004266 proptype_T *pt = text_prop_type_by_id(
4267 wp->w_buffer, text_props[tpi].tp_type);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004268
Bram Moolenaard74af422019-06-28 21:38:00 +02004269 if (pt != NULL && pt->pt_hl_id > 0)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004270 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004271 int pt_attr = syn_id2attr(pt->pt_hl_id);
4272
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004273 text_prop_type = pt;
Bram Moolenaarde24a872019-05-05 15:48:00 +02004274 text_prop_attr =
4275 hl_combine_attr(text_prop_attr, pt_attr);
4276 text_prop_combine = pt->pt_flags & PT_FLAG_COMBINE;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004277 }
4278 }
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004279 }
4280 }
4281#endif
4282
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004283 /* Decide which of the highlight attributes to use. */
4284 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004285#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004286 if (area_attr != 0)
4287 char_attr = hl_combine_attr(line_attr, area_attr);
4288 else if (search_attr != 0)
4289 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004290# ifdef FEAT_TEXT_PROP
4291 else if (text_prop_type != NULL)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02004292 {
4293 char_attr = hl_combine_attr(
4294 line_attr != 0 ? line_attr : win_attr, text_prop_attr);
4295 }
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004296# endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004297 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004298 || vcol < fromcol || vcol_prev < fromcol_prev
4299 || vcol >= tocol))
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004300 // Use line_attr when not in the Visual or 'incsearch' area
4301 // (area_attr may be 0 when "noinvcur" is set).
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004302 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004303#else
4304 if (area_attr != 0)
4305 char_attr = area_attr;
4306 else if (search_attr != 0)
4307 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004308#endif
4309 else
4310 {
4311 attr_pri = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004312#ifdef FEAT_TEXT_PROP
4313 if (text_prop_type != NULL)
Bram Moolenaarde24a872019-05-05 15:48:00 +02004314 {
4315 if (text_prop_combine)
4316 char_attr = hl_combine_attr(
4317 syntax_attr, text_prop_attr);
4318 else
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02004319 char_attr = hl_combine_attr(
4320 win_attr, text_prop_attr);
Bram Moolenaarde24a872019-05-05 15:48:00 +02004321 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004322 else
4323#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004324#ifdef FEAT_SYN_HL
4325 if (has_syntax)
4326 char_attr = syntax_attr;
4327 else
4328#endif
4329 char_attr = 0;
4330 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004332 if (char_attr == 0)
4333 char_attr = win_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334
4335 /*
4336 * Get the next character to put on the screen.
4337 */
4338 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004339 * The "p_extra" points to the extra stuff that is inserted to
4340 * represent special characters (non-printable stuff) and other
4341 * things. When all characters are the same, c_extra is used.
Bram Moolenaar83a52172019-01-16 22:41:54 +01004342 * If c_final is set, it will compulsorily be used at the end.
Bram Moolenaara064ac82007-08-05 18:10:54 +00004343 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4344 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4346 */
4347 if (n_extra > 0)
4348 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004349 if (c_extra != NUL || (n_extra == 1 && c_final != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004351 c = (n_extra == 1 && c_final != NUL) ? c_final : c_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004353 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 {
4355 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004356 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004357 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 }
4359 else
4360 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 }
4362 else
4363 {
4364 c = *p_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 if (has_mbyte)
4366 {
4367 mb_c = c;
4368 if (enc_utf8)
4369 {
4370 /* If the UTF-8 character is more than one byte:
4371 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004372 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 mb_utf8 = FALSE;
4374 if (mb_l > n_extra)
4375 mb_l = 1;
4376 else if (mb_l > 1)
4377 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004378 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004380 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 }
4382 }
4383 else
4384 {
4385 /* if this is a DBCS character, put it in "mb_c" */
4386 mb_l = MB_BYTE2LEN(c);
4387 if (mb_l >= n_extra)
4388 mb_l = 1;
4389 else if (mb_l > 1)
4390 mb_c = (c << 8) + p_extra[1];
4391 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004392 if (mb_l == 0) /* at the NUL at end-of-line */
4393 mb_l = 1;
4394
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 /* If a double-width char doesn't fit display a '>' in the
4396 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004397 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398# ifdef FEAT_RIGHTLEFT
4399 wp->w_p_rl ? (col <= 0) :
4400# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004401 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 && (*mb_char2cells)(mb_c) == 2)
4403 {
4404 c = '>';
4405 mb_c = c;
4406 mb_l = 1;
4407 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004408 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 /* put the pointer back to output the double-width
4410 * character at the start of the next line. */
4411 ++n_extra;
4412 --p_extra;
4413 }
4414 else
4415 {
4416 n_extra -= mb_l - 1;
4417 p_extra += mb_l - 1;
4418 }
4419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 ++p_extra;
4421 }
4422 --n_extra;
4423 }
4424 else
4425 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004426#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004427 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004428#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004429
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004430 if (p_extra_free != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01004431 VIM_CLEAR(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 /*
4433 * Get a character from the line itself.
4434 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004435 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004436#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004437 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004438#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 if (has_mbyte)
4440 {
4441 mb_c = c;
4442 if (enc_utf8)
4443 {
4444 /* If the UTF-8 character is more than one byte: Decode it
4445 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004446 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 mb_utf8 = FALSE;
4448 if (mb_l > 1)
4449 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004450 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 /* Overlong encoded ASCII or ASCII with composing char
4452 * is displayed normally, except a NUL. */
4453 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004454 {
4455 c = mb_c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004456#ifdef FEAT_LINEBREAK
Bram Moolenaar88e76882017-02-27 20:33:46 +01004457 c0 = mb_c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004458#endif
Bram Moolenaar88e76882017-02-27 20:33:46 +01004459 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004461
4462 /* At start of the line we can have a composing char.
4463 * Draw it as a space with a composing char. */
4464 if (utf_iscomposing(mb_c))
4465 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004466 int i;
4467
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004468 for (i = Screen_mco - 1; i > 0; --i)
4469 u8cc[i] = u8cc[i - 1];
4470 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004471 mb_c = ' ';
4472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 }
4474
4475 if ((mb_l == 1 && c >= 0x80)
4476 || (mb_l >= 1 && mb_c == 0)
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004477 || (mb_l > 1 && (!vim_isprintc(mb_c))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 {
4479 /*
4480 * Illegal UTF-8 byte: display as <xx>.
4481 * Non-BMP character : display as ? or fullwidth ?.
4482 */
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004483 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004484# ifdef FEAT_RIGHTLEFT
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004485 if (wp->w_p_rl) /* reverse */
4486 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004487# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 p_extra = extra;
4489 c = *p_extra;
4490 mb_c = mb_ptr2char_adv(&p_extra);
4491 mb_utf8 = (c >= 0x80);
4492 n_extra = (int)STRLEN(p_extra);
4493 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004494 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 if (area_attr == 0 && search_attr == 0)
4496 {
4497 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004498 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 saved_attr2 = char_attr; /* save current attr */
4500 }
4501 }
4502 else if (mb_l == 0) /* at the NUL at end-of-line */
4503 mb_l = 1;
4504#ifdef FEAT_ARABIC
4505 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4506 {
4507 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004508 int pc, pc1, nc;
4509 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510
4511 /* The idea of what is the previous and next
4512 * character depends on 'rightleft'. */
4513 if (wp->w_p_rl)
4514 {
4515 pc = prev_c;
4516 pc1 = prev_c1;
4517 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004518 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 }
4520 else
4521 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004522 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004524 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 }
4526 prev_c = mb_c;
4527
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004528 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 }
4530 else
4531 prev_c = mb_c;
4532#endif
4533 }
4534 else /* enc_dbcs */
4535 {
4536 mb_l = MB_BYTE2LEN(c);
4537 if (mb_l == 0) /* at the NUL at end-of-line */
4538 mb_l = 1;
4539 else if (mb_l > 1)
4540 {
4541 /* We assume a second byte below 32 is illegal.
4542 * Hopefully this is OK for all double-byte encodings!
4543 */
4544 if (ptr[1] >= 32)
4545 mb_c = (c << 8) + ptr[1];
4546 else
4547 {
4548 if (ptr[1] == NUL)
4549 {
4550 /* head byte at end of line */
4551 mb_l = 1;
4552 transchar_nonprint(extra, c);
4553 }
4554 else
4555 {
4556 /* illegal tail byte */
4557 mb_l = 2;
4558 STRCPY(extra, "XX");
4559 }
4560 p_extra = extra;
4561 n_extra = (int)STRLEN(extra) - 1;
4562 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004563 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 c = *p_extra++;
4565 if (area_attr == 0 && search_attr == 0)
4566 {
4567 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004568 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 saved_attr2 = char_attr; /* save current attr */
4570 }
4571 mb_c = c;
4572 }
4573 }
4574 }
4575 /* If a double-width char doesn't fit display a '>' in the
4576 * last column; the character is displayed at the start of the
4577 * next line. */
4578 if ((
4579# ifdef FEAT_RIGHTLEFT
4580 wp->w_p_rl ? (col <= 0) :
4581# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004582 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 && (*mb_char2cells)(mb_c) == 2)
4584 {
4585 c = '>';
4586 mb_c = c;
4587 mb_utf8 = FALSE;
4588 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004589 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02004590 // Put pointer back so that the character will be
4591 // displayed at the start of the next line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 --ptr;
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02004593#ifdef FEAT_CONCEAL
4594 did_decrement_ptr = TRUE;
4595#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 }
4597 else if (*ptr != NUL)
4598 ptr += mb_l - 1;
4599
4600 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004601 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004602 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004603 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004606 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004607 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 c = ' ';
4609 if (area_attr == 0 && search_attr == 0)
4610 {
4611 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004612 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 saved_attr2 = char_attr; /* save current attr */
4614 }
4615 mb_c = c;
4616 mb_utf8 = FALSE;
4617 mb_l = 1;
4618 }
4619
4620 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 ++ptr;
4622
4623 if (extra_check)
4624 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004625#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004626 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004627#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004628
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004629#ifdef FEAT_TERMINAL
4630 if (get_term_attr)
4631 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02004632 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004633
4634 if (!attr_pri)
4635 char_attr = syntax_attr;
4636 else
4637 char_attr = hl_combine_attr(syntax_attr, char_attr);
4638 }
4639#endif
4640
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004641#ifdef FEAT_SYN_HL
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004642 // Get syntax attribute, unless still at the start of the line
4643 // (double-wide char that doesn't fit).
Bram Moolenaar217ad922005-03-20 22:37:15 +00004644 v = (long)(ptr - line);
4645 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 {
4647 /* Get the syntax attribute for the character. If there
4648 * is an error, disable syntax highlighting. */
4649 save_did_emsg = did_emsg;
4650 did_emsg = FALSE;
4651
Bram Moolenaar217ad922005-03-20 22:37:15 +00004652 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004653# ifdef FEAT_SPELL
4654 has_spell ? &can_spell :
4655# endif
4656 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657
4658 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004659 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004660 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004661 has_syntax = FALSE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004662 syntax_attr = 0;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 else
4665 did_emsg = save_did_emsg;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004666
4667 // combine syntax attribute with 'wincolor'
4668 if (win_attr != 0)
4669 syntax_attr = hl_combine_attr(win_attr, syntax_attr);
4670
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004671#ifdef SYN_TIME_LIMIT
4672 if (wp->w_s->b_syn_slow)
4673 has_syntax = FALSE;
4674#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675
4676 /* Need to get the line again, a multi-line regexp may
4677 * have made it invalid. */
4678 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4679 ptr = line + v;
4680
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004681# ifdef FEAT_TEXT_PROP
Bram Moolenaarde24a872019-05-05 15:48:00 +02004682 // Text properties overrule syntax highlighting or combine.
4683 if (text_prop_attr == 0 || text_prop_combine)
4684# endif
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004685 {
Bram Moolenaarde24a872019-05-05 15:48:00 +02004686 int comb_attr = syntax_attr;
4687# ifdef FEAT_TEXT_PROP
4688 comb_attr = hl_combine_attr(text_prop_attr, comb_attr);
4689# endif
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004690 if (!attr_pri)
Bram Moolenaarde24a872019-05-05 15:48:00 +02004691 char_attr = comb_attr;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004692 else
Bram Moolenaarde24a872019-05-05 15:48:00 +02004693 char_attr = hl_combine_attr(comb_attr, char_attr);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004694 }
Bram Moolenaarc095b282010-07-20 22:33:34 +02004695# ifdef FEAT_CONCEAL
4696 /* no concealing past the end of the line, it interferes
4697 * with line highlighting */
4698 if (c == NUL)
4699 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004700 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004701 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004702# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004704#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004705
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004706#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004707 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004708 * Only do this when there is no syntax highlighting, the
4709 * @Spell cluster is not used or the current syntax item
4710 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004711 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004712 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004713 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004714 if (c != 0 && (
4715# ifdef FEAT_SYN_HL
4716 !has_syntax ||
4717# endif
4718 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004719 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004720 char_u *prev_ptr, *p;
4721 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004722 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaare7566042005-06-17 22:00:15 +00004723 if (has_mbyte)
4724 {
4725 prev_ptr = ptr - mb_l;
4726 v -= mb_l - 1;
4727 }
4728 else
Bram Moolenaare7566042005-06-17 22:00:15 +00004729 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004730
4731 /* Use nextline[] if possible, it has the start of the
4732 * next line concatenated. */
4733 if ((prev_ptr - line) - nextlinecol >= 0)
4734 p = nextline + (prev_ptr - line) - nextlinecol;
4735 else
4736 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004737 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004738 len = spell_check(wp, p, &spell_hlf, &cap_col,
4739 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004740 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004741
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004742 /* In Insert mode only highlight a word that
4743 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004744 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004745 && (State & INSERT) != 0
4746 && wp->w_cursor.lnum == lnum
4747 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004748 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004749 && wp->w_cursor.col < (colnr_T)word_end)
4750 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004751 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004752 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004753 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004754
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004755 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004756 && (p - nextline) + len > nextline_idx)
4757 {
4758 /* Remember that the good word continues at the
4759 * start of the next line. */
4760 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004761 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004762 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004763
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004764 /* Turn index into actual attributes. */
4765 if (spell_hlf != HLF_COUNT)
4766 spell_attr = highlight_attr[spell_hlf];
4767
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004768 if (cap_col > 0)
4769 {
4770 if (p != prev_ptr
4771 && (p - nextline) + cap_col >= nextline_idx)
4772 {
4773 /* Remember that the word in the next line
4774 * must start with a capital. */
4775 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004776 cap_col = (int)((p - nextline) + cap_col
4777 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004778 }
4779 else
4780 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004781 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004782 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004783 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004784 }
4785 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004786 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004787 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004788 char_attr = hl_combine_attr(char_attr, spell_attr);
4789 else
4790 char_attr = hl_combine_attr(spell_attr, char_attr);
4791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792#endif
4793#ifdef FEAT_LINEBREAK
4794 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004795 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004797 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004798 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 {
Bram Moolenaar4df70292015-03-21 14:20:16 +01004800 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004801 char_u *p = ptr - (mb_off + 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004802
Bram Moolenaar597a4222014-06-25 14:39:50 +02004803 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004804 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004805 NULL) - 1;
Bram Moolenaar02631462017-09-22 15:20:32 +02004806 if (c == TAB && n_extra + col > wp->w_width)
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004807# ifdef FEAT_VARTABS
Bram Moolenaara87b72c2018-06-25 21:24:51 +02004808 n_extra = tabstop_padding(vcol, wp->w_buffer->b_p_ts,
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004809 wp->w_buffer->b_p_vts_array) - 1;
4810# else
Bram Moolenaara3650912014-11-19 13:21:57 +01004811 n_extra = (int)wp->w_buffer->b_p_ts
4812 - vcol % (int)wp->w_buffer->b_p_ts - 1;
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004813# endif
Bram Moolenaara3650912014-11-19 13:21:57 +01004814
Bram Moolenaar4df70292015-03-21 14:20:16 +01004815 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004816 c_final = NUL;
Bram Moolenaar1c465442017-03-12 20:10:05 +01004817 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004818 {
4819#ifdef FEAT_CONCEAL
4820 if (c == TAB)
4821 /* See "Tab alignment" below. */
4822 FIX_FOR_BOGUSCOLS;
4823#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004824 if (!wp->w_p_list)
4825 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 }
4828#endif
4829
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004830 // 'list': Change char 160 to lcs_nbsp and space to lcs_space.
4831 // But not when the character is followed by a composing
4832 // character (use mb_l to check that).
4833 if (wp->w_p_list
4834 && ((((c == 160 && mb_l == 1)
4835 || (mb_utf8
4836 && ((mb_c == 160 && mb_l == 2)
4837 || (mb_c == 0x202f && mb_l == 3))))
4838 && lcs_nbsp)
4839 || (c == ' '
4840 && mb_l == 1
4841 && lcs_space
4842 && ptr - line <= trailcol)))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004843 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004844 c = (c == ' ') ? lcs_space : lcs_nbsp;
4845 if (area_attr == 0 && search_attr == 0)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004846 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004847 n_attr = 1;
4848 extra_attr = HL_ATTR(HLF_8);
4849 saved_attr2 = char_attr; /* save current attr */
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004850 }
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004851 mb_c = c;
4852 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004853 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004854 mb_utf8 = TRUE;
4855 u8cc[0] = 0;
4856 c = 0xc0;
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004857 }
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004858 else
4859 mb_utf8 = FALSE;
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004860 }
4861
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4863 {
4864 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004865 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866 {
4867 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004868 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 saved_attr2 = char_attr; /* save current attr */
4870 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004872 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 {
4874 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004875 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004876 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 }
4878 else
4879 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 }
4881 }
4882
4883 /*
4884 * Handling of non-printable characters.
4885 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004886 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 {
4888 /*
4889 * when getting a character from the file, we may have to
4890 * turn it into something else on the way to putting it
4891 * into "ScreenLines".
4892 */
4893 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4894 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004895 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004896 long vcol_adjusted = vcol; /* removed showbreak length */
4897#ifdef FEAT_LINEBREAK
4898 /* only adjust the tab_len, when at the first column
4899 * after the showbreak value was drawn */
4900 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4901 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4902#endif
Bram Moolenaar69cbbec2019-08-17 14:10:56 +02004903 // tab amount depends on current column
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004904#ifdef FEAT_VARTABS
4905 tab_len = tabstop_padding(vcol_adjusted,
4906 wp->w_buffer->b_p_ts,
4907 wp->w_buffer->b_p_vts_array) - 1;
4908#else
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004909 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004910 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4911#endif
Bram Moolenaard574ea22015-01-14 19:35:14 +01004912
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004913#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004914 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004915#endif
Bram Moolenaar69cbbec2019-08-17 14:10:56 +02004916 // tab amount depends on current column
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004917 n_extra = tab_len;
4918#ifdef FEAT_LINEBREAK
4919 else
4920 {
Bram Moolenaar69cbbec2019-08-17 14:10:56 +02004921 char_u *p;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01004922 int len;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004923 int i;
4924 int saved_nextra = n_extra;
4925
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004926#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004927 if (vcol_off > 0)
Bram Moolenaar69cbbec2019-08-17 14:10:56 +02004928 // there are characters to conceal
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004929 tab_len += vcol_off;
Bram Moolenaar69cbbec2019-08-17 14:10:56 +02004930 // boguscols before FIX_FOR_BOGUSCOLS macro from above
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004931 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
4932 && n_extra > tab_len)
4933 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004934#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004935
Bram Moolenaar69cbbec2019-08-17 14:10:56 +02004936 // if n_extra > 0, it gives the number of chars, to
4937 // use for a tab, else we need to calculate the width
4938 // for a tab
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004939 len = (tab_len * mb_char2len(lcs_tab2));
4940 if (n_extra > 0)
4941 len += n_extra - tab_len;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004942 c = lcs_tab1;
Bram Moolenaar964b3742019-05-24 18:54:09 +02004943 p = alloc(len + 1);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004944 vim_memset(p, ' ', len);
4945 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004946 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004947 p_extra_free = p;
4948 for (i = 0; i < tab_len; i++)
4949 {
Bram Moolenaar69cbbec2019-08-17 14:10:56 +02004950 int lcs = lcs_tab2;
4951
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004952 if (*p == NUL)
4953 {
4954 tab_len = i;
4955 break;
4956 }
Bram Moolenaar69cbbec2019-08-17 14:10:56 +02004957
4958 // if lcs_tab3 is given, need to change the char
4959 // for tab
4960 if (lcs_tab3 && i == tab_len - 1)
4961 lcs = lcs_tab3;
4962 mb_char2bytes(lcs, p);
4963 p += mb_char2len(lcs);
4964 n_extra += mb_char2len(lcs)
4965 - (saved_nextra > 0 ? 1 : 0);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004966 }
4967 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004968#ifdef FEAT_CONCEAL
Bram Moolenaar69cbbec2019-08-17 14:10:56 +02004969 // n_extra will be increased by FIX_FOX_BOGUSCOLS
4970 // macro below, so need to adjust for that here
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004971 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004972 n_extra -= vcol_off;
4973#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004974 }
4975#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004976#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004977 {
4978 int vc_saved = vcol_off;
4979
4980 /* Tab alignment should be identical regardless of
4981 * 'conceallevel' value. So tab compensates of all
4982 * previous concealed characters, and thus resets
4983 * vcol_off and boguscols accumulated so far in the
4984 * line. Note that the tab can be longer than
4985 * 'tabstop' when there are concealed characters. */
4986 FIX_FOR_BOGUSCOLS;
4987
4988 /* Make sure, the highlighting for the tab char will be
4989 * correctly set further below (effectively reverts the
4990 * FIX_FOR_BOGSUCOLS macro */
4991 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004992 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004993 tab_len += vc_saved;
4994 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004995#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 if (wp->w_p_list)
4998 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004999 c = (n_extra == 0 && lcs_tab3) ? lcs_tab3 : lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005000#ifdef FEAT_LINEBREAK
5001 if (wp->w_p_lbr)
5002 c_extra = NUL; /* using p_extra from above */
5003 else
5004#endif
5005 c_extra = lcs_tab2;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005006 c_final = lcs_tab3;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005007 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005008 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 saved_attr2 = char_attr; /* save current attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005011 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 {
5013 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005014 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005015 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 }
5018 else
5019 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01005020 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 c_extra = ' ';
5022 c = ' ';
5023 }
5024 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005025 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02005026 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005027 || ((fromcol >= 0 || fromcol_prev >= 0)
5028 && tocol > vcol
5029 && VIsual_mode != Ctrl_V
5030 && (
5031# ifdef FEAT_RIGHTLEFT
5032 wp->w_p_rl ? (col >= 0) :
5033# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005034 (col < wp->w_width))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005035 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005036 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005037 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02005038 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005040 /* Display a '$' after the line or highlight an extra
5041 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042#if defined(FEAT_DIFF) || defined(LINE_ATTR)
5043 /* For a diff line the highlighting continues after the
5044 * "$". */
5045 if (
5046# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005047 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048# ifdef LINE_ATTR
5049 &&
5050# endif
5051# endif
5052# ifdef LINE_ATTR
5053 line_attr == 0
5054# endif
5055 )
5056#endif
5057 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 /* In virtualedit, visual selections may extend
5059 * beyond end of line. */
5060 if (area_highlighting && virtual_active()
5061 && tocol != MAXCOL && vcol < tocol)
5062 n_extra = 0;
5063 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 {
5065 p_extra = at_end_str;
5066 n_extra = 1;
5067 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005068 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 }
5070 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02005071 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005072 c = lcs_eol;
5073 else
5074 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 lcs_eol_one = -1;
5076 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005077 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005079 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 n_attr = 1;
5081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005083 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 {
5085 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005086 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005087 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 }
5089 else
5090 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 }
5092 else if (c != NUL)
5093 {
5094 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02005095 if (n_extra == 0)
5096 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097#ifdef FEAT_RIGHTLEFT
5098 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
5099 rl_mirror(p_extra); /* reverse "<12>" */
5100#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005102 c_final = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005103#ifdef FEAT_LINEBREAK
5104 if (wp->w_p_lbr)
5105 {
5106 char_u *p;
5107
5108 c = *p_extra;
Bram Moolenaar964b3742019-05-24 18:54:09 +02005109 p = alloc(n_extra + 1);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005110 vim_memset(p, ' ', n_extra);
5111 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5112 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005113 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005114 p_extra_free = p_extra = p;
5115 }
5116 else
5117#endif
5118 {
5119 n_extra = byte2cells(c) - 1;
5120 c = *p_extra++;
5121 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005122 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 {
5124 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005125 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 saved_attr2 = char_attr; /* save current attr */
5127 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 else if (VIsual_active
5131 && (VIsual_mode == Ctrl_V
5132 || VIsual_mode == 'v')
5133 && virtual_active()
5134 && tocol != MAXCOL
5135 && vcol < tocol
5136 && (
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005137#ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 wp->w_p_rl ? (col >= 0) :
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005139#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005140 (col < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 {
5142 c = ' ';
5143 --ptr; /* put it back at the NUL */
5144 }
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005145#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 else if ((
5147# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005148 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005150# ifdef FEAT_TERMINAL
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005151 win_attr != 0 ||
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005152# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 ) && (
5155# ifdef FEAT_RIGHTLEFT
5156 wp->w_p_rl ? (col >= 0) :
5157# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005158 (col
5159# ifdef FEAT_CONCEAL
5160 - boguscols
5161# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005162 < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 {
5164 /* Highlight until the right side of the window */
5165 c = ' ';
5166 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005167
5168 /* Remember we do the char for line highlighting. */
5169 ++did_line_attr;
5170
5171 /* don't do search HL for the rest of the line */
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005172 if (line_attr != 0 && char_attr == search_attr
5173 && (did_line_attr > 1
5174 || (wp->w_p_list && lcs_eol > 0)))
Bram Moolenaar91170f82006-05-05 21:15:17 +00005175 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176# ifdef FEAT_DIFF
5177 if (diff_hlf == HLF_TXD)
5178 {
5179 diff_hlf = HLF_CHD;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005180 if (vi_attr == 0 || char_attr != vi_attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005181 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005182 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005183 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5184 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005185 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005186 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 }
5188# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005189# ifdef FEAT_TERMINAL
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005190 if (win_attr != 0)
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005191 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005192 char_attr = win_attr;
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005193 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5194 char_attr = hl_combine_attr(char_attr,
5195 HL_ATTR(HLF_CUL));
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02005196 else if (line_attr)
5197 char_attr = hl_combine_attr(char_attr, line_attr);
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005198 }
5199# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 }
5201#endif
5202 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005203
5204#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005205 if ( wp->w_p_cole > 0
5206 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaarab62c192019-03-30 16:39:05 +01005207 conceal_cursor_line(wp))
5208 && ((syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005209 && !(lnum_in_visual_area
5210 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005211 {
5212 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005213 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005214 && (syn_get_sub_char() != NUL || match_conc
5215 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005216 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005217 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005218 /* First time at this concealed item: display one
5219 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005220 if (match_conc)
5221 c = match_conc;
5222 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005223 c = syn_get_sub_char();
5224 else if (lcs_conceal != NUL)
5225 c = lcs_conceal;
5226 else
5227 c = ' ';
5228
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005229 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005230
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005231 if (n_extra > 0)
5232 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005233 vcol += n_extra;
5234 if (wp->w_p_wrap && n_extra > 0)
5235 {
5236# ifdef FEAT_RIGHTLEFT
5237 if (wp->w_p_rl)
5238 {
5239 col -= n_extra;
5240 boguscols -= n_extra;
5241 }
5242 else
5243# endif
5244 {
5245 boguscols += n_extra;
5246 col += n_extra;
5247 }
5248 }
5249 n_extra = 0;
5250 n_attr = 0;
5251 }
5252 else if (n_skip == 0)
5253 {
5254 is_concealing = TRUE;
5255 n_skip = 1;
5256 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005257 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005258 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005259 {
5260 mb_utf8 = TRUE;
5261 u8cc[0] = 0;
5262 c = 0xc0;
5263 }
5264 else
5265 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005266 }
5267 else
5268 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005269 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005270 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005271 }
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02005272
5273 if (n_skip > 0 && did_decrement_ptr)
5274 // not showing the '>', put pointer back to avoid getting stuck
5275 ++ptr;
5276
5277#endif // FEAT_CONCEAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 }
5279
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005280#ifdef FEAT_CONCEAL
5281 /* In the cursor line and we may be concealing characters: correct
5282 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005283 if (!did_wcol && draw_state == WL_LINE
5284 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005285 && conceal_cursor_line(wp)
5286 && (int)wp->w_virtcol <= vcol + n_skip)
5287 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005288# ifdef FEAT_RIGHTLEFT
5289 if (wp->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02005290 wp->w_wcol = wp->w_width - col + boguscols - 1;
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005291 else
5292# endif
5293 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005294 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005295 did_wcol = TRUE;
5296 }
5297#endif
5298
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 /* Don't override visual selection highlighting. */
5300 if (n_attr > 0
5301 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005302 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 char_attr = extra_attr;
5304
Bram Moolenaar81695252004-12-29 20:58:21 +00005305#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 /* XIM don't send preedit_start and preedit_end, but they send
5307 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5308 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005309 if (p_imst == IM_ON_THE_SPOT
5310 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005311 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 && (State & INSERT)
5313 && !p_imdisable
5314 && im_is_preediting()
5315 && draw_state == WL_LINE)
5316 {
5317 colnr_T tcol;
5318
5319 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005320 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 else
5322 tcol = preedit_end_col;
5323 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5324 {
5325 if (feedback_old_attr < 0)
5326 {
5327 feedback_col = 0;
5328 feedback_old_attr = char_attr;
5329 }
5330 char_attr = im_get_feedback_attr(feedback_col);
5331 if (char_attr < 0)
5332 char_attr = feedback_old_attr;
5333 feedback_col++;
5334 }
5335 else if (feedback_old_attr >= 0)
5336 {
5337 char_attr = feedback_old_attr;
5338 feedback_old_attr = -1;
5339 feedback_col = 0;
5340 }
5341 }
5342#endif
5343 /*
5344 * Handle the case where we are in column 0 but not on the first
5345 * character of the line and the user wants us to show us a
5346 * special character (via 'listchars' option "precedes:<char>".
5347 */
5348 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005349 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5351#ifdef FEAT_DIFF
5352 && filler_todo <= 0
5353#endif
5354 && draw_state > WL_NR
5355 && c != NUL)
5356 {
5357 c = lcs_prec;
5358 lcs_prec_todo = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005359 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5360 {
5361 /* Double-width character being overwritten by the "precedes"
5362 * character, need to fill up half the character. */
5363 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005364 c_final = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005365 n_extra = 1;
5366 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005367 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005370 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 {
5372 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005373 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005374 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 }
5376 else
5377 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005378 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 {
5380 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005381 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 n_attr3 = 1;
5383 }
5384 }
5385
5386 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005387 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 */
Bram Moolenaarf914a332019-07-20 15:09:56 +02005389 if ((c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005390#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005391 || did_line_attr == 1
5392#endif
Bram Moolenaarf914a332019-07-20 15:09:56 +02005393 ) && eol_hl_off == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005395#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarbbca7732019-07-24 18:13:16 +02005396 // flag to indicate whether prevcol equals startcol of search_hl or
5397 // one of the matches
5398 int prevcol_hl_flag = get_prevcol_hl_flag(wp, &search_hl,
5399 (long)(ptr - line) - (c == NUL));
Bram Moolenaar91170f82006-05-05 21:15:17 +00005400#endif
Bram Moolenaarbbca7732019-07-24 18:13:16 +02005401 // Invert at least one char, used for Visual and empty line or
5402 // highlight match at end of line. If it's beyond the last
5403 // char on the screen, just overwrite that one (tricky!) Not
5404 // needed when a '$' was displayed for 'list'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005406 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005407 && (VIsual_mode != Ctrl_V
5408 || lnum == VIsual.lnum
5409 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005410 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarbbca7732019-07-24 18:13:16 +02005412 // highlight 'hlsearch' match at end of line
5413 || (prevcol_hl_flag
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005414# ifdef FEAT_SYN_HL
5415 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
5416 && !(wp == curwin && VIsual_active))
5417# endif
5418# ifdef FEAT_DIFF
5419 && diff_hlf == (hlf_T)0
5420# endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005421# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005422 && did_line_attr <= 1
5423# endif
5424 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425#endif
5426 ))
5427 {
5428 int n = 0;
5429
5430#ifdef FEAT_RIGHTLEFT
5431 if (wp->w_p_rl)
5432 {
5433 if (col < 0)
5434 n = 1;
5435 }
5436 else
5437#endif
5438 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005439 if (col >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 n = -1;
5441 }
5442 if (n != 0)
5443 {
5444 /* At the window boundary, highlight the last character
5445 * instead (better than nothing). */
5446 off += n;
5447 col += n;
5448 }
5449 else
5450 {
5451 /* Add a blank character to highlight. */
5452 ScreenLines[off] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 if (enc_utf8)
5454 ScreenLinesUC[off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 }
5456#ifdef FEAT_SEARCH_EXTRA
5457 if (area_attr == 0)
5458 {
Bram Moolenaarbbca7732019-07-24 18:13:16 +02005459 // Use attributes from match with highest priority among
5460 // 'search_hl' and the match list.
5461 get_search_match_hl(wp, &search_hl,
5462 (long)(ptr - line), &char_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 }
5464#endif
5465 ScreenAttrs[off] = char_attr;
5466#ifdef FEAT_RIGHTLEFT
5467 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005468 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005470 --off;
5471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 else
5473#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005474 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005476 ++off;
5477 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005478 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005479 eol_hl_off = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005481 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482
Bram Moolenaar91170f82006-05-05 21:15:17 +00005483 /*
5484 * At end of the text line.
5485 */
5486 if (c == NUL)
5487 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005488#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005489 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005490 if (wp->w_p_wrap)
5491 v = wp->w_skipcol;
5492 else
5493 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005494
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005495 /* check if line ends before left margin */
5496 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005497 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005498#ifdef FEAT_CONCEAL
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01005499 // Get rid of the boguscols now, we want to draw until the right
5500 // edge for 'cursorcolumn'.
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005501 col -= boguscols;
5502 boguscols = 0;
5503#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005504
5505 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005506 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005507
5508 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005509 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5510 && (int)wp->w_virtcol <
Bram Moolenaar02631462017-09-22 15:20:32 +02005511 wp->w_width * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005512 && lnum != wp->w_cursor.lnum)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005513 || draw_color_col
5514 || win_attr != 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005515# ifdef FEAT_RIGHTLEFT
5516 && !wp->w_p_rl
5517# endif
5518 )
5519 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005520 int rightmost_vcol = 0;
5521 int i;
5522
5523 if (wp->w_p_cuc)
5524 rightmost_vcol = wp->w_virtcol;
5525 if (draw_color_col)
5526 /* determine rightmost colorcolumn to possibly draw */
5527 for (i = 0; color_cols[i] >= 0; ++i)
5528 if (rightmost_vcol < color_cols[i])
5529 rightmost_vcol = color_cols[i];
5530
Bram Moolenaar02631462017-09-22 15:20:32 +02005531 while (col < wp->w_width)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005532 {
5533 ScreenLines[off] = ' ';
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005534 if (enc_utf8)
5535 ScreenLinesUC[off] = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005536 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005537 if (draw_color_col)
5538 draw_color_col = advance_color_col(VCOL_HLC,
5539 &color_cols);
5540
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005541 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005542 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005543 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005544 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005545 else
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005546 ScreenAttrs[off++] = win_attr;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005547
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005548 if (VCOL_HLC >= rightmost_vcol && win_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005549 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005550
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005551 ++vcol;
5552 }
5553 }
5554#endif
5555
Bram Moolenaar53f81742017-09-22 14:35:51 +02005556 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005557 (int)wp->w_width, screen_line_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 row++;
5559
5560 /*
5561 * Update w_cline_height and w_cline_folded if the cursor line was
5562 * updated (saves a call to plines() later).
5563 */
5564 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5565 {
5566 curwin->w_cline_row = startrow;
5567 curwin->w_cline_height = row - startrow;
5568#ifdef FEAT_FOLDING
5569 curwin->w_cline_folded = FALSE;
5570#endif
5571 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5572 }
5573
5574 break;
5575 }
5576
Bram Moolenaara5c6a0b2019-05-08 20:20:46 +02005577 // Show "extends" character from 'listchars' if beyond the line end and
5578 // 'list' is set.
5579 if (lcs_ext != NUL
5580 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 && !wp->w_p_wrap
5582#ifdef FEAT_DIFF
5583 && filler_todo <= 0
5584#endif
5585 && (
5586#ifdef FEAT_RIGHTLEFT
5587 wp->w_p_rl ? col == 0 :
5588#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005589 col == wp->w_width - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005591 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5593 {
5594 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005595 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005597 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 {
5599 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005600 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005601 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 }
5603 else
5604 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 }
5606
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005607#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005608 /* advance to the next 'colorcolumn' */
5609 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005610 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005611
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005612 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005613 * highlight the cursor position itself.
5614 * Also highlight the 'colorcolumn' if it is different than
5615 * 'cursorcolumn' */
5616 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005617 if (draw_state == WL_LINE && !lnum_in_visual_area
5618 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005619 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005620 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005621 && lnum != wp->w_cursor.lnum)
5622 {
5623 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005624 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005625 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005626 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005627 {
5628 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005629 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005630 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005631 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005632#endif
5633
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634 /*
5635 * Store character to be displayed.
5636 * Skip characters that are left of the screen for 'nowrap'.
5637 */
5638 vcol_prev = vcol;
5639 if (draw_state < WL_LINE || n_skip <= 0)
5640 {
5641 /*
5642 * Store the character.
5643 */
Bram Moolenaara12a1612019-01-24 16:39:02 +01005644#if defined(FEAT_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5646 {
5647 /* A double-wide character is: put first halve in left cell. */
5648 --off;
5649 --col;
5650 }
5651#endif
5652 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005654 {
5655 if ((mb_c & 0xff00) == 0x8e00)
5656 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 else if (enc_utf8)
5660 {
5661 if (mb_utf8)
5662 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005663 int i;
5664
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005666 if ((c & 0xff) == 0)
5667 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005668 for (i = 0; i < Screen_mco; ++i)
5669 {
5670 ScreenLinesC[i][off] = u8cc[i];
5671 if (u8cc[i] == 0)
5672 break;
5673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 }
5675 else
5676 ScreenLinesUC[off] = 0;
5677 }
5678 if (multi_attr)
5679 {
5680 ScreenAttrs[off] = multi_attr;
5681 multi_attr = 0;
5682 }
5683 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 ScreenAttrs[off] = char_attr;
5685
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5687 {
5688 /* Need to fill two screen columns. */
5689 ++off;
5690 ++col;
5691 if (enc_utf8)
5692 /* UTF-8: Put a 0 in the second screen char. */
5693 ScreenLines[off] = 0;
5694 else
5695 /* DBCS: Put second byte in the second screen char. */
5696 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005697 if (draw_state > WL_NR
5698#ifdef FEAT_DIFF
5699 && filler_todo <= 0
5700#endif
5701 )
5702 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 /* When "tocol" is halfway a character, set it to the end of
5704 * the character, otherwise highlighting won't stop. */
5705 if (tocol == vcol)
5706 ++tocol;
5707#ifdef FEAT_RIGHTLEFT
5708 if (wp->w_p_rl)
5709 {
5710 /* now it's time to backup one cell */
5711 --off;
5712 --col;
5713 }
5714#endif
5715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716#ifdef FEAT_RIGHTLEFT
5717 if (wp->w_p_rl)
5718 {
5719 --off;
5720 --col;
5721 }
5722 else
5723#endif
5724 {
5725 ++off;
5726 ++col;
5727 }
5728 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005729#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005730 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005731 {
5732 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005733 ++vcol_off;
5734 if (n_extra > 0)
5735 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005736 if (wp->w_p_wrap)
5737 {
5738 /*
5739 * Special voodoo required if 'wrap' is on.
5740 *
5741 * Advance the column indicator to force the line
5742 * drawing to wrap early. This will make the line
5743 * take up the same screen space when parts are concealed,
5744 * so that cursor line computations aren't messed up.
5745 *
5746 * To avoid the fictitious advance of 'col' causing
5747 * trailing junk to be written out of the screen line
5748 * we are building, 'boguscols' keeps track of the number
5749 * of bad columns we have advanced.
5750 */
5751 if (n_extra > 0)
5752 {
5753 vcol += n_extra;
5754# ifdef FEAT_RIGHTLEFT
5755 if (wp->w_p_rl)
5756 {
5757 col -= n_extra;
5758 boguscols -= n_extra;
5759 }
5760 else
5761# endif
5762 {
5763 col += n_extra;
5764 boguscols += n_extra;
5765 }
5766 n_extra = 0;
5767 n_attr = 0;
5768 }
5769
5770
Bram Moolenaar860cae12010-06-05 23:22:07 +02005771 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5772 {
5773 /* Need to fill two screen columns. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01005774# ifdef FEAT_RIGHTLEFT
Bram Moolenaar860cae12010-06-05 23:22:07 +02005775 if (wp->w_p_rl)
5776 {
5777 --boguscols;
5778 --col;
5779 }
5780 else
Bram Moolenaara12a1612019-01-24 16:39:02 +01005781# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02005782 {
5783 ++boguscols;
5784 ++col;
5785 }
5786 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005787
5788# ifdef FEAT_RIGHTLEFT
5789 if (wp->w_p_rl)
5790 {
5791 --boguscols;
5792 --col;
5793 }
5794 else
5795# endif
5796 {
5797 ++boguscols;
5798 ++col;
5799 }
5800 }
5801 else
5802 {
5803 if (n_extra > 0)
5804 {
5805 vcol += n_extra;
5806 n_extra = 0;
5807 n_attr = 0;
5808 }
5809 }
5810
5811 }
5812#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813 else
5814 --n_skip;
5815
Bram Moolenaar64486672010-05-16 15:46:46 +02005816 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5817 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005818 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819#ifdef FEAT_DIFF
5820 && filler_todo <= 0
5821#endif
5822 )
5823 ++vcol;
5824
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005825#ifdef FEAT_SYN_HL
5826 if (vcol_save_attr >= 0)
5827 char_attr = vcol_save_attr;
5828#endif
5829
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 /* restore attributes after "predeces" in 'listchars' */
5831 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5832 char_attr = saved_attr3;
5833
5834 /* restore attributes after last 'listchars' or 'number' char */
5835 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5836 char_attr = saved_attr2;
5837
5838 /*
5839 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005840 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 */
5842 if ((
5843#ifdef FEAT_RIGHTLEFT
5844 wp->w_p_rl ? (col < 0) :
5845#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005846 (col >= wp->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847 && (*ptr != NUL
5848#ifdef FEAT_DIFF
5849 || filler_todo > 0
5850#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005851 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5853 )
5854 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005855#ifdef FEAT_CONCEAL
Bram Moolenaar53f81742017-09-22 14:35:51 +02005856 screen_line(screen_row, wp->w_wincol, col - boguscols,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005857 (int)wp->w_width, screen_line_flags);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005858 boguscols = 0;
5859#else
Bram Moolenaar53f81742017-09-22 14:35:51 +02005860 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005861 (int)wp->w_width, screen_line_flags);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005862#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 ++row;
5864 ++screen_row;
5865
5866 /* When not wrapping and finished diff lines, or when displayed
5867 * '$' and highlighting until last column, break here. */
5868 if ((!wp->w_p_wrap
5869#ifdef FEAT_DIFF
5870 && filler_todo <= 0
5871#endif
5872 ) || lcs_eol_one == -1)
5873 break;
5874
5875 /* When the window is too narrow draw all "@" lines. */
5876 if (draw_state != WL_LINE
5877#ifdef FEAT_DIFF
5878 && filler_todo <= 0
5879#endif
5880 )
5881 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01005882 win_draw_end(wp, '@', ' ', TRUE, row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884 row = endrow;
5885 }
5886
5887 /* When line got too long for screen break here. */
5888 if (row == endrow)
5889 {
5890 ++row;
5891 break;
5892 }
5893
5894 if (screen_cur_row == screen_row - 1
5895#ifdef FEAT_DIFF
5896 && filler_todo <= 0
5897#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005898 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 {
5900 /* Remember that the line wraps, used for modeless copy. */
5901 LineWraps[screen_row - 1] = TRUE;
5902
5903 /*
5904 * Special trick to make copy/paste of wrapped lines work with
5905 * xterm/screen: write an extra character beyond the end of
5906 * the line. This will work with all terminal types
5907 * (regardless of the xn,am settings).
5908 * Only do this on a fast tty.
5909 * Only do this if the cursor is on the current line
5910 * (something has been written in it).
5911 * Don't do this for the GUI.
5912 * Don't do this for double-width characters.
5913 * Don't do this for a window not at the right screen border.
5914 */
5915 if (p_tf
5916#ifdef FEAT_GUI
5917 && !gui.in_use
5918#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005920 && ((*mb_off2cells)(LineOffset[screen_row],
5921 LineOffset[screen_row] + screen_Columns)
5922 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005924 + (int)Columns - 2,
5925 LineOffset[screen_row] + screen_Columns)
Bram Moolenaara12a1612019-01-24 16:39:02 +01005926 == 2)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927 {
5928 /* First make sure we are at the end of the screen line,
5929 * then output the same character again to let the
5930 * terminal know about the wrap. If the terminal doesn't
5931 * auto-wrap, we overwrite the character. */
Bram Moolenaar02631462017-09-22 15:20:32 +02005932 if (screen_cur_col != wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 screen_char(LineOffset[screen_row - 1]
5934 + (unsigned)Columns - 1,
5935 screen_row - 1, (int)(Columns - 1));
5936
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 /* When there is a multi-byte character, just output a
5938 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005939 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5940 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 out_char(' ');
5942 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943 out_char(ScreenLines[LineOffset[screen_row - 1]
5944 + (Columns - 1)]);
5945 /* force a redraw of the first char on the next line */
5946 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5947 screen_start(); /* don't know where cursor is now */
5948 }
5949 }
5950
5951 col = 0;
5952 off = (unsigned)(current_ScreenLine - ScreenLines);
5953#ifdef FEAT_RIGHTLEFT
5954 if (wp->w_p_rl)
5955 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005956 col = wp->w_width - 1; /* col is not used if breaking! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 off += col;
5958 }
5959#endif
5960
5961 /* reset the drawing state for the start of a wrapped line */
5962 draw_state = WL_START;
5963 saved_n_extra = n_extra;
5964 saved_p_extra = p_extra;
5965 saved_c_extra = c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005966 saved_c_final = c_final;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 saved_char_attr = char_attr;
5968 n_extra = 0;
5969 lcs_prec_todo = lcs_prec;
5970#ifdef FEAT_LINEBREAK
5971# ifdef FEAT_DIFF
5972 if (filler_todo <= 0)
5973# endif
5974 need_showbreak = TRUE;
5975#endif
5976#ifdef FEAT_DIFF
5977 --filler_todo;
5978 /* When the filler lines are actually below the last line of the
5979 * file, don't draw the line itself, break here. */
5980 if (filler_todo == 0 && wp->w_botfill)
5981 break;
5982#endif
5983 }
5984
5985 } /* for every character in the line */
5986
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005987#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005988 /* After an empty line check first word for capital. */
5989 if (*skipwhite(line) == NUL)
5990 {
5991 capcol_lnum = lnum + 1;
5992 cap_col = 0;
5993 }
5994#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01005995#ifdef FEAT_TEXT_PROP
5996 vim_free(text_props);
5997 vim_free(text_prop_idxs);
5998#endif
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005999
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01006000 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 return row;
6002}
6003
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006004/*
6005 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006006 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006007 */
6008 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006009comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006010{
6011 int i;
6012
6013 for (i = 0; i < Screen_mco; ++i)
6014 {
6015 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
6016 return TRUE;
6017 if (ScreenLinesC[i][off_from] == 0)
6018 break;
6019 }
6020 return FALSE;
6021}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006022
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023/*
6024 * Check whether the given character needs redrawing:
6025 * - the (first byte of the) character is different
6026 * - the attributes are different
6027 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006028 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029 */
6030 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006031char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032{
6033 if (cols > 0
6034 && ((ScreenLines[off_from] != ScreenLines[off_to]
6035 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036 || (enc_dbcs != 0
6037 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
6038 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
6039 ? ScreenLines2[off_from] != ScreenLines2[off_to]
6040 : (cols > 1 && ScreenLines[off_from + 1]
6041 != ScreenLines[off_to + 1])))
6042 || (enc_utf8
6043 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6044 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006045 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006046 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6047 && ScreenLines[off_from + 1]
Bram Moolenaara12a1612019-01-24 16:39:02 +01006048 != ScreenLines[off_to + 1])))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049 return TRUE;
6050 return FALSE;
6051}
6052
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006053#if defined(FEAT_TERMINAL) || defined(PROTO)
6054/*
6055 * Return the index in ScreenLines[] for the current screen line.
6056 */
6057 int
6058screen_get_current_line_off()
6059{
6060 return (int)(current_ScreenLine - ScreenLines);
6061}
6062#endif
6063
Bram Moolenaarc662ec92019-06-23 00:15:57 +02006064#ifdef FEAT_TEXT_PROP
6065/*
6066 * Return TRUE if this position has a higher level popup or this cell is
6067 * transparent in the current popup.
6068 */
6069 static int
6070blocked_by_popup(int row, int col)
6071{
6072 int off;
6073
6074 if (!popup_visible)
6075 return FALSE;
6076 off = row * screen_Columns + col;
6077 return popup_mask[off] > screen_zindex || popup_transparent[off];
6078}
6079#endif
6080
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081/*
6082 * Move one "cooked" screen line to the screen, but only the characters that
6083 * have actually changed. Handle insert/delete character.
6084 * "coloff" gives the first column on the screen for this line.
6085 * "endcol" gives the columns where valid characters are.
6086 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6087 * needs to be cleared, negative otherwise.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006088 * "flags" can have bits:
6089 * SLF_POPUP popup window
6090 * SLF_RIGHTLEFT rightleft window:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6092 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6093 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006094 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006095screen_line(
6096 int row,
6097 int coloff,
6098 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006099 int clear_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006100 int flags UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101{
6102 unsigned off_from;
6103 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006104 unsigned max_off_from;
6105 unsigned max_off_to;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 int force = FALSE; /* force update rest of the line */
6109 int redraw_this /* bool: does character need redraw? */
6110#ifdef FEAT_GUI
6111 = TRUE /* For GUI when while-loop empty */
6112#endif
6113 ;
6114 int redraw_next; /* redraw_this for next character */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 int clear_next = FALSE;
6116 int char_cells; /* 1: normal char */
6117 /* 2: occupies two display cells */
6118# define CHAR_CELLS char_cells
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006120 /* Check for illegal row and col, just in case. */
6121 if (row >= Rows)
6122 row = Rows - 1;
6123 if (endcol > Columns)
6124 endcol = Columns;
6125
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126# ifdef FEAT_CLIPBOARD
6127 clip_may_clear_selection(row, row);
6128# endif
6129
6130 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6131 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006132 max_off_from = off_from + screen_Columns;
6133 max_off_to = LineOffset[row] + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134
6135#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006136 if (flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137 {
6138 /* Clear rest first, because it's left of the text. */
6139 if (clear_width > 0)
6140 {
6141 while (col <= endcol && ScreenLines[off_to] == ' '
6142 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006143 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 {
6145 ++off_to;
6146 ++col;
6147 }
6148 if (col <= endcol)
6149 screen_fill(row, row + 1, col + coloff,
6150 endcol + coloff + 1, ' ', ' ', 0);
6151 }
6152 col = endcol + 1;
6153 off_to = LineOffset[row] + col + coloff;
6154 off_from += col;
6155 endcol = (clear_width > 0 ? clear_width : -clear_width);
6156 }
6157#endif /* FEAT_RIGHTLEFT */
6158
6159 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6160
6161 while (col < endcol)
6162 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006164 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 else
6166 char_cells = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167
6168 redraw_this = redraw_next;
6169 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6170 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6171
6172#ifdef FEAT_GUI
6173 /* If the next character was bold, then redraw the current character to
6174 * remove any pixels that might have spilt over into us. This only
6175 * happens in the GUI.
6176 */
6177 if (redraw_next && gui.in_use)
6178 {
6179 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006180 if (hl > HL_ALL)
6181 hl = syn_attr2attr(hl);
6182 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 redraw_this = TRUE;
6184 }
6185#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02006186#ifdef FEAT_TEXT_PROP
Bram Moolenaarc662ec92019-06-23 00:15:57 +02006187 if (blocked_by_popup(row, col + coloff))
Bram Moolenaar33796b32019-06-08 16:01:13 +02006188 redraw_this = FALSE;
6189#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190 if (redraw_this)
6191 {
6192 /*
6193 * Special handling when 'xs' termcap flag set (hpterm):
6194 * Attributes for characters are stored at the position where the
6195 * cursor is when writing the highlighting code. The
6196 * start-highlighting code must be written with the cursor on the
6197 * first highlighted character. The stop-highlighting code must
6198 * be written with the cursor just after the last highlighted
6199 * character.
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01006200 * Overwriting a character doesn't remove its highlighting. Need
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201 * to clear the rest of the line, and force redrawing it
6202 * completely.
6203 */
6204 if ( p_wiv
6205 && !force
6206#ifdef FEAT_GUI
6207 && !gui.in_use
6208#endif
6209 && ScreenAttrs[off_to] != 0
6210 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6211 {
6212 /*
6213 * Need to remove highlighting attributes here.
6214 */
6215 windgoto(row, col + coloff);
6216 out_str(T_CE); /* clear rest of this screen line */
6217 screen_start(); /* don't know where cursor is now */
6218 force = TRUE; /* force redraw of rest of the line */
6219 redraw_next = TRUE; /* or else next char would miss out */
6220
6221 /*
6222 * If the previous character was highlighted, need to stop
6223 * highlighting at this character.
6224 */
6225 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6226 {
6227 screen_attr = ScreenAttrs[off_to - 1];
6228 term_windgoto(row, col + coloff);
6229 screen_stop_highlight();
6230 }
6231 else
6232 screen_attr = 0; /* highlighting has stopped */
6233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 if (enc_dbcs != 0)
6235 {
6236 /* Check if overwriting a double-byte with a single-byte or
6237 * the other way around requires another character to be
6238 * redrawn. For UTF-8 this isn't needed, because comparing
6239 * ScreenLinesUC[] is sufficient. */
6240 if (char_cells == 1
6241 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006242 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 {
6244 /* Writing a single-cell character over a double-cell
6245 * character: need to redraw the next cell. */
6246 ScreenLines[off_to + 1] = 0;
6247 redraw_next = TRUE;
6248 }
6249 else if (char_cells == 2
6250 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006251 && (*mb_off2cells)(off_to, max_off_to) == 1
6252 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253 {
6254 /* Writing the second half of a double-cell character over
6255 * a double-cell character: need to redraw the second
6256 * cell. */
6257 ScreenLines[off_to + 2] = 0;
6258 redraw_next = TRUE;
6259 }
6260
6261 if (enc_dbcs == DBCS_JPNU)
6262 ScreenLines2[off_to] = ScreenLines2[off_from];
6263 }
6264 /* When writing a single-width character over a double-width
6265 * character and at the end of the redrawn text, need to clear out
6266 * the right halve of the old character.
6267 * Also required when writing the right halve of a double-width
6268 * char over the left halve of an existing one. */
6269 if (has_mbyte && col + char_cells == endcol
6270 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006271 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006273 && (*mb_off2cells)(off_to, max_off_to) == 1
6274 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275 clear_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276
6277 ScreenLines[off_to] = ScreenLines[off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 if (enc_utf8)
6279 {
6280 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6281 if (ScreenLinesUC[off_from] != 0)
6282 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006283 int i;
6284
6285 for (i = 0; i < Screen_mco; ++i)
6286 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287 }
6288 }
6289 if (char_cells == 2)
6290 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291
6292#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006293 /* The bold trick makes a single column of pixels appear in the
6294 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295 * character should be redrawn too. This happens for our own GUI
6296 * and for some xterms. */
6297 if (
6298# ifdef FEAT_GUI
6299 gui.in_use
6300# endif
6301# if defined(FEAT_GUI) && defined(UNIX)
6302 ||
6303# endif
6304# ifdef UNIX
6305 term_is_xterm
6306# endif
6307 )
6308 {
6309 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006310 if (hl > HL_ALL)
6311 hl = syn_attr2attr(hl);
6312 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313 redraw_next = TRUE;
6314 }
6315#endif
6316 ScreenAttrs[off_to] = ScreenAttrs[off_from];
Bram Moolenaara12a1612019-01-24 16:39:02 +01006317
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006318 /* For simplicity set the attributes of second half of a
6319 * double-wide character equal to the first half. */
6320 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006322
6323 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326 screen_char(off_to, row, col + coloff);
6327 }
6328 else if ( p_wiv
6329#ifdef FEAT_GUI
6330 && !gui.in_use
6331#endif
6332 && col + coloff > 0)
6333 {
6334 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6335 {
6336 /*
6337 * Don't output stop-highlight when moving the cursor, it will
6338 * stop the highlighting when it should continue.
6339 */
6340 screen_attr = 0;
6341 }
6342 else if (screen_attr != 0)
6343 screen_stop_highlight();
6344 }
6345
6346 off_to += CHAR_CELLS;
6347 off_from += CHAR_CELLS;
6348 col += CHAR_CELLS;
6349 }
6350
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351 if (clear_next)
6352 {
6353 /* Clear the second half of a double-wide character of which the left
6354 * half was overwritten with a single-wide character. */
6355 ScreenLines[off_to] = ' ';
6356 if (enc_utf8)
6357 ScreenLinesUC[off_to] = 0;
6358 screen_char(off_to, row, col + coloff);
6359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360
6361 if (clear_width > 0
6362#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006363 && !(flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364#endif
6365 )
6366 {
6367#ifdef FEAT_GUI
6368 int startCol = col;
6369#endif
6370
6371 /* blank out the rest of the line */
6372 while (col < clear_width && ScreenLines[off_to] == ' '
6373 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006374 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 {
6376 ++off_to;
6377 ++col;
6378 }
6379 if (col < clear_width)
6380 {
6381#ifdef FEAT_GUI
6382 /*
6383 * In the GUI, clearing the rest of the line may leave pixels
6384 * behind if the first character cleared was bold. Some bold
6385 * fonts spill over the left. In this case we redraw the previous
6386 * character too. If we didn't skip any blanks above, then we
6387 * only redraw if the character wasn't already redrawn anyway.
6388 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006389 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 {
6391 hl = ScreenAttrs[off_to];
6392 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006393 {
6394 int prev_cells = 1;
Bram Moolenaara12a1612019-01-24 16:39:02 +01006395
Bram Moolenaar9c697322006-10-09 20:11:17 +00006396 if (enc_utf8)
6397 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6398 * that its width is 2. */
6399 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6400 else if (enc_dbcs != 0)
6401 {
6402 /* find previous character by counting from first
6403 * column and get its width. */
6404 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006405 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006406
6407 while (off < off_to)
6408 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006409 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006410 off += prev_cells;
6411 }
6412 }
6413
6414 if (enc_dbcs != 0 && prev_cells > 1)
6415 screen_char_2(off_to - prev_cells, row,
6416 col + coloff - prev_cells);
6417 else
Bram Moolenaar9c697322006-10-09 20:11:17 +00006418 screen_char(off_to - prev_cells, row,
6419 col + coloff - prev_cells);
6420 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421 }
6422#endif
6423 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6424 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 off_to += clear_width - col;
6426 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427 }
6428 }
6429
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006430 if (clear_width > 0
6431#ifdef FEAT_TEXT_PROP
6432 && !(flags & SLF_POPUP) // no separator for popup window
6433#endif
6434 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006436 // For a window that has a right neighbor, draw the separator char
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006437 // right of the window contents. But not on top of a popup window.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006438 if (coloff + col < Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006440#ifdef FEAT_TEXT_PROP
Bram Moolenaarc662ec92019-06-23 00:15:57 +02006441 if (!blocked_by_popup(row, col + coloff))
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006442#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006444 int c;
6445
6446 c = fillchar_vsep(&hl);
6447 if (ScreenLines[off_to] != (schar_T)c
6448 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6449 != (c >= 0x80 ? c : 0))
6450 || ScreenAttrs[off_to] != hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006452 ScreenLines[off_to] = c;
6453 ScreenAttrs[off_to] = hl;
6454 if (enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006456 if (c >= 0x80)
6457 {
6458 ScreenLinesUC[off_to] = c;
6459 ScreenLinesC[0][off_to] = 0;
6460 }
6461 else
6462 ScreenLinesUC[off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 }
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006464 screen_char(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 }
6467 }
6468 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469 LineWraps[row] = FALSE;
6470 }
6471}
6472
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006473#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006475 * Mirror text "str" for right-left displaying.
6476 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006478 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006479rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480{
6481 char_u *p1, *p2;
6482 int t;
6483
6484 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6485 {
6486 t = *p1;
6487 *p1 = *p2;
6488 *p2 = t;
6489 }
6490}
6491#endif
6492
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493/*
6494 * mark all status lines for redraw; used after first :cd
6495 */
6496 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006497status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498{
6499 win_T *wp;
6500
Bram Moolenaar29323592016-07-24 22:04:11 +02006501 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502 if (wp->w_status_height)
6503 {
6504 wp->w_redr_status = TRUE;
6505 redraw_later(VALID);
6506 }
6507}
6508
6509/*
6510 * mark all status lines of the current buffer for redraw
6511 */
6512 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006513status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514{
6515 win_T *wp;
6516
Bram Moolenaar29323592016-07-24 22:04:11 +02006517 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6519 {
6520 wp->w_redr_status = TRUE;
6521 redraw_later(VALID);
6522 }
6523}
6524
6525/*
6526 * Redraw all status lines that need to be redrawn.
6527 */
6528 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006529redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530{
6531 win_T *wp;
6532
Bram Moolenaar29323592016-07-24 22:04:11 +02006533 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006535 win_redr_status(wp, FALSE);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006536 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006537 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539
Bram Moolenaar4033c552017-09-16 20:54:51 +02006540#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541/*
6542 * Redraw all status lines at the bottom of frame "frp".
6543 */
6544 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006545win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546{
6547 if (frp->fr_layout == FR_LEAF)
6548 frp->fr_win->w_redr_status = TRUE;
6549 else if (frp->fr_layout == FR_ROW)
6550 {
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006551 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 win_redraw_last_status(frp);
6553 }
6554 else /* frp->fr_layout == FR_COL */
6555 {
6556 frp = frp->fr_child;
6557 while (frp->fr_next != NULL)
6558 frp = frp->fr_next;
6559 win_redraw_last_status(frp);
6560 }
6561}
6562#endif
6563
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564/*
6565 * Draw the verticap separator right of window "wp" starting with line "row".
6566 */
6567 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006568draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569{
6570 int hl;
6571 int c;
6572
6573 if (wp->w_vsep_width)
6574 {
6575 /* draw the vertical separator right of this window */
6576 c = fillchar_vsep(&hl);
6577 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6578 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6579 c, ' ', hl);
6580 }
6581}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006582
6583#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006584static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585
6586/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006587 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588 */
6589 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006590status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591{
6592 int len = 0;
6593
6594#ifdef FEAT_MENU
6595 int emenu = (xp->xp_context == EXPAND_MENUS
6596 || xp->xp_context == EXPAND_MENUNAMES);
6597
6598 /* Check for menu separators - replace with '|'. */
6599 if (emenu && menu_is_separator(s))
6600 return 1;
6601#endif
6602
6603 while (*s != NUL)
6604 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006605 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006606 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006607 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608 }
6609
6610 return len;
6611}
6612
6613/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006614 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006615 * These are backslashes used for escaping. Do show backslashes in help tags.
6616 */
6617 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006618skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006619{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006620 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006621#ifdef FEAT_MENU
6622 || ((xp->xp_context == EXPAND_MENUS
6623 || xp->xp_context == EXPAND_MENUNAMES)
6624 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6625#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006626 )
6627 {
6628#ifndef BACKSLASH_IN_FILENAME
6629 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6630 return 2;
6631#endif
6632 return 1;
6633 }
6634 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006635}
6636
6637/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638 * Show wildchar matches in the status line.
6639 * Show at least the "match" item.
6640 * We start at item 'first_match' in the list and show all matches that fit.
6641 *
6642 * If inversion is possible we use it. Else '=' characters are used.
6643 */
6644 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006645win_redr_status_matches(
6646 expand_T *xp,
6647 int num_matches,
6648 char_u **matches, /* list of matches */
6649 int match,
6650 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651{
6652#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6653 int row;
6654 char_u *buf;
6655 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006656 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657 int fillchar;
6658 int attr;
6659 int i;
6660 int highlight = TRUE;
6661 char_u *selstart = NULL;
6662 int selstart_col = 0;
6663 char_u *selend = NULL;
6664 static int first_match = 0;
6665 int add_left = FALSE;
6666 char_u *s;
6667#ifdef FEAT_MENU
6668 int emenu;
6669#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671
6672 if (matches == NULL) /* interrupted completion? */
6673 return;
6674
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006675 if (has_mbyte)
Bram Moolenaar964b3742019-05-24 18:54:09 +02006676 buf = alloc(Columns * MB_MAXBYTES + 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006677 else
Bram Moolenaar964b3742019-05-24 18:54:09 +02006678 buf = alloc(Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679 if (buf == NULL)
6680 return;
6681
6682 if (match == -1) /* don't show match but original text */
6683 {
6684 match = 0;
6685 highlight = FALSE;
6686 }
6687 /* count 1 for the ending ">" */
6688 clen = status_match_len(xp, L_MATCH(match)) + 3;
6689 if (match == 0)
6690 first_match = 0;
6691 else if (match < first_match)
6692 {
6693 /* jumping left, as far as we can go */
6694 first_match = match;
6695 add_left = TRUE;
6696 }
6697 else
6698 {
6699 /* check if match fits on the screen */
6700 for (i = first_match; i < match; ++i)
6701 clen += status_match_len(xp, L_MATCH(i)) + 2;
6702 if (first_match > 0)
6703 clen += 2;
6704 /* jumping right, put match at the left */
6705 if ((long)clen > Columns)
6706 {
6707 first_match = match;
6708 /* if showing the last match, we can add some on the left */
6709 clen = 2;
6710 for (i = match; i < num_matches; ++i)
6711 {
6712 clen += status_match_len(xp, L_MATCH(i)) + 2;
6713 if ((long)clen >= Columns)
6714 break;
6715 }
6716 if (i == num_matches)
6717 add_left = TRUE;
6718 }
6719 }
6720 if (add_left)
6721 while (first_match > 0)
6722 {
6723 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6724 if ((long)clen >= Columns)
6725 break;
6726 --first_match;
6727 }
6728
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006729 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730
6731 if (first_match == 0)
6732 {
6733 *buf = NUL;
6734 len = 0;
6735 }
6736 else
6737 {
6738 STRCPY(buf, "< ");
6739 len = 2;
6740 }
6741 clen = len;
6742
6743 i = first_match;
6744 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6745 {
6746 if (i == match)
6747 {
6748 selstart = buf + len;
6749 selstart_col = clen;
6750 }
6751
6752 s = L_MATCH(i);
6753 /* Check for menu separators - replace with '|' */
6754#ifdef FEAT_MENU
6755 emenu = (xp->xp_context == EXPAND_MENUS
6756 || xp->xp_context == EXPAND_MENUNAMES);
6757 if (emenu && menu_is_separator(s))
6758 {
6759 STRCPY(buf + len, transchar('|'));
6760 l = (int)STRLEN(buf + len);
6761 len += l;
6762 clen += l;
6763 }
6764 else
6765#endif
6766 for ( ; *s != NUL; ++s)
6767 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006768 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769 clen += ptr2cells(s);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006770 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771 {
6772 STRNCPY(buf + len, s, l);
6773 s += l - 1;
6774 len += l;
6775 }
6776 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777 {
6778 STRCPY(buf + len, transchar_byte(*s));
6779 len += (int)STRLEN(buf + len);
6780 }
6781 }
6782 if (i == match)
6783 selend = buf + len;
6784
6785 *(buf + len++) = ' ';
6786 *(buf + len++) = ' ';
6787 clen += 2;
6788 if (++i == num_matches)
6789 break;
6790 }
6791
6792 if (i != num_matches)
6793 {
6794 *(buf + len++) = '>';
6795 ++clen;
6796 }
6797
6798 buf[len] = NUL;
6799
6800 row = cmdline_row - 1;
6801 if (row >= 0)
6802 {
6803 if (wild_menu_showing == 0)
6804 {
6805 if (msg_scrolled > 0)
6806 {
6807 /* Put the wildmenu just above the command line. If there is
6808 * no room, scroll the screen one line up. */
6809 if (cmdline_row == Rows - 1)
6810 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02006811 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 ++msg_scrolled;
6813 }
6814 else
6815 {
6816 ++cmdline_row;
6817 ++row;
6818 }
6819 wild_menu_showing = WM_SCROLLED;
6820 }
6821 else
6822 {
6823 /* Create status line if needed by setting 'laststatus' to 2.
6824 * Set 'winminheight' to zero to avoid that the window is
6825 * resized. */
6826 if (lastwin->w_status_height == 0)
6827 {
6828 save_p_ls = p_ls;
6829 save_p_wmh = p_wmh;
6830 p_ls = 2;
6831 p_wmh = 0;
6832 last_status(FALSE);
6833 }
6834 wild_menu_showing = WM_SHOWN;
6835 }
6836 }
6837
6838 screen_puts(buf, row, 0, attr);
6839 if (selstart != NULL && highlight)
6840 {
6841 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006842 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843 }
6844
6845 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6846 }
6847
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849 vim_free(buf);
6850}
6851#endif
6852
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853/*
6854 * Redraw the status line of window wp.
6855 *
6856 * If inversion is possible we use it. Else '=' characters are used.
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006857 * If "ignore_pum" is TRUE, also redraw statusline when the popup menu is
6858 * displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859 */
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006860 static void
Bram Moolenaar829adb72018-06-24 19:24:03 +02006861win_redr_status(win_T *wp, int ignore_pum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862{
6863 int row;
6864 char_u *p;
6865 int len;
6866 int fillchar;
6867 int attr;
6868 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006869 static int busy = FALSE;
6870
6871 /* It's possible to get here recursively when 'statusline' (indirectly)
6872 * invokes ":redrawstatus". Simply ignore the call then. */
6873 if (busy)
6874 return;
6875 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876
6877 wp->w_redr_status = FALSE;
6878 if (wp->w_status_height == 0)
6879 {
6880 /* no status line, can only be last window */
6881 redraw_cmdline = TRUE;
6882 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006883 else if (!redrawing()
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006884 // don't update status line when popup menu is visible and may be
6885 // drawn over it, unless it will be redrawn later
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006886 || (!ignore_pum && pum_visible()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 {
6888 /* Don't redraw right now, do it later. */
6889 wp->w_redr_status = TRUE;
6890 }
6891#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006892 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893 {
6894 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006895 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896 }
6897#endif
6898 else
6899 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006900 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006902 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903 p = NameBuff;
6904 len = (int)STRLEN(p);
6905
Bram Moolenaard85f2712017-07-28 21:51:57 +02006906 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907#ifdef FEAT_QUICKFIX
6908 || wp->w_p_pvw
6909#endif
6910 || bufIsChanged(wp->w_buffer)
6911 || wp->w_buffer->b_p_ro)
6912 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02006913 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006915 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916 len += (int)STRLEN(p + len);
6917 }
6918#ifdef FEAT_QUICKFIX
6919 if (wp->w_p_pvw)
6920 {
6921 STRCPY(p + len, _("[Preview]"));
6922 len += (int)STRLEN(p + len);
6923 }
6924#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02006925 if (bufIsChanged(wp->w_buffer)
6926#ifdef FEAT_TERMINAL
6927 && !bt_terminal(wp->w_buffer)
6928#endif
6929 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930 {
6931 STRCPY(p + len, "[+]");
6932 len += 3;
6933 }
6934 if (wp->w_buffer->b_p_ro)
6935 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006936 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01006937 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938 }
6939
Bram Moolenaar02631462017-09-22 15:20:32 +02006940 this_ru_col = ru_col - (Columns - wp->w_width);
6941 if (this_ru_col < (wp->w_width + 1) / 2)
6942 this_ru_col = (wp->w_width + 1) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943 if (this_ru_col <= 1)
6944 {
6945 p = (char_u *)"<"; /* No room for file name! */
6946 len = 1;
6947 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01006948 else if (has_mbyte)
6949 {
6950 int clen = 0, i;
6951
6952 /* Count total number of display cells. */
6953 clen = mb_string2cells(p, -1);
6954
6955 /* Find first character that will fit.
6956 * Going from start to end is much faster for DBCS. */
6957 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
6958 i += (*mb_ptr2len)(p + i))
6959 clen -= (*mb_ptr2cells)(p + i);
6960 len = clen;
6961 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962 {
Bram Moolenaara12a1612019-01-24 16:39:02 +01006963 p = p + i - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 *p = '<';
Bram Moolenaara12a1612019-01-24 16:39:02 +01006965 ++len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966 }
6967
Bram Moolenaara12a1612019-01-24 16:39:02 +01006968 }
6969 else if (len > this_ru_col - 1)
6970 {
6971 p += len - (this_ru_col - 1);
6972 *p = '<';
6973 len = this_ru_col - 1;
6974 }
6975
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar53f81742017-09-22 14:35:51 +02006977 screen_puts(p, row, wp->w_wincol, attr);
6978 screen_fill(row, row + 1, len + wp->w_wincol,
6979 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02006981 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6983 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
Bram Moolenaar53f81742017-09-22 14:35:51 +02006984 - 1 + wp->w_wincol), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985
6986#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +02006987 win_redr_ruler(wp, TRUE, ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988#endif
6989 }
6990
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 /*
6992 * May need to draw the character below the vertical separator.
6993 */
6994 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6995 {
6996 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006997 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998 else
6999 fillchar = fillchar_vsep(&attr);
7000 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
7001 attr);
7002 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007003 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004}
7005
Bram Moolenaar238a5642006-02-21 22:12:05 +00007006#ifdef FEAT_STL_OPT
7007/*
7008 * Redraw the status line according to 'statusline' and take care of any
7009 * errors encountered.
7010 */
7011 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007012redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00007013{
Bram Moolenaar362f3562009-11-03 16:20:34 +00007014 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02007015 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007016
7017 /* When called recursively return. This can happen when the statusline
7018 * contains an expression that triggers a redraw. */
7019 if (entered)
7020 return;
7021 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007022
Bram Moolenaara742e082016-04-05 21:10:38 +02007023 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007024 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02007025 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007026 {
7027 /* When there is an error disable the statusline, otherwise the
7028 * display is messed up with errors and a redraw triggers the problem
7029 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007030 set_string_option_direct((char_u *)"statusline", -1,
7031 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007032 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007033 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007034 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007035 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007036}
7037#endif
7038
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039/*
7040 * Return TRUE if the status line of window "wp" is connected to the status
7041 * line of the window right of it. If not, then it's a vertical separator.
7042 * Only call if (wp->w_vsep_width != 0).
7043 */
7044 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007045stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046{
7047 frame_T *fr;
7048
7049 fr = wp->w_frame;
7050 while (fr->fr_parent != NULL)
7051 {
7052 if (fr->fr_parent->fr_layout == FR_COL)
7053 {
7054 if (fr->fr_next != NULL)
7055 break;
7056 }
7057 else
7058 {
7059 if (fr->fr_next != NULL)
7060 return TRUE;
7061 }
7062 fr = fr->fr_parent;
7063 }
7064 return FALSE;
7065}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068/*
7069 * Get the value to show for the language mappings, active 'keymap'.
7070 */
7071 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007072get_keymap_str(
7073 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007074 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007075 char_u *buf, /* buffer for the result */
7076 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077{
7078 char_u *p;
7079
7080 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7081 return FALSE;
7082
7083 {
7084#ifdef FEAT_EVAL
7085 buf_T *old_curbuf = curbuf;
7086 win_T *old_curwin = curwin;
7087 char_u *s;
7088
7089 curbuf = wp->w_buffer;
7090 curwin = wp;
7091 STRCPY(buf, "b:keymap_name"); /* must be writable */
7092 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007093 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094 --emsg_skip;
7095 curbuf = old_curbuf;
7096 curwin = old_curwin;
7097 if (p == NULL || *p == NUL)
7098#endif
7099 {
7100#ifdef FEAT_KEYMAP
7101 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7102 p = wp->w_buffer->b_p_keymap;
7103 else
7104#endif
7105 p = (char_u *)"lang";
7106 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007107 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108 buf[0] = NUL;
7109#ifdef FEAT_EVAL
7110 vim_free(s);
7111#endif
7112 }
7113 return buf[0] != NUL;
7114}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115
7116#if defined(FEAT_STL_OPT) || defined(PROTO)
7117/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007118 * Redraw the status line or ruler of window "wp".
7119 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 */
7121 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007122win_redr_custom(
7123 win_T *wp,
7124 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007126 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127 int attr;
7128 int curattr;
7129 int row;
7130 int col = 0;
7131 int maxwidth;
7132 int width;
7133 int n;
7134 int len;
7135 int fillchar;
7136 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007137 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007139 struct stl_hlrec hltab[STL_MAX_ITEM];
7140 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007141 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007142 win_T *ewp;
7143 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144
Bram Moolenaar1d633412013-12-11 15:52:01 +01007145 /* There is a tiny chance that this gets called recursively: When
7146 * redrawing a status line triggers redrawing the ruler or tabline.
7147 * Avoid trouble by not allowing recursion. */
7148 if (entered)
7149 return;
7150 entered = TRUE;
7151
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007153 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007155 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007156 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007157 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007158 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007159 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007160 maxwidth = Columns;
7161# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007162 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007163# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007165 else
7166 {
7167 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007168 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02007169 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007170
7171 if (draw_ruler)
7172 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007173 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007174 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007175 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007176 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007177 if (*++stl == '-')
7178 stl++;
7179 if (atoi((char *)stl))
7180 while (VIM_ISDIGIT(*stl))
7181 stl++;
7182 if (*stl++ != '(')
7183 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007184 }
Bram Moolenaar02631462017-09-22 15:20:32 +02007185 col = ru_col - (Columns - wp->w_width);
7186 if (col < (wp->w_width + 1) / 2)
7187 col = (wp->w_width + 1) / 2;
7188 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007189 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007190 {
7191 row = Rows - 1;
7192 --maxwidth; /* writing in last column may cause scrolling */
7193 fillchar = ' ';
7194 attr = 0;
7195 }
7196
7197# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007198 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007199# endif
7200 }
7201 else
7202 {
7203 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007204 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007205 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007206 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007207# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007208 use_sandbox = was_set_insecurely((char_u *)"statusline",
7209 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007210# endif
7211 }
7212
Bram Moolenaar53f81742017-09-22 14:35:51 +02007213 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007214 }
7215
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007217 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218
Bram Moolenaar61452852011-02-01 18:01:11 +01007219 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7220 * the cursor away and back. */
7221 ewp = wp == NULL ? curwin : wp;
7222 p_crb_save = ewp->w_p_crb;
7223 ewp->w_p_crb = FALSE;
7224
Bram Moolenaar362f3562009-11-03 16:20:34 +00007225 /* Make a copy, because the statusline may include a function call that
7226 * might change the option value and free the memory. */
7227 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007228 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007229 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007230 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007231 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007232 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007234 /* Make all characters printable. */
7235 p = transstr(buf);
7236 if (p != NULL)
7237 {
7238 vim_strncpy(buf, p, sizeof(buf) - 1);
7239 vim_free(p);
7240 }
7241
7242 /* fill up with "fillchar" */
7243 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007244 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246 len += (*mb_char2bytes)(fillchar, buf + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247 ++width;
7248 }
7249 buf[len] = NUL;
7250
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007251 /*
7252 * Draw each snippet with the specified highlighting.
7253 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 curattr = attr;
7255 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007256 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007258 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259 screen_puts_len(p, len, row, col, curattr);
7260 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007261 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007263 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007265 else if (hltab[n].userhl < 0)
7266 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007267#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007268 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7269 && wp->w_status_height != 0)
7270 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007271 else if (wp != NULL && bt_terminal(wp->w_buffer)
7272 && wp->w_status_height != 0)
7273 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02007274#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007275 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007276 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007278 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279 }
7280 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007281
7282 if (wp == NULL)
7283 {
7284 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7285 col = 0;
7286 len = 0;
7287 p = buf;
7288 fillchar = 0;
7289 for (n = 0; tabtab[n].start != NULL; n++)
7290 {
7291 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7292 while (col < len)
7293 TabPageIdxs[col++] = fillchar;
7294 p = tabtab[n].start;
7295 fillchar = tabtab[n].userhl;
7296 }
7297 while (col < Columns)
7298 TabPageIdxs[col++] = fillchar;
7299 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007300
7301theend:
7302 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303}
7304
7305#endif /* FEAT_STL_OPT */
7306
7307/*
7308 * Output a single character directly to the screen and update ScreenLines.
7309 */
7310 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007311screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313 char_u buf[MB_MAXBYTES + 1];
7314
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007315 if (has_mbyte)
7316 buf[(*mb_char2bytes)(c, buf)] = NUL;
7317 else
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007318 {
7319 buf[0] = c;
7320 buf[1] = NUL;
7321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322 screen_puts(buf, row, col, attr);
7323}
7324
7325/*
7326 * Get a single character directly from ScreenLines into "bytes[]".
7327 * Also return its attribute in *attrp;
7328 */
7329 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007330screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331{
7332 unsigned off;
7333
7334 /* safety check */
7335 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7336 {
7337 off = LineOffset[row] + col;
7338 *attrp = ScreenAttrs[off];
7339 bytes[0] = ScreenLines[off];
7340 bytes[1] = NUL;
7341
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342 if (enc_utf8 && ScreenLinesUC[off] != 0)
7343 bytes[utfc_char2bytes(off, bytes)] = NUL;
7344 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7345 {
7346 bytes[0] = ScreenLines[off];
7347 bytes[1] = ScreenLines2[off];
7348 bytes[2] = NUL;
7349 }
7350 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7351 {
7352 bytes[1] = ScreenLines[off + 1];
7353 bytes[2] = NUL;
7354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 }
7356}
7357
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007358/*
7359 * Return TRUE if composing characters for screen posn "off" differs from
7360 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007361 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007362 */
7363 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007364screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007365{
7366 int i;
7367
7368 for (i = 0; i < Screen_mco; ++i)
7369 {
7370 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7371 return TRUE;
7372 if (u8cc[i] == 0)
7373 break;
7374 }
7375 return FALSE;
7376}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007377
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378/*
7379 * Put string '*text' on the screen at position 'row' and 'col', with
7380 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7381 * Note: only outputs within one row, message is truncated at screen boundary!
7382 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7383 */
7384 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007385screen_puts(
7386 char_u *text,
7387 int row,
7388 int col,
7389 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390{
7391 screen_puts_len(text, -1, row, col, attr);
7392}
7393
7394/*
7395 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7396 * a NUL.
7397 */
7398 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007399screen_puts_len(
7400 char_u *text,
7401 int textlen,
7402 int row,
7403 int col,
7404 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405{
7406 unsigned off;
7407 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007408 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 int c;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007410 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411 int mbyte_blen = 1;
7412 int mbyte_cells = 1;
7413 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007414 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415 int clear_next_cell = FALSE;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007416#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007418 int pc, nc, nc1;
7419 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007421 int force_redraw_this;
7422 int force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007423 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424
Bram Moolenaar0b4c9ed2019-06-03 22:04:23 +02007425 // Safety check. The check for negative row and column is to fix issue
7426 // #4102. TODO: find out why row/col could be negative.
7427 if (ScreenLines == NULL
7428 || row >= screen_Rows || row < 0
7429 || col >= screen_Columns || col < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007431 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432
Bram Moolenaarc236c162008-07-13 17:41:49 +00007433 /* When drawing over the right halve of a double-wide char clear out the
7434 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007435 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaara12a1612019-01-24 16:39:02 +01007436#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00007437 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01007438#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007439 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007440 {
7441 ScreenLines[off - 1] = ' ';
7442 ScreenAttrs[off - 1] = 0;
7443 if (enc_utf8)
7444 {
7445 ScreenLinesUC[off - 1] = 0;
7446 ScreenLinesC[0][off - 1] = 0;
7447 }
7448 /* redraw the previous cell, make it empty */
7449 screen_char(off - 1, row, col - 1);
7450 /* force the cell at "col" to be redrawn */
7451 force_redraw_next = TRUE;
7452 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007453
Bram Moolenaar367329b2007-08-30 11:53:22 +00007454 max_off = LineOffset[row] + screen_Columns;
Bram Moolenaara064ac82007-08-05 18:10:54 +00007455 while (col < screen_Columns
7456 && (len < 0 || (int)(ptr - text) < len)
7457 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458 {
7459 c = *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 /* check if this is the first byte of a multibyte */
7461 if (has_mbyte)
7462 {
7463 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007464 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007466 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7468 mbyte_cells = 1;
7469 else if (enc_dbcs != 0)
7470 mbyte_cells = mbyte_blen;
7471 else /* enc_utf8 */
7472 {
7473 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007474 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475 (int)((text + len) - ptr));
7476 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007477 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007479#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7481 {
7482 /* Do Arabic shaping. */
7483 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7484 {
7485 /* Past end of string to be displayed. */
7486 nc = NUL;
7487 nc1 = NUL;
7488 }
7489 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007490 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007491 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7492 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007493 nc1 = pcc[0];
7494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495 pc = prev_c;
7496 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007497 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 }
7499 else
7500 prev_c = u8c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007501#endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007502 if (col + mbyte_cells > screen_Columns)
7503 {
7504 /* Only 1 cell left, but character requires 2 cells:
7505 * display a '>' in the last column to avoid wrapping. */
7506 c = '>';
7507 mbyte_cells = 1;
7508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509 }
7510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007512 force_redraw_this = force_redraw_next;
7513 force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007514
7515 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516 || (mbyte_cells == 2
7517 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7518 || (enc_dbcs == DBCS_JPNU
7519 && c == 0x8e
7520 && ScreenLines2[off] != ptr[1])
7521 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007522 && (ScreenLinesUC[off] !=
7523 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7524 || (ScreenLinesUC[off] != 0
7525 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007527 || exmode_active;
7528
Bram Moolenaar24a5ac52019-06-08 19:01:18 +02007529 if ((need_redraw || force_redraw_this)
7530#ifdef FEAT_TEXT_PROP
Bram Moolenaarc662ec92019-06-23 00:15:57 +02007531 && !blocked_by_popup(row, col)
Bram Moolenaar24a5ac52019-06-08 19:01:18 +02007532#endif
7533 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534 {
7535#if defined(FEAT_GUI) || defined(UNIX)
7536 /* The bold trick makes a single row of pixels appear in the next
7537 * character. When a bold character is removed, the next
7538 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007539 * and for some xterms. */
7540 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541# ifdef FEAT_GUI
7542 gui.in_use
7543# endif
7544# if defined(FEAT_GUI) && defined(UNIX)
7545 ||
7546# endif
7547# ifdef UNIX
7548 term_is_xterm
7549# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007550 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007552 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007554 if (n > HL_ALL)
7555 n = syn_attr2attr(n);
7556 if (n & HL_BOLD)
7557 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558 }
7559#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560 /* When at the end of the text and overwriting a two-cell
7561 * character with a one-cell character, need to clear the next
7562 * cell. Also when overwriting the left halve of a two-cell char
7563 * with the right halve of a two-cell char. Do this only once
7564 * (mb_off2cells() may return 2 on the right halve). */
7565 if (clear_next_cell)
7566 clear_next_cell = FALSE;
7567 else if (has_mbyte
7568 && (len < 0 ? ptr[mbyte_blen] == NUL
7569 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007570 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007572 && (*mb_off2cells)(off, max_off) == 1
7573 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 clear_next_cell = TRUE;
7575
7576 /* Make sure we never leave a second byte of a double-byte behind,
7577 * it confuses mb_off2cells(). */
7578 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007579 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007581 && (*mb_off2cells)(off, max_off) == 1
7582 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583 ScreenLines[off + mbyte_blen] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584 ScreenLines[off] = c;
7585 ScreenAttrs[off] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586 if (enc_utf8)
7587 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007588 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589 ScreenLinesUC[off] = 0;
7590 else
7591 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007592 int i;
7593
Bram Moolenaar071d4272004-06-13 20:20:40 +00007594 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007595 for (i = 0; i < Screen_mco; ++i)
7596 {
7597 ScreenLinesC[i][off] = u8cc[i];
7598 if (u8cc[i] == 0)
7599 break;
7600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601 }
7602 if (mbyte_cells == 2)
7603 {
7604 ScreenLines[off + 1] = 0;
7605 ScreenAttrs[off + 1] = attr;
7606 }
7607 screen_char(off, row, col);
7608 }
7609 else if (mbyte_cells == 2)
7610 {
7611 ScreenLines[off + 1] = ptr[1];
7612 ScreenAttrs[off + 1] = attr;
7613 screen_char_2(off, row, col);
7614 }
7615 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7616 {
7617 ScreenLines2[off] = ptr[1];
7618 screen_char(off, row, col);
7619 }
7620 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621 screen_char(off, row, col);
7622 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623 if (has_mbyte)
7624 {
7625 off += mbyte_cells;
7626 col += mbyte_cells;
7627 ptr += mbyte_blen;
7628 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007629 {
7630 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007631 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007632 len = -1;
7633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634 }
7635 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636 {
7637 ++off;
7638 ++col;
7639 ++ptr;
7640 }
7641 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007642
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007643 /* If we detected the next character needs to be redrawn, but the text
7644 * doesn't extend up to there, update the character here. */
7645 if (force_redraw_next && col < screen_Columns)
7646 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007647 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7648 screen_char_2(off, row, col);
7649 else
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007650 screen_char(off, row, col);
7651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652}
7653
7654#ifdef FEAT_SEARCH_EXTRA
7655/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007656 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657 */
7658 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007659start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660{
7661 if (p_hls && !no_hlsearch)
7662 {
7663 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007664 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007665# ifdef FEAT_RELTIME
7666 /* Set the time limit to 'redrawtime'. */
7667 profile_setlimit(p_rdt, &search_hl.tm);
7668# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669 }
7670}
7671
7672/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007673 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674 */
7675 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007676end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677{
7678 if (search_hl.rm.regprog != NULL)
7679 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007680 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681 search_hl.rm.regprog = NULL;
7682 }
7683}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02007684#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02007685
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007687screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688{
7689 attrentry_T *aep = NULL;
7690
7691 screen_attr = attr;
7692 if (full_screen
Bram Moolenaar4f974752019-02-17 17:44:42 +01007693#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694 && termcap_active
7695#endif
7696 )
7697 {
7698#ifdef FEAT_GUI
7699 if (gui.in_use)
7700 {
7701 char buf[20];
7702
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007703 /* The GUI handles this internally. */
7704 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705 OUT_STR(buf);
7706 }
7707 else
7708#endif
7709 {
7710 if (attr > HL_ALL) /* special HL attr. */
7711 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007712 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713 aep = syn_cterm_attr2entry(attr);
7714 else
7715 aep = syn_term_attr2entry(attr);
7716 if (aep == NULL) /* did ":syntax clear" */
7717 attr = 0;
7718 else
7719 attr = aep->ae_attr;
7720 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01007721 if ((attr & HL_BOLD) && *T_MD != NUL) /* bold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007723 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007724#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007725 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
7726 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
7727 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007728#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007729 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007730 /* If the Normal FG color has BOLD attribute and the new HL
7731 * has a FG color defined, clear BOLD. */
7732 out_str(T_ME);
Bram Moolenaar45a00002017-12-22 21:12:34 +01007733 if ((attr & HL_STANDOUT) && *T_SO != NUL) /* standout */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734 out_str(T_SO);
Bram Moolenaar45a00002017-12-22 21:12:34 +01007735 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) /* undercurl */
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01007736 out_str(T_UCS);
7737 if (((attr & HL_UNDERLINE) /* underline or undercurl */
Bram Moolenaar45a00002017-12-22 21:12:34 +01007738 || ((attr & HL_UNDERCURL) && *T_UCS == NUL))
7739 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740 out_str(T_US);
Bram Moolenaar45a00002017-12-22 21:12:34 +01007741 if ((attr & HL_ITALIC) && *T_CZH != NUL) /* italic */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742 out_str(T_CZH);
Bram Moolenaar45a00002017-12-22 21:12:34 +01007743 if ((attr & HL_INVERSE) && *T_MR != NUL) /* inverse (reverse) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744 out_str(T_MR);
Bram Moolenaar45a00002017-12-22 21:12:34 +01007745 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) /* strike */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02007746 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747
7748 /*
7749 * Output the color or start string after bold etc., in case the
7750 * bold etc. override the color setting.
7751 */
7752 if (aep != NULL)
7753 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007754#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007755 /* When 'termguicolors' is set but fg or bg is unset,
7756 * fall back to the cterm colors. This helps for SpellBad,
7757 * where the GUI uses a red undercurl. */
7758 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007760 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007761 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007762 }
7763 else
7764#endif
7765 if (t_colors > 1)
7766 {
7767 if (aep->ae_u.cterm.fg_color)
7768 term_fg_color(aep->ae_u.cterm.fg_color - 1);
7769 }
7770#ifdef FEAT_TERMGUICOLORS
7771 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
7772 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007773 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007774 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775 }
7776 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007777#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007778 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007780 if (aep->ae_u.cterm.bg_color)
7781 term_bg_color(aep->ae_u.cterm.bg_color - 1);
7782 }
7783
Bram Moolenaarf708ac52018-03-12 21:48:32 +01007784 if (!IS_CTERM)
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007785 {
7786 if (aep->ae_u.term.start != NULL)
7787 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788 }
7789 }
7790 }
7791 }
7792}
7793
7794 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007795screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796{
7797 int do_ME = FALSE; /* output T_ME code */
7798
7799 if (screen_attr != 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01007800#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801 && termcap_active
7802#endif
7803 )
7804 {
7805#ifdef FEAT_GUI
7806 if (gui.in_use)
7807 {
7808 char buf[20];
7809
7810 /* use internal GUI code */
7811 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
7812 OUT_STR(buf);
7813 }
7814 else
7815#endif
7816 {
7817 if (screen_attr > HL_ALL) /* special HL attr. */
7818 {
7819 attrentry_T *aep;
7820
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007821 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822 {
7823 /*
7824 * Assume that t_me restores the original colors!
7825 */
7826 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007827 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007828#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007829 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
7830 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
7831 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007832#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007833 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007834#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007835 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
7836 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
7837 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007838#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007839 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840 do_ME = TRUE;
7841 }
7842 else
7843 {
7844 aep = syn_term_attr2entry(screen_attr);
7845 if (aep != NULL && aep->ae_u.term.stop != NULL)
7846 {
7847 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
7848 do_ME = TRUE;
7849 else
7850 out_str(aep->ae_u.term.stop);
7851 }
7852 }
7853 if (aep == NULL) /* did ":syntax clear" */
7854 screen_attr = 0;
7855 else
7856 screen_attr = aep->ae_attr;
7857 }
7858
7859 /*
7860 * Often all ending-codes are equal to T_ME. Avoid outputting the
7861 * same sequence several times.
7862 */
7863 if (screen_attr & HL_STANDOUT)
7864 {
7865 if (STRCMP(T_SE, T_ME) == 0)
7866 do_ME = TRUE;
7867 else
7868 out_str(T_SE);
7869 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01007870 if ((screen_attr & HL_UNDERCURL) && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01007871 {
7872 if (STRCMP(T_UCE, T_ME) == 0)
7873 do_ME = TRUE;
7874 else
7875 out_str(T_UCE);
7876 }
7877 if ((screen_attr & HL_UNDERLINE)
Bram Moolenaar45a00002017-12-22 21:12:34 +01007878 || ((screen_attr & HL_UNDERCURL) && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879 {
7880 if (STRCMP(T_UE, T_ME) == 0)
7881 do_ME = TRUE;
7882 else
7883 out_str(T_UE);
7884 }
7885 if (screen_attr & HL_ITALIC)
7886 {
7887 if (STRCMP(T_CZR, T_ME) == 0)
7888 do_ME = TRUE;
7889 else
7890 out_str(T_CZR);
7891 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02007892 if (screen_attr & HL_STRIKETHROUGH)
7893 {
7894 if (STRCMP(T_STE, T_ME) == 0)
7895 do_ME = TRUE;
7896 else
7897 out_str(T_STE);
7898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
7900 out_str(T_ME);
7901
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007902#ifdef FEAT_TERMGUICOLORS
7903 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007905 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007906 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007907 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007908 term_bg_rgb_color(cterm_normal_bg_gui_color);
7909 }
7910 else
7911#endif
7912 {
7913 if (t_colors > 1)
7914 {
7915 /* set Normal cterm colors */
7916 if (cterm_normal_fg_color != 0)
7917 term_fg_color(cterm_normal_fg_color - 1);
7918 if (cterm_normal_bg_color != 0)
7919 term_bg_color(cterm_normal_bg_color - 1);
7920 if (cterm_normal_fg_bold)
7921 out_str(T_MD);
7922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923 }
7924 }
7925 }
7926 screen_attr = 0;
7927}
7928
7929/*
7930 * Reset the colors for a cterm. Used when leaving Vim.
7931 * The machine specific code may override this again.
7932 */
7933 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007934reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007936 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937 {
7938 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007939#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007940 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
7941 || cterm_normal_bg_gui_color != INVALCOLOR)
7942 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007943#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007945#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946 {
7947 out_str(T_OP);
7948 screen_attr = -1;
7949 }
7950 if (cterm_normal_fg_bold)
7951 {
7952 out_str(T_ME);
7953 screen_attr = -1;
7954 }
7955 }
7956}
7957
7958/*
7959 * Put character ScreenLines["off"] on the screen at position "row" and "col",
7960 * using the attributes from ScreenAttrs["off"].
7961 */
7962 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007963screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964{
7965 int attr;
7966
7967 /* Check for illegal values, just in case (could happen just after
7968 * resizing). */
7969 if (row >= screen_Rows || col >= screen_Columns)
7970 return;
7971
Bram Moolenaar33796b32019-06-08 16:01:13 +02007972 // Skip if under the popup menu.
7973 // Popup windows with zindex higher than POPUPMENU_ZINDEX go on top.
7974 if (pum_under_menu(row, col)
Bram Moolenaare2c453d2019-08-21 14:37:09 +02007975#ifdef FEAT_TEXT_PROP
Bram Moolenaar33796b32019-06-08 16:01:13 +02007976 && screen_zindex <= POPUPMENU_ZINDEX
Bram Moolenaare2c453d2019-08-21 14:37:09 +02007977#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02007978 )
Bram Moolenaarae654382019-01-17 21:09:05 +01007979 return;
Bram Moolenaar33796b32019-06-08 16:01:13 +02007980#ifdef FEAT_TEXT_PROP
Bram Moolenaarc662ec92019-06-23 00:15:57 +02007981 if (blocked_by_popup(row, col))
Bram Moolenaar33796b32019-06-08 16:01:13 +02007982 return;
7983#endif
7984
Bram Moolenaar494838a2015-02-10 19:20:37 +01007985 /* Outputting a character in the last cell on the screen may scroll the
7986 * screen up. Only do it when the "xn" termcap property is set, otherwise
7987 * mark the character invalid (update it when scrolled up). */
7988 if (*T_XN == NUL
7989 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990#ifdef FEAT_RIGHTLEFT
7991 /* account for first command-line character in rightleft mode */
7992 && !cmdmsg_rl
7993#endif
7994 )
7995 {
7996 ScreenAttrs[off] = (sattr_T)-1;
7997 return;
7998 }
7999
8000 /*
8001 * Stop highlighting first, so it's easier to move the cursor.
8002 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003 if (screen_char_attr != 0)
8004 attr = screen_char_attr;
8005 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006 attr = ScreenAttrs[off];
8007 if (screen_attr != attr)
8008 screen_stop_highlight();
8009
8010 windgoto(row, col);
8011
8012 if (screen_attr != attr)
8013 screen_start_highlight(attr);
8014
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015 if (enc_utf8 && ScreenLinesUC[off] != 0)
8016 {
8017 char_u buf[MB_MAXBYTES + 1];
8018
Bram Moolenaarcb070082016-04-02 22:14:51 +02008019 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008020 {
8021 if (*p_ambw == 'd'
Bram Moolenaara12a1612019-01-24 16:39:02 +01008022#ifdef FEAT_GUI
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008023 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008024#endif
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008025 )
8026 {
8027 /* Clear the two screen cells. If the character is actually
8028 * single width it won't change the second cell. */
8029 out_str((char_u *)" ");
8030 term_windgoto(row, col);
8031 }
8032 /* not sure where the cursor is after drawing the ambiguous width
8033 * character */
Bram Moolenaarcb070082016-04-02 22:14:51 +02008034 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008035 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02008036 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008038
8039 /* Convert the UTF-8 character to bytes and write it. */
8040 buf[utfc_char2bytes(off, buf)] = NUL;
8041 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042 }
8043 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047 /* double-byte character in single-width cell */
8048 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8049 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050 }
8051
8052 screen_cur_col++;
8053}
8054
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055/*
8056 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8057 * on the screen at position 'row' and 'col'.
8058 * The attributes of the first byte is used for all. This is required to
8059 * output the two bytes of a double-byte character with nothing in between.
8060 */
8061 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008062screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063{
8064 /* Check for illegal values (could be wrong when screen was resized). */
8065 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8066 return;
8067
8068 /* Outputting the last character on the screen may scrollup the screen.
8069 * Don't to it! Mark the character invalid (update it when scrolled up) */
8070 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8071 {
8072 ScreenAttrs[off] = (sattr_T)-1;
8073 return;
8074 }
8075
8076 /* Output the first byte normally (positions the cursor), then write the
8077 * second byte directly. */
8078 screen_char(off, row, col);
8079 out_char(ScreenLines[off + 1]);
8080 ++screen_cur_col;
8081}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083/*
8084 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8085 * This uses the contents of ScreenLines[] and doesn't change it.
8086 */
8087 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008088screen_draw_rectangle(
8089 int row,
8090 int col,
8091 int height,
8092 int width,
8093 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094{
8095 int r, c;
8096 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008097 int max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008099 /* Can't use ScreenLines unless initialized */
8100 if (ScreenLines == NULL)
8101 return;
8102
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 if (invert)
8104 screen_char_attr = HL_INVERSE;
8105 for (r = row; r < row + height; ++r)
8106 {
8107 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008108 max_off = off + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109 for (c = col; c < col + width; ++c)
8110 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00008111 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112 {
8113 screen_char_2(off + c, r, c);
8114 ++c;
8115 }
8116 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117 {
8118 screen_char(off + c, r, c);
Bram Moolenaar367329b2007-08-30 11:53:22 +00008119 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120 ++c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 }
8122 }
8123 }
8124 screen_char_attr = 0;
8125}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127/*
8128 * Redraw the characters for a vertically split window.
8129 */
8130 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008131redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132{
8133 int col;
8134 int width;
8135
8136# ifdef FEAT_CLIPBOARD
8137 clip_may_clear_selection(row, end - 1);
8138# endif
8139
8140 if (wp == NULL)
8141 {
8142 col = 0;
8143 width = Columns;
8144 }
8145 else
8146 {
8147 col = wp->w_wincol;
8148 width = wp->w_width;
8149 }
8150 screen_draw_rectangle(row, col, end - row, width, FALSE);
8151}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008153 static void
8154space_to_screenline(int off, int attr)
8155{
8156 ScreenLines[off] = ' ';
8157 ScreenAttrs[off] = attr;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008158 if (enc_utf8)
8159 ScreenLinesUC[off] = 0;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008160}
8161
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162/*
8163 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8164 * with character 'c1' in first column followed by 'c2' in the other columns.
8165 * Use attributes 'attr'.
8166 */
8167 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008168screen_fill(
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008169 int start_row,
8170 int end_row,
8171 int start_col,
8172 int end_col,
8173 int c1,
8174 int c2,
8175 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008177 int row;
8178 int col;
8179 int off;
8180 int end_off;
8181 int did_delete;
8182 int c;
8183 int norm_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008185 int force_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186#endif
8187
8188 if (end_row > screen_Rows) /* safety check */
8189 end_row = screen_Rows;
8190 if (end_col > screen_Columns) /* safety check */
8191 end_col = screen_Columns;
8192 if (ScreenLines == NULL
8193 || start_row >= end_row
8194 || start_col >= end_col) /* nothing to do */
8195 return;
8196
8197 /* it's a "normal" terminal when not in a GUI or cterm */
8198 norm_term = (
8199#ifdef FEAT_GUI
8200 !gui.in_use &&
8201#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008202 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008203 for (row = start_row; row < end_row; ++row)
8204 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008205 if (has_mbyte
Bram Moolenaara12a1612019-01-24 16:39:02 +01008206#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00008207 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008208#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00008209 )
8210 {
8211 /* When drawing over the right halve of a double-wide char clear
8212 * out the left halve. When drawing over the left halve of a
8213 * double wide-char clear out the right halve. Only needed in a
8214 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008215 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008216 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008217 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008218 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220 /*
8221 * Try to use delete-line termcap code, when no attributes or in a
8222 * "normal" terminal, where a bold/italic space is just a
8223 * space.
8224 */
8225 did_delete = FALSE;
8226 if (c2 == ' '
8227 && end_col == Columns
8228 && can_clear(T_CE)
8229 && (attr == 0
8230 || (norm_term
8231 && attr <= HL_ALL
8232 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8233 {
8234 /*
8235 * check if we really need to clear something
8236 */
8237 col = start_col;
8238 if (c1 != ' ') /* don't clear first char */
8239 ++col;
8240
8241 off = LineOffset[row] + col;
8242 end_off = LineOffset[row] + end_col;
8243
8244 /* skip blanks (used often, keep it fast!) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 if (enc_utf8)
8246 while (off < end_off && ScreenLines[off] == ' '
8247 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8248 ++off;
8249 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250 while (off < end_off && ScreenLines[off] == ' '
8251 && ScreenAttrs[off] == 0)
8252 ++off;
8253 if (off < end_off) /* something to be cleared */
8254 {
8255 col = off - LineOffset[row];
8256 screen_stop_highlight();
8257 term_windgoto(row, col);/* clear rest of this screen line */
8258 out_str(T_CE);
8259 screen_start(); /* don't know where cursor is now */
8260 col = end_col - col;
8261 while (col--) /* clear chars in ScreenLines */
8262 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008263 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008264 ++off;
8265 }
8266 }
8267 did_delete = TRUE; /* the chars are cleared now */
8268 }
8269
8270 off = LineOffset[row] + start_col;
8271 c = c1;
8272 for (col = start_col; col < end_col; ++col)
8273 {
Bram Moolenaar33796b32019-06-08 16:01:13 +02008274 if ((ScreenLines[off] != c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008275 || (enc_utf8 && (int)ScreenLinesUC[off]
8276 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277 || ScreenAttrs[off] != attr
8278#if defined(FEAT_GUI) || defined(UNIX)
8279 || force_next
8280#endif
8281 )
Bram Moolenaar33796b32019-06-08 16:01:13 +02008282#ifdef FEAT_TEXT_PROP
8283 // Skip if under a(nother) popup.
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008284 && !blocked_by_popup(row, col)
Bram Moolenaar33796b32019-06-08 16:01:13 +02008285#endif
8286 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287 {
8288#if defined(FEAT_GUI) || defined(UNIX)
8289 /* The bold trick may make a single row of pixels appear in
8290 * the next character. When a bold character is removed, the
8291 * next character should be redrawn too. This happens for our
8292 * own GUI and for some xterms. */
8293 if (
8294# ifdef FEAT_GUI
8295 gui.in_use
8296# endif
8297# if defined(FEAT_GUI) && defined(UNIX)
8298 ||
8299# endif
8300# ifdef UNIX
8301 term_is_xterm
8302# endif
8303 )
8304 {
8305 if (ScreenLines[off] != ' '
8306 && (ScreenAttrs[off] > HL_ALL
8307 || ScreenAttrs[off] & HL_BOLD))
8308 force_next = TRUE;
8309 else
8310 force_next = FALSE;
8311 }
8312#endif
8313 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314 if (enc_utf8)
8315 {
8316 if (c >= 0x80)
8317 {
8318 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008319 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320 }
8321 else
8322 ScreenLinesUC[off] = 0;
8323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324 ScreenAttrs[off] = attr;
8325 if (!did_delete || c != ' ')
8326 screen_char(off, row, col);
8327 }
8328 ++off;
8329 if (col == start_col)
8330 {
8331 if (did_delete)
8332 break;
8333 c = c2;
8334 }
8335 }
8336 if (end_col == Columns)
8337 LineWraps[row] = FALSE;
8338 if (row == Rows - 1) /* overwritten the command line */
8339 {
8340 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02008341 if (start_col == 0 && end_col == Columns
8342 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008344 if (start_col == 0)
8345 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 }
8347 }
8348}
8349
8350/*
8351 * Check if there should be a delay. Used before clearing or redrawing the
8352 * screen or the command line.
8353 */
8354 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008355check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356{
8357 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8358 && !did_wait_return
8359 && emsg_silent == 0)
8360 {
8361 out_flush();
8362 ui_delay(1000L, TRUE);
8363 emsg_on_display = FALSE;
8364 if (check_msg_scroll)
8365 msg_scroll = FALSE;
8366 }
8367}
8368
8369/*
Bram Moolenaarca57ab52019-04-13 14:53:16 +02008370 * Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect.
8371 */
8372 static void
8373clear_TabPageIdxs(void)
8374{
8375 int scol;
8376
8377 for (scol = 0; scol < Columns; ++scol)
8378 TabPageIdxs[scol] = 0;
8379}
8380
8381/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008383 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384 * Returns TRUE if there is a valid screen to write to.
8385 * Returns FALSE when starting up and screen not initialized yet.
8386 */
8387 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008388screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008390 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391 return (ScreenLines != NULL);
8392}
8393
8394/*
8395 * Resize the shell to Rows and Columns.
8396 * Allocate ScreenLines[] and associated items.
8397 *
8398 * There may be some time between setting Rows and Columns and (re)allocating
8399 * ScreenLines[]. This happens when starting up and when (manually) changing
8400 * the shell size. Always use screen_Rows and screen_Columns to access items
8401 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8402 * final size of the shell is needed.
8403 */
8404 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008405screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406{
8407 int new_row, old_row;
8408#ifdef FEAT_GUI
8409 int old_Rows;
8410#endif
8411 win_T *wp;
8412 int outofmem = FALSE;
8413 int len;
8414 schar_T *new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008415 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008416 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008418 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419 sattr_T *new_ScreenAttrs;
8420 unsigned *new_LineOffset;
8421 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008422 short *new_TabPageIdxs;
Bram Moolenaar33796b32019-06-08 16:01:13 +02008423#ifdef FEAT_TEXT_PROP
8424 short *new_popup_mask;
Bram Moolenaar4c063a02019-06-10 21:24:12 +02008425 short *new_popup_mask_next;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008426 char *new_popup_transparent;
Bram Moolenaar33796b32019-06-08 16:01:13 +02008427#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00008428 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008430 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008431 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008433retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434 /*
8435 * Allocation of the screen buffers is done only when the size changes and
8436 * when Rows and Columns have been set and we have started doing full
8437 * screen stuff.
8438 */
8439 if ((ScreenLines != NULL
8440 && Rows == screen_Rows
8441 && Columns == screen_Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442 && enc_utf8 == (ScreenLinesUC != NULL)
8443 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaara12a1612019-01-24 16:39:02 +01008444 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445 || Rows == 0
8446 || Columns == 0
8447 || (!full_screen && ScreenLines == NULL))
8448 return;
8449
8450 /*
8451 * It's possible that we produce an out-of-memory message below, which
8452 * will cause this function to be called again. To break the loop, just
8453 * return here.
8454 */
8455 if (entered)
8456 return;
8457 entered = TRUE;
8458
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008459 /*
8460 * Note that the window sizes are updated before reallocating the arrays,
8461 * thus we must not redraw here!
8462 */
8463 ++RedrawingDisabled;
8464
Bram Moolenaar071d4272004-06-13 20:20:40 +00008465 win_new_shellsize(); /* fit the windows in the new sized shell */
8466
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467 comp_col(); /* recompute columns for shown command and ruler */
8468
8469 /*
8470 * We're changing the size of the screen.
8471 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8472 * - Move lines from the old arrays into the new arrays, clear extra
8473 * lines (unless the screen is going to be cleared).
8474 * - Free the old arrays.
8475 *
8476 * If anything fails, make ScreenLines NULL, so we don't do anything!
8477 * Continuing with the old ScreenLines may result in a crash, because the
8478 * size is wrong.
8479 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008480 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008482 if (aucmd_win != NULL)
8483 win_free_lsize(aucmd_win);
Bram Moolenaar8caaf822019-06-01 18:11:22 +02008484#ifdef FEAT_TEXT_PROP
8485 // global popup windows
8486 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
8487 win_free_lsize(wp);
8488 // tab-local popup windows
8489 FOR_ALL_TABPAGES(tp)
8490 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
8491 win_free_lsize(wp);
8492#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008494 new_ScreenLines = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
Bram Moolenaar216b7102010-03-23 13:56:59 +01008495 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496 if (enc_utf8)
8497 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008498 new_ScreenLinesUC = LALLOC_MULT(u8char_T, (Rows + 1) * Columns);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008499 for (i = 0; i < p_mco; ++i)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008500 new_ScreenLinesC[i] = LALLOC_CLEAR_MULT(u8char_T,
8501 (Rows + 1) * Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502 }
8503 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008504 new_ScreenLines2 = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
8505 new_ScreenAttrs = LALLOC_MULT(sattr_T, (Rows + 1) * Columns);
8506 new_LineOffset = LALLOC_MULT(unsigned, Rows);
8507 new_LineWraps = LALLOC_MULT(char_u, Rows);
8508 new_TabPageIdxs = LALLOC_MULT(short, Columns);
Bram Moolenaar33796b32019-06-08 16:01:13 +02008509#ifdef FEAT_TEXT_PROP
8510 new_popup_mask = LALLOC_MULT(short, Rows * Columns);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02008511 new_popup_mask_next = LALLOC_MULT(short, Rows * Columns);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008512 new_popup_transparent = LALLOC_MULT(char, Rows * Columns);
Bram Moolenaar33796b32019-06-08 16:01:13 +02008513#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008515 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516 {
8517 if (win_alloc_lines(wp) == FAIL)
8518 {
8519 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008520 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521 }
8522 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008523 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8524 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008525 outofmem = TRUE;
Bram Moolenaar8caaf822019-06-01 18:11:22 +02008526#ifdef FEAT_TEXT_PROP
8527 // global popup windows
8528 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
8529 if (win_alloc_lines(wp) == FAIL)
8530 {
8531 outofmem = TRUE;
8532 goto give_up;
8533 }
8534 // tab-local popup windows
8535 FOR_ALL_TABPAGES(tp)
8536 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
8537 if (win_alloc_lines(wp) == FAIL)
8538 {
8539 outofmem = TRUE;
8540 goto give_up;
8541 }
8542#endif
8543
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008544give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008546 for (i = 0; i < p_mco; ++i)
8547 if (new_ScreenLinesC[i] == NULL)
8548 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008549 if (new_ScreenLines == NULL
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008550 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552 || new_ScreenAttrs == NULL
8553 || new_LineOffset == NULL
8554 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008555 || new_TabPageIdxs == NULL
Bram Moolenaar33796b32019-06-08 16:01:13 +02008556#ifdef FEAT_TEXT_PROP
8557 || new_popup_mask == NULL
Bram Moolenaar4c063a02019-06-10 21:24:12 +02008558 || new_popup_mask_next == NULL
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008559 || new_popup_transparent == NULL
Bram Moolenaar33796b32019-06-08 16:01:13 +02008560#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561 || outofmem)
8562 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008563 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008564 {
8565 /* guess the size */
8566 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8567
8568 /* Remember we did this to avoid getting outofmem messages over
8569 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008570 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008571 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01008572 VIM_CLEAR(new_ScreenLines);
Bram Moolenaard23a8232018-02-10 18:45:26 +01008573 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008574 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01008575 VIM_CLEAR(new_ScreenLinesC[i]);
8576 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaard23a8232018-02-10 18:45:26 +01008577 VIM_CLEAR(new_ScreenAttrs);
8578 VIM_CLEAR(new_LineOffset);
8579 VIM_CLEAR(new_LineWraps);
8580 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar33796b32019-06-08 16:01:13 +02008581#ifdef FEAT_TEXT_PROP
8582 VIM_CLEAR(new_popup_mask);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02008583 VIM_CLEAR(new_popup_mask_next);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008584 VIM_CLEAR(new_popup_transparent);
Bram Moolenaar33796b32019-06-08 16:01:13 +02008585#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586 }
8587 else
8588 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008589 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008590
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591 for (new_row = 0; new_row < Rows; ++new_row)
8592 {
8593 new_LineOffset[new_row] = new_row * Columns;
8594 new_LineWraps[new_row] = FALSE;
8595
8596 /*
8597 * If the screen is not going to be cleared, copy as much as
8598 * possible from the old screen to the new one and clear the rest
8599 * (used when resizing the window at the "--more--" prompt or when
8600 * executing an external command, for the GUI).
8601 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008602 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008603 {
8604 (void)vim_memset(new_ScreenLines + new_row * Columns,
8605 ' ', (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606 if (enc_utf8)
8607 {
8608 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8609 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008610 for (i = 0; i < p_mco; ++i)
8611 (void)vim_memset(new_ScreenLinesC[i]
8612 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613 0, (size_t)Columns * sizeof(u8char_T));
8614 }
8615 if (enc_dbcs == DBCS_JPNU)
8616 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8617 0, (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8619 0, (size_t)Columns * sizeof(sattr_T));
8620 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008621 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622 {
8623 if (screen_Columns < Columns)
8624 len = screen_Columns;
8625 else
8626 len = Columns;
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008627 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008628 * may be invalid now. Also when p_mco changes. */
8629 if (!(enc_utf8 && ScreenLinesUC == NULL)
8630 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008631 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8632 ScreenLines + LineOffset[old_row],
8633 (size_t)len * sizeof(schar_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008634 if (enc_utf8 && ScreenLinesUC != NULL
8635 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636 {
8637 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8638 ScreenLinesUC + LineOffset[old_row],
8639 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008640 for (i = 0; i < p_mco; ++i)
8641 mch_memmove(new_ScreenLinesC[i]
8642 + new_LineOffset[new_row],
8643 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644 (size_t)len * sizeof(u8char_T));
8645 }
8646 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8647 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8648 ScreenLines2 + LineOffset[old_row],
8649 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8651 ScreenAttrs + LineOffset[old_row],
8652 (size_t)len * sizeof(sattr_T));
8653 }
8654 }
8655 }
8656 /* Use the last line of the screen for the current line. */
8657 current_ScreenLine = new_ScreenLines + Rows * Columns;
Bram Moolenaar6ace95e2019-08-13 23:09:49 +02008658
8659#ifdef FEAT_TEXT_PROP
8660 vim_memset(new_popup_mask, 0, Rows * Columns * sizeof(short));
8661 vim_memset(new_popup_transparent, 0, Rows * Columns * sizeof(char));
8662#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663 }
8664
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008665 free_screenlines();
8666
Bram Moolenaar6ace95e2019-08-13 23:09:49 +02008667 // NOTE: this may result in all pointers to become NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668 ScreenLines = new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008669 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008670 for (i = 0; i < p_mco; ++i)
8671 ScreenLinesC[i] = new_ScreenLinesC[i];
8672 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008673 ScreenLines2 = new_ScreenLines2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674 ScreenAttrs = new_ScreenAttrs;
8675 LineOffset = new_LineOffset;
8676 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008677 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar33796b32019-06-08 16:01:13 +02008678#ifdef FEAT_TEXT_PROP
8679 popup_mask = new_popup_mask;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008680 popup_mask_next = new_popup_mask_next;
8681 popup_transparent = new_popup_transparent;
Bram Moolenaar33796b32019-06-08 16:01:13 +02008682 popup_mask_refresh = TRUE;
8683#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684
8685 /* It's important that screen_Rows and screen_Columns reflect the actual
8686 * size of ScreenLines[]. Set them before calling anything. */
8687#ifdef FEAT_GUI
8688 old_Rows = screen_Rows;
8689#endif
8690 screen_Rows = Rows;
8691 screen_Columns = Columns;
8692
8693 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008694 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695 screenclear2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696#ifdef FEAT_GUI
8697 else if (gui.in_use
8698 && !gui.starting
8699 && ScreenLines != NULL
8700 && old_Rows != Rows)
8701 {
8702 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8703 /*
8704 * Adjust the position of the cursor, for when executing an external
8705 * command.
8706 */
8707 if (msg_row >= Rows) /* Rows got smaller */
8708 msg_row = Rows - 1; /* put cursor at last row */
8709 else if (Rows > old_Rows) /* Rows got bigger */
8710 msg_row += Rows - old_Rows; /* put cursor in same place */
8711 if (msg_col >= Columns) /* Columns got smaller */
8712 msg_col = Columns - 1; /* put cursor at last column */
8713 }
8714#endif
Bram Moolenaarca57ab52019-04-13 14:53:16 +02008715 clear_TabPageIdxs();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008718 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008719
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008720 /*
8721 * Do not apply autocommands more than 3 times to avoid an endless loop
8722 * in case applying autocommands always changes Rows or Columns.
8723 */
8724 if (starting == 0 && ++retry_count <= 3)
8725 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008726 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008727 /* In rare cases, autocommands may have altered Rows or Columns,
8728 * jump back to check if we need to allocate the screen again. */
8729 goto retry;
8730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731}
8732
8733 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008734free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008735{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008736 int i;
8737
Bram Moolenaar33796b32019-06-08 16:01:13 +02008738 VIM_CLEAR(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008739 for (i = 0; i < Screen_mco; ++i)
Bram Moolenaar33796b32019-06-08 16:01:13 +02008740 VIM_CLEAR(ScreenLinesC[i]);
8741 VIM_CLEAR(ScreenLines2);
8742 VIM_CLEAR(ScreenLines);
8743 VIM_CLEAR(ScreenAttrs);
8744 VIM_CLEAR(LineOffset);
8745 VIM_CLEAR(LineWraps);
8746 VIM_CLEAR(TabPageIdxs);
8747#ifdef FEAT_TEXT_PROP
8748 VIM_CLEAR(popup_mask);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02008749 VIM_CLEAR(popup_mask_next);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008750 VIM_CLEAR(popup_transparent);
Bram Moolenaar33796b32019-06-08 16:01:13 +02008751#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008752}
8753
8754 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008755screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008756{
8757 check_for_delay(FALSE);
8758 screenalloc(FALSE); /* allocate screen buffers if size changed */
8759 screenclear2(); /* clear the screen */
8760}
8761
8762 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008763screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764{
8765 int i;
8766
8767 if (starting == NO_SCREEN || ScreenLines == NULL
8768#ifdef FEAT_GUI
8769 || (gui.in_use && gui.starting)
8770#endif
8771 )
8772 return;
8773
8774#ifdef FEAT_GUI
8775 if (!gui.in_use)
8776#endif
8777 screen_attr = -1; /* force setting the Normal colors */
8778 screen_stop_highlight(); /* don't want highlighting here */
8779
8780#ifdef FEAT_CLIPBOARD
8781 /* disable selection without redrawing it */
8782 clip_scroll_selection(9999);
8783#endif
8784
8785 /* blank out ScreenLines */
8786 for (i = 0; i < Rows; ++i)
8787 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02008788 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789 LineWraps[i] = FALSE;
8790 }
8791
8792 if (can_clear(T_CL))
8793 {
8794 out_str(T_CL); /* clear the display */
8795 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008796 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008797 }
8798 else
8799 {
8800 /* can't clear the screen, mark all chars with invalid attributes */
8801 for (i = 0; i < Rows; ++i)
8802 lineinvalid(LineOffset[i], (int)Columns);
8803 clear_cmdline = TRUE;
8804 }
8805
8806 screen_cleared = TRUE; /* can use contents of ScreenLines now */
8807
8808 win_rest_invalid(firstwin);
8809 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008810 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811 if (must_redraw == CLEAR) /* no need to clear again */
8812 must_redraw = NOT_VALID;
8813 compute_cmdrow();
8814 msg_row = cmdline_row; /* put cursor on last line for messages */
8815 msg_col = 0;
8816 screen_start(); /* don't know where cursor is now */
8817 msg_scrolled = 0; /* can't scroll back */
8818 msg_didany = FALSE;
8819 msg_didout = FALSE;
8820}
8821
8822/*
8823 * Clear one line in ScreenLines.
8824 */
8825 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02008826lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827{
8828 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829 if (enc_utf8)
8830 (void)vim_memset(ScreenLinesUC + off, 0,
8831 (size_t)width * sizeof(u8char_T));
Bram Moolenaarcfce7172017-08-17 20:31:48 +02008832 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833}
8834
8835/*
8836 * Mark one line in ScreenLines invalid by setting the attributes to an
8837 * invalid value.
8838 */
8839 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008840lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008841{
8842 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
8843}
8844
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845/*
8846 * Copy part of a Screenline for vertically split window "wp".
8847 */
8848 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008849linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850{
8851 unsigned off_to = LineOffset[to] + wp->w_wincol;
8852 unsigned off_from = LineOffset[from] + wp->w_wincol;
8853
8854 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
8855 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856 if (enc_utf8)
8857 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008858 int i;
8859
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
8861 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008862 for (i = 0; i < p_mco; ++i)
8863 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
8864 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865 }
8866 if (enc_dbcs == DBCS_JPNU)
8867 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
8868 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
8870 wp->w_width * sizeof(sattr_T));
8871}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872
8873/*
8874 * Return TRUE if clearing with term string "p" would work.
8875 * It can't work when the string is empty or it won't set the right background.
Bram Moolenaar33796b32019-06-08 16:01:13 +02008876 * Don't clear to end-of-line when there are popups, it may cause flicker.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877 */
8878 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008879can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880{
8881 return (*p != NUL && (t_colors <= 1
8882#ifdef FEAT_GUI
8883 || gui.in_use
8884#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008885#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008886 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02008887 || (!p_tgc && cterm_normal_bg_color == 0)
8888#else
8889 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008890#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02008891 || *T_UT != NUL)
8892#ifdef FEAT_TEXT_PROP
8893 && !(p == T_CE && popup_visible)
8894#endif
8895 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00008896}
8897
8898/*
8899 * Reset cursor position. Use whenever cursor was moved because of outputting
8900 * something directly to the screen (shell commands) or a terminal control
8901 * code.
8902 */
8903 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008904screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008905{
8906 screen_cur_row = screen_cur_col = 9999;
8907}
8908
8909/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008910 * Move the cursor to position "row","col" in the screen.
8911 * This tries to find the most efficient way to move, minimizing the number of
8912 * characters sent to the terminal.
8913 */
8914 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008915windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008917 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918 int i;
8919 int plan;
8920 int cost;
8921 int wouldbe_col;
8922 int noinvcurs;
8923 char_u *bs;
8924 int goto_cost;
8925 int attr;
8926
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008927#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
8929
8930#define PLAN_LE 1
8931#define PLAN_CR 2
8932#define PLAN_NL 3
8933#define PLAN_WRITE 4
8934 /* Can't use ScreenLines unless initialized */
8935 if (ScreenLines == NULL)
8936 return;
8937
8938 if (col != screen_cur_col || row != screen_cur_row)
8939 {
8940 /* Check for valid position. */
8941 if (row < 0) /* window without text lines? */
8942 row = 0;
8943 if (row >= screen_Rows)
8944 row = screen_Rows - 1;
8945 if (col >= screen_Columns)
8946 col = screen_Columns - 1;
8947
8948 /* check if no cursor movement is allowed in highlight mode */
8949 if (screen_attr && *T_MS == NUL)
8950 noinvcurs = HIGHL_COST;
8951 else
8952 noinvcurs = 0;
8953 goto_cost = GOTO_COST + noinvcurs;
8954
8955 /*
8956 * Plan how to do the positioning:
8957 * 1. Use CR to move it to column 0, same row.
8958 * 2. Use T_LE to move it a few columns to the left.
8959 * 3. Use NL to move a few lines down, column 0.
8960 * 4. Move a few columns to the right with T_ND or by writing chars.
8961 *
8962 * Don't do this if the cursor went beyond the last column, the cursor
8963 * position is unknown then (some terminals wrap, some don't )
8964 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008965 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00008966 * characters to move the cursor to the right.
8967 */
8968 if (row >= screen_cur_row && screen_cur_col < Columns)
8969 {
8970 /*
8971 * If the cursor is in the same row, bigger col, we can use CR
8972 * or T_LE.
8973 */
8974 bs = NULL; /* init for GCC */
8975 attr = screen_attr;
8976 if (row == screen_cur_row && col < screen_cur_col)
8977 {
8978 /* "le" is preferred over "bc", because "bc" is obsolete */
8979 if (*T_LE)
8980 bs = T_LE; /* "cursor left" */
8981 else
8982 bs = T_BC; /* "backspace character (old) */
8983 if (*bs)
8984 cost = (screen_cur_col - col) * (int)STRLEN(bs);
8985 else
8986 cost = 999;
8987 if (col + 1 < cost) /* using CR is less characters */
8988 {
8989 plan = PLAN_CR;
8990 wouldbe_col = 0;
8991 cost = 1; /* CR is just one character */
8992 }
8993 else
8994 {
8995 plan = PLAN_LE;
8996 wouldbe_col = col;
8997 }
8998 if (noinvcurs) /* will stop highlighting */
8999 {
9000 cost += noinvcurs;
9001 attr = 0;
9002 }
9003 }
9004
9005 /*
9006 * If the cursor is above where we want to be, we can use CR LF.
9007 */
9008 else if (row > screen_cur_row)
9009 {
9010 plan = PLAN_NL;
9011 wouldbe_col = 0;
9012 cost = (row - screen_cur_row) * 2; /* CR LF */
9013 if (noinvcurs) /* will stop highlighting */
9014 {
9015 cost += noinvcurs;
9016 attr = 0;
9017 }
9018 }
9019
9020 /*
9021 * If the cursor is in the same row, smaller col, just use write.
9022 */
9023 else
9024 {
9025 plan = PLAN_WRITE;
9026 wouldbe_col = screen_cur_col;
9027 cost = 0;
9028 }
9029
9030 /*
9031 * Check if any characters that need to be written have the
9032 * correct attributes. Also avoid UTF-8 characters.
9033 */
9034 i = col - wouldbe_col;
9035 if (i > 0)
9036 cost += i;
9037 if (cost < goto_cost && i > 0)
9038 {
9039 /*
9040 * Check if the attributes are correct without additionally
9041 * stopping highlighting.
9042 */
9043 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9044 while (i && *p++ == attr)
9045 --i;
9046 if (i != 0)
9047 {
9048 /*
9049 * Try if it works when highlighting is stopped here.
9050 */
9051 if (*--p == 0)
9052 {
9053 cost += noinvcurs;
9054 while (i && *p++ == 0)
9055 --i;
9056 }
9057 if (i != 0)
9058 cost = 999; /* different attributes, don't do it */
9059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009060 if (enc_utf8)
9061 {
9062 /* Don't use an UTF-8 char for positioning, it's slow. */
9063 for (i = wouldbe_col; i < col; ++i)
9064 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9065 {
9066 cost = 999;
9067 break;
9068 }
9069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070 }
9071
9072 /*
9073 * We can do it without term_windgoto()!
9074 */
9075 if (cost < goto_cost)
9076 {
9077 if (plan == PLAN_LE)
9078 {
9079 if (noinvcurs)
9080 screen_stop_highlight();
9081 while (screen_cur_col > col)
9082 {
9083 out_str(bs);
9084 --screen_cur_col;
9085 }
9086 }
9087 else if (plan == PLAN_CR)
9088 {
9089 if (noinvcurs)
9090 screen_stop_highlight();
9091 out_char('\r');
9092 screen_cur_col = 0;
9093 }
9094 else if (plan == PLAN_NL)
9095 {
9096 if (noinvcurs)
9097 screen_stop_highlight();
9098 while (screen_cur_row < row)
9099 {
9100 out_char('\n');
9101 ++screen_cur_row;
9102 }
9103 screen_cur_col = 0;
9104 }
9105
9106 i = col - screen_cur_col;
9107 if (i > 0)
9108 {
9109 /*
9110 * Use cursor-right if it's one character only. Avoids
9111 * removing a line of pixels from the last bold char, when
9112 * using the bold trick in the GUI.
9113 */
9114 if (T_ND[0] != NUL && T_ND[1] == NUL)
9115 {
9116 while (i-- > 0)
9117 out_char(*T_ND);
9118 }
9119 else
9120 {
9121 int off;
9122
9123 off = LineOffset[row] + screen_cur_col;
9124 while (i-- > 0)
9125 {
9126 if (ScreenAttrs[off] != screen_attr)
9127 screen_stop_highlight();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009129 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009130 if (enc_dbcs == DBCS_JPNU
9131 && ScreenLines[off] == 0x8e)
9132 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009133 ++off;
9134 }
9135 }
9136 }
9137 }
9138 }
9139 else
9140 cost = 999;
9141
9142 if (cost >= goto_cost)
9143 {
9144 if (noinvcurs)
9145 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009146 if (row == screen_cur_row && (col > screen_cur_col)
9147 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009148 term_cursor_right(col - screen_cur_col);
9149 else
9150 term_windgoto(row, col);
9151 }
9152 screen_cur_row = row;
9153 screen_cur_col = col;
9154 }
9155}
9156
9157/*
9158 * Set cursor to its position in the current window.
9159 */
9160 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009161setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009162{
Bram Moolenaar987723e2018-03-06 11:43:04 +01009163 setcursor_mayforce(FALSE);
9164}
9165
9166/*
9167 * Set cursor to its position in the current window.
9168 * When "force" is TRUE also when not redrawing.
9169 */
9170 void
9171setcursor_mayforce(int force)
9172{
9173 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009174 {
9175 validate_cursor();
9176 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009177 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009178#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009179 /* With 'rightleft' set and the cursor on a double-wide
9180 * character, position it on the leftmost column. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01009181 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol
9182 - ((has_mbyte
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009183 && (*mb_ptr2cells)(ml_get_cursor()) == 2
Bram Moolenaara12a1612019-01-24 16:39:02 +01009184 && vim_isprintc(gchar_cursor())) ? 2 : 1)) :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185#endif
9186 curwin->w_wcol));
9187 }
9188}
9189
9190
9191/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009192 * Insert 'line_count' lines at 'row' in window 'wp'.
9193 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9194 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195 * scrolling.
9196 * Returns FAIL if the lines are not inserted, OK for success.
9197 */
9198 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009199win_ins_lines(
9200 win_T *wp,
9201 int row,
9202 int line_count,
9203 int invalid,
9204 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009205{
9206 int did_delete;
9207 int nextrow;
9208 int lastrow;
9209 int retval;
9210
9211 if (invalid)
9212 wp->w_lines_valid = 0;
9213
9214 if (wp->w_height < 5)
9215 return FAIL;
9216
9217 if (line_count > wp->w_height - row)
9218 line_count = wp->w_height - row;
9219
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009220 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009221 if (retval != MAYBE)
9222 return retval;
9223
9224 /*
9225 * If there is a next window or a status line, we first try to delete the
9226 * lines at the bottom to avoid messing what is after the window.
Bram Moolenaarc363fe12019-08-04 18:13:46 +02009227 * If this fails and there are following windows, don't do anything to
9228 * avoid messing up those windows, better just redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009229 */
9230 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009231 if (wp->w_next != NULL || wp->w_status_height)
9232 {
9233 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009234 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009235 did_delete = TRUE;
9236 else if (wp->w_next)
9237 return FAIL;
9238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009239 /*
9240 * if no lines deleted, blank the lines that will end up below the window
9241 */
9242 if (!did_delete)
9243 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009244 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009245 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02009246 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009247 lastrow = nextrow + line_count;
9248 if (lastrow > Rows)
9249 lastrow = Rows;
9250 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009251 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009252 ' ', ' ', 0);
9253 }
9254
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009255 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009256 == FAIL)
9257 {
Bram Moolenaarc363fe12019-08-04 18:13:46 +02009258 // deletion will have messed up other windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00009259 if (did_delete)
9260 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009261 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009262 win_rest_invalid(W_NEXT(wp));
9263 }
9264 return FAIL;
9265 }
9266
9267 return OK;
9268}
9269
9270/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009271 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009272 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9273 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9274 * scrolling
9275 * Return OK for success, FAIL if the lines are not deleted.
9276 */
9277 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009278win_del_lines(
9279 win_T *wp,
9280 int row,
9281 int line_count,
9282 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009283 int mayclear,
9284 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009285{
9286 int retval;
9287
9288 if (invalid)
9289 wp->w_lines_valid = 0;
9290
9291 if (line_count > wp->w_height - row)
9292 line_count = wp->w_height - row;
9293
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009294 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009295 if (retval != MAYBE)
9296 return retval;
9297
9298 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009299 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009300 return FAIL;
9301
Bram Moolenaar071d4272004-06-13 20:20:40 +00009302 /*
9303 * If there are windows or status lines below, try to put them at the
9304 * correct place. If we can't do that, they have to be redrawn.
9305 */
9306 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9307 {
9308 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009309 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009310 {
9311 wp->w_redr_status = TRUE;
9312 win_rest_invalid(wp->w_next);
9313 }
9314 }
9315 /*
9316 * If this is the last window and there is no status line, redraw the
9317 * command line later.
9318 */
9319 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009320 redraw_cmdline = TRUE;
9321 return OK;
9322}
9323
9324/*
9325 * Common code for win_ins_lines() and win_del_lines().
9326 * Returns OK or FAIL when the work has been done.
9327 * Returns MAYBE when not finished yet.
9328 */
9329 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009330win_do_lines(
9331 win_T *wp,
9332 int row,
9333 int line_count,
9334 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009335 int del,
9336 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009337{
9338 int retval;
9339
9340 if (!redrawing() || line_count <= 0)
9341 return FAIL;
9342
Bram Moolenaar33796b32019-06-08 16:01:13 +02009343 // When inserting lines would result in loss of command output, just redraw
9344 // the lines.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009345 if (no_win_do_lines_ins && !del)
9346 return FAIL;
9347
Bram Moolenaar33796b32019-06-08 16:01:13 +02009348 // only a few lines left: redraw is faster
Bram Moolenaar4033c552017-09-16 20:54:51 +02009349 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009350 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009351 if (!no_win_do_lines_ins)
Bram Moolenaar33796b32019-06-08 16:01:13 +02009352 screenclear(); // will set wp->w_lines_valid to 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00009353 return FAIL;
9354 }
9355
Bram Moolenaar33796b32019-06-08 16:01:13 +02009356#ifdef FEAT_TEXT_PROP
Bram Moolenaar4c063a02019-06-10 21:24:12 +02009357 // this doesn't work when there are popups visible
Bram Moolenaar33796b32019-06-08 16:01:13 +02009358 if (popup_visible)
9359 return FAIL;
9360#endif
9361
9362 // Delete all remaining lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00009363 if (row + line_count >= wp->w_height)
9364 {
9365 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009366 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009367 ' ', ' ', 0);
9368 return OK;
9369 }
9370
9371 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009372 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009373 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009374 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009375 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009376 if (!no_win_do_lines_ins)
9377 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009378
9379 /*
9380 * If the terminal can set a scroll region, use that.
9381 * Always do this in a vertically split window. This will redraw from
9382 * ScreenLines[] when t_CV isn't defined. That's faster than using
9383 * win_line().
9384 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009385 * a character in the lower right corner of the scroll region may cause a
9386 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009387 */
Bram Moolenaar02631462017-09-22 15:20:32 +02009388 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009389 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009390 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009391 scroll_region_set(wp, row);
9392 if (del)
9393 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009394 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009395 else
9396 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009397 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009398 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009399 scroll_region_reset();
9400 return retval;
9401 }
9402
Bram Moolenaar071d4272004-06-13 20:20:40 +00009403 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9404 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009405
9406 return MAYBE;
9407}
9408
9409/*
9410 * window 'wp' and everything after it is messed up, mark it for redraw
9411 */
9412 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009413win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009414{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009415 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009416 {
9417 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009418 wp->w_redr_status = TRUE;
9419 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009420 }
9421 redraw_cmdline = TRUE;
9422}
9423
9424/*
9425 * The rest of the routines in this file perform screen manipulations. The
9426 * given operation is performed physically on the screen. The corresponding
9427 * change is also made to the internal screen image. In this way, the editor
9428 * anticipates the effect of editing changes on the appearance of the screen.
9429 * That way, when we call screenupdate a complete redraw isn't usually
9430 * necessary. Another advantage is that we can keep adding code to anticipate
9431 * screen changes, and in the meantime, everything still works.
9432 */
9433
9434/*
9435 * types for inserting or deleting lines
9436 */
9437#define USE_T_CAL 1
9438#define USE_T_CDL 2
9439#define USE_T_AL 3
9440#define USE_T_CE 4
9441#define USE_T_DL 5
9442#define USE_T_SR 6
9443#define USE_NL 7
9444#define USE_T_CD 8
9445#define USE_REDRAW 9
9446
9447/*
9448 * insert lines on the screen and update ScreenLines[]
9449 * 'end' is the line after the scrolled part. Normally it is Rows.
9450 * When scrolling region used 'off' is the offset from the top for the region.
9451 * 'row' and 'end' are relative to the start of the region.
9452 *
9453 * return FAIL for failure, OK for success.
9454 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009455 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009456screen_ins_lines(
9457 int off,
9458 int row,
9459 int line_count,
9460 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009461 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01009462 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009463{
9464 int i;
9465 int j;
9466 unsigned temp;
9467 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009468 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469 int type;
9470 int result_empty;
9471 int can_ce = can_clear(T_CE);
9472
9473 /*
9474 * FAIL if
9475 * - there is no valid screen
9476 * - the screen has to be redrawn completely
9477 * - the line count is less than one
9478 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009479 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar33796b32019-06-08 16:01:13 +02009480 * - there is a popup window
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481 */
Bram Moolenaar33796b32019-06-08 16:01:13 +02009482 if (!screen_valid(TRUE)
9483 || line_count <= 0 || line_count > p_ttyscroll
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009484#ifdef FEAT_CLIPBOARD
9485 || (clip_star.state != SELECT_CLEARED
9486 && redrawing_for_callback > 0)
9487#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02009488#ifdef FEAT_TEXT_PROP
9489 || popup_visible
9490#endif
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009491 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492 return FAIL;
9493
9494 /*
9495 * There are seven ways to insert lines:
9496 * 0. When in a vertically split window and t_CV isn't set, redraw the
9497 * characters from ScreenLines[].
9498 * 1. Use T_CD (clear to end of display) if it exists and the result of
9499 * the insert is just empty lines
9500 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9501 * present or line_count > 1. It looks better if we do all the inserts
9502 * at once.
9503 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9504 * insert is just empty lines and T_CE is not present or line_count >
9505 * 1.
9506 * 4. Use T_AL (insert line) if it exists.
9507 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9508 * just empty lines.
9509 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9510 * just empty lines.
9511 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9512 * the 'da' flag is not set or we have clear line capability.
9513 * 8. redraw the characters from ScreenLines[].
9514 *
9515 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9516 * the scrollbar for the window. It does have insert line, use that if it
9517 * exists.
9518 */
9519 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009520 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9521 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009522 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009523 type = USE_T_CD;
9524 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9525 type = USE_T_CAL;
9526 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9527 type = USE_T_CDL;
9528 else if (*T_AL != NUL)
9529 type = USE_T_AL;
9530 else if (can_ce && result_empty)
9531 type = USE_T_CE;
9532 else if (*T_DL != NUL && result_empty)
9533 type = USE_T_DL;
9534 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9535 type = USE_T_SR;
9536 else
9537 return FAIL;
9538
9539 /*
9540 * For clearing the lines screen_del_lines() is used. This will also take
9541 * care of t_db if necessary.
9542 */
9543 if (type == USE_T_CD || type == USE_T_CDL ||
9544 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009545 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009546
9547 /*
9548 * If text is retained below the screen, first clear or delete as many
9549 * lines at the bottom of the window as are about to be inserted so that
9550 * the deleted lines won't later surface during a screen_del_lines.
9551 */
9552 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009553 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009554
9555#ifdef FEAT_CLIPBOARD
9556 /* Remove a modeless selection when inserting lines halfway the screen
9557 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009558 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009559 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009560 else
9561 clip_scroll_selection(-line_count);
9562#endif
9563
Bram Moolenaar071d4272004-06-13 20:20:40 +00009564#ifdef FEAT_GUI
9565 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9566 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009567 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009568#endif
9569
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009570 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
9571 cursor_col = wp->w_wincol;
9572
Bram Moolenaar071d4272004-06-13 20:20:40 +00009573 if (*T_CCS != NUL) /* cursor relative to region */
9574 cursor_row = row;
9575 else
9576 cursor_row = row + off;
9577
9578 /*
9579 * Shift LineOffset[] line_count down to reflect the inserted lines.
9580 * Clear the inserted lines in ScreenLines[].
9581 */
9582 row += off;
9583 end += off;
9584 for (i = 0; i < line_count; ++i)
9585 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009586 if (wp != NULL && wp->w_width != Columns)
9587 {
9588 /* need to copy part of a line */
9589 j = end - 1 - i;
9590 while ((j -= line_count) >= row)
9591 linecopy(j + line_count, j, wp);
9592 j += line_count;
9593 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009594 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
9595 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596 else
9597 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9598 LineWraps[j] = FALSE;
9599 }
9600 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601 {
9602 j = end - 1 - i;
9603 temp = LineOffset[j];
9604 while ((j -= line_count) >= row)
9605 {
9606 LineOffset[j + line_count] = LineOffset[j];
9607 LineWraps[j + line_count] = LineWraps[j];
9608 }
9609 LineOffset[j + line_count] = temp;
9610 LineWraps[j + line_count] = FALSE;
9611 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009612 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613 else
9614 lineinvalid(temp, (int)Columns);
9615 }
9616 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009617
9618 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009619 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009620 if (clear_attr != 0)
9621 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009622
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623 /* redraw the characters */
9624 if (type == USE_REDRAW)
9625 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02009626 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009627 {
9628 term_append_lines(line_count);
9629 screen_start(); /* don't know where cursor is now */
9630 }
9631 else
9632 {
9633 for (i = 0; i < line_count; i++)
9634 {
9635 if (type == USE_T_AL)
9636 {
9637 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009638 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009639 out_str(T_AL);
9640 }
9641 else /* type == USE_T_SR */
9642 out_str(T_SR);
9643 screen_start(); /* don't know where cursor is now */
9644 }
9645 }
9646
9647 /*
9648 * With scroll-reverse and 'da' flag set we need to clear the lines that
9649 * have been scrolled down into the region.
9650 */
9651 if (type == USE_T_SR && *T_DA)
9652 {
9653 for (i = 0; i < line_count; ++i)
9654 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009655 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009656 out_str(T_CE);
9657 screen_start(); /* don't know where cursor is now */
9658 }
9659 }
9660
9661#ifdef FEAT_GUI
9662 gui_can_update_cursor();
9663 if (gui.in_use)
9664 out_flush(); /* always flush after a scroll */
9665#endif
9666 return OK;
9667}
9668
9669/*
Bram Moolenaar107abd22016-08-12 14:08:25 +02009670 * Delete lines on the screen and update ScreenLines[].
9671 * "end" is the line after the scrolled part. Normally it is Rows.
9672 * When scrolling region used "off" is the offset from the top for the region.
9673 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009674 *
9675 * Return OK for success, FAIL if the lines are not deleted.
9676 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009678screen_del_lines(
9679 int off,
9680 int row,
9681 int line_count,
9682 int end,
9683 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009684 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +01009685 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009686{
9687 int j;
9688 int i;
9689 unsigned temp;
9690 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009691 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009692 int cursor_end;
9693 int result_empty; /* result is empty until end of region */
9694 int can_delete; /* deleting line codes can be used */
9695 int type;
9696
9697 /*
9698 * FAIL if
9699 * - there is no valid screen
9700 * - the screen has to be redrawn completely
9701 * - the line count is less than one
9702 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009703 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009704 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009705 if (!screen_valid(TRUE) || line_count <= 0
9706 || (!force && line_count > p_ttyscroll)
9707#ifdef FEAT_CLIPBOARD
9708 || (clip_star.state != SELECT_CLEARED
9709 && redrawing_for_callback > 0)
9710#endif
9711 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009712 return FAIL;
9713
9714 /*
9715 * Check if the rest of the current region will become empty.
9716 */
9717 result_empty = row + line_count >= end;
9718
9719 /*
9720 * We can delete lines only when 'db' flag not set or when 'ce' option
9721 * available.
9722 */
9723 can_delete = (*T_DB == NUL || can_clear(T_CE));
9724
9725 /*
9726 * There are six ways to delete lines:
9727 * 0. When in a vertically split window and t_CV isn't set, redraw the
9728 * characters from ScreenLines[].
9729 * 1. Use T_CD if it exists and the result is empty.
9730 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9731 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9732 * none of the other ways work.
9733 * 4. Use T_CE (erase line) if the result is empty.
9734 * 5. Use T_DL (delete line) if it exists.
9735 * 6. redraw the characters from ScreenLines[].
9736 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009737 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9738 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009739 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740 type = USE_T_CD;
9741#if defined(__BEOS__) && defined(BEOS_DR8)
9742 /*
9743 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9744 * its internal termcap... this works okay for tests which test *T_DB !=
9745 * NUL. It has the disadvantage that the user cannot use any :set t_*
9746 * command to get T_DB (back) to empty_option, only :set term=... will do
9747 * the trick...
9748 * Anyway, this hack will hopefully go away with the next OS release.
9749 * (Olaf Seibert)
9750 */
9751 else if (row == 0 && T_DB == empty_option
9752 && (line_count == 1 || *T_CDL == NUL))
9753#else
9754 else if (row == 0 && (
9755#ifndef AMIGA
9756 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9757 * up, so use delete-line command */
9758 line_count == 1 ||
9759#endif
9760 *T_CDL == NUL))
9761#endif
9762 type = USE_NL;
9763 else if (*T_CDL != NUL && line_count > 1 && can_delete)
9764 type = USE_T_CDL;
9765 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +02009766 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009767 type = USE_T_CE;
9768 else if (*T_DL != NUL && can_delete)
9769 type = USE_T_DL;
9770 else if (*T_CDL != NUL && can_delete)
9771 type = USE_T_CDL;
9772 else
9773 return FAIL;
9774
9775#ifdef FEAT_CLIPBOARD
9776 /* Remove a modeless selection when deleting lines halfway the screen or
9777 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009778 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009779 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780 else
9781 clip_scroll_selection(line_count);
9782#endif
9783
Bram Moolenaar071d4272004-06-13 20:20:40 +00009784#ifdef FEAT_GUI
9785 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9786 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009787 gui_dont_update_cursor(gui.cursor_row >= row + off
9788 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009789#endif
9790
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009791 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
9792 cursor_col = wp->w_wincol;
9793
Bram Moolenaar071d4272004-06-13 20:20:40 +00009794 if (*T_CCS != NUL) /* cursor relative to region */
9795 {
9796 cursor_row = row;
9797 cursor_end = end;
9798 }
9799 else
9800 {
9801 cursor_row = row + off;
9802 cursor_end = end + off;
9803 }
9804
9805 /*
9806 * Now shift LineOffset[] line_count up to reflect the deleted lines.
9807 * Clear the inserted lines in ScreenLines[].
9808 */
9809 row += off;
9810 end += off;
9811 for (i = 0; i < line_count; ++i)
9812 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009813 if (wp != NULL && wp->w_width != Columns)
9814 {
9815 /* need to copy part of a line */
9816 j = row + i;
9817 while ((j += line_count) <= end - 1)
9818 linecopy(j - line_count, j, wp);
9819 j -= line_count;
9820 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009821 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
9822 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009823 else
9824 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9825 LineWraps[j] = FALSE;
9826 }
9827 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009828 {
9829 /* whole width, moving the line pointers is faster */
9830 j = row + i;
9831 temp = LineOffset[j];
9832 while ((j += line_count) <= end - 1)
9833 {
9834 LineOffset[j - line_count] = LineOffset[j];
9835 LineWraps[j - line_count] = LineWraps[j];
9836 }
9837 LineOffset[j - line_count] = temp;
9838 LineWraps[j - line_count] = FALSE;
9839 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009840 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009841 else
9842 lineinvalid(temp, (int)Columns);
9843 }
9844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009845
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009846 if (screen_attr != clear_attr)
9847 screen_stop_highlight();
9848 if (clear_attr != 0)
9849 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009850
Bram Moolenaar071d4272004-06-13 20:20:40 +00009851 /* redraw the characters */
9852 if (type == USE_REDRAW)
9853 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02009854 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009855 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009856 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009857 out_str(T_CD);
9858 screen_start(); /* don't know where cursor is now */
9859 }
9860 else if (type == USE_T_CDL)
9861 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009862 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009863 term_delete_lines(line_count);
9864 screen_start(); /* don't know where cursor is now */
9865 }
9866 /*
9867 * Deleting lines at top of the screen or scroll region: Just scroll
9868 * the whole screen (scroll region) up by outputting newlines on the
9869 * last line.
9870 */
9871 else if (type == USE_NL)
9872 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009873 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009874 for (i = line_count; --i >= 0; )
9875 out_char('\n'); /* cursor will remain on same line */
9876 }
9877 else
9878 {
9879 for (i = line_count; --i >= 0; )
9880 {
9881 if (type == USE_T_DL)
9882 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009883 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009884 out_str(T_DL); /* delete a line */
9885 }
9886 else /* type == USE_T_CE */
9887 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009888 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009889 out_str(T_CE); /* erase a line */
9890 }
9891 screen_start(); /* don't know where cursor is now */
9892 }
9893 }
9894
9895 /*
9896 * If the 'db' flag is set, we need to clear the lines that have been
9897 * scrolled up at the bottom of the region.
9898 */
9899 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
9900 {
9901 for (i = line_count; i > 0; --i)
9902 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009903 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009904 out_str(T_CE); /* erase a line */
9905 screen_start(); /* don't know where cursor is now */
9906 }
9907 }
9908
9909#ifdef FEAT_GUI
9910 gui_can_update_cursor();
9911 if (gui.in_use)
9912 out_flush(); /* always flush after a scroll */
9913#endif
9914
9915 return OK;
9916}
9917
9918/*
Bram Moolenaarcb574f42019-01-25 22:29:57 +01009919 * Return TRUE when postponing displaying the mode message: when not redrawing
9920 * or inside a mapping.
9921 */
9922 int
9923skip_showmode()
9924{
9925 // Call char_avail() only when we are going to show something, because it
9926 // takes a bit of time. redrawing() may also call char_avail_avail().
9927 if (global_busy
9928 || msg_silent != 0
9929 || !redrawing()
9930 || (char_avail() && !KeyTyped))
9931 {
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02009932 redraw_mode = TRUE; // show mode later
Bram Moolenaarcb574f42019-01-25 22:29:57 +01009933 return TRUE;
9934 }
9935 return FALSE;
9936}
9937
9938/*
Bram Moolenaar81226e02018-02-20 21:44:45 +01009939 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009940 *
9941 * If clear_cmdline is TRUE, clear the rest of the cmdline.
9942 * If clear_cmdline is FALSE there may be a message there that needs to be
9943 * cleared only if a mode is shown.
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02009944 * If redraw_mode is TRUE show or clear the mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009945 * Return the length of the message (0 if no message).
9946 */
9947 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009948showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009949{
9950 int need_clear;
9951 int length = 0;
9952 int do_mode;
9953 int attr;
9954 int nwr_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009955 int sub_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009956
Bram Moolenaar7df351e2006-01-23 22:30:28 +00009957 do_mode = ((p_smd && msg_silent == 0)
9958 && ((State & INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +02009959 || restart_edit != NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009960 || VIsual_active));
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009961 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009962 {
Bram Moolenaarcb574f42019-01-25 22:29:57 +01009963 if (skip_showmode())
9964 return 0; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00009965
9966 nwr_save = need_wait_return;
9967
9968 /* wait a bit before overwriting an important message */
9969 check_for_delay(FALSE);
9970
9971 /* if the cmdline is more than one line high, erase top lines */
9972 need_clear = clear_cmdline;
9973 if (clear_cmdline && cmdline_row < Rows - 1)
9974 msg_clr_cmdline(); /* will reset clear_cmdline */
9975
9976 /* Position on the last line in the window, column 0 */
9977 msg_pos_mode();
9978 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +01009979 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009980 if (do_mode)
9981 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01009982 msg_puts_attr("--", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009983#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00009984 if (
Bram Moolenaar09092152010-08-08 16:38:42 +02009985# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +00009986 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +02009987# else
Bram Moolenaarc236c162008-07-13 17:41:49 +00009988 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +00009989# endif
Bram Moolenaar09092152010-08-08 16:38:42 +02009990 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02009991# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar32526b32019-01-19 17:43:09 +01009992 msg_puts_attr(" IM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009993# else
Bram Moolenaar32526b32019-01-19 17:43:09 +01009994 msg_puts_attr(" XIM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009995# endif
9996#endif
9997#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
9998 if (gui.in_use)
9999 {
10000 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010001 {
10002 /* HANGUL */
10003 if (enc_utf8)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010004 msg_puts_attr(" \355\225\234\352\270\200", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010005 else
Bram Moolenaar32526b32019-01-19 17:43:09 +010010006 msg_puts_attr(" \307\321\261\333", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010007 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010008 }
10009#endif
Bram Moolenaarea389e92014-05-28 21:40:52 +020010010 /* CTRL-X in Insert mode */
10011 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010012 {
10013 /* These messages can get long, avoid a wrap in a narrow
10014 * window. Prefer showing edit_submode_extra. */
10015 length = (Rows - msg_row) * Columns - 3;
10016 if (edit_submode_extra != NULL)
10017 length -= vim_strsize(edit_submode_extra);
10018 if (length > 0)
10019 {
10020 if (edit_submode_pre != NULL)
10021 length -= vim_strsize(edit_submode_pre);
10022 if (length - vim_strsize(edit_submode) > 0)
10023 {
10024 if (edit_submode_pre != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010025 msg_puts_attr((char *)edit_submode_pre, attr);
10026 msg_puts_attr((char *)edit_submode, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010027 }
10028 if (edit_submode_extra != NULL)
10029 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010030 msg_puts_attr(" ", attr); /* add a space in between */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010031 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010032 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010033 else
10034 sub_attr = attr;
Bram Moolenaar32526b32019-01-19 17:43:09 +010010035 msg_puts_attr((char *)edit_submode_extra, sub_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010036 }
10037 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010038 }
10039 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010040 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010041 if (State & VREPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010042 msg_puts_attr(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010043 else if (State & REPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010044 msg_puts_attr(_(" REPLACE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010045 else if (State & INSERT)
10046 {
10047#ifdef FEAT_RIGHTLEFT
10048 if (p_ri)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010049 msg_puts_attr(_(" REVERSE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010050#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +010010051 msg_puts_attr(_(" INSERT"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010052 }
Bram Moolenaar942b4542018-06-17 16:23:34 +020010053 else if (restart_edit == 'I' || restart_edit == 'A')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010054 msg_puts_attr(_(" (insert)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010055 else if (restart_edit == 'R')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010056 msg_puts_attr(_(" (replace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057 else if (restart_edit == 'V')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010058 msg_puts_attr(_(" (vreplace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010059#ifdef FEAT_RIGHTLEFT
10060 if (p_hkmap)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010061 msg_puts_attr(_(" Hebrew"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010062#endif
10063#ifdef FEAT_KEYMAP
10064 if (State & LANGMAP)
10065 {
10066# ifdef FEAT_ARABIC
10067 if (curwin->w_p_arab)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010068 msg_puts_attr(_(" Arabic"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010069 else
10070# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010071 if (get_keymap_str(curwin, (char_u *)" (%s)",
10072 NameBuff, MAXPATHL))
Bram Moolenaar32526b32019-01-19 17:43:09 +010010073 msg_puts_attr((char *)NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010074 }
10075#endif
10076 if ((State & INSERT) && p_paste)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010077 msg_puts_attr(_(" (paste)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010078
Bram Moolenaar071d4272004-06-13 20:20:40 +000010079 if (VIsual_active)
10080 {
10081 char *p;
10082
10083 /* Don't concatenate separate words to avoid translation
10084 * problems. */
10085 switch ((VIsual_select ? 4 : 0)
10086 + (VIsual_mode == Ctrl_V) * 2
10087 + (VIsual_mode == 'V'))
10088 {
10089 case 0: p = N_(" VISUAL"); break;
10090 case 1: p = N_(" VISUAL LINE"); break;
10091 case 2: p = N_(" VISUAL BLOCK"); break;
10092 case 4: p = N_(" SELECT"); break;
10093 case 5: p = N_(" SELECT LINE"); break;
10094 default: p = N_(" SELECT BLOCK"); break;
10095 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010096 msg_puts_attr(_(p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010097 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010098 msg_puts_attr(" --", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010099 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010100
Bram Moolenaar071d4272004-06-13 20:20:40 +000010101 need_clear = TRUE;
10102 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010103 if (reg_recording != 0
Bram Moolenaare2c453d2019-08-21 14:37:09 +020010104 && edit_submode == NULL) // otherwise it gets too long
Bram Moolenaar071d4272004-06-13 20:20:40 +000010105 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010106 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010107 need_clear = TRUE;
10108 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010109
10110 mode_displayed = TRUE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010111 if (need_clear || clear_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010112 msg_clr_eos();
10113 msg_didout = FALSE; /* overwrite this message */
10114 length = msg_col;
10115 msg_col = 0;
10116 need_wait_return = nwr_save; /* never ask for hit-return for this */
10117 }
10118 else if (clear_cmdline && msg_silent == 0)
10119 /* Clear the whole command line. Will reset "clear_cmdline". */
10120 msg_clr_cmdline();
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010121 else if (redraw_mode)
10122 {
10123 msg_pos_mode();
10124 msg_clr_eos();
10125 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010126
10127#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010128 /* In Visual mode the size of the selected area must be redrawn. */
10129 if (VIsual_active)
10130 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010131
10132 /* If the last window has no status line, the ruler is after the mode
10133 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010134 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +020010135 win_redr_ruler(lastwin, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010136#endif
10137 redraw_cmdline = FALSE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010138 redraw_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010139 clear_cmdline = FALSE;
10140
10141 return length;
10142}
10143
10144/*
10145 * Position for a mode message.
10146 */
10147 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010148msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010149{
10150 msg_col = 0;
10151 msg_row = Rows - 1;
10152}
10153
10154/*
10155 * Delete mode message. Used when ESC is typed which is expected to end
10156 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010157 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010158 */
10159 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010160unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010161{
10162 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010163 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010164 */
10165 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10166 redraw_cmdline = TRUE; /* delete mode later */
10167 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010168 clearmode();
10169}
10170
10171/*
10172 * Clear the mode message.
10173 */
10174 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010175clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010176{
Bram Moolenaar2abad542018-05-19 14:43:45 +020010177 int save_msg_row = msg_row;
10178 int save_msg_col = msg_col;
10179
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010180 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010181 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010182 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010183 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +020010184
10185 msg_col = save_msg_col;
10186 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010187}
10188
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010189 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010190recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010191{
Bram Moolenaar32526b32019-01-19 17:43:09 +010010192 msg_puts_attr(_("recording"), attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010193 if (!shortmess(SHM_RECORDING))
10194 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010195 char s[4];
10196
10197 sprintf(s, " @%c", reg_recording);
10198 msg_puts_attr(s, attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010199 }
10200}
10201
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010202/*
10203 * Draw the tab pages line at the top of the Vim window.
10204 */
Bram Moolenaare12bab32019-01-08 22:02:56 +010010205 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010206draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010207{
10208 int tabcount = 0;
10209 tabpage_T *tp;
10210 int tabwidth;
10211 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010212 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010213 int attr;
10214 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010215 win_T *cwp;
10216 int wincount;
10217 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010218 int c;
10219 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010220 int attr_sel = HL_ATTR(HLF_TPS);
10221 int attr_nosel = HL_ATTR(HLF_TP);
10222 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010223 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010224 int room;
10225 int use_sep_chars = (t_colors < 8
10226#ifdef FEAT_GUI
10227 && !gui.in_use
10228#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010229#ifdef FEAT_TERMGUICOLORS
10230 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010231#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010232 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010233
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010234 if (ScreenLines == NULL)
10235 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010236 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010237
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010238#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010239 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010240 if (gui_use_tabline())
10241 {
10242 gui_update_tabline();
10243 return;
10244 }
10245#endif
10246
10247 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010248 return;
10249
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010250#if defined(FEAT_STL_OPT)
Bram Moolenaarca57ab52019-04-13 14:53:16 +020010251 clear_TabPageIdxs();
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010252
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010253 /* Use the 'tabline' option if it's set. */
10254 if (*p_tal != NUL)
10255 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010256 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010257
10258 /* Check for an error. If there is one we would loop in redrawing the
10259 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010260 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010261 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010262 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010263 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010264 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010265 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010266 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010267 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010268#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010269 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010270 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010271 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010272
Bram Moolenaar238a5642006-02-21 22:12:05 +000010273 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10274 if (tabwidth < 6)
10275 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010276
Bram Moolenaar238a5642006-02-21 22:12:05 +000010277 attr = attr_nosel;
10278 tabcount = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010279 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10280 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010281 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010282 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010283
Bram Moolenaar238a5642006-02-21 22:12:05 +000010284 if (tp->tp_topframe == topframe)
10285 attr = attr_sel;
10286 if (use_sep_chars && col > 0)
10287 screen_putchar('|', 0, col++, attr);
10288
10289 if (tp->tp_topframe != topframe)
10290 attr = attr_nosel;
10291
10292 screen_putchar(' ', 0, col++, attr);
10293
10294 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010295 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010296 cwp = curwin;
10297 wp = firstwin;
10298 }
10299 else
10300 {
10301 cwp = tp->tp_curwin;
10302 wp = tp->tp_firstwin;
10303 }
10304
10305 modified = FALSE;
10306 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10307 if (bufIsChanged(wp->w_buffer))
10308 modified = TRUE;
10309 if (modified || wincount > 1)
10310 {
10311 if (wincount > 1)
10312 {
10313 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010314 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010315 if (col + len >= Columns - 3)
10316 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010317 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010318#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010319 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010320#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010321 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010322#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010323 );
10324 col += len;
10325 }
10326 if (modified)
10327 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10328 screen_putchar(' ', 0, col++, attr);
10329 }
10330
10331 room = scol - col + tabwidth - 1;
10332 if (room > 0)
10333 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010334 /* Get buffer name in NameBuff[] */
10335 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010336 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010337 len = vim_strsize(NameBuff);
10338 p = NameBuff;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010339 if (has_mbyte)
10340 while (len > room)
10341 {
10342 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010343 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010344 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010010345 else if (len > room)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010346 {
10347 p += len - room;
10348 len = room;
10349 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010350 if (len > Columns - col - 1)
10351 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010352
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010353 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010354 col += len;
10355 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010356 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010357
10358 /* Store the tab page number in TabPageIdxs[], so that
10359 * jump_to_mouse() knows where each one is. */
10360 ++tabcount;
10361 while (scol < col)
10362 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010363 }
10364
Bram Moolenaar238a5642006-02-21 22:12:05 +000010365 if (use_sep_chars)
10366 c = '_';
10367 else
10368 c = ' ';
10369 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010370
10371 /* Put an "X" for closing the current tab if there are several. */
10372 if (first_tabpage->tp_next != NULL)
10373 {
10374 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10375 TabPageIdxs[Columns - 1] = -999;
10376 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010377 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010378
10379 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10380 * set. */
10381 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010382}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010383
10384/*
10385 * Get buffer name for "buf" into NameBuff[].
10386 * Takes care of special buffer names and translates special characters.
10387 */
10388 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010389get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010390{
10391 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010392 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010393 else
10394 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10395 trans_characters(NameBuff, MAXPATHL);
10396}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010397
Bram Moolenaar071d4272004-06-13 20:20:40 +000010398/*
10399 * Get the character to use in a status line. Get its attributes in "*attr".
10400 */
10401 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010402fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010403{
10404 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010405
10406#ifdef FEAT_TERMINAL
10407 if (bt_terminal(wp->w_buffer))
10408 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010409 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010410 {
10411 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010412 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010413 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010414 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010415 {
10416 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010417 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010418 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010419 }
10420 else
10421#endif
10422 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010423 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010424 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010425 fill = fill_stl;
10426 }
10427 else
10428 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010429 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010430 fill = fill_stlnc;
10431 }
10432 /* Use fill when there is highlighting, and highlighting of current
10433 * window differs, or the fillchars differ, or this is not the
10434 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010435 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010436 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010437 || (fill_stl != fill_stlnc)))
10438 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010439 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010440 return '^';
10441 return '=';
10442}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010443
Bram Moolenaar071d4272004-06-13 20:20:40 +000010444/*
10445 * Get the character to use in a separator between vertically split windows.
10446 * Get its attributes in "*attr".
10447 */
10448 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010449fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010450{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010451 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010452 if (*attr == 0 && fill_vert == ' ')
10453 return '|';
10454 else
10455 return fill_vert;
10456}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010457
10458/*
10459 * Return TRUE if redrawing should currently be done.
10460 */
10461 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010462redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010463{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010464#ifdef FEAT_EVAL
10465 if (disable_redraw_for_testing)
10466 return 0;
10467 else
10468#endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +020010469 return ((!RedrawingDisabled
10470#ifdef FEAT_EVAL
10471 || ignore_redraw_flag_for_testing
10472#endif
10473 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010474}
10475
10476/*
10477 * Return TRUE if printing messages should currently be done.
10478 */
10479 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010480messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010481{
10482 return (!(p_lz && char_avail() && !KeyTyped));
10483}
10484
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010485#ifdef FEAT_MENU
10486/*
10487 * Draw the window toolbar.
10488 */
10489 static void
10490redraw_win_toolbar(win_T *wp)
10491{
10492 vimmenu_T *menu;
10493 int item_idx = 0;
10494 int item_count = 0;
10495 int col = 0;
10496 int next_col;
10497 int off = (int)(current_ScreenLine - ScreenLines);
10498 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
10499 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
10500
10501 vim_free(wp->w_winbar_items);
10502 for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
10503 ++item_count;
Bram Moolenaarc799fe22019-05-28 23:08:19 +020010504 wp->w_winbar_items = ALLOC_CLEAR_MULT(winbar_item_T, item_count + 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010505
10506 /* TODO: use fewer spaces if there is not enough room */
10507 for (menu = wp->w_winbar->children;
Bram Moolenaar02631462017-09-22 15:20:32 +020010508 menu != NULL && col < wp->w_width; menu = menu->next)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010509 {
10510 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010511 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010512 break;
10513 if (col > 1)
10514 {
10515 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010516 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010517 break;
10518 }
10519
10520 wp->w_winbar_items[item_idx].wb_startcol = col;
10521 space_to_screenline(off + col, button_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010522 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010523 break;
10524
10525 next_col = text_to_screenline(wp, menu->name, col);
10526 while (col < next_col)
10527 {
10528 ScreenAttrs[off + col] = button_attr;
10529 ++col;
10530 }
10531 wp->w_winbar_items[item_idx].wb_endcol = col;
10532 wp->w_winbar_items[item_idx].wb_menu = menu;
10533 ++item_idx;
10534
Bram Moolenaar02631462017-09-22 15:20:32 +020010535 if (col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010536 break;
10537 space_to_screenline(off + col, button_attr);
10538 ++col;
10539 }
Bram Moolenaar02631462017-09-22 15:20:32 +020010540 while (col < wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010541 {
10542 space_to_screenline(off + col, fill_attr);
10543 ++col;
10544 }
10545 wp->w_winbar_items[item_idx].wb_menu = NULL; /* end marker */
10546
Bram Moolenaar02631462017-09-22 15:20:32 +020010547 screen_line(wp->w_winrow, wp->w_wincol, (int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +020010548 (int)wp->w_width, 0);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010549}
10550#endif
Bram Moolenaar491ac282018-06-17 14:47:55 +020010551
Bram Moolenaar071d4272004-06-13 20:20:40 +000010552/*
10553 * Show current status info in ruler and various other places
10554 * If always is FALSE, only show ruler if position has changed.
10555 */
10556 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010557showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010558{
10559 if (!always && !redrawing())
10560 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010561 if (pum_visible())
10562 {
10563 /* Don't redraw right now, do it later. */
10564 curwin->w_redr_status = TRUE;
10565 return;
10566 }
Bram Moolenaar4033c552017-09-16 20:54:51 +020010567#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010568 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar362f3562009-11-03 16:20:34 +000010569 redraw_custom_statusline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010570 else
10571#endif
10572#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +020010573 win_redr_ruler(curwin, always, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010574#endif
10575
10576#ifdef FEAT_TITLE
10577 if (need_maketitle
10578# ifdef FEAT_STL_OPT
10579 || (p_icon && (stl_syntax & STL_IN_ICON))
10580 || (p_title && (stl_syntax & STL_IN_TITLE))
10581# endif
10582 )
10583 maketitle();
10584#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010585 /* Redraw the tab pages line if needed. */
10586 if (redraw_tabline)
10587 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010588}
10589
10590#ifdef FEAT_CMDL_INFO
10591 static void
Bram Moolenaar491ac282018-06-17 14:47:55 +020010592win_redr_ruler(win_T *wp, int always, int ignore_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010593{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010594#define RULER_BUF_LEN 70
10595 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010596 int row;
10597 int fillchar;
10598 int attr;
10599 int empty_line = FALSE;
10600 colnr_T virtcol;
10601 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010602 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010603 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010604 int this_ru_col;
10605 int off = 0;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +010010606 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010607
10608 /* If 'ruler' off or redrawing disabled, don't do anything */
10609 if (!p_ru)
10610 return;
10611
10612 /*
10613 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10614 * after deleting lines, before cursor.lnum is corrected.
10615 */
10616 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10617 return;
10618
Bram Moolenaare2c453d2019-08-21 14:37:09 +020010619 // 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
10629#ifdef FEAT_STL_OPT
10630 if (*p_ruf)
10631 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010632 int save_called_emsg = called_emsg;
10633
10634 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010635 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010636 if (called_emsg)
10637 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010638 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010639 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010640 return;
10641 }
10642#endif
10643
10644 /*
10645 * Check if not in Insert mode and the line is empty (will show "0-1").
10646 */
10647 if (!(State & INSERT)
10648 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10649 empty_line = TRUE;
10650
10651 /*
10652 * Only draw the ruler when something changed.
10653 */
10654 validate_virtcol_win(wp);
10655 if ( redraw_cmdline
10656 || always
10657 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10658 || wp->w_cursor.col != wp->w_ru_cursor.col
10659 || wp->w_virtcol != wp->w_ru_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +000010660 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
Bram Moolenaar071d4272004-06-13 20:20:40 +000010661 || wp->w_topline != wp->w_ru_topline
10662 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10663#ifdef FEAT_DIFF
10664 || wp->w_topfill != wp->w_ru_topfill
10665#endif
10666 || empty_line != wp->w_ru_empty)
10667 {
10668 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010669 if (wp->w_status_height)
10670 {
10671 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010672 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar53f81742017-09-22 14:35:51 +020010673 off = wp->w_wincol;
Bram Moolenaar02631462017-09-22 15:20:32 +020010674 width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010675 }
10676 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010677 {
10678 row = Rows - 1;
10679 fillchar = ' ';
10680 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010681 width = Columns;
10682 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010683 }
10684
10685 /* In list mode virtcol needs to be recomputed */
10686 virtcol = wp->w_virtcol;
10687 if (wp->w_p_list && lcs_tab1 == NUL)
10688 {
10689 wp->w_p_list = FALSE;
10690 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10691 wp->w_p_list = TRUE;
10692 }
10693
10694 /*
10695 * Some sprintfs return the length, some return a pointer.
10696 * To avoid portability problems we use strlen() here.
10697 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010698 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010699 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10700 ? 0L
10701 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010702 len = STRLEN(buffer);
10703 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010704 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10705 (int)virtcol + 1);
10706
10707 /*
10708 * Add a "50%" if there is room for it.
10709 * On the last line, don't print in the last column (scrolls the
10710 * screen up on some terminals).
10711 */
10712 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010713 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010714 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010715 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010716 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010717 this_ru_col = ru_col - (Columns - width);
10718 if (this_ru_col < 0)
10719 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010720 /* Never use more than half the window/screen width, leave the other
10721 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010722 if (this_ru_col < (width + 1) / 2)
10723 this_ru_col = (width + 1) / 2;
10724 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010725 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010010726 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010727 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010728 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010729 if (has_mbyte)
10730 i += (*mb_char2bytes)(fillchar, buffer + i);
10731 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010732 buffer[i++] = fillchar;
10733 ++o;
10734 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010735 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010736 }
10737 /* Truncate at window boundary. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010738 if (has_mbyte)
10739 {
10740 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010741 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010742 {
10743 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010744 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010745 {
10746 buffer[i] = NUL;
10747 break;
10748 }
10749 }
10750 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010010751 else if (this_ru_col + (int)STRLEN(buffer) > width)
Bram Moolenaar4033c552017-09-16 20:54:51 +020010752 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010753
Bram Moolenaar4033c552017-09-16 20:54:51 +020010754 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010755 i = redraw_cmdline;
10756 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020010757 this_ru_col + off + (int)STRLEN(buffer),
10758 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010759 fillchar, fillchar, attr);
10760 /* don't redraw the cmdline because of showing the ruler */
10761 redraw_cmdline = i;
10762 wp->w_ru_cursor = wp->w_cursor;
10763 wp->w_ru_virtcol = wp->w_virtcol;
10764 wp->w_ru_empty = empty_line;
10765 wp->w_ru_topline = wp->w_topline;
10766 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10767#ifdef FEAT_DIFF
10768 wp->w_ru_topfill = wp->w_topfill;
10769#endif
10770 }
10771}
10772#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010773
10774#if defined(FEAT_LINEBREAK) || defined(PROTO)
10775/*
Bram Moolenaar64486672010-05-16 15:46:46 +020010776 * Return the width of the 'number' and 'relativenumber' column.
10777 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010778 * Otherwise it depends on 'numberwidth' and the line count.
10779 */
10780 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010781number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010782{
10783 int n;
10784 linenr_T lnum;
10785
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020010786 if (wp->w_p_rnu && !wp->w_p_nu)
10787 /* cursor line shows "0" */
10788 lnum = wp->w_height;
10789 else
10790 /* cursor line shows absolute line number */
10791 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020010792
Bram Moolenaar6b314672015-03-20 15:42:10 +010010793 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010794 return wp->w_nrwidth_width;
10795 wp->w_nrwidth_line_count = lnum;
10796
10797 n = 0;
10798 do
10799 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010800 lnum /= 10;
10801 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010802 } while (lnum > 0);
10803
10804 /* 'numberwidth' gives the minimal width plus one */
10805 if (n < wp->w_p_nuw - 1)
10806 n = wp->w_p_nuw - 1;
10807
Bram Moolenaare4b407f2019-07-04 11:59:28 +020010808# ifdef FEAT_SIGNS
10809 // If 'signcolumn' is set to 'number' and there is a sign to display, then
10810 // the minimal width for the number column is 2.
10811 if (n < 2 && (wp->w_buffer->b_signlist != NULL)
10812 && (*wp->w_p_scl == 'n' && *(wp->w_p_scl + 1) == 'u'))
10813 n = 2;
10814# endif
10815
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010816 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010010817 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010818 return n;
10819}
10820#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010821
Bram Moolenaar113e1072019-01-20 15:30:40 +010010822#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010823/*
10824 * Return the current cursor column. This is the actual position on the
10825 * screen. First column is 0.
10826 */
10827 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010828screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010829{
10830 return screen_cur_col;
10831}
10832
10833/*
10834 * Return the current cursor row. This is the actual position on the screen.
10835 * First row is 0.
10836 */
10837 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010838screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010839{
10840 return screen_cur_row;
10841}
Bram Moolenaar113e1072019-01-20 15:30:40 +010010842#endif