blob: 14b946538e1eecbf512f813edec35319b6f7739b [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
Bram Moolenaar017ba072019-09-14 21:01:23 +0200161#ifdef FEAT_SYN_HL
162static void margin_columns_win(win_T *wp, int *left_col, int *right_col);
163#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165/* Ugly global: overrule attribute used by screen_char() */
166static int screen_char_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167
Bram Moolenaarc0aa4822017-07-16 14:04:29 +0200168#ifdef FEAT_RIGHTLEFT
169# define HAS_RIGHTLEFT(x) x
170#else
171# define HAS_RIGHTLEFT(x) FALSE
172#endif
173
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200174// flags for screen_line()
175#define SLF_RIGHTLEFT 1
176#define SLF_POPUP 2
177
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178/*
179 * Redraw the current window later, with update_screen(type).
180 * Set must_redraw only if not already set to a higher value.
Bram Moolenaarae654382019-01-17 21:09:05 +0100181 * E.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 */
183 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100184redraw_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185{
186 redraw_win_later(curwin, type);
187}
188
189 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100190redraw_win_later(
191 win_T *wp,
192 int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193{
Bram Moolenaar4f198282017-10-23 21:53:30 +0200194 if (!exiting && wp->w_redr_type < type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 {
196 wp->w_redr_type = type;
197 if (type >= NOT_VALID)
198 wp->w_lines_valid = 0;
199 if (must_redraw < type) /* must_redraw is the maximum of all windows */
200 must_redraw = type;
201 }
202}
203
204/*
205 * Force a complete redraw later. Also resets the highlighting. To be used
206 * after executing a shell command that messes up the screen.
207 */
208 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100209redraw_later_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210{
211 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000212#ifdef FEAT_GUI
213 if (gui.in_use)
214 /* Use a code that will reset gui.highlight_mask in
215 * gui_stop_highlight(). */
216 screen_attr = HL_ALL + 1;
217 else
218#endif
219 /* Use attributes that is very unlikely to appear in text. */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +0200220 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE | HL_STRIKETHROUGH;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221}
222
223/*
224 * Mark all windows to be redrawn later.
225 */
226 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100227redraw_all_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228{
229 win_T *wp;
230
231 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232 redraw_win_later(wp, type);
Bram Moolenaar04b4e1a2019-01-06 22:22:07 +0100233 // This may be needed when switching tabs.
234 if (must_redraw < type)
235 must_redraw = type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236}
237
238/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000239 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240 */
241 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100242redraw_curbuf_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243{
244 redraw_buf_later(curbuf, type);
245}
246
247 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100248redraw_buf_later(buf_T *buf, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249{
250 win_T *wp;
251
252 FOR_ALL_WINDOWS(wp)
253 {
254 if (wp->w_buffer == buf)
255 redraw_win_later(wp, type);
256 }
257}
258
Bram Moolenaar113e1072019-01-20 15:30:40 +0100259#if defined(FEAT_SIGNS) || defined(PROTO)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200260 void
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100261redraw_buf_line_later(buf_T *buf, linenr_T lnum)
262{
263 win_T *wp;
264
265 FOR_ALL_WINDOWS(wp)
266 if (wp->w_buffer == buf && lnum >= wp->w_topline
267 && lnum < wp->w_botline)
268 redrawWinline(wp, lnum);
269}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100270#endif
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100271
Bram Moolenaar113e1072019-01-20 15:30:40 +0100272#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100273 void
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200274redraw_buf_and_status_later(buf_T *buf, int type)
275{
276 win_T *wp;
277
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200278#ifdef FEAT_WILDMENU
Bram Moolenaar86033562017-07-12 20:24:41 +0200279 if (wild_menu_showing != 0)
280 /* Don't redraw while the command line completion is displayed, it
281 * would disappear. */
282 return;
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200283#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200284 FOR_ALL_WINDOWS(wp)
285 {
286 if (wp->w_buffer == buf)
287 {
288 redraw_win_later(wp, type);
289 wp->w_redr_status = TRUE;
290 }
291 }
292}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100293#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200294
Bram Moolenaar113e1072019-01-20 15:30:40 +0100295#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296/*
Bram Moolenaar2951b772013-07-03 12:45:31 +0200297 * Redraw as soon as possible. When the command line is not scrolled redraw
298 * right away and restore what was on the command line.
299 * Return a code indicating what happened.
300 */
301 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100302redraw_asap(int type)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200303{
304 int rows;
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200305 int cols = screen_Columns;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200306 int r;
307 int ret = 0;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200308 schar_T *screenline; /* copy from ScreenLines[] */
309 sattr_T *screenattr; /* copy from ScreenAttrs[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200310 int i;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200311 u8char_T *screenlineUC = NULL; /* copy from ScreenLinesUC[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200312 u8char_T *screenlineC[MAX_MCO]; /* copy from ScreenLinesC[][] */
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200313 schar_T *screenline2 = NULL; /* copy from ScreenLines2[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200314
315 redraw_later(type);
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200316 if (msg_scrolled || (State != NORMAL && State != NORMAL_BUSY) || exiting)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200317 return ret;
318
319 /* Allocate space to save the text displayed in the command line area. */
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200320 rows = screen_Rows - cmdline_row;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200321 screenline = LALLOC_MULT(schar_T, rows * cols);
322 screenattr = LALLOC_MULT(sattr_T, rows * cols);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200323 if (screenline == NULL || screenattr == NULL)
324 ret = 2;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200325 if (enc_utf8)
326 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200327 screenlineUC = LALLOC_MULT(u8char_T, rows * cols);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200328 if (screenlineUC == NULL)
329 ret = 2;
330 for (i = 0; i < p_mco; ++i)
331 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200332 screenlineC[i] = LALLOC_MULT(u8char_T, rows * cols);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200333 if (screenlineC[i] == NULL)
334 ret = 2;
335 }
336 }
337 if (enc_dbcs == DBCS_JPNU)
338 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200339 screenline2 = LALLOC_MULT(schar_T, rows * cols);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200340 if (screenline2 == NULL)
341 ret = 2;
342 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200343
344 if (ret != 2)
345 {
346 /* Save the text displayed in the command line area. */
347 for (r = 0; r < rows; ++r)
348 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200349 mch_memmove(screenline + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200350 ScreenLines + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200351 (size_t)cols * sizeof(schar_T));
352 mch_memmove(screenattr + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200353 ScreenAttrs + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200354 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200355 if (enc_utf8)
356 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200357 mch_memmove(screenlineUC + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200358 ScreenLinesUC + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200359 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200360 for (i = 0; i < p_mco; ++i)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200361 mch_memmove(screenlineC[i] + r * cols,
362 ScreenLinesC[i] + LineOffset[cmdline_row + r],
363 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200364 }
365 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200366 mch_memmove(screenline2 + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200367 ScreenLines2 + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200368 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200369 }
370
371 update_screen(0);
372 ret = 3;
373
374 if (must_redraw == 0)
375 {
376 int off = (int)(current_ScreenLine - ScreenLines);
377
378 /* Restore the text displayed in the command line area. */
379 for (r = 0; r < rows; ++r)
380 {
381 mch_memmove(current_ScreenLine,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200382 screenline + r * cols,
383 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200384 mch_memmove(ScreenAttrs + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200385 screenattr + r * cols,
386 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200387 if (enc_utf8)
388 {
389 mch_memmove(ScreenLinesUC + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200390 screenlineUC + r * cols,
391 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200392 for (i = 0; i < p_mco; ++i)
393 mch_memmove(ScreenLinesC[i] + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200394 screenlineC[i] + r * cols,
395 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200396 }
397 if (enc_dbcs == DBCS_JPNU)
398 mch_memmove(ScreenLines2 + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200399 screenline2 + r * cols,
400 (size_t)cols * sizeof(schar_T));
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200401 screen_line(cmdline_row + r, 0, cols, cols, 0);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200402 }
403 ret = 4;
404 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200405 }
406
407 vim_free(screenline);
408 vim_free(screenattr);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200409 if (enc_utf8)
410 {
411 vim_free(screenlineUC);
412 for (i = 0; i < p_mco; ++i)
413 vim_free(screenlineC[i]);
414 }
415 if (enc_dbcs == DBCS_JPNU)
416 vim_free(screenline2);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200417
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200418 /* Show the intro message when appropriate. */
419 maybe_intro_message();
420
421 setcursor();
422
Bram Moolenaar2951b772013-07-03 12:45:31 +0200423 return ret;
424}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100425#endif
Bram Moolenaar2951b772013-07-03 12:45:31 +0200426
427/*
Bram Moolenaar975b5272016-03-15 23:10:59 +0100428 * Invoked after an asynchronous callback is called.
429 * If an echo command was used the cursor needs to be put back where
430 * it belongs. If highlighting was changed a redraw is needed.
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200431 * If "call_update_screen" is FALSE don't call update_screen() when at the
432 * command line.
Bram Moolenaar975b5272016-03-15 23:10:59 +0100433 */
434 void
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200435redraw_after_callback(int call_update_screen)
Bram Moolenaar975b5272016-03-15 23:10:59 +0100436{
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200437 ++redrawing_for_callback;
438
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100439 if (State == HITRETURN || State == ASKMORE)
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200440 ; // do nothing
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100441 else if (State & CMDLINE)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200442 {
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200443 // Don't redraw when in prompt_for_number().
444 if (cmdline_row > 0)
445 {
446 // Redrawing only works when the screen didn't scroll. Don't clear
447 // wildmenu entries.
448 if (msg_scrolled == 0
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200449#ifdef FEAT_WILDMENU
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200450 && wild_menu_showing == 0
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200451#endif
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200452 && call_update_screen)
453 update_screen(0);
454
455 // Redraw in the same position, so that the user can continue
456 // editing the command.
457 redrawcmdline_ex(FALSE);
458 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200459 }
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200460 else if (State & (NORMAL | INSERT | TERMINAL))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100461 {
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200462 // keep the command line if possible
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200463 update_screen(VALID_NO_UPDATE);
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100464 setcursor();
465 }
Bram Moolenaar975b5272016-03-15 23:10:59 +0100466 cursor_on();
Bram Moolenaar975b5272016-03-15 23:10:59 +0100467#ifdef FEAT_GUI
Bram Moolenaara338adc2018-01-31 20:51:47 +0100468 if (gui.in_use && !gui_mch_is_blink_off())
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200469 // Don't update the cursor when it is blinking and off to avoid
470 // flicker.
Bram Moolenaara338adc2018-01-31 20:51:47 +0100471 out_flush_cursor(FALSE, FALSE);
472 else
Bram Moolenaar975b5272016-03-15 23:10:59 +0100473#endif
Bram Moolenaaracda04f2018-02-08 09:57:28 +0100474 out_flush();
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200475
476 --redrawing_for_callback;
Bram Moolenaar975b5272016-03-15 23:10:59 +0100477}
478
479/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480 * Changed something in the current window, at buffer line "lnum", that
481 * requires that line and possibly other lines to be redrawn.
482 * Used when entering/leaving Insert mode with the cursor on a folded line.
483 * Used to remove the "$" from a change command.
484 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
485 * may become invalid and the whole window will have to be redrawn.
486 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100488redrawWinline(
Bram Moolenaar90a99792018-09-12 21:52:18 +0200489 win_T *wp,
Bram Moolenaarae12f4b2019-01-09 20:51:04 +0100490 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491{
Bram Moolenaar90a99792018-09-12 21:52:18 +0200492 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
493 wp->w_redraw_top = lnum;
494 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
495 wp->w_redraw_bot = lnum;
496 redraw_win_later(wp, VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497}
498
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200499/*
500 * To be called when "updating_screen" was set before and now the postponed
501 * side effects may take place.
502 */
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200503 void
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200504after_updating_screen(int may_resize_shell UNUSED)
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200505{
506 updating_screen = FALSE;
507#ifdef FEAT_GUI
508 if (may_resize_shell)
509 gui_may_resize_shell();
510#endif
511#ifdef FEAT_TERMINAL
512 term_check_channel_closed_recently();
513#endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +0200514
515#ifdef HAVE_DROP_FILE
516 // If handle_drop() was called while updating_screen was TRUE need to
517 // handle the drop now.
518 handle_any_postponed_drop();
519#endif
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200520}
521
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522/*
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200523 * Update all windows that are editing the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 */
525 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100526update_curbuf(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527{
528 redraw_curbuf_later(type);
529 update_screen(type);
530}
531
532/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533 * Based on the current value of curwin->w_topline, transfer a screenfull
534 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
Bram Moolenaar072412e2017-09-13 22:11:35 +0200535 * Return OK when the screen was updated, FAIL if it was not done.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536 */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200537 int
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200538update_screen(int type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539{
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200540 int type = type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 win_T *wp;
542 static int did_intro = FALSE;
543#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
544 int did_one;
545#endif
Bram Moolenaar144445d2016-07-08 21:41:54 +0200546#ifdef FEAT_GUI
Bram Moolenaar107abd22016-08-12 14:08:25 +0200547 int did_undraw = FALSE;
Bram Moolenaar1f3601e2019-04-26 20:33:00 +0200548 int gui_cursor_col = 0;
549 int gui_cursor_row = 0;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200550#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200551 int no_update = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000553 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554 if (!screen_valid(TRUE))
Bram Moolenaar072412e2017-09-13 22:11:35 +0200555 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200557 if (type == VALID_NO_UPDATE)
558 {
559 no_update = TRUE;
560 type = 0;
561 }
562
Bram Moolenaara3347722019-05-11 21:14:24 +0200563#ifdef FEAT_EVAL
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200564 {
565 buf_T *buf;
566
567 // Before updating the screen, notify any listeners of changed text.
568 FOR_ALL_BUFFERS(buf)
569 invoke_listeners(buf);
570 }
Bram Moolenaara3347722019-05-11 21:14:24 +0200571#endif
572
Bram Moolenaar4f57eef2019-08-24 20:54:19 +0200573#ifdef FEAT_DIFF
574 // May have postponed updating diffs.
575 if (need_diff_redraw)
576 diff_redraw(TRUE);
577#endif
578
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 if (must_redraw)
580 {
581 if (type < must_redraw) /* use maximal type */
582 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000583
584 /* must_redraw is reset here, so that when we run into some weird
585 * reason to redraw while busy redrawing (e.g., asynchronous
586 * scrolling), or update_topline() in win_update() will cause a
587 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588 must_redraw = 0;
589 }
590
Bram Moolenaar6eddadf2018-05-06 16:40:16 +0200591 /* May need to update w_lines[]. */
592 if (curwin->w_lines_valid == 0 && type < NOT_VALID
593#ifdef FEAT_TERMINAL
594 && !term_do_update_window(curwin)
595#endif
596 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597 type = NOT_VALID;
598
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000599 /* Postpone the redrawing when it's not needed and when being called
600 * recursively. */
601 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 {
603 redraw_later(type); /* remember type for next time */
604 must_redraw = type;
605 if (type > INVERTED_ALL)
606 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200607 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 }
Bram Moolenaarec572ad2019-07-07 14:26:59 +0200609 updating_screen = TRUE;
Bram Moolenaar33796b32019-06-08 16:01:13 +0200610
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200611#ifdef FEAT_TEXT_PROP
Bram Moolenaar4c063a02019-06-10 21:24:12 +0200612 // Update popup_mask if needed. This may set w_redraw_top and w_redraw_bot
613 // in some windows.
614 may_update_popup_mask(type);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200615#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617#ifdef FEAT_SYN_HL
618 ++display_tick; /* let syntax code know we're in a next round of
619 * display updating */
620#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200621 if (no_update)
622 ++no_win_do_lines_ins;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623
624 /*
625 * if the screen was scrolled up when displaying a message, scroll it down
626 */
627 if (msg_scrolled)
628 {
629 clear_cmdline = TRUE;
630 if (msg_scrolled > Rows - 5) /* clearing is faster */
631 type = CLEAR;
632 else if (type != CLEAR)
633 {
634 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200635 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, 0, NULL)
636 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 type = CLEAR;
638 FOR_ALL_WINDOWS(wp)
639 {
Bram Moolenaar98fb65c2019-06-02 20:33:32 +0200640 if (wp->w_winrow < msg_scrolled)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 {
642 if (W_WINROW(wp) + wp->w_height > msg_scrolled
643 && wp->w_redr_type < REDRAW_TOP
644 && wp->w_lines_valid > 0
645 && wp->w_topline == wp->w_lines[0].wl_lnum)
646 {
647 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
648 wp->w_redr_type = REDRAW_TOP;
649 }
650 else
651 {
652 wp->w_redr_type = NOT_VALID;
Bram Moolenaare0de17d2017-09-24 16:24:34 +0200653 if (W_WINROW(wp) + wp->w_height + wp->w_status_height
654 <= msg_scrolled)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 }
657 }
658 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200659 if (!no_update)
660 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000661 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 }
663 msg_scrolled = 0;
664 need_wait_return = FALSE;
665 }
666
667 /* reset cmdline_row now (may have been changed temporarily) */
668 compute_cmdrow();
669
670 /* Check for changed highlighting */
671 if (need_highlight_changed)
672 highlight_changed();
673
674 if (type == CLEAR) /* first clear screen */
675 {
676 screenclear(); /* will reset clear_cmdline */
677 type = NOT_VALID;
Bram Moolenaar9f5f7bf2017-06-28 20:45:26 +0200678 /* must_redraw may be set indirectly, avoid another redraw later */
679 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 }
681
682 if (clear_cmdline) /* going to clear cmdline (done below) */
683 check_for_delay(FALSE);
684
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000685#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200686 /* Force redraw when width of 'number' or 'relativenumber' column
687 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000688 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200689 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
690 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000691 curwin->w_redr_type = NOT_VALID;
692#endif
693
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 /*
695 * Only start redrawing if there is really something to do.
696 */
697 if (type == INVERTED)
698 update_curswant();
699 if (curwin->w_redr_type < type
700 && !((type == VALID
701 && curwin->w_lines[0].wl_valid
702#ifdef FEAT_DIFF
703 && curwin->w_topfill == curwin->w_old_topfill
704 && curwin->w_botfill == curwin->w_old_botfill
705#endif
706 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000708 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
710 && curwin->w_old_visual_mode == VIsual_mode
711 && (curwin->w_valid & VALID_VIRTCOL)
712 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 ))
714 curwin->w_redr_type = type;
715
Bram Moolenaar5a305422006-04-28 22:38:25 +0000716 /* Redraw the tab pages line if needed. */
717 if (redraw_tabline || type >= NOT_VALID)
718 draw_tabline();
Bram Moolenaar5a305422006-04-28 22:38:25 +0000719
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720#ifdef FEAT_SYN_HL
721 /*
722 * Correct stored syntax highlighting info for changes in each displayed
723 * buffer. Each buffer must only be done once.
724 */
725 FOR_ALL_WINDOWS(wp)
726 {
727 if (wp->w_buffer->b_mod_set)
728 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 win_T *wwp;
730
731 /* Check if we already did this buffer. */
732 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
733 if (wwp->w_buffer == wp->w_buffer)
734 break;
Bram Moolenaar4033c552017-09-16 20:54:51 +0200735 if (wwp == wp && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 syn_stack_apply_changes(wp->w_buffer);
737 }
738 }
739#endif
740
741 /*
742 * Go from top to bottom through the windows, redrawing the ones that need
743 * it.
744 */
745#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
746 did_one = FALSE;
747#endif
748#ifdef FEAT_SEARCH_EXTRA
749 search_hl.rm.regprog = NULL;
750#endif
751 FOR_ALL_WINDOWS(wp)
752 {
753 if (wp->w_redr_type != 0)
754 {
755 cursor_off();
756#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
757 if (!did_one)
758 {
759 did_one = TRUE;
760# ifdef FEAT_SEARCH_EXTRA
761 start_search_hl();
762# endif
763# ifdef FEAT_CLIPBOARD
764 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200765 if (clip_star.available && clip_isautosel_star())
766 clip_update_selection(&clip_star);
767 if (clip_plus.available && clip_isautosel_plus())
768 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769# endif
770#ifdef FEAT_GUI
771 /* Remove the cursor before starting to do anything, because
772 * scrolling may make it difficult to redraw the text under
773 * it. */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200774 if (gui.in_use && wp == curwin)
Bram Moolenaar144445d2016-07-08 21:41:54 +0200775 {
776 gui_cursor_col = gui.cursor_col;
777 gui_cursor_row = gui.cursor_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 gui_undraw_cursor();
Bram Moolenaar107abd22016-08-12 14:08:25 +0200779 did_undraw = TRUE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200780 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781#endif
782 }
783#endif
784 win_update(wp);
785 }
786
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 /* redraw status line after the window to minimize cursor movement */
788 if (wp->w_redr_status)
789 {
790 cursor_off();
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200791 win_redr_status(wp, TRUE); // any popup menu will be redrawn below
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 }
794#if defined(FEAT_SEARCH_EXTRA)
795 end_search_hl();
796#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100797 /* May need to redraw the popup menu. */
Bram Moolenaar491ac282018-06-17 14:47:55 +0200798 pum_may_redraw();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 /* Reset b_mod_set flags. Going through all windows is probably faster
801 * than going through all buffers (there could be many buffers). */
Bram Moolenaar29323592016-07-24 22:04:11 +0200802 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 wp->w_buffer->b_mod_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +0200805#ifdef FEAT_TEXT_PROP
806 // Display popup windows on top of the windows and command line.
807 update_popups(win_update);
808#endif
809
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200810 after_updating_screen(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811
812 /* Clear or redraw the command line. Done last, because scrolling may
813 * mess up the command line. */
Bram Moolenaar4c25bd72019-04-20 23:38:07 +0200814 if (clear_cmdline || redraw_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 showmode();
816
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200817 if (no_update)
818 --no_win_do_lines_ins;
819
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200821 if (!did_intro)
822 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 did_intro = TRUE;
824
825#ifdef FEAT_GUI
826 /* Redraw the cursor and update the scrollbars when all screen updating is
827 * done. */
828 if (gui.in_use)
829 {
Bram Moolenaar107abd22016-08-12 14:08:25 +0200830 if (did_undraw && !gui_mch_is_blink_off())
Bram Moolenaar144445d2016-07-08 21:41:54 +0200831 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100832 mch_disable_flush();
833 out_flush(); /* required before updating the cursor */
834 mch_enable_flush();
835
Bram Moolenaar144445d2016-07-08 21:41:54 +0200836 /* Put the GUI position where the cursor was, gui_update_cursor()
837 * uses that. */
838 gui.col = gui_cursor_col;
839 gui.row = gui_cursor_row;
Bram Moolenaar84dbd492016-10-02 23:09:31 +0200840 gui.col = mb_fix_col(gui.col, gui.row);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara338adc2018-01-31 20:51:47 +0100842 gui_may_flush();
Bram Moolenaar65549bd2016-07-08 22:52:37 +0200843 screen_cur_col = gui.col;
844 screen_cur_row = gui.row;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200845 }
Bram Moolenaara338adc2018-01-31 20:51:47 +0100846 else
847 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 gui_update_scrollbars(FALSE);
849 }
850#endif
Bram Moolenaar072412e2017-09-13 22:11:35 +0200851 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852}
853
Bram Moolenaar1fa8fdd2019-02-25 05:41:15 +0100854#if defined(FEAT_NETBEANS_INTG) || defined(FEAT_GUI)
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100855/*
856 * Prepare for updating one or more windows.
857 * Caller must check for "updating_screen" already set to avoid recursiveness.
858 */
859 static void
860update_prepare(void)
861{
862 cursor_off();
863 updating_screen = TRUE;
864#ifdef FEAT_GUI
865 /* Remove the cursor before starting to do anything, because scrolling may
866 * make it difficult to redraw the text under it. */
867 if (gui.in_use)
868 gui_undraw_cursor();
869#endif
870#ifdef FEAT_SEARCH_EXTRA
871 start_search_hl();
872#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +0200873#ifdef FEAT_TEXT_PROP
874 // Update popup_mask if needed.
Bram Moolenaar68acb412019-06-26 03:40:36 +0200875 may_update_popup_mask(must_redraw);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200876#endif
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100877}
878
879/*
880 * Finish updating one or more windows.
881 */
882 static void
883update_finish(void)
884{
Bram Moolenaar4c25bd72019-04-20 23:38:07 +0200885 if (redraw_cmdline || redraw_mode)
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100886 showmode();
887
888# ifdef FEAT_SEARCH_EXTRA
889 end_search_hl();
890# endif
891
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200892 after_updating_screen(TRUE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100893
894# ifdef FEAT_GUI
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100895 /* Redraw the cursor and update the scrollbars when all screen updating is
896 * done. */
897 if (gui.in_use)
898 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100899 out_flush_cursor(FALSE, FALSE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100900 gui_update_scrollbars(FALSE);
901 }
902# endif
903}
904#endif
905
Bram Moolenaar860cae12010-06-05 23:22:07 +0200906#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200907/*
908 * Return TRUE if the cursor line in window "wp" may be concealed, according
909 * to the 'concealcursor' option.
910 */
911 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100912conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200913{
914 int c;
915
916 if (*wp->w_p_cocu == NUL)
917 return FALSE;
918 if (get_real_state() & VISUAL)
919 c = 'v';
920 else if (State & INSERT)
921 c = 'i';
922 else if (State & NORMAL)
923 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200924 else if (State & CMDLINE)
925 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200926 else
927 return FALSE;
928 return vim_strchr(wp->w_p_cocu, c) != NULL;
929}
930
931/*
932 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
933 */
934 void
Bram Moolenaarb9464822018-05-10 15:09:49 +0200935conceal_check_cursor_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200936{
937 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
938 {
939 need_cursor_line_redraw = TRUE;
940 /* Need to recompute cursor column, e.g., when starting Visual mode
941 * without concealing. */
942 curs_columns(TRUE);
943 }
944}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945#endif
946
Bram Moolenaar113e1072019-01-20 15:30:40 +0100947#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100949update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950{
951 win_T *wp;
952 int doit = FALSE;
953
954# ifdef FEAT_FOLDING
955 win_foldinfo.fi_level = 0;
956# endif
957
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100958 // update/delete a specific sign
959 redraw_buf_line_later(buf, lnum);
960
961 // check if it resulted in the need to redraw a window
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963 if (wp->w_redr_type != 0)
964 doit = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100966 /* Return when there is nothing to do, screen updating is already
Bram Moolenaar07920482017-08-01 20:53:30 +0200967 * happening (recursive call), messages on the screen or still starting up.
968 */
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100969 if (!doit || updating_screen
Bram Moolenaar07920482017-08-01 20:53:30 +0200970 || State == ASKMORE || State == HITRETURN
971 || msg_scrolled
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100972#ifdef FEAT_GUI
973 || gui.starting
974#endif
975 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 return;
977
978 /* update all windows that need updating */
979 update_prepare();
980
Bram Moolenaar29323592016-07-24 22:04:11 +0200981 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 {
983 if (wp->w_redr_type != 0)
984 win_update(wp);
985 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200986 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988
989 update_finish();
990}
991#endif
992
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200993/*
994 * Get 'wincolor' attribute for window "wp". If not set and "wp" is a popup
995 * window then get the "Pmenu" highlight attribute.
996 */
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200997 int
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200998get_wcr_attr(win_T *wp)
999{
1000 int wcr_attr = 0;
1001
1002 if (*wp->w_p_wcr != NUL)
1003 wcr_attr = syn_name2attr(wp->w_p_wcr);
1004#ifdef FEAT_TEXT_PROP
Bram Moolenaarc363fe12019-08-04 18:13:46 +02001005 else if (WIN_IS_POPUP(wp))
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001006 {
1007 if (wp->w_popup_flags & POPF_INFO)
1008 wcr_attr = HL_ATTR(HLF_PSI); // PmenuSel
1009 else
1010 wcr_attr = HL_ATTR(HLF_PNI); // Pmenu
1011 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001012#endif
1013 return wcr_attr;
1014}
1015
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016#if defined(FEAT_GUI) || defined(PROTO)
1017/*
1018 * Update a single window, its status line and maybe the command line msg.
1019 * Used for the GUI scrollbar.
1020 */
1021 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001022updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023{
Bram Moolenaar19f990e2009-11-25 12:08:03 +00001024 /* return if already busy updating */
1025 if (updating_screen)
1026 return;
1027
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 update_prepare();
1029
1030#ifdef FEAT_CLIPBOARD
1031 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001032 if (clip_star.available && clip_isautosel_star())
1033 clip_update_selection(&clip_star);
1034 if (clip_plus.available && clip_isautosel_plus())
1035 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001037
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001039
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001040 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001041 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001042 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001043
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 if (wp->w_redr_status
1045# ifdef FEAT_CMDL_INFO
1046 || p_ru
1047# endif
1048# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001049 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050# endif
1051 )
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001052 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053
Bram Moolenaar988c4332019-06-02 14:12:11 +02001054#ifdef FEAT_TEXT_PROP
1055 // Display popup windows on top of everything.
Bram Moolenaara540f8a2019-06-14 19:23:57 +02001056 update_popups(win_update);
Bram Moolenaar988c4332019-06-02 14:12:11 +02001057#endif
1058
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 update_finish();
1060}
1061#endif
1062
1063/*
1064 * Update a single window.
1065 *
1066 * This may cause the windows below it also to be redrawn (when clearing the
1067 * screen or scrolling lines).
1068 *
1069 * How the window is redrawn depends on wp->w_redr_type. Each type also
1070 * implies the one below it.
1071 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001072 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1074 * INVERTED redraw the changed part of the Visual area
1075 * INVERTED_ALL redraw the whole Visual area
1076 * VALID 1. scroll up/down to adjust for a changed w_topline
1077 * 2. update lines at the top when scrolled down
1078 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001079 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 * b_mod_top and b_mod_bot.
1081 * - if wp->w_redraw_top non-zero, redraw lines between
1082 * wp->w_redraw_top and wp->w_redr_bot.
1083 * - continue redrawing when syntax status is invalid.
1084 * 4. if scrolled up, update lines at the bottom.
1085 * This results in three areas that may need updating:
1086 * top: from first row to top_end (when scrolled down)
1087 * mid: from mid_start to mid_end (update inversion or changed text)
1088 * bot: from bot_start to last row (when scrolled up)
1089 */
1090 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001091win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092{
1093 buf_T *buf = wp->w_buffer;
1094 int type;
1095 int top_end = 0; /* Below last row of the top area that needs
1096 updating. 0 when no top area updating. */
1097 int mid_start = 999;/* first row of the mid area that needs
1098 updating. 999 when no mid area updating. */
1099 int mid_end = 0; /* Below last row of the mid area that needs
1100 updating. 0 when no mid area updating. */
1101 int bot_start = 999;/* first row of the bot area that needs
1102 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 int scrolled_down = FALSE; /* TRUE when scrolled down when
1104 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarfbfb7572019-07-25 20:53:03 +02001106 int top_to_mod = FALSE; // redraw above mod_top
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107#endif
1108
1109 int row; /* current window row to display */
1110 linenr_T lnum; /* current buffer lnum to display */
1111 int idx; /* current index in w_lines[] */
1112 int srow; /* starting row of the current line */
1113
1114 int eof = FALSE; /* if TRUE, we hit the end of the file */
1115 int didline = FALSE; /* if TRUE, we finished the last line */
1116 int i;
1117 long j;
1118 static int recursive = FALSE; /* being called recursively */
1119 int old_botline = wp->w_botline;
1120#ifdef FEAT_FOLDING
1121 long fold_count;
1122#endif
1123#ifdef FEAT_SYN_HL
1124 /* remember what happened to the previous line, to know if
1125 * check_visual_highlight() can be used */
1126#define DID_NONE 1 /* didn't update a line */
1127#define DID_LINE 2 /* updated a normal line */
1128#define DID_FOLD 3 /* updated a folded line */
1129 int did_update = DID_NONE;
1130 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1131#endif
1132 linenr_T mod_top = 0;
1133 linenr_T mod_bot = 0;
1134#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1135 int save_got_int;
1136#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001137#ifdef SYN_TIME_LIMIT
1138 proftime_T syntax_tm;
1139#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140
1141 type = wp->w_redr_type;
1142
1143 if (type == NOT_VALID)
1144 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 wp->w_lines_valid = 0;
1147 }
1148
1149 /* Window is zero-height: nothing to draw. */
Bram Moolenaar415a6932017-12-05 20:31:07 +01001150 if (wp->w_height + WINBAR_HEIGHT(wp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 {
1152 wp->w_redr_type = 0;
1153 return;
1154 }
1155
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 /* Window is zero-width: Only need to draw the separator. */
1157 if (wp->w_width == 0)
1158 {
1159 /* draw the vertical separator right of this window */
1160 draw_vsep_win(wp, 0);
1161 wp->w_redr_type = 0;
1162 return;
1163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001165#ifdef FEAT_TERMINAL
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001166 // If this window contains a terminal, redraw works completely differently.
1167 if (term_do_update_window(wp))
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001168 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001169 term_update_window(wp);
Bram Moolenaar181ca992018-02-13 21:19:21 +01001170# ifdef FEAT_MENU
1171 /* Draw the window toolbar, if there is one. */
1172 if (winbar_height(wp) > 0)
1173 redraw_win_toolbar(wp);
1174# endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001175 wp->w_redr_type = 0;
1176 return;
1177 }
1178#endif
1179
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarbbca7732019-07-24 18:13:16 +02001181 init_search_hl(wp, &search_hl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182#endif
1183
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001184#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001185 /* Force redraw when width of 'number' or 'relativenumber' column
1186 * changes. */
1187 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001188 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001189 {
1190 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001191 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001192 }
1193 else
1194#endif
1195
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1197 {
1198 /*
1199 * When there are both inserted/deleted lines and specific lines to be
1200 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1201 * everything (only happens when redrawing is off for while).
1202 */
1203 type = NOT_VALID;
1204 }
1205 else
1206 {
1207 /*
1208 * Set mod_top to the first line that needs displaying because of
1209 * changes. Set mod_bot to the first line after the changes.
1210 */
1211 mod_top = wp->w_redraw_top;
1212 if (wp->w_redraw_bot != 0)
1213 mod_bot = wp->w_redraw_bot + 1;
1214 else
1215 mod_bot = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 if (buf->b_mod_set)
1217 {
1218 if (mod_top == 0 || mod_top > buf->b_mod_top)
1219 {
1220 mod_top = buf->b_mod_top;
1221#ifdef FEAT_SYN_HL
1222 /* Need to redraw lines above the change that may be included
1223 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001224 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001226 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 if (mod_top < 1)
1228 mod_top = 1;
1229 }
1230#endif
1231 }
1232 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1233 mod_bot = buf->b_mod_bot;
1234
1235#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarfbfb7572019-07-25 20:53:03 +02001236 // When 'hlsearch' is on and using a multi-line search pattern, a
1237 // change in one line may make the Search highlighting in a
1238 // previous line invalid. Simple solution: redraw all visible
1239 // lines above the change.
1240 // Same for a match pattern.
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001241 if (search_hl.rm.regprog != NULL
1242 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001244 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001245 {
Bram Moolenaarfbfb7572019-07-25 20:53:03 +02001246 matchitem_T *cur = wp->w_match_head;
1247
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001248 while (cur != NULL)
1249 {
1250 if (cur->match.regprog != NULL
1251 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001252 {
1253 top_to_mod = TRUE;
1254 break;
1255 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001256 cur = cur->next;
1257 }
1258 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259#endif
1260 }
1261#ifdef FEAT_FOLDING
1262 if (mod_top != 0 && hasAnyFolding(wp))
1263 {
1264 linenr_T lnumt, lnumb;
1265
1266 /*
1267 * A change in a line can cause lines above it to become folded or
1268 * unfolded. Find the top most buffer line that may be affected.
1269 * If the line was previously folded and displayed, get the first
1270 * line of that fold. If the line is folded now, get the first
1271 * folded line. Use the minimum of these two.
1272 */
1273
1274 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1275 * the line below it. If there is no valid entry, use w_topline.
1276 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1277 * to this line. If there is no valid entry, use MAXLNUM. */
1278 lnumt = wp->w_topline;
1279 lnumb = MAXLNUM;
1280 for (i = 0; i < wp->w_lines_valid; ++i)
1281 if (wp->w_lines[i].wl_valid)
1282 {
1283 if (wp->w_lines[i].wl_lastlnum < mod_top)
1284 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1285 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1286 {
1287 lnumb = wp->w_lines[i].wl_lnum;
1288 /* When there is a fold column it might need updating
1289 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001290 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 ++lnumb;
1292 }
1293 }
1294
1295 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1296 if (mod_top > lnumt)
1297 mod_top = lnumt;
1298
1299 /* Now do the same for the bottom line (one above mod_bot). */
1300 --mod_bot;
1301 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1302 ++mod_bot;
1303 if (mod_bot < lnumb)
1304 mod_bot = lnumb;
1305 }
1306#endif
1307
1308 /* When a change starts above w_topline and the end is below
1309 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001310 * If the end of the change is above w_topline: do like no change was
1311 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 if (mod_top != 0 && mod_top < wp->w_topline)
1313 {
1314 if (mod_bot > wp->w_topline)
1315 mod_top = wp->w_topline;
1316#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001317 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 top_end = 1;
1319#endif
1320 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001321
1322 /* When line numbers are displayed need to redraw all lines below
1323 * inserted/deleted lines. */
1324 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1325 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 }
Bram Moolenaar895d9662019-01-31 21:57:21 +01001327 wp->w_redraw_top = 0; // reset for next time
1328 wp->w_redraw_bot = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329
1330 /*
1331 * When only displaying the lines at the top, set top_end. Used when
1332 * window has scrolled down for msg_scrolled.
1333 */
1334 if (type == REDRAW_TOP)
1335 {
1336 j = 0;
1337 for (i = 0; i < wp->w_lines_valid; ++i)
1338 {
1339 j += wp->w_lines[i].wl_size;
1340 if (j >= wp->w_upd_rows)
1341 {
1342 top_end = j;
1343 break;
1344 }
1345 }
1346 if (top_end == 0)
1347 /* not found (cannot happen?): redraw everything */
1348 type = NOT_VALID;
1349 else
1350 /* top area defined, the rest is VALID */
1351 type = VALID;
1352 }
1353
Bram Moolenaar367329b2007-08-30 11:53:22 +00001354 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001355 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1356 * non-zero and thus not FALSE) will indicate that screenclear() was not
1357 * called. */
1358 if (screen_cleared)
1359 screen_cleared = MAYBE;
1360
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 /*
1362 * If there are no changes on the screen that require a complete redraw,
1363 * handle three cases:
1364 * 1: we are off the top of the screen by a few lines: scroll down
1365 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1366 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1367 * w_lines[] that needs updating.
1368 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001369 if ((type == VALID || type == SOME_VALID
1370 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371#ifdef FEAT_DIFF
1372 && !wp->w_botfill && !wp->w_old_botfill
1373#endif
1374 )
1375 {
1376 if (mod_top != 0 && wp->w_topline == mod_top)
1377 {
1378 /*
1379 * w_topline is the first changed line, the scrolling will be done
1380 * further down.
1381 */
1382 }
1383 else if (wp->w_lines[0].wl_valid
1384 && (wp->w_topline < wp->w_lines[0].wl_lnum
1385#ifdef FEAT_DIFF
1386 || (wp->w_topline == wp->w_lines[0].wl_lnum
1387 && wp->w_topfill > wp->w_old_topfill)
1388#endif
1389 ))
1390 {
1391 /*
1392 * New topline is above old topline: May scroll down.
1393 */
1394#ifdef FEAT_FOLDING
1395 if (hasAnyFolding(wp))
1396 {
1397 linenr_T ln;
1398
1399 /* count the number of lines we are off, counting a sequence
1400 * of folded lines as one */
1401 j = 0;
1402 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1403 {
1404 ++j;
1405 if (j >= wp->w_height - 2)
1406 break;
1407 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1408 }
1409 }
1410 else
1411#endif
1412 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1413 if (j < wp->w_height - 2) /* not too far off */
1414 {
1415 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1416#ifdef FEAT_DIFF
1417 /* insert extra lines for previously invisible filler lines */
1418 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1419 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1420 - wp->w_old_topfill;
1421#endif
1422 if (i < wp->w_height - 2) /* less than a screen off */
1423 {
1424 /*
1425 * Try to insert the correct number of lines.
1426 * If not the last window, delete the lines at the bottom.
1427 * win_ins_lines may fail when the terminal can't do it.
1428 */
1429 if (i > 0)
1430 check_for_delay(FALSE);
1431 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1432 {
1433 if (wp->w_lines_valid != 0)
1434 {
1435 /* Need to update rows that are new, stop at the
1436 * first one that scrolled down. */
1437 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439
1440 /* Move the entries that were scrolled, disable
1441 * the entries for the lines to be redrawn. */
1442 if ((wp->w_lines_valid += j) > wp->w_height)
1443 wp->w_lines_valid = wp->w_height;
1444 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1445 wp->w_lines[idx] = wp->w_lines[idx - j];
1446 while (idx >= 0)
1447 wp->w_lines[idx--].wl_valid = FALSE;
1448 }
1449 }
1450 else
1451 mid_start = 0; /* redraw all lines */
1452 }
1453 else
1454 mid_start = 0; /* redraw all lines */
1455 }
1456 else
1457 mid_start = 0; /* redraw all lines */
1458 }
1459 else
1460 {
1461 /*
1462 * New topline is at or below old topline: May scroll up.
1463 * When topline didn't change, find first entry in w_lines[] that
1464 * needs updating.
1465 */
1466
1467 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1468 j = -1;
1469 row = 0;
1470 for (i = 0; i < wp->w_lines_valid; i++)
1471 {
1472 if (wp->w_lines[i].wl_valid
1473 && wp->w_lines[i].wl_lnum == wp->w_topline)
1474 {
1475 j = i;
1476 break;
1477 }
1478 row += wp->w_lines[i].wl_size;
1479 }
1480 if (j == -1)
1481 {
1482 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1483 * lines */
1484 mid_start = 0;
1485 }
1486 else
1487 {
1488 /*
1489 * Try to delete the correct number of lines.
1490 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1491 */
1492#ifdef FEAT_DIFF
1493 /* If the topline didn't change, delete old filler lines,
1494 * otherwise delete filler lines of the new topline... */
1495 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1496 row += wp->w_old_topfill;
1497 else
1498 row += diff_check_fill(wp, wp->w_topline);
1499 /* ... but don't delete new filler lines. */
1500 row -= wp->w_topfill;
1501#endif
1502 if (row > 0)
1503 {
1504 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001505 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1506 == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 bot_start = wp->w_height - row;
1508 else
1509 mid_start = 0; /* redraw all lines */
1510 }
1511 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1512 {
1513 /*
1514 * Skip the lines (below the deleted lines) that are still
1515 * valid and don't need redrawing. Copy their info
1516 * upwards, to compensate for the deleted lines. Set
1517 * bot_start to the first row that needs redrawing.
1518 */
1519 bot_start = 0;
1520 idx = 0;
1521 for (;;)
1522 {
1523 wp->w_lines[idx] = wp->w_lines[j];
1524 /* stop at line that didn't fit, unless it is still
1525 * valid (no lines deleted) */
1526 if (row > 0 && bot_start + row
1527 + (int)wp->w_lines[j].wl_size > wp->w_height)
1528 {
1529 wp->w_lines_valid = idx + 1;
1530 break;
1531 }
1532 bot_start += wp->w_lines[idx++].wl_size;
1533
1534 /* stop at the last valid entry in w_lines[].wl_size */
1535 if (++j >= wp->w_lines_valid)
1536 {
1537 wp->w_lines_valid = idx;
1538 break;
1539 }
1540 }
1541#ifdef FEAT_DIFF
1542 /* Correct the first entry for filler lines at the top
1543 * when it won't get updated below. */
1544 if (wp->w_p_diff && bot_start > 0)
1545 wp->w_lines[0].wl_size =
1546 plines_win_nofill(wp, wp->w_topline, TRUE)
1547 + wp->w_topfill;
1548#endif
1549 }
1550 }
1551 }
1552
1553 /* When starting redraw in the first line, redraw all lines. When
1554 * there is only one window it's probably faster to clear the screen
1555 * first. */
1556 if (mid_start == 0)
1557 {
1558 mid_end = wp->w_height;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001559 if (ONE_WINDOW && !WIN_IS_POPUP(wp))
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001560 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001561 /* Clear the screen when it was not done by win_del_lines() or
1562 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1563 * then. */
1564 if (screen_cleared != TRUE)
1565 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001566 /* The screen was cleared, redraw the tab pages line. */
1567 if (redraw_tabline)
1568 draw_tabline();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001571
1572 /* When win_del_lines() or win_ins_lines() caused the screen to be
1573 * cleared (only happens for the first window) or when screenclear()
1574 * was called directly above, "must_redraw" will have been set to
1575 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1576 if (screen_cleared == TRUE)
1577 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 }
1579 else
1580 {
1581 /* Not VALID or INVERTED: redraw all lines. */
1582 mid_start = 0;
1583 mid_end = wp->w_height;
1584 }
1585
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001586 if (type == SOME_VALID)
1587 {
1588 /* SOME_VALID: redraw all lines. */
1589 mid_start = 0;
1590 mid_end = wp->w_height;
1591 type = NOT_VALID;
1592 }
1593
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 /* check if we are updating or removing the inverted part */
1595 if ((VIsual_active && buf == curwin->w_buffer)
1596 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1597 {
1598 linenr_T from, to;
1599
1600 if (VIsual_active)
1601 {
1602 if (VIsual_active
1603 && (VIsual_mode != wp->w_old_visual_mode
1604 || type == INVERTED_ALL))
1605 {
1606 /*
1607 * If the type of Visual selection changed, redraw the whole
1608 * selection. Also when the ownership of the X selection is
1609 * gained or lost.
1610 */
1611 if (curwin->w_cursor.lnum < VIsual.lnum)
1612 {
1613 from = curwin->w_cursor.lnum;
1614 to = VIsual.lnum;
1615 }
1616 else
1617 {
1618 from = VIsual.lnum;
1619 to = curwin->w_cursor.lnum;
1620 }
1621 /* redraw more when the cursor moved as well */
1622 if (wp->w_old_cursor_lnum < from)
1623 from = wp->w_old_cursor_lnum;
1624 if (wp->w_old_cursor_lnum > to)
1625 to = wp->w_old_cursor_lnum;
1626 if (wp->w_old_visual_lnum < from)
1627 from = wp->w_old_visual_lnum;
1628 if (wp->w_old_visual_lnum > to)
1629 to = wp->w_old_visual_lnum;
1630 }
1631 else
1632 {
1633 /*
1634 * Find the line numbers that need to be updated: The lines
1635 * between the old cursor position and the current cursor
1636 * position. Also check if the Visual position changed.
1637 */
1638 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1639 {
1640 from = curwin->w_cursor.lnum;
1641 to = wp->w_old_cursor_lnum;
1642 }
1643 else
1644 {
1645 from = wp->w_old_cursor_lnum;
1646 to = curwin->w_cursor.lnum;
1647 if (from == 0) /* Visual mode just started */
1648 from = to;
1649 }
1650
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001651 if (VIsual.lnum != wp->w_old_visual_lnum
1652 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 {
1654 if (wp->w_old_visual_lnum < from
1655 && wp->w_old_visual_lnum != 0)
1656 from = wp->w_old_visual_lnum;
1657 if (wp->w_old_visual_lnum > to)
1658 to = wp->w_old_visual_lnum;
1659 if (VIsual.lnum < from)
1660 from = VIsual.lnum;
1661 if (VIsual.lnum > to)
1662 to = VIsual.lnum;
1663 }
1664 }
1665
1666 /*
1667 * If in block mode and changed column or curwin->w_curswant:
1668 * update all lines.
1669 * First compute the actual start and end column.
1670 */
1671 if (VIsual_mode == Ctrl_V)
1672 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001673 colnr_T fromc, toc;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001674#if defined(FEAT_LINEBREAK)
Bram Moolenaar404406a2014-10-09 13:24:43 +02001675 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676
Bram Moolenaar404406a2014-10-09 13:24:43 +02001677 if (curwin->w_p_lbr)
1678 ve_flags = VE_ALL;
1679#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001681#if defined(FEAT_LINEBREAK)
Bram Moolenaar404406a2014-10-09 13:24:43 +02001682 ve_flags = save_ve_flags;
1683#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 ++toc;
1685 if (curwin->w_curswant == MAXCOL)
1686 toc = MAXCOL;
1687
1688 if (fromc != wp->w_old_cursor_fcol
1689 || toc != wp->w_old_cursor_lcol)
1690 {
1691 if (from > VIsual.lnum)
1692 from = VIsual.lnum;
1693 if (to < VIsual.lnum)
1694 to = VIsual.lnum;
1695 }
1696 wp->w_old_cursor_fcol = fromc;
1697 wp->w_old_cursor_lcol = toc;
1698 }
1699 }
1700 else
1701 {
1702 /* Use the line numbers of the old Visual area. */
1703 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1704 {
1705 from = wp->w_old_cursor_lnum;
1706 to = wp->w_old_visual_lnum;
1707 }
1708 else
1709 {
1710 from = wp->w_old_visual_lnum;
1711 to = wp->w_old_cursor_lnum;
1712 }
1713 }
1714
1715 /*
1716 * There is no need to update lines above the top of the window.
1717 */
1718 if (from < wp->w_topline)
1719 from = wp->w_topline;
1720
1721 /*
1722 * If we know the value of w_botline, use it to restrict the update to
1723 * the lines that are visible in the window.
1724 */
1725 if (wp->w_valid & VALID_BOTLINE)
1726 {
1727 if (from >= wp->w_botline)
1728 from = wp->w_botline - 1;
1729 if (to >= wp->w_botline)
1730 to = wp->w_botline - 1;
1731 }
1732
1733 /*
1734 * Find the minimal part to be updated.
1735 * Watch out for scrolling that made entries in w_lines[] invalid.
1736 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1737 * top_end; need to redraw from top_end to the "to" line.
1738 * A middle mouse click with a Visual selection may change the text
1739 * above the Visual area and reset wl_valid, do count these for
1740 * mid_end (in srow).
1741 */
1742 if (mid_start > 0)
1743 {
1744 lnum = wp->w_topline;
1745 idx = 0;
1746 srow = 0;
1747 if (scrolled_down)
1748 mid_start = top_end;
1749 else
1750 mid_start = 0;
1751 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1752 {
1753 if (wp->w_lines[idx].wl_valid)
1754 mid_start += wp->w_lines[idx].wl_size;
1755 else if (!scrolled_down)
1756 srow += wp->w_lines[idx].wl_size;
1757 ++idx;
1758# ifdef FEAT_FOLDING
1759 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1760 lnum = wp->w_lines[idx].wl_lnum;
1761 else
1762# endif
1763 ++lnum;
1764 }
1765 srow += mid_start;
1766 mid_end = wp->w_height;
1767 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1768 {
1769 if (wp->w_lines[idx].wl_valid
1770 && wp->w_lines[idx].wl_lnum >= to + 1)
1771 {
1772 /* Only update until first row of this line */
1773 mid_end = srow;
1774 break;
1775 }
1776 srow += wp->w_lines[idx].wl_size;
1777 }
1778 }
1779 }
1780
1781 if (VIsual_active && buf == curwin->w_buffer)
1782 {
1783 wp->w_old_visual_mode = VIsual_mode;
1784 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1785 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001786 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 wp->w_old_curswant = curwin->w_curswant;
1788 }
1789 else
1790 {
1791 wp->w_old_visual_mode = 0;
1792 wp->w_old_cursor_lnum = 0;
1793 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001794 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796
1797#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1798 /* reset got_int, otherwise regexp won't work */
1799 save_got_int = got_int;
1800 got_int = 0;
1801#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001802#ifdef SYN_TIME_LIMIT
1803 /* Set the time limit to 'redrawtime'. */
1804 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02001805 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001806#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807#ifdef FEAT_FOLDING
1808 win_foldinfo.fi_level = 0;
1809#endif
1810
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001811#ifdef FEAT_MENU
1812 /*
1813 * Draw the window toolbar, if there is one.
1814 * TODO: only when needed.
1815 */
1816 if (winbar_height(wp) > 0)
1817 redraw_win_toolbar(wp);
1818#endif
1819
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 /*
1821 * Update all the window rows.
1822 */
1823 idx = 0; /* first entry in w_lines[].wl_size */
1824 row = 0;
1825 srow = 0;
1826 lnum = wp->w_topline; /* first line shown in window */
1827 for (;;)
1828 {
1829 /* stop updating when reached the end of the window (check for _past_
1830 * the end of the window is at the end of the loop) */
1831 if (row == wp->w_height)
1832 {
1833 didline = TRUE;
1834 break;
1835 }
1836
1837 /* stop updating when hit the end of the file */
1838 if (lnum > buf->b_ml.ml_line_count)
1839 {
1840 eof = TRUE;
1841 break;
1842 }
1843
1844 /* Remember the starting row of the line that is going to be dealt
1845 * with. It is used further down when the line doesn't fit. */
1846 srow = row;
1847
1848 /*
1849 * Update a line when it is in an area that needs updating, when it
1850 * has changes or w_lines[idx] is invalid.
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02001851 * "bot_start" may be halfway a wrapped line after using
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 * win_del_lines(), check if the current line includes it.
1853 * When syntax folding is being used, the saved syntax states will
1854 * already have been updated, we can't see where the syntax state is
1855 * the same again, just update until the end of the window.
1856 */
1857 if (row < top_end
1858 || (row >= mid_start && row < mid_end)
1859#ifdef FEAT_SEARCH_EXTRA
1860 || top_to_mod
1861#endif
1862 || idx >= wp->w_lines_valid
1863 || (row + wp->w_lines[idx].wl_size > bot_start)
1864 || (mod_top != 0
1865 && (lnum == mod_top
1866 || (lnum >= mod_top
1867 && (lnum < mod_bot
1868#ifdef FEAT_SYN_HL
1869 || did_update == DID_FOLD
1870 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001871 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 && (
1873# ifdef FEAT_FOLDING
1874 (foldmethodIsSyntax(wp)
1875 && hasAnyFolding(wp)) ||
1876# endif
1877 syntax_check_changed(lnum)))
1878#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001879#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001880 /* match in fixed position might need redraw
1881 * if lines were inserted or deleted */
1882 || (wp->w_match_head != NULL
1883 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001884#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 )))))
1886 {
1887#ifdef FEAT_SEARCH_EXTRA
1888 if (lnum == mod_top)
1889 top_to_mod = FALSE;
1890#endif
1891
1892 /*
1893 * When at start of changed lines: May scroll following lines
1894 * up or down to minimize redrawing.
1895 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001896 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 */
1898 if (lnum == mod_top
1899 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001900 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 {
1902 int old_rows = 0;
1903 int new_rows = 0;
1904 int xtra_rows;
1905 linenr_T l;
1906
1907 /* Count the old number of window rows, using w_lines[], which
1908 * should still contain the sizes for the lines as they are
1909 * currently displayed. */
1910 for (i = idx; i < wp->w_lines_valid; ++i)
1911 {
1912 /* Only valid lines have a meaningful wl_lnum. Invalid
1913 * lines are part of the changed area. */
1914 if (wp->w_lines[i].wl_valid
1915 && wp->w_lines[i].wl_lnum == mod_bot)
1916 break;
1917 old_rows += wp->w_lines[i].wl_size;
1918#ifdef FEAT_FOLDING
1919 if (wp->w_lines[i].wl_valid
1920 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1921 {
1922 /* Must have found the last valid entry above mod_bot.
1923 * Add following invalid entries. */
1924 ++i;
1925 while (i < wp->w_lines_valid
1926 && !wp->w_lines[i].wl_valid)
1927 old_rows += wp->w_lines[i++].wl_size;
1928 break;
1929 }
1930#endif
1931 }
1932
1933 if (i >= wp->w_lines_valid)
1934 {
1935 /* We can't find a valid line below the changed lines,
1936 * need to redraw until the end of the window.
1937 * Inserting/deleting lines has no use. */
1938 bot_start = 0;
1939 }
1940 else
1941 {
1942 /* Able to count old number of rows: Count new window
1943 * rows, and may insert/delete lines */
1944 j = idx;
1945 for (l = lnum; l < mod_bot; ++l)
1946 {
1947#ifdef FEAT_FOLDING
1948 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1949 ++new_rows;
1950 else
1951#endif
1952#ifdef FEAT_DIFF
1953 if (l == wp->w_topline)
1954 new_rows += plines_win_nofill(wp, l, TRUE)
1955 + wp->w_topfill;
1956 else
1957#endif
1958 new_rows += plines_win(wp, l, TRUE);
1959 ++j;
1960 if (new_rows > wp->w_height - row - 2)
1961 {
1962 /* it's getting too much, must redraw the rest */
1963 new_rows = 9999;
1964 break;
1965 }
1966 }
1967 xtra_rows = new_rows - old_rows;
1968 if (xtra_rows < 0)
1969 {
1970 /* May scroll text up. If there is not enough
1971 * remaining text or scrolling fails, must redraw the
1972 * rest. If scrolling works, must redraw the text
1973 * below the scrolled text. */
1974 if (row - xtra_rows >= wp->w_height - 2)
1975 mod_bot = MAXLNUM;
1976 else
1977 {
1978 check_for_delay(FALSE);
1979 if (win_del_lines(wp, row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001980 -xtra_rows, FALSE, FALSE, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 mod_bot = MAXLNUM;
1982 else
1983 bot_start = wp->w_height + xtra_rows;
1984 }
1985 }
1986 else if (xtra_rows > 0)
1987 {
1988 /* May scroll text down. If there is not enough
1989 * remaining text of scrolling fails, must redraw the
1990 * rest. */
1991 if (row + xtra_rows >= wp->w_height - 2)
1992 mod_bot = MAXLNUM;
1993 else
1994 {
1995 check_for_delay(FALSE);
1996 if (win_ins_lines(wp, row + old_rows,
1997 xtra_rows, FALSE, FALSE) == FAIL)
1998 mod_bot = MAXLNUM;
1999 else if (top_end > row + old_rows)
2000 /* Scrolled the part at the top that requires
2001 * updating down. */
2002 top_end += xtra_rows;
2003 }
2004 }
2005
2006 /* When not updating the rest, may need to move w_lines[]
2007 * entries. */
2008 if (mod_bot != MAXLNUM && i != j)
2009 {
2010 if (j < i)
2011 {
2012 int x = row + new_rows;
2013
2014 /* move entries in w_lines[] upwards */
2015 for (;;)
2016 {
2017 /* stop at last valid entry in w_lines[] */
2018 if (i >= wp->w_lines_valid)
2019 {
2020 wp->w_lines_valid = j;
2021 break;
2022 }
2023 wp->w_lines[j] = wp->w_lines[i];
2024 /* stop at a line that won't fit */
2025 if (x + (int)wp->w_lines[j].wl_size
2026 > wp->w_height)
2027 {
2028 wp->w_lines_valid = j + 1;
2029 break;
2030 }
2031 x += wp->w_lines[j++].wl_size;
2032 ++i;
2033 }
2034 if (bot_start > x)
2035 bot_start = x;
2036 }
2037 else /* j > i */
2038 {
2039 /* move entries in w_lines[] downwards */
2040 j -= i;
2041 wp->w_lines_valid += j;
2042 if (wp->w_lines_valid > wp->w_height)
2043 wp->w_lines_valid = wp->w_height;
2044 for (i = wp->w_lines_valid; i - j >= idx; --i)
2045 wp->w_lines[i] = wp->w_lines[i - j];
2046
2047 /* The w_lines[] entries for inserted lines are
2048 * now invalid, but wl_size may be used above.
2049 * Reset to zero. */
2050 while (i >= idx)
2051 {
2052 wp->w_lines[i].wl_size = 0;
2053 wp->w_lines[i--].wl_valid = FALSE;
2054 }
2055 }
2056 }
2057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 }
2059
2060#ifdef FEAT_FOLDING
2061 /*
2062 * When lines are folded, display one line for all of them.
2063 * Otherwise, display normally (can be several display lines when
2064 * 'wrap' is on).
2065 */
2066 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2067 if (fold_count != 0)
2068 {
2069 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2070 ++row;
2071 --fold_count;
2072 wp->w_lines[idx].wl_folded = TRUE;
2073 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2074# ifdef FEAT_SYN_HL
2075 did_update = DID_FOLD;
2076# endif
2077 }
2078 else
2079#endif
2080 if (idx < wp->w_lines_valid
2081 && wp->w_lines[idx].wl_valid
2082 && wp->w_lines[idx].wl_lnum == lnum
2083 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002084 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002085 && !WIN_IS_POPUP(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 && srow + wp->w_lines[idx].wl_size > wp->w_height
2087#ifdef FEAT_DIFF
2088 && diff_check_fill(wp, lnum) == 0
2089#endif
2090 )
2091 {
2092 /* This line is not going to fit. Don't draw anything here,
2093 * will draw "@ " lines below. */
2094 row = wp->w_height + 1;
2095 }
2096 else
2097 {
2098#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarbbca7732019-07-24 18:13:16 +02002099 prepare_search_hl(wp, &search_hl, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100#endif
2101#ifdef FEAT_SYN_HL
2102 /* Let the syntax stuff know we skipped a few lines. */
2103 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002104 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 syntax_end_parsing(syntax_last_parsed + 1);
2106#endif
2107
2108 /*
2109 * Display one line.
2110 */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002111 row = win_line(wp, lnum, srow, wp->w_height,
2112 mod_top == 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113
2114#ifdef FEAT_FOLDING
2115 wp->w_lines[idx].wl_folded = FALSE;
2116 wp->w_lines[idx].wl_lastlnum = lnum;
2117#endif
2118#ifdef FEAT_SYN_HL
2119 did_update = DID_LINE;
2120 syntax_last_parsed = lnum;
2121#endif
2122 }
2123
2124 wp->w_lines[idx].wl_lnum = lnum;
2125 wp->w_lines[idx].wl_valid = TRUE;
Bram Moolenaar0e19fc02017-10-28 14:45:16 +02002126
2127 /* Past end of the window or end of the screen. Note that after
2128 * resizing wp->w_height may be end up too big. That's a problem
2129 * elsewhere, but prevent a crash here. */
2130 if (row > wp->w_height || row + wp->w_winrow >= Rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 {
2132 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002133 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2135 ++idx;
2136 break;
2137 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002138 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 wp->w_lines[idx].wl_size = row - srow;
2140 ++idx;
2141#ifdef FEAT_FOLDING
2142 lnum += fold_count + 1;
2143#else
2144 ++lnum;
2145#endif
2146 }
2147 else
2148 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002149 if (wp->w_p_rnu)
2150 {
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002151#ifdef FEAT_FOLDING
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002152 // 'relativenumber' set: The text doesn't need to be drawn, but
2153 // the number column nearly always does.
Bram Moolenaar7701f302018-10-02 21:20:32 +02002154 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2155 if (fold_count != 0)
Bram Moolenaar7701f302018-10-02 21:20:32 +02002156 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
Bram Moolenaar7701f302018-10-02 21:20:32 +02002157 else
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002158#endif
Bram Moolenaar7701f302018-10-02 21:20:32 +02002159 (void)win_line(wp, lnum, srow, wp->w_height, TRUE, TRUE);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002160 }
2161
2162 // This line does not need to be drawn, advance to the next one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 row += wp->w_lines[idx++].wl_size;
2164 if (row > wp->w_height) /* past end of screen */
2165 break;
2166#ifdef FEAT_FOLDING
2167 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2168#else
2169 ++lnum;
2170#endif
2171#ifdef FEAT_SYN_HL
2172 did_update = DID_NONE;
2173#endif
2174 }
2175
2176 if (lnum > buf->b_ml.ml_line_count)
2177 {
2178 eof = TRUE;
2179 break;
2180 }
2181 }
2182 /*
2183 * End of loop over all window lines.
2184 */
2185
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002186#ifdef FEAT_VTP
2187 /* Rewrite the character at the end of the screen line. */
2188 if (use_vtp())
2189 {
2190 int i;
2191
2192 for (i = 0; i < Rows; ++i)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002193 if (enc_utf8)
2194 if ((*mb_off2cells)(LineOffset[i] + Columns - 2,
2195 LineOffset[i] + screen_Columns) > 1)
2196 screen_draw_rectangle(i, Columns - 2, 1, 2, FALSE);
2197 else
2198 screen_draw_rectangle(i, Columns - 1, 1, 1, FALSE);
2199 else
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002200 screen_char(LineOffset[i] + Columns - 1, i, Columns - 1);
2201 }
2202#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203
2204 if (idx > wp->w_lines_valid)
2205 wp->w_lines_valid = idx;
2206
2207#ifdef FEAT_SYN_HL
2208 /*
2209 * Let the syntax stuff know we stop parsing here.
2210 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002211 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 syntax_end_parsing(syntax_last_parsed + 1);
2213#endif
2214
2215 /*
2216 * If we didn't hit the end of the file, and we didn't finish the last
2217 * line we were working on, then the line didn't fit.
2218 */
2219 wp->w_empty_rows = 0;
2220#ifdef FEAT_DIFF
2221 wp->w_filler_rows = 0;
2222#endif
2223 if (!eof && !didline)
2224 {
2225 if (lnum == wp->w_topline)
2226 {
2227 /*
2228 * Single line that does not fit!
2229 * Don't overwrite it, it can be edited.
2230 */
2231 wp->w_botline = lnum + 1;
2232 }
2233#ifdef FEAT_DIFF
2234 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2235 {
2236 /* Window ends in filler lines. */
2237 wp->w_botline = lnum;
2238 wp->w_filler_rows = wp->w_height - srow;
2239 }
2240#endif
Bram Moolenaar17146962019-05-30 00:12:11 +02002241#ifdef FEAT_TEXT_PROP
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002242 else if (WIN_IS_POPUP(wp))
Bram Moolenaar17146962019-05-30 00:12:11 +02002243 {
2244 // popup line that doesn't fit is left as-is
2245 wp->w_botline = lnum;
2246 }
2247#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002248 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2249 {
2250 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2251
2252 /*
2253 * Last line isn't finished: Display "@@@" in the last screen line.
2254 */
Bram Moolenaar53f81742017-09-22 14:35:51 +02002255 screen_puts_len((char_u *)"@@", 2, scr_row, wp->w_wincol,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002256 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002257 screen_fill(scr_row, scr_row + 1,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002258 (int)wp->w_wincol + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002259 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002260 set_empty_rows(wp, srow);
2261 wp->w_botline = lnum;
2262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2264 {
2265 /*
2266 * Last line isn't finished: Display "@@@" at the end.
2267 */
2268 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2269 W_WINROW(wp) + wp->w_height,
2270 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002271 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 set_empty_rows(wp, srow);
2273 wp->w_botline = lnum;
2274 }
2275 else
2276 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002277 win_draw_end(wp, '@', ' ', TRUE, srow, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 wp->w_botline = lnum;
2279 }
2280 }
2281 else
2282 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 if (eof) /* we hit the end of the file */
2285 {
2286 wp->w_botline = buf->b_ml.ml_line_count + 1;
2287#ifdef FEAT_DIFF
2288 j = diff_check_fill(wp, wp->w_botline);
2289 if (j > 0 && !wp->w_botfill)
2290 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002291 // Display filler lines at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 if (char2cells(fill_diff) > 1)
2293 i = '-';
2294 else
2295 i = fill_diff;
2296 if (row + j > wp->w_height)
2297 j = wp->w_height - row;
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002298 win_draw_end(wp, i, i, TRUE, row, row + (int)j, HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 row += j;
2300 }
2301#endif
2302 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002303 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 wp->w_botline = lnum;
2305
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002306 // Make sure the rest of the screen is blank
2307 // put '~'s on rows that aren't part of the file.
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002308 win_draw_end(wp, WIN_IS_POPUP(wp) ? ' ' : '~',
2309 ' ', FALSE, row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 }
2311
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002312#ifdef SYN_TIME_LIMIT
2313 syn_set_timeout(NULL);
2314#endif
2315
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 /* Reset the type of redrawing required, the window has been updated. */
2317 wp->w_redr_type = 0;
2318#ifdef FEAT_DIFF
2319 wp->w_old_topfill = wp->w_topfill;
2320 wp->w_old_botfill = wp->w_botfill;
2321#endif
2322
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002323 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 {
2325 /*
2326 * There is a trick with w_botline. If we invalidate it on each
2327 * change that might modify it, this will cause a lot of expensive
2328 * calls to plines() in update_topline() each time. Therefore the
2329 * value of w_botline is often approximated, and this value is used to
2330 * compute the value of w_topline. If the value of w_botline was
2331 * wrong, check that the value of w_topline is correct (cursor is on
2332 * the visible part of the text). If it's not, we need to redraw
2333 * again. Mostly this just means scrolling up a few lines, so it
2334 * doesn't look too bad. Only do this for the current window (where
2335 * changes are relevant).
2336 */
2337 wp->w_valid |= VALID_BOTLINE;
2338 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2339 {
2340 recursive = TRUE;
2341 curwin->w_valid &= ~VALID_TOPLINE;
2342 update_topline(); /* may invalidate w_botline again */
2343 if (must_redraw != 0)
2344 {
2345 /* Don't update for changes in buffer again. */
2346 i = curbuf->b_mod_set;
2347 curbuf->b_mod_set = FALSE;
2348 win_update(curwin);
2349 must_redraw = 0;
2350 curbuf->b_mod_set = i;
2351 }
2352 recursive = FALSE;
2353 }
2354 }
2355
2356#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2357 /* restore got_int, unless CTRL-C was hit while redrawing */
2358 if (!got_int)
2359 got_int = save_got_int;
2360#endif
2361}
2362
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363/*
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002364 * Call screen_fill() with the columns adjusted for 'rightleft' if needed.
2365 * Return the new offset.
2366 */
2367 static int
2368screen_fill_end(
2369 win_T *wp,
2370 int c1,
2371 int c2,
2372 int off,
2373 int width,
2374 int row,
2375 int endrow,
2376 int attr)
2377{
2378 int nn = off + width;
2379
2380 if (nn > wp->w_width)
2381 nn = wp->w_width;
2382#ifdef FEAT_RIGHTLEFT
2383 if (wp->w_p_rl)
2384 {
2385 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2386 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - off,
2387 c1, c2, attr);
2388 }
2389 else
2390#endif
2391 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2392 wp->w_wincol + off, (int)wp->w_wincol + nn,
2393 c1, c2, attr);
2394 return nn;
2395}
2396
2397/*
2398 * Clear lines near the end the window and mark the unused lines with "c1".
2399 * use "c2" as the filler character.
2400 * When "draw_margin" is TRUE then draw the sign, fold and number columns.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 */
2402 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002403win_draw_end(
2404 win_T *wp,
2405 int c1,
2406 int c2,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002407 int draw_margin,
Bram Moolenaar05540972016-01-30 20:31:25 +01002408 int row,
2409 int endrow,
2410 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 int n = 0;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002413 int attr = HL_ATTR(hl);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002414 int wcr_attr = get_wcr_attr(wp);
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002415
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002416 attr = hl_combine_attr(wcr_attr, attr);
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002417
2418 if (draw_margin)
2419 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002420#ifdef FEAT_FOLDING
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002421 int fdc = compute_foldcolumn(wp, 0);
2422
2423 if (fdc > 0)
2424 // draw the fold column
2425 n = screen_fill_end(wp, ' ', ' ', n, fdc,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002426 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_FC)));
Bram Moolenaar1c934292015-01-27 16:39:29 +01002427#endif
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002428#ifdef FEAT_SIGNS
2429 if (signcolumn_on(wp))
2430 // draw the sign column
2431 n = screen_fill_end(wp, ' ', ' ', n, 2,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002432 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_SC)));
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002433#endif
2434 if ((wp->w_p_nu || wp->w_p_rnu)
2435 && vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
2436 // draw the number column
2437 n = screen_fill_end(wp, ' ', ' ', n, number_width(wp) + 1,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002438 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_N)));
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440
2441#ifdef FEAT_RIGHTLEFT
2442 if (wp->w_p_rl)
2443 {
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 wp->w_wincol, W_ENDCOL(wp) - 1 - n,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002446 c2, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002448 W_ENDCOL(wp) - 1 - n, W_ENDCOL(wp) - n,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002449 c1, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 }
2451 else
2452#endif
2453 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002455 wp->w_wincol + n, (int)W_ENDCOL(wp),
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002456 c1, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 }
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002458
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 set_empty_rows(wp, row);
2460}
2461
Bram Moolenaar1a384422010-07-14 19:53:30 +02002462#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02002463/*
2464 * Advance **color_cols and return TRUE when there are columns to draw.
2465 */
2466 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002467advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002468{
2469 while (**color_cols >= 0 && vcol > **color_cols)
2470 ++*color_cols;
2471 return (**color_cols >= 0);
2472}
2473#endif
2474
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002475#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
2476/*
2477 * Copy "text" to ScreenLines using "attr".
2478 * Returns the next screen column.
2479 */
2480 static int
2481text_to_screenline(win_T *wp, char_u *text, int col)
2482{
2483 int off = (int)(current_ScreenLine - ScreenLines);
2484
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002485 if (has_mbyte)
2486 {
2487 int cells;
2488 int u8c, u8cc[MAX_MCO];
2489 int i;
2490 int idx;
2491 int c_len;
2492 char_u *p;
2493# ifdef FEAT_ARABIC
2494 int prev_c = 0; /* previous Arabic character */
2495 int prev_c1 = 0; /* first composing char for prev_c */
2496# endif
2497
2498# ifdef FEAT_RIGHTLEFT
2499 if (wp->w_p_rl)
2500 idx = off;
2501 else
2502# endif
2503 idx = off + col;
2504
2505 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2506 for (p = text; *p != NUL; )
2507 {
2508 cells = (*mb_ptr2cells)(p);
2509 c_len = (*mb_ptr2len)(p);
Bram Moolenaar02631462017-09-22 15:20:32 +02002510 if (col + cells > wp->w_width
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002511# ifdef FEAT_RIGHTLEFT
2512 - (wp->w_p_rl ? col : 0)
2513# endif
2514 )
2515 break;
2516 ScreenLines[idx] = *p;
2517 if (enc_utf8)
2518 {
2519 u8c = utfc_ptr2char(p, u8cc);
2520 if (*p < 0x80 && u8cc[0] == 0)
2521 {
2522 ScreenLinesUC[idx] = 0;
2523#ifdef FEAT_ARABIC
2524 prev_c = u8c;
2525#endif
2526 }
2527 else
2528 {
2529#ifdef FEAT_ARABIC
2530 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2531 {
2532 /* Do Arabic shaping. */
2533 int pc, pc1, nc;
2534 int pcc[MAX_MCO];
2535 int firstbyte = *p;
2536
2537 /* The idea of what is the previous and next
2538 * character depends on 'rightleft'. */
2539 if (wp->w_p_rl)
2540 {
2541 pc = prev_c;
2542 pc1 = prev_c1;
2543 nc = utf_ptr2char(p + c_len);
2544 prev_c1 = u8cc[0];
2545 }
2546 else
2547 {
2548 pc = utfc_ptr2char(p + c_len, pcc);
2549 nc = prev_c;
2550 pc1 = pcc[0];
2551 }
2552 prev_c = u8c;
2553
2554 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
2555 pc, pc1, nc);
2556 ScreenLines[idx] = firstbyte;
2557 }
2558 else
2559 prev_c = u8c;
2560#endif
2561 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar9ba61172019-01-24 18:20:17 +01002562 ScreenLinesUC[idx] = u8c;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002563 for (i = 0; i < Screen_mco; ++i)
2564 {
2565 ScreenLinesC[i][idx] = u8cc[i];
2566 if (u8cc[i] == 0)
2567 break;
2568 }
2569 }
2570 if (cells > 1)
2571 ScreenLines[idx + 1] = 0;
2572 }
2573 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2574 /* double-byte single width character */
2575 ScreenLines2[idx] = p[1];
2576 else if (cells > 1)
2577 /* double-width character */
2578 ScreenLines[idx + 1] = p[1];
2579 col += cells;
2580 idx += cells;
2581 p += c_len;
2582 }
2583 }
2584 else
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002585 {
2586 int len = (int)STRLEN(text);
2587
Bram Moolenaar02631462017-09-22 15:20:32 +02002588 if (len > wp->w_width - col)
2589 len = wp->w_width - col;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002590 if (len > 0)
2591 {
2592#ifdef FEAT_RIGHTLEFT
2593 if (wp->w_p_rl)
Bram Moolenaarc6663882019-02-22 19:14:54 +01002594 mch_memmove(current_ScreenLine, text, len);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002595 else
2596#endif
Bram Moolenaarc6663882019-02-22 19:14:54 +01002597 mch_memmove(current_ScreenLine + col, text, len);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002598 col += len;
2599 }
2600 }
2601 return col;
2602}
2603#endif
2604
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605#ifdef FEAT_FOLDING
2606/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002607 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2608 * space is available for window "wp", minus "col".
2609 */
2610 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002611compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002612{
2613 int fdc = wp->w_p_fdc;
2614 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
Bram Moolenaar02631462017-09-22 15:20:32 +02002615 int wwidth = wp->w_width;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002616
2617 if (fdc > wwidth - (col + wmw))
2618 fdc = wwidth - (col + wmw);
2619 return fdc;
2620}
2621
2622/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 * Display one folded line.
2624 */
2625 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002626fold_line(
2627 win_T *wp,
2628 long fold_count,
2629 foldinfo_T *foldinfo,
2630 linenr_T lnum,
2631 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002633 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 pos_T *top, *bot;
2635 linenr_T lnume = lnum + fold_count - 1;
2636 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002637 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 int col;
2640 int txtcol;
2641 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002642 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643
2644 /* Build the fold line:
2645 * 1. Add the cmdwin_type for the command-line window
2646 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002647 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 * 4. Compose the text
2649 * 5. Add the text
2650 * 6. set highlighting for the Visual area an other text
2651 */
2652 col = 0;
2653
2654 /*
2655 * 1. Add the cmdwin_type for the command-line window
2656 * Ignores 'rightleft', this window is never right-left.
2657 */
2658#ifdef FEAT_CMDWIN
2659 if (cmdwin_type != 0 && wp == curwin)
2660 {
2661 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002662 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 if (enc_utf8)
2664 ScreenLinesUC[off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 ++col;
2666 }
2667#endif
2668
2669 /*
2670 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002671 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002673 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 if (fdc > 0)
2675 {
2676 fill_foldcolumn(buf, wp, TRUE, lnum);
2677#ifdef FEAT_RIGHTLEFT
2678 if (wp->w_p_rl)
2679 {
2680 int i;
2681
Bram Moolenaar02631462017-09-22 15:20:32 +02002682 copy_text_attr(off + wp->w_width - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002683 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 /* reverse the fold column */
2685 for (i = 0; i < fdc; ++i)
Bram Moolenaar02631462017-09-22 15:20:32 +02002686 ScreenLines[off + wp->w_width - i - 1 - col] = buf[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 }
2688 else
2689#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002690 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 col += fdc;
2692 }
2693
2694#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6f470022018-04-10 18:47:20 +02002695# define RL_MEMSET(p, v, l) \
2696 do { \
2697 if (wp->w_p_rl) \
2698 for (ri = 0; ri < l; ++ri) \
2699 ScreenAttrs[off + (wp->w_width - (p) - (l)) + ri] = v; \
2700 else \
2701 for (ri = 0; ri < l; ++ri) \
2702 ScreenAttrs[off + (p) + ri] = v; \
2703 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704#else
Bram Moolenaar6f470022018-04-10 18:47:20 +02002705# define RL_MEMSET(p, v, l) \
2706 do { \
2707 for (ri = 0; ri < l; ++ri) \
2708 ScreenAttrs[off + (p) + ri] = v; \
2709 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710#endif
2711
Bram Moolenaar64486672010-05-16 15:46:46 +02002712 /* Set all attributes of the 'number' or 'relativenumber' column and the
2713 * text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002714 RL_MEMSET(col, HL_ATTR(HLF_FL), wp->w_width - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715
2716#ifdef FEAT_SIGNS
2717 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002718 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002720 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 if (len > 0)
2722 {
2723 if (len > 2)
2724 len = 2;
2725# ifdef FEAT_RIGHTLEFT
2726 if (wp->w_p_rl)
2727 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002728 copy_text_attr(off + wp->w_width - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002729 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 else
2731# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002732 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 col += len;
2734 }
2735 }
2736#endif
2737
2738 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002739 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002741 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002743 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 if (len > 0)
2745 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002746 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002747 long num;
2748 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002749
2750 if (len > w + 1)
2751 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002752
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002753 if (wp->w_p_nu && !wp->w_p_rnu)
2754 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002755 num = (long)lnum;
2756 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002757 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002758 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002759 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002760 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002761 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002762 /* 'number' + 'relativenumber': cursor line shows absolute
2763 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002764 num = lnum;
2765 fmt = "%-*ld ";
2766 }
2767 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002768
Bram Moolenaar700e7342013-01-30 12:31:36 +01002769 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770#ifdef FEAT_RIGHTLEFT
2771 if (wp->w_p_rl)
2772 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002773 copy_text_attr(off + wp->w_width - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002774 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 else
2776#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002777 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 col += len;
2779 }
2780 }
2781
2782 /*
2783 * 4. Compose the folded-line string with 'foldtext', if set.
2784 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002785 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786
2787 txtcol = col; /* remember where text starts */
2788
2789 /*
2790 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2791 * Right-left text is put in columns 0 - number-col, normal text is put
2792 * in columns number-col - window-width.
2793 */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002794 col = text_to_screenline(wp, text, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795
2796 /* Fill the rest of the line with the fold filler */
2797#ifdef FEAT_RIGHTLEFT
2798 if (wp->w_p_rl)
2799 col -= txtcol;
2800#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02002801 while (col < wp->w_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802#ifdef FEAT_RIGHTLEFT
2803 - (wp->w_p_rl ? txtcol : 0)
2804#endif
2805 )
2806 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 if (enc_utf8)
2808 {
2809 if (fill_fold >= 0x80)
2810 {
2811 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002812 ScreenLinesC[0][off + col] = 0;
Bram Moolenaaracda04f2018-02-08 09:57:28 +01002813 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 }
2815 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002816 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002818 ScreenLines[off + col] = fill_fold;
2819 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002820 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002822 else
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002823 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 }
2825
2826 if (text != buf)
2827 vim_free(text);
2828
2829 /*
2830 * 6. set highlighting for the Visual area an other text.
2831 * If all folded lines are in the Visual area, highlight the line.
2832 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2834 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002835 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 {
2837 /* Visual is after curwin->w_cursor */
2838 top = &curwin->w_cursor;
2839 bot = &VIsual;
2840 }
2841 else
2842 {
2843 /* Visual is before curwin->w_cursor */
2844 top = &VIsual;
2845 bot = &curwin->w_cursor;
2846 }
2847 if (lnum >= top->lnum
2848 && lnume <= bot->lnum
2849 && (VIsual_mode != 'v'
2850 || ((lnum > top->lnum
2851 || (lnum == top->lnum
2852 && top->col == 0))
2853 && (lnume < bot->lnum
2854 || (lnume == bot->lnum
2855 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002856 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 {
2858 if (VIsual_mode == Ctrl_V)
2859 {
2860 /* Visual block mode: highlight the chars part of the block */
Bram Moolenaar02631462017-09-22 15:20:32 +02002861 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002863 if (wp->w_old_cursor_lcol != MAXCOL
2864 && wp->w_old_cursor_lcol + txtcol
Bram Moolenaar02631462017-09-22 15:20:32 +02002865 < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 len = wp->w_old_cursor_lcol;
2867 else
Bram Moolenaar02631462017-09-22 15:20:32 +02002868 len = wp->w_width - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002869 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002870 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 }
2872 }
2873 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002874 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 /* Set all attributes of the text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002876 RL_MEMSET(txtcol, HL_ATTR(HLF_V), wp->w_width - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 }
2879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002881#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002882 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2883 if (wp->w_p_cc_cols)
2884 {
2885 int i = 0;
2886 int j = wp->w_p_cc_cols[i];
2887 int old_txtcol = txtcol;
2888
2889 while (j > -1)
2890 {
2891 txtcol += j;
2892 if (wp->w_p_wrap)
2893 txtcol -= wp->w_skipcol;
2894 else
2895 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002896 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002897 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002898 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002899 txtcol = old_txtcol;
2900 j = wp->w_p_cc_cols[++i];
2901 }
2902 }
2903
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002904 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002905 if (wp->w_p_cuc)
2906 {
2907 txtcol += wp->w_virtcol;
2908 if (wp->w_p_wrap)
2909 txtcol -= wp->w_skipcol;
2910 else
2911 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002912 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaar85595c52008-10-02 16:04:05 +00002913 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002914 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00002915 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002916#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917
Bram Moolenaar02631462017-09-22 15:20:32 +02002918 screen_line(row + W_WINROW(wp), wp->w_wincol, (int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002919 (int)wp->w_width, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920
2921 /*
2922 * Update w_cline_height and w_cline_folded if the cursor line was
2923 * updated (saves a call to plines() later).
2924 */
2925 if (wp == curwin
2926 && lnum <= curwin->w_cursor.lnum
2927 && lnume >= curwin->w_cursor.lnum)
2928 {
2929 curwin->w_cline_row = row;
2930 curwin->w_cline_height = 1;
2931 curwin->w_cline_folded = TRUE;
2932 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2933 }
2934}
2935
2936/*
2937 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2938 */
2939 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002940copy_text_attr(
2941 int off,
2942 char_u *buf,
2943 int len,
2944 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002946 int i;
2947
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 mch_memmove(ScreenLines + off, buf, (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 if (enc_utf8)
2950 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002951 for (i = 0; i < len; ++i)
2952 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953}
2954
2955/*
2956 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002957 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 */
2959 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002960fill_foldcolumn(
2961 char_u *p,
2962 win_T *wp,
2963 int closed, /* TRUE of FALSE */
2964 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965{
2966 int i = 0;
2967 int level;
2968 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002969 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002970 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971
2972 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002973 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974
2975 level = win_foldinfo.fi_level;
2976 if (level > 0)
2977 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002978 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002979 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002980
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 /* If the column is too narrow, we start at the lowest level that
2982 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002983 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 if (first_level < 1)
2985 first_level = 1;
2986
Bram Moolenaar1c934292015-01-27 16:39:29 +01002987 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 {
2989 if (win_foldinfo.fi_lnum == lnum
2990 && first_level + i >= win_foldinfo.fi_low_level)
2991 p[i] = '-';
2992 else if (first_level == 1)
2993 p[i] = '|';
2994 else if (first_level + i <= 9)
2995 p[i] = '0' + first_level + i;
2996 else
2997 p[i] = '>';
2998 if (first_level + i == level)
2999 break;
3000 }
3001 }
3002 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01003003 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004}
3005#endif /* FEAT_FOLDING */
3006
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01003007#ifdef FEAT_TEXT_PROP
3008static textprop_T *current_text_props = NULL;
3009static buf_T *current_buf = NULL;
3010
3011 static int
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01003012text_prop_compare(const void *s1, const void *s2)
3013{
3014 int idx1, idx2;
3015 proptype_T *pt1, *pt2;
3016 colnr_T col1, col2;
3017
3018 idx1 = *(int *)s1;
3019 idx2 = *(int *)s2;
3020 pt1 = text_prop_type_by_id(current_buf, current_text_props[idx1].tp_type);
3021 pt2 = text_prop_type_by_id(current_buf, current_text_props[idx2].tp_type);
3022 if (pt1 == pt2)
3023 return 0;
3024 if (pt1 == NULL)
3025 return -1;
3026 if (pt2 == NULL)
3027 return 1;
3028 if (pt1->pt_priority != pt2->pt_priority)
3029 return pt1->pt_priority > pt2->pt_priority ? 1 : -1;
3030 col1 = current_text_props[idx1].tp_col;
3031 col2 = current_text_props[idx2].tp_col;
3032 return col1 == col2 ? 0 : col1 > col2 ? 1 : -1;
3033}
3034#endif
3035
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003036#ifdef FEAT_SIGNS
3037/*
3038 * Get information needed to display the sign in line 'lnum' in window 'wp'.
3039 * If 'nrcol' is TRUE, the sign is going to be displayed in the number column.
3040 * Otherwise the sign is going to be displayed in the sign column.
3041 */
3042 static void
3043get_sign_display_info(
3044 int nrcol,
3045 win_T *wp,
Bram Moolenaar4e038572019-07-04 18:28:35 +02003046 linenr_T lnum UNUSED,
3047 sign_attrs_T *sattr,
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003048 int wcr_attr,
3049 int row,
3050 int startrow,
Bram Moolenaarbf8c3ad2019-06-19 14:28:43 +02003051 int filler_lines UNUSED,
3052 int filler_todo UNUSED,
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003053 int *c_extrap,
3054 int *c_finalp,
3055 char_u *extra,
3056 char_u **pp_extra,
3057 int *n_extrap,
3058 int *char_attrp)
3059{
3060 int text_sign;
3061# ifdef FEAT_SIGN_ICONS
3062 int icon_sign;
3063# endif
3064
3065 // Draw two cells with the sign value or blank.
3066 *c_extrap = ' ';
3067 *c_finalp = NUL;
3068 if (nrcol)
3069 *n_extrap = number_width(wp) + 1;
3070 else
3071 {
3072 *char_attrp = hl_combine_attr(wcr_attr, HL_ATTR(HLF_SC));
3073 *n_extrap = 2;
3074 }
3075
3076 if (row == startrow
3077#ifdef FEAT_DIFF
3078 + filler_lines && filler_todo <= 0
3079#endif
3080 )
3081 {
Bram Moolenaar4e038572019-07-04 18:28:35 +02003082 text_sign = (sattr->text != NULL) ? sattr->typenr : 0;
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003083# ifdef FEAT_SIGN_ICONS
Bram Moolenaar4e038572019-07-04 18:28:35 +02003084 icon_sign = (sattr->icon != NULL) ? sattr->typenr : 0;
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003085 if (gui.in_use && icon_sign != 0)
3086 {
3087 // Use the image in this position.
Bram Moolenaar4dff4ae2019-06-19 16:31:28 +02003088 if (nrcol)
3089 {
3090 *c_extrap = NUL;
3091 sprintf((char *)extra, "%-*c ", number_width(wp), SIGN_BYTE);
3092 *pp_extra = extra;
3093 *n_extrap = (int)STRLEN(*pp_extra);
3094 }
3095 else
3096 *c_extrap = SIGN_BYTE;
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003097# ifdef FEAT_NETBEANS_INTG
Bram Moolenaar4e038572019-07-04 18:28:35 +02003098 if (netbeans_active() && (buf_signcount(wp->w_buffer, lnum) > 1))
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003099 {
Bram Moolenaar4dff4ae2019-06-19 16:31:28 +02003100 if (nrcol)
3101 {
3102 *c_extrap = NUL;
3103 sprintf((char *)extra, "%-*c ", number_width(wp),
3104 MULTISIGN_BYTE);
3105 *pp_extra = extra;
3106 *n_extrap = (int)STRLEN(*pp_extra);
3107 }
3108 else
3109 *c_extrap = MULTISIGN_BYTE;
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003110 }
3111# endif
Bram Moolenaar4dff4ae2019-06-19 16:31:28 +02003112 *c_finalp = NUL;
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003113 *char_attrp = icon_sign;
3114 }
3115 else
3116# endif
3117 if (text_sign != 0)
3118 {
Bram Moolenaar4e038572019-07-04 18:28:35 +02003119 *pp_extra = sattr->text;
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003120 if (*pp_extra != NULL)
3121 {
3122 if (nrcol)
3123 {
Bram Moolenaard6bcff42019-07-18 12:48:16 +02003124 int n, width = number_width(wp) - 2;
3125
3126 for (n = 0; n < width; n++)
3127 extra[n] = ' ';
3128 extra[n] = 0;
3129 STRCAT(extra, *pp_extra);
3130 STRCAT(extra, " ");
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003131 *pp_extra = extra;
3132 }
3133 *c_extrap = NUL;
3134 *c_finalp = NUL;
3135 *n_extrap = (int)STRLEN(*pp_extra);
3136 }
Bram Moolenaar4e038572019-07-04 18:28:35 +02003137 *char_attrp = sattr->texthl;
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003138 }
3139 }
3140}
3141#endif
3142
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143/*
3144 * Display line "lnum" of window 'wp' on the screen.
3145 * Start at row "startrow", stop when "endrow" is reached.
3146 * wp->w_virtcol needs to be valid.
3147 *
3148 * Return the number of last row the line occupies.
3149 */
3150 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003151win_line(
3152 win_T *wp,
3153 linenr_T lnum,
3154 int startrow,
3155 int endrow,
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003156 int nochange UNUSED, // not updating for changed text
3157 int number_only) // only update the number column
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158{
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003159 int col = 0; // visual column on screen
3160 unsigned off; // offset in ScreenLines/ScreenAttrs
3161 int c = 0; // init for GCC
3162 long vcol = 0; // virtual column (for tabs)
Bram Moolenaard574ea22015-01-14 19:35:14 +01003163#ifdef FEAT_LINEBREAK
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003164 long vcol_sbr = -1; // virtual column after showbreak
Bram Moolenaard574ea22015-01-14 19:35:14 +01003165#endif
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003166 long vcol_prev = -1; // "vcol" of previous character
3167 char_u *line; // current line
3168 char_u *ptr; // current position in "line"
3169 int row; // row in the window, excl w_winrow
3170 int screen_row; // row on the screen, incl w_winrow
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003172 char_u extra[21]; // "%ld " and 'fdc' must fit in here
3173 int n_extra = 0; // number of extra chars
3174 char_u *p_extra = NULL; // string of extra chars, plus NUL
3175 char_u *p_extra_free = NULL; // p_extra needs to be freed
3176 int c_extra = NUL; // extra chars, all the same
3177 int c_final = NUL; // final char, mandatory if set
3178 int extra_attr = 0; // attributes when n_extra != 0
3179 static char_u *at_end_str = (char_u *)""; // used for p_extra when
3180 // displaying lcs_eol at end-of-line
3181 int lcs_eol_one = lcs_eol; // lcs_eol until it's been used
3182 int lcs_prec_todo = lcs_prec; // lcs_prec until it's been used
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003184 // saved "extra" items for when draw_state becomes WL_LINE (again)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 int saved_n_extra = 0;
3186 char_u *saved_p_extra = NULL;
3187 int saved_c_extra = 0;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003188 int saved_c_final = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 int saved_char_attr = 0;
3190
3191 int n_attr = 0; /* chars with special attr */
3192 int saved_attr2 = 0; /* char_attr saved for n_attr */
3193 int n_attr3 = 0; /* chars with overruling special attr */
3194 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
3195
3196 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
3197
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003198 int fromcol = -10; // start of inverting
3199 int tocol = MAXCOL; // end of inverting
3200 int fromcol_prev = -2; // start of inverting after cursor
3201 int noinvcur = FALSE; // don't invert the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003203 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 pos_T pos;
3205 long v;
3206
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003207 int char_attr = 0; // attributes for next character
3208 int attr_pri = FALSE; // char_attr has priority
3209 int area_highlighting = FALSE; // Visual or incsearch highlighting
3210 // in this line
3211 int vi_attr = 0; // attributes for Visual and incsearch
3212 // highlighting
3213 int wcr_attr = 0; // attributes from 'wincolor'
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003214 int win_attr = 0; // background for whole window, except
3215 // margins and "~" lines.
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003216 int area_attr = 0; // attributes desired by highlighting
3217 int search_attr = 0; // attributes desired by 'hlsearch'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003219 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 int syntax_attr = 0; /* attributes desired by syntax */
3221 int has_syntax = FALSE; /* this buffer has syntax highl. */
3222 int save_did_emsg;
Bram Moolenaar1a384422010-07-14 19:53:30 +02003223 int draw_color_col = FALSE; /* highlight colorcolumn */
3224 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003225#endif
Bram Moolenaarf914a332019-07-20 15:09:56 +02003226 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003227#ifdef FEAT_TEXT_PROP
3228 int text_prop_count;
3229 int text_prop_next = 0; // next text property to use
3230 textprop_T *text_props = NULL;
3231 int *text_prop_idxs = NULL;
3232 int text_props_active = 0;
3233 proptype_T *text_prop_type = NULL;
3234 int text_prop_attr = 0;
Bram Moolenaarde24a872019-05-05 15:48:00 +02003235 int text_prop_combine = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003236#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003237#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003238 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003239# define SPWORDLEN 150
3240 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003241 int nextlinecol = 0; /* column where nextline[] starts */
3242 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003243 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003244 int spell_attr = 0; /* attributes desired by spelling */
3245 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003246 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3247 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003248 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003249 static int cap_col = -1; /* column to check for Cap word */
3250 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003251 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003253 int extra_check = 0; // has extra highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 int multi_attr = 0; /* attributes desired by multibyte */
3255 int mb_l = 1; /* multi-byte byte length */
3256 int mb_c = 0; /* decoded multi-byte character */
3257 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003258 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaare9726e32019-06-19 18:01:21 +02003259#if defined(FEAT_DIFF) || defined(FEAT_SIGNS)
Bram Moolenaarbf8c3ad2019-06-19 14:28:43 +02003260 int filler_lines = 0; /* nr of filler lines to be drawn */
3261 int filler_todo = 0; /* nr of filler lines still to do + 1 */
Bram Moolenaare9726e32019-06-19 18:01:21 +02003262#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263#ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003264 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 int change_start = MAXCOL; /* first col of changed area */
3266 int change_end = -1; /* last col of changed area */
3267#endif
3268 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3269#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003270 int need_showbreak = FALSE; /* overlong line, skipping first x
3271 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003273#if defined(FEAT_SIGNS) || defined(FEAT_QUICKFIX) \
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003274 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275# define LINE_ATTR
Bram Moolenaar017ba072019-09-14 21:01:23 +02003276 int line_attr = 0; // attribute for the whole line
3277 int line_attr_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278#endif
Bram Moolenaarb4d9b892019-07-04 22:59:06 +02003279#ifdef FEAT_SIGNS
3280 int sign_present = FALSE;
3281 sign_attrs_T sattr;
3282#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283#ifdef FEAT_ARABIC
3284 int prev_c = 0; /* previous Arabic character */
3285 int prev_c1 = 0; /* first composing char for prev_c */
3286#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003287#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003288 int did_line_attr = 0;
3289#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003290#ifdef FEAT_TERMINAL
3291 int get_term_attr = FALSE;
3292#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02003293#ifdef FEAT_SYN_HL
3294 int cul_attr = 0; // set when 'cursorline' active
3295
3296 // 'cursorlineopt' has "screenline" and cursor is in this line
3297 int cul_screenline = FALSE;
3298
3299 // margin columns for the screen line, needed for when 'cursorlineopt'
3300 // contains "screenline"
3301 int left_curline_col = 0;
3302 int right_curline_col = 0;
3303#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304
3305 /* draw_state: items that are drawn in sequence: */
3306#define WL_START 0 /* nothing done yet */
3307#ifdef FEAT_CMDWIN
3308# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3309#else
3310# define WL_CMDLINE WL_START
3311#endif
3312#ifdef FEAT_FOLDING
3313# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3314#else
3315# define WL_FOLD WL_CMDLINE
3316#endif
3317#ifdef FEAT_SIGNS
3318# define WL_SIGN WL_FOLD + 1 /* column for signs */
3319#else
3320# define WL_SIGN WL_FOLD /* column for signs */
3321#endif
3322#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003323#ifdef FEAT_LINEBREAK
3324# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003326# define WL_BRI WL_NR
3327#endif
3328#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3329# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3330#else
3331# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332#endif
3333#define WL_LINE WL_SBR + 1 /* text in the line */
3334 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003335#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 int feedback_col = 0;
3337 int feedback_old_attr = -1;
3338#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003339 int screen_line_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340
Bram Moolenaarfbfb7572019-07-25 20:53:03 +02003341#if defined(FEAT_CONCEAL) || defined(FEAT_SEARCH_EXTRA)
Bram Moolenaar07d13562019-07-24 18:43:08 +02003342 int match_conc = 0; // cchar for match functions
Bram Moolenaarfbfb7572019-07-25 20:53:03 +02003343#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02003344#ifdef FEAT_CONCEAL
3345 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003346 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003347 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003348 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003349 int is_concealing = FALSE;
Bram Moolenaar07d13562019-07-24 18:43:08 +02003350 int boguscols = 0; // nonexistent columns added to force
3351 // wrapping
3352 int vcol_off = 0; // offset for concealed characters
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003353 int did_wcol = FALSE;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003354 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003355# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003356# define FIX_FOR_BOGUSCOLS \
3357 { \
3358 n_extra += vcol_off; \
3359 vcol -= vcol_off; \
3360 vcol_off = 0; \
3361 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003362 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003363 boguscols = 0; \
3364 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003365#else
3366# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003367#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368
3369 if (startrow > endrow) /* past the end already! */
3370 return startrow;
3371
3372 row = startrow;
3373 screen_row = row + W_WINROW(wp);
3374
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003375 if (!number_only)
3376 {
3377 /*
3378 * To speed up the loop below, set extra_check when there is linebreak,
3379 * trailing white space and/or syntax processing to be done.
3380 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381#ifdef FEAT_LINEBREAK
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003382 extra_check = wp->w_p_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383#endif
3384#ifdef FEAT_SYN_HL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003385 if (syntax_present(wp) && !wp->w_s->b_syn_error
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003386# ifdef SYN_TIME_LIMIT
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003387 && !wp->w_s->b_syn_slow
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003388# endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003389 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003391 /* Prepare for syntax highlighting in this line. When there is an
3392 * error, stop syntax highlighting. */
3393 save_did_emsg = did_emsg;
3394 did_emsg = FALSE;
3395 syntax_start(wp, lnum);
3396 if (did_emsg)
3397 wp->w_s->b_syn_error = TRUE;
3398 else
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003399 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003400 did_emsg = save_did_emsg;
3401#ifdef SYN_TIME_LIMIT
3402 if (!wp->w_s->b_syn_slow)
3403#endif
3404 {
3405 has_syntax = TRUE;
3406 extra_check = TRUE;
3407 }
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003410
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003411 /* Check for columns to display for 'colorcolumn'. */
3412 color_cols = wp->w_p_cc_cols;
3413 if (color_cols != NULL)
3414 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003415#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003416
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003417#ifdef FEAT_TERMINAL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003418 if (term_show_buffer(wp->w_buffer))
3419 {
3420 extra_check = TRUE;
3421 get_term_attr = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003422 win_attr = term_get_attr(wp->w_buffer, lnum, -1);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003423 }
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003424#endif
3425
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003426#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003427 if (wp->w_p_spell
3428 && *wp->w_s->b_p_spl != NUL
3429 && wp->w_s->b_langp.ga_len > 0
3430 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003431 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003432 /* Prepare for spell checking. */
3433 has_spell = TRUE;
3434 extra_check = TRUE;
3435
Bram Moolenaar7701f302018-10-02 21:20:32 +02003436 /* Get the start of the next line, so that words that wrap to the
3437 * next line are found too: "et<line-break>al.".
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003438 * Trick: skip a few chars for C/shell/Vim comments */
3439 nextline[SPWORDLEN] = NUL;
3440 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3441 {
3442 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3443 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3444 }
3445
Bram Moolenaar7701f302018-10-02 21:20:32 +02003446 /* When a word wrapped from the previous line the start of the
3447 * current line is valid. */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003448 if (lnum == checked_lnum)
3449 cur_checked_col = checked_col;
3450 checked_lnum = 0;
3451
3452 /* When there was a sentence end in the previous line may require a
3453 * word starting with capital in this line. In line 1 always check
3454 * the first word. */
3455 if (lnum != capcol_lnum)
3456 cap_col = -1;
3457 if (lnum == 1)
3458 cap_col = 0;
3459 capcol_lnum = 0;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461#endif
3462
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003463 /*
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003464 * handle Visual active in this window
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003465 */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003466 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003468 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 {
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003470 // Visual is after curwin->w_cursor
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003471 top = &curwin->w_cursor;
3472 bot = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 }
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003474 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 {
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003476 // Visual is before curwin->w_cursor
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003477 top = &VIsual;
3478 bot = &curwin->w_cursor;
3479 }
3480 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003481 if (VIsual_mode == Ctrl_V)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003482 {
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003483 // block mode
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003484 if (lnum_in_visual_area)
3485 {
3486 fromcol = wp->w_old_cursor_fcol;
3487 tocol = wp->w_old_cursor_lcol;
3488 }
3489 }
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003490 else
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003491 {
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003492 // non-block mode
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003493 if (lnum > top->lnum && lnum <= bot->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 fromcol = 0;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003495 else if (lnum == top->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 {
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003497 if (VIsual_mode == 'V') // linewise
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003498 fromcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 else
3500 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003501 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3502 if (gchar_pos(top) == NUL)
3503 tocol = fromcol + 1;
3504 }
3505 }
3506 if (VIsual_mode != 'V' && lnum == bot->lnum)
3507 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003508 if (*p_sel == 'e' && bot->col == 0 && bot->coladd == 0)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003509 {
3510 fromcol = -10;
3511 tocol = MAXCOL;
3512 }
3513 else if (bot->col == MAXCOL)
3514 tocol = MAXCOL;
3515 else
3516 {
3517 pos = *bot;
3518 if (*p_sel == 'e')
3519 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3520 else
3521 {
3522 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3523 ++tocol;
3524 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 }
3526 }
3527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003529 /* Check if the character under the cursor should not be inverted */
3530 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003531#ifdef FEAT_GUI
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003532 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003533#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003534 )
3535 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003537 /* if inverting in this line set area_highlighting */
3538 if (fromcol >= 0)
3539 {
3540 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003541 vi_attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003543 if ((clip_star.available && !clip_star.owned
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003544 && clip_isautosel_star())
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003545 || (clip_plus.available && !clip_plus.owned
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003546 && clip_isautosel_plus()))
3547 vi_attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003552 /*
3553 * handle 'incsearch' and ":s///c" highlighting
3554 */
3555 else if (highlight_match
3556 && wp == curwin
3557 && lnum >= curwin->w_cursor.lnum
3558 && lnum <= curwin->w_cursor.lnum + search_match_lines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003560 if (lnum == curwin->w_cursor.lnum)
3561 getvcol(curwin, &(curwin->w_cursor),
Bram Moolenaarc6663882019-02-22 19:14:54 +01003562 (colnr_T *)&fromcol, NULL, NULL);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003563 else
3564 fromcol = 0;
3565 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3566 {
3567 pos.lnum = lnum;
3568 pos.col = search_match_endcol;
3569 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3570 }
3571 else
3572 tocol = MAXCOL;
3573 /* do at least one character; happens when past end of line */
3574 if (fromcol == tocol)
3575 tocol = fromcol + 1;
3576 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003577 vi_attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 }
3580
3581#ifdef FEAT_DIFF
3582 filler_lines = diff_check(wp, lnum);
3583 if (filler_lines < 0)
3584 {
3585 if (filler_lines == -1)
3586 {
3587 if (diff_find_change(wp, lnum, &change_start, &change_end))
3588 diff_hlf = HLF_ADD; /* added line */
3589 else if (change_start == 0)
3590 diff_hlf = HLF_TXD; /* changed text */
3591 else
3592 diff_hlf = HLF_CHD; /* changed line */
3593 }
3594 else
3595 diff_hlf = HLF_ADD; /* added line */
3596 filler_lines = 0;
3597 area_highlighting = TRUE;
3598 }
3599 if (lnum == wp->w_topline)
3600 filler_lines = wp->w_topfill;
3601 filler_todo = filler_lines;
3602#endif
3603
Bram Moolenaar4e038572019-07-04 18:28:35 +02003604#ifdef FEAT_SIGNS
3605 sign_present = buf_get_signattrs(wp->w_buffer, lnum, &sattr);
3606#endif
3607
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608#ifdef LINE_ATTR
3609# ifdef FEAT_SIGNS
3610 /* If this line has a sign with line highlighting set line_attr. */
Bram Moolenaar4e038572019-07-04 18:28:35 +02003611 if (sign_present)
3612 line_attr = sattr.linehl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003614# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003616 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003617 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618# endif
3619 if (line_attr != 0)
3620 area_highlighting = TRUE;
3621#endif
3622
3623 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3624 ptr = line;
3625
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003626#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003627 if (has_spell && !number_only)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003628 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003629 /* For checking first word with a capital skip white space. */
3630 if (cap_col == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003631 cap_col = getwhitecols(line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003632
Bram Moolenaar30abd282005-06-22 22:35:10 +00003633 /* To be able to spell-check over line boundaries copy the end of the
3634 * current line into nextline[]. Above the start of the next line was
3635 * copied to nextline[SPWORDLEN]. */
3636 if (nextline[SPWORDLEN] == NUL)
3637 {
3638 /* No next line or it is empty. */
3639 nextlinecol = MAXCOL;
3640 nextline_idx = 0;
3641 }
3642 else
3643 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003644 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003645 if (v < SPWORDLEN)
3646 {
3647 /* Short line, use it completely and append the start of the
3648 * next line. */
3649 nextlinecol = 0;
3650 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003651 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003652 nextline_idx = v + 1;
3653 }
3654 else
3655 {
3656 /* Long line, use only the last SPWORDLEN bytes. */
3657 nextlinecol = v - SPWORDLEN;
3658 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3659 nextline_idx = SPWORDLEN + 1;
3660 }
3661 }
3662 }
3663#endif
3664
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003665 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 {
Bram Moolenaar895d9662019-01-31 21:57:21 +01003667 if (lcs_space || lcs_trail || lcs_nbsp)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003668 extra_check = TRUE;
3669 /* find start of trailing whitespace */
3670 if (lcs_trail)
3671 {
3672 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003673 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003674 --trailcol;
3675 trailcol += (colnr_T) (ptr - line);
3676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 }
3678
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003679 wcr_attr = get_wcr_attr(wp);
3680 if (wcr_attr != 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003681 {
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003682 win_attr = wcr_attr;
3683 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003684 }
3685#ifdef FEAT_TEXT_PROP
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02003686 if (WIN_IS_POPUP(wp))
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003687 screen_line_flags |= SLF_POPUP;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003688#endif
3689
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 /*
3691 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3692 * first character to be displayed.
3693 */
3694 if (wp->w_p_wrap)
3695 v = wp->w_skipcol;
3696 else
3697 v = wp->w_leftcol;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003698 if (v > 0 && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 char_u *prev_ptr = ptr;
Bram Moolenaara12a1612019-01-24 16:39:02 +01003701
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 while (vcol < v && *ptr != NUL)
3703 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003704 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 vcol += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 prev_ptr = ptr;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003707 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 }
3709
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003710 /* When:
3711 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003712 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003713 * - 'virtualedit' is set, or
3714 * - the visual mode is active,
3715 * the end of the line may be before the start of the displayed part.
3716 */
3717 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003718#ifdef FEAT_SYN_HL
3719 wp->w_p_cuc || draw_color_col ||
3720#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003721 virtual_active() ||
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003722 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 vcol = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724
3725 /* Handle a character that's not completely on the screen: Put ptr at
3726 * that character but skip the first few screen characters. */
3727 if (vcol > v)
3728 {
3729 vcol -= c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 ptr = prev_ptr;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003731 /* If the character fits on the screen, don't need to skip it.
3732 * Except for a TAB. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01003733 if (( (*mb_ptr2cells)(ptr) >= c || *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003734 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 }
3736
3737 /*
3738 * Adjust for when the inverted text is before the screen,
3739 * and when the start of the inverted text is before the screen.
3740 */
3741 if (tocol <= vcol)
3742 fromcol = 0;
3743 else if (fromcol >= 0 && fromcol < vcol)
3744 fromcol = vcol;
3745
3746#ifdef FEAT_LINEBREAK
3747 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3748 if (wp->w_p_wrap)
3749 need_showbreak = TRUE;
3750#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003751#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003752 /* When spell checking a word we need to figure out the start of the
3753 * word and if it's badly spelled or not. */
3754 if (has_spell)
3755 {
3756 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003757 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003758 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003759
3760 pos = wp->w_cursor;
3761 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003762 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003763 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003764
3765 /* spell_move_to() may call ml_get() and make "line" invalid */
3766 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3767 ptr = line + linecol;
3768
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003769 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003770 {
3771 /* no bad word found at line start, don't check until end of a
3772 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003773 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003774 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003775 }
3776 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003777 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003778 /* bad word found, use attributes until end of word */
3779 word_end = wp->w_cursor.col + len + 1;
3780
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003781 /* Turn index into actual attributes. */
3782 if (spell_hlf != HLF_COUNT)
3783 spell_attr = highlight_attr[spell_hlf];
3784 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003785 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003786
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003787# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003788 /* Need to restart syntax highlighting for this line. */
3789 if (has_syntax)
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003790 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003791# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003792 }
3793#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 }
3795
3796 /*
3797 * Correct highlighting for cursor that can't be disabled.
3798 * Avoids having to check this for each character.
3799 */
3800 if (fromcol >= 0)
3801 {
3802 if (noinvcur)
3803 {
3804 if ((colnr_T)fromcol == wp->w_virtcol)
3805 {
3806 /* highlighting starts at cursor, let it start just after the
3807 * cursor */
3808 fromcol_prev = fromcol;
3809 fromcol = -1;
3810 }
3811 else if ((colnr_T)fromcol < wp->w_virtcol)
3812 /* restart highlighting after the cursor */
3813 fromcol_prev = wp->w_virtcol;
3814 }
3815 if (fromcol >= tocol)
3816 fromcol = -1;
3817 }
3818
3819#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarbbca7732019-07-24 18:13:16 +02003820 if (!number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003822 v = (long)(ptr - line);
Bram Moolenaarbbca7732019-07-24 18:13:16 +02003823 area_highlighting |= prepare_search_hl_line(wp, lnum, (colnr_T)v,
3824 &line, &search_hl, &search_attr);
3825 ptr = line + v; // "line" may have been updated
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 }
3827#endif
3828
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003829#ifdef FEAT_SYN_HL
Bram Moolenaar8156ed32019-03-09 11:46:15 +01003830 // Cursor line highlighting for 'cursorline' in the current window.
3831 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003832 {
Bram Moolenaar017ba072019-09-14 21:01:23 +02003833 // Do not show the cursor line in the text when Visual mode is active,
3834 // because it's not clear what is selected then. Do update
3835 // w_last_cursorline.
3836 if (!(wp == curwin && VIsual_active)
3837 && wp->w_p_culopt_flags != CULOPT_NBR)
Bram Moolenaar8156ed32019-03-09 11:46:15 +01003838 {
Bram Moolenaar017ba072019-09-14 21:01:23 +02003839 cul_screenline = (wp->w_p_wrap
3840 && (wp->w_p_culopt_flags & CULOPT_SCRLINE));
3841
3842 // Only set line_attr here when "screenline" is not present in
3843 // 'cursorlineopt'. Otherwise it's done later.
3844 if (!cul_screenline)
3845 {
3846 cul_attr = HL_ATTR(HLF_CUL);
3847 line_attr = cul_attr;
3848 wp->w_last_cursorline = wp->w_cursor.lnum;
3849 }
3850 else
3851 {
3852 line_attr_save = line_attr;
3853 wp->w_last_cursorline = 0;
3854 margin_columns_win(wp, &left_curline_col, &right_curline_col);
3855 }
Bram Moolenaar8156ed32019-03-09 11:46:15 +01003856 area_highlighting = TRUE;
3857 }
Bram Moolenaar017ba072019-09-14 21:01:23 +02003858 else
3859 wp->w_last_cursorline = wp->w_cursor.lnum;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003860 }
3861#endif
3862
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003863#ifdef FEAT_TEXT_PROP
3864 {
3865 char_u *prop_start;
3866
3867 text_prop_count = get_text_props(wp->w_buffer, lnum,
3868 &prop_start, FALSE);
3869 if (text_prop_count > 0)
3870 {
3871 // Make a copy of the properties, so that they are properly
3872 // aligned.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003873 text_props = ALLOC_MULT(textprop_T, text_prop_count);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003874 if (text_props != NULL)
3875 mch_memmove(text_props, prop_start,
3876 text_prop_count * sizeof(textprop_T));
3877
3878 // Allocate an array for the indexes.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003879 text_prop_idxs = ALLOC_MULT(int, text_prop_count);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003880 area_highlighting = TRUE;
3881 extra_check = TRUE;
3882 }
3883 }
3884#endif
3885
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003886 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 col = 0;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003888
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889#ifdef FEAT_RIGHTLEFT
3890 if (wp->w_p_rl)
3891 {
3892 /* Rightleft window: process the text in the normal direction, but put
3893 * it in current_ScreenLine[] from right to left. Start at the
3894 * rightmost column of the window. */
Bram Moolenaar02631462017-09-22 15:20:32 +02003895 col = wp->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 off += col;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003897 screen_line_flags |= SLF_RIGHTLEFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 }
3899#endif
3900
3901 /*
3902 * Repeat for the whole displayed line.
3903 */
3904 for (;;)
3905 {
Bram Moolenaarfbfb7572019-07-25 20:53:03 +02003906#if defined(FEAT_CONCEAL) || defined(FEAT_SEARCH_EXTRA)
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02003907 int has_match_conc = 0; // match wants to conceal
Bram Moolenaarfbfb7572019-07-25 20:53:03 +02003908#endif
Bram Moolenaar07d13562019-07-24 18:43:08 +02003909#ifdef FEAT_CONCEAL
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02003910 int did_decrement_ptr = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003911#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 /* Skip this quickly when working on the text. */
3913 if (draw_state != WL_LINE)
3914 {
3915#ifdef FEAT_CMDWIN
3916 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3917 {
3918 draw_state = WL_CMDLINE;
3919 if (cmdwin_type != 0 && wp == curwin)
3920 {
3921 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003923 c_extra = cmdwin_type;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003924 c_final = NUL;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003925 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 }
3927 }
3928#endif
3929
3930#ifdef FEAT_FOLDING
3931 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3932 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003933 int fdc = compute_foldcolumn(wp, 0);
3934
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003936 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003938 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003939 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003940 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003941 p_extra_free = alloc(12 + 1);
3942
3943 if (p_extra_free != NULL)
3944 {
3945 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3946 n_extra = fdc;
3947 p_extra_free[n_extra] = NUL;
3948 p_extra = p_extra_free;
3949 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003950 c_final = NUL;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003951 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_FC));
Bram Moolenaar62706602016-12-09 19:28:48 +01003952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 }
3954 }
3955#endif
3956
3957#ifdef FEAT_SIGNS
3958 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3959 {
3960 draw_state = WL_SIGN;
3961 /* Show the sign column when there are any signs in this
3962 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003963 if (signcolumn_on(wp))
Bram Moolenaar4e038572019-07-04 18:28:35 +02003964 get_sign_display_info(FALSE, wp, lnum, &sattr, wcr_attr,
3965 row, startrow, filler_lines, filler_todo, &c_extra,
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003966 &c_final, extra, &p_extra, &n_extra, &char_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 }
3968#endif
3969
3970 if (draw_state == WL_NR - 1 && n_extra == 0)
3971 {
3972 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003973 /* Display the absolute or relative line number. After the
3974 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3975 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 && (row == startrow
3977#ifdef FEAT_DIFF
3978 + filler_lines
3979#endif
3980 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3981 {
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003982#ifdef FEAT_SIGNS
3983 // If 'signcolumn' is set to 'number' and a sign is present
3984 // in 'lnum', then display the sign instead of the line
3985 // number.
3986 if ((*wp->w_p_scl == 'n' && *(wp->w_p_scl + 1) == 'u')
Bram Moolenaar4e038572019-07-04 18:28:35 +02003987 && sign_present)
3988 get_sign_display_info(TRUE, wp, lnum, &sattr, wcr_attr,
3989 row, startrow, filler_lines, filler_todo,
3990 &c_extra, &c_final, extra, &p_extra, &n_extra,
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003991 &char_attr);
3992 else
3993#endif
3994 {
3995 /* Draw the line number (empty space after wrapping). */
3996 if (row == startrow
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997#ifdef FEAT_DIFF
3998 + filler_lines
3999#endif
4000 )
Bram Moolenaar394c5d82019-06-17 21:48:05 +02004001 {
Bram Moolenaar64486672010-05-16 15:46:46 +02004002 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01004003 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02004004
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004005 if (wp->w_p_nu && !wp->w_p_rnu)
4006 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02004007 num = (long)lnum;
4008 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01004009 {
Bram Moolenaar64486672010-05-16 15:46:46 +02004010 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01004011 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004012 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01004013 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004014 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01004015 num = lnum;
4016 fmt = "%-*ld ";
4017 }
4018 }
Bram Moolenaar64486672010-05-16 15:46:46 +02004019
Bram Moolenaar700e7342013-01-30 12:31:36 +01004020 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02004021 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 if (wp->w_skipcol > 0)
4023 for (p_extra = extra; *p_extra == ' '; ++p_extra)
4024 *p_extra = '-';
4025#ifdef FEAT_RIGHTLEFT
4026 if (wp->w_p_rl) /* reverse line numbers */
Bram Moolenaare73f9112019-03-29 18:29:54 +01004027 {
4028 char_u *p1, *p2;
4029 int t;
4030
4031 // like rl_mirror(), but keep the space at the end
4032 p2 = skiptowhite(extra) - 1;
4033 for (p1 = extra; p1 < p2; ++p1, --p2)
4034 {
4035 t = *p1;
4036 *p1 = *p2;
4037 *p2 = t;
4038 }
4039 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040#endif
4041 p_extra = extra;
4042 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004043 c_final = NUL;
Bram Moolenaar394c5d82019-06-17 21:48:05 +02004044 }
4045 else
4046 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004048 c_final = NUL;
Bram Moolenaar394c5d82019-06-17 21:48:05 +02004049 }
4050 n_extra = number_width(wp) + 1;
4051 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_N));
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004052#ifdef FEAT_SYN_HL
Bram Moolenaar017ba072019-09-14 21:01:23 +02004053 // When 'cursorline' is set highlight the line number of
4054 // the current line differently.
4055 // When 'cursorlineopt' has "screenline" only highlight
4056 // the line number itself.
4057 // TODO: Can we use CursorLine instead of CursorLineNr
4058 // when CursorLineNr isn't set?
Bram Moolenaar394c5d82019-06-17 21:48:05 +02004059 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar017ba072019-09-14 21:01:23 +02004060 && (wp->w_p_culopt_flags & CULOPT_NBR)
4061 && (row == startrow
4062 || wp->w_p_culopt_flags & CULOPT_LINE)
4063 && lnum == wp->w_cursor.lnum)
Bram Moolenaar193ffd12019-05-25 22:57:30 +02004064 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_CLN));
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004065#endif
Bram Moolenaar394c5d82019-06-17 21:48:05 +02004066 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 }
4068 }
4069
Bram Moolenaar597a4222014-06-25 14:39:50 +02004070#ifdef FEAT_LINEBREAK
4071 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
4072 && n_extra == 0 && *p_sbr != NUL)
4073 /* draw indent after showbreak value */
4074 draw_state = WL_BRI;
4075 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
4076 /* After the showbreak, draw the breakindent */
4077 draw_state = WL_BRI - 1;
4078
4079 /* draw 'breakindent': indent wrapped text accordingly */
4080 if (draw_state == WL_BRI - 1 && n_extra == 0)
4081 {
4082 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01004083 /* if need_showbreak is set, breakindent also applies */
4084 if (wp->w_p_bri && n_extra == 0
4085 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004086# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004087 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004088# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004089 )
4090 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01004091 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004092# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004093 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004094 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004095 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004096# ifdef FEAT_SYN_HL
Bram Moolenaar017ba072019-09-14 21:01:23 +02004097 if (cul_attr != 0)
4098 char_attr = hl_combine_attr(char_attr, cul_attr);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004099# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02004100 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004101# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02004102 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02004103 c_extra = ' ';
4104 n_extra = get_breakindent_win(wp,
4105 ml_get_buf(wp->w_buffer, lnum, FALSE));
4106 /* Correct end of highlighted area for 'breakindent',
4107 * required when 'linebreak' is also set. */
4108 if (tocol == vcol)
4109 tocol += n_extra;
4110 }
4111 }
4112#endif
4113
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
4115 if (draw_state == WL_SBR - 1 && n_extra == 0)
4116 {
4117 draw_state = WL_SBR;
4118# ifdef FEAT_DIFF
4119 if (filler_todo > 0)
4120 {
4121 /* Draw "deleted" diff line(s). */
4122 if (char2cells(fill_diff) > 1)
Bram Moolenaar83a52172019-01-16 22:41:54 +01004123 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 c_extra = '-';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004125 c_final = NUL;
4126 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 else
Bram Moolenaar83a52172019-01-16 22:41:54 +01004128 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 c_extra = fill_diff;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004130 c_final = NUL;
4131 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132# ifdef FEAT_RIGHTLEFT
4133 if (wp->w_p_rl)
4134 n_extra = col + 1;
4135 else
4136# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004137 n_extra = wp->w_width - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004138 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 }
4140# endif
4141# ifdef FEAT_LINEBREAK
4142 if (*p_sbr != NUL && need_showbreak)
4143 {
4144 /* Draw 'showbreak' at the start of each broken line. */
4145 p_extra = p_sbr;
4146 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004147 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004149 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004151 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 /* Correct end of highlighted area for 'showbreak',
4153 * required when 'linebreak' is also set. */
4154 if (tocol == vcol)
4155 tocol += n_extra;
Bram Moolenaar017ba072019-09-14 21:01:23 +02004156# ifdef FEAT_SYN_HL
4157 // combine 'showbreak' with 'cursorline'
4158 if (cul_attr != 0)
4159 char_attr = hl_combine_attr(char_attr, cul_attr);
4160# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 }
4162# endif
4163 }
4164#endif
4165
4166 if (draw_state == WL_LINE - 1 && n_extra == 0)
4167 {
4168 draw_state = WL_LINE;
4169 if (saved_n_extra)
4170 {
4171 /* Continue item from end of wrapped line. */
4172 n_extra = saved_n_extra;
4173 c_extra = saved_c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004174 c_final = saved_c_final;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 p_extra = saved_p_extra;
4176 char_attr = saved_char_attr;
4177 }
4178 else
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004179 char_attr = win_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 }
4181 }
Bram Moolenaar017ba072019-09-14 21:01:23 +02004182#ifdef FEAT_SYN_HL
4183 if (cul_screenline)
4184 {
4185 if (draw_state == WL_LINE
4186 && vcol >= left_curline_col
4187 && vcol < right_curline_col)
4188 {
4189 cul_attr = HL_ATTR(HLF_CUL);
4190 line_attr = cul_attr;
4191 }
4192 else
4193 {
4194 cul_attr = 0;
4195 line_attr = line_attr_save;
4196 }
4197 }
4198#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004200 // When still displaying '$' of change command, stop at cursor.
4201 // When only displaying the (relative) line number and that's done,
4202 // stop here.
4203 if ((dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004204 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205#ifdef FEAT_DIFF
4206 && filler_todo <= 0
4207#endif
4208 )
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004209 || (number_only && draw_state > WL_NR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 {
Bram Moolenaar02631462017-09-22 15:20:32 +02004211 screen_line(screen_row, wp->w_wincol, col, -(int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004212 screen_line_flags);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004213 /* Pretend we have finished updating the window. Except when
4214 * 'cursorcolumn' is set. */
4215#ifdef FEAT_SYN_HL
4216 if (wp->w_p_cuc)
4217 row = wp->w_cline_row + wp->w_cline_height;
4218 else
4219#endif
4220 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 break;
4222 }
4223
Bram Moolenaar637532b2019-01-03 21:44:40 +01004224 if (draw_state == WL_LINE && (area_highlighting
4225#ifdef FEAT_SPELL
4226 || has_spell
4227#endif
4228 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 {
4230 /* handle Visual or match highlighting in this line */
4231 if (vcol == fromcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4233 && (*mb_ptr2cells)(ptr) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004235 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 && vcol < tocol))
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004237 area_attr = vi_attr; /* start highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 else if (area_attr != 0
4239 && (vcol == tocol
4240 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242
4243#ifdef FEAT_SEARCH_EXTRA
4244 if (!n_extra)
4245 {
4246 /*
Bram Moolenaarbbca7732019-07-24 18:13:16 +02004247 * Check for start/end of 'hlsearch' and other matches.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 * After end, check for start/end of next match.
4249 * When another match, have to check for start again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004251 v = (long)(ptr - line);
Bram Moolenaarbbca7732019-07-24 18:13:16 +02004252 search_attr = update_search_hl(wp, lnum, (colnr_T)v, &line,
4253 &search_hl, &has_match_conc, &match_conc,
4254 did_line_attr, lcs_eol_one);
4255 ptr = line + v; // "line" may have been changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 }
4257#endif
4258
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004260 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004262 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4263 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004265 if (diff_hlf == HLF_TXD && ptr - line > change_end
4266 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004268 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaar410e98a2019-09-09 22:05:49 +02004269 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaar017ba072019-09-14 21:01:23 +02004270 && wp->w_p_culopt_flags != CULOPT_NBR
4271 && (!cul_screenline || (vcol >= left_curline_col
4272 && vcol <= right_curline_col)))
4273 line_attr = hl_combine_attr(
4274 line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 }
4276#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004277
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004278#ifdef FEAT_TEXT_PROP
4279 if (text_props != NULL)
4280 {
4281 int pi;
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004282 int bcol = (int)(ptr - line);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004283
Bram Moolenaara956bf62019-06-19 17:34:24 +02004284 if (n_extra > 0)
4285 --bcol; // still working on the previous char, e.g. Tab
4286
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004287 // Check if any active property ends.
4288 for (pi = 0; pi < text_props_active; ++pi)
4289 {
4290 int tpi = text_prop_idxs[pi];
4291
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004292 if (bcol >= text_props[tpi].tp_col - 1
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004293 + text_props[tpi].tp_len)
4294 {
4295 if (pi + 1 < text_props_active)
4296 mch_memmove(text_prop_idxs + pi,
4297 text_prop_idxs + pi + 1,
4298 sizeof(int)
4299 * (text_props_active - (pi + 1)));
4300 --text_props_active;
4301 --pi;
4302 }
4303 }
4304
4305 // Add any text property that starts in this column.
4306 while (text_prop_next < text_prop_count
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004307 && bcol >= text_props[text_prop_next].tp_col - 1)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004308 text_prop_idxs[text_props_active++] = text_prop_next++;
4309
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004310 text_prop_attr = 0;
Bram Moolenaarde24a872019-05-05 15:48:00 +02004311 text_prop_combine = FALSE;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004312 if (text_props_active > 0)
4313 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004314 // Sort the properties on priority and/or starting last.
4315 // Then combine the attributes, highest priority last.
4316 current_text_props = text_props;
4317 current_buf = wp->w_buffer;
4318 qsort((void *)text_prop_idxs, (size_t)text_props_active,
4319 sizeof(int), text_prop_compare);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004320
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004321 for (pi = 0; pi < text_props_active; ++pi)
4322 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004323 int tpi = text_prop_idxs[pi];
Bram Moolenaarde24a872019-05-05 15:48:00 +02004324 proptype_T *pt = text_prop_type_by_id(
4325 wp->w_buffer, text_props[tpi].tp_type);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004326
Bram Moolenaard74af422019-06-28 21:38:00 +02004327 if (pt != NULL && pt->pt_hl_id > 0)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004328 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004329 int pt_attr = syn_id2attr(pt->pt_hl_id);
4330
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004331 text_prop_type = pt;
Bram Moolenaarde24a872019-05-05 15:48:00 +02004332 text_prop_attr =
4333 hl_combine_attr(text_prop_attr, pt_attr);
4334 text_prop_combine = pt->pt_flags & PT_FLAG_COMBINE;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004335 }
4336 }
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004337 }
4338 }
4339#endif
4340
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004341 /* Decide which of the highlight attributes to use. */
4342 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004343#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004344 if (area_attr != 0)
4345 char_attr = hl_combine_attr(line_attr, area_attr);
4346 else if (search_attr != 0)
4347 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004348# ifdef FEAT_TEXT_PROP
4349 else if (text_prop_type != NULL)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02004350 {
4351 char_attr = hl_combine_attr(
4352 line_attr != 0 ? line_attr : win_attr, text_prop_attr);
4353 }
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004354# endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004355 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004356 || vcol < fromcol || vcol_prev < fromcol_prev
4357 || vcol >= tocol))
Bram Moolenaar017ba072019-09-14 21:01:23 +02004358 {
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004359 // Use line_attr when not in the Visual or 'incsearch' area
4360 // (area_attr may be 0 when "noinvcur" is set).
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004361 char_attr = line_attr;
Bram Moolenaar017ba072019-09-14 21:01:23 +02004362 attr_pri = FALSE;
4363 }
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004364#else
4365 if (area_attr != 0)
4366 char_attr = area_attr;
4367 else if (search_attr != 0)
4368 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004369#endif
4370 else
4371 {
4372 attr_pri = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004373#ifdef FEAT_TEXT_PROP
4374 if (text_prop_type != NULL)
Bram Moolenaarde24a872019-05-05 15:48:00 +02004375 {
4376 if (text_prop_combine)
4377 char_attr = hl_combine_attr(
4378 syntax_attr, text_prop_attr);
4379 else
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02004380 char_attr = hl_combine_attr(
4381 win_attr, text_prop_attr);
Bram Moolenaarde24a872019-05-05 15:48:00 +02004382 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004383 else
4384#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004385#ifdef FEAT_SYN_HL
4386 if (has_syntax)
4387 char_attr = syntax_attr;
4388 else
4389#endif
4390 char_attr = 0;
4391 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004393 if (char_attr == 0)
4394 char_attr = win_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395
4396 /*
4397 * Get the next character to put on the screen.
4398 */
4399 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004400 * The "p_extra" points to the extra stuff that is inserted to
4401 * represent special characters (non-printable stuff) and other
4402 * things. When all characters are the same, c_extra is used.
Bram Moolenaar83a52172019-01-16 22:41:54 +01004403 * If c_final is set, it will compulsorily be used at the end.
Bram Moolenaara064ac82007-08-05 18:10:54 +00004404 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4405 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4407 */
4408 if (n_extra > 0)
4409 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004410 if (c_extra != NUL || (n_extra == 1 && c_final != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004412 c = (n_extra == 1 && c_final != NUL) ? c_final : c_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004414 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 {
4416 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004417 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004418 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 }
4420 else
4421 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 }
4423 else
4424 {
4425 c = *p_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 if (has_mbyte)
4427 {
4428 mb_c = c;
4429 if (enc_utf8)
4430 {
4431 /* If the UTF-8 character is more than one byte:
4432 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004433 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 mb_utf8 = FALSE;
4435 if (mb_l > n_extra)
4436 mb_l = 1;
4437 else if (mb_l > 1)
4438 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004439 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004441 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 }
4443 }
4444 else
4445 {
4446 /* if this is a DBCS character, put it in "mb_c" */
4447 mb_l = MB_BYTE2LEN(c);
4448 if (mb_l >= n_extra)
4449 mb_l = 1;
4450 else if (mb_l > 1)
4451 mb_c = (c << 8) + p_extra[1];
4452 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004453 if (mb_l == 0) /* at the NUL at end-of-line */
4454 mb_l = 1;
4455
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 /* If a double-width char doesn't fit display a '>' in the
4457 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004458 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459# ifdef FEAT_RIGHTLEFT
4460 wp->w_p_rl ? (col <= 0) :
4461# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004462 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 && (*mb_char2cells)(mb_c) == 2)
4464 {
4465 c = '>';
4466 mb_c = c;
4467 mb_l = 1;
4468 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004469 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar017ba072019-09-14 21:01:23 +02004470#ifdef FEAT_SYN_HL
4471 if (cul_attr)
4472 multi_attr = hl_combine_attr(multi_attr, cul_attr);
4473#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 /* put the pointer back to output the double-width
4475 * character at the start of the next line. */
4476 ++n_extra;
4477 --p_extra;
4478 }
4479 else
4480 {
4481 n_extra -= mb_l - 1;
4482 p_extra += mb_l - 1;
4483 }
4484 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 ++p_extra;
4486 }
4487 --n_extra;
4488 }
4489 else
4490 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004491#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004492 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004493#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004494
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004495 if (p_extra_free != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01004496 VIM_CLEAR(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 /*
4498 * Get a character from the line itself.
4499 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004500 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004501#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004502 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004503#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 if (has_mbyte)
4505 {
4506 mb_c = c;
4507 if (enc_utf8)
4508 {
4509 /* If the UTF-8 character is more than one byte: Decode it
4510 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004511 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 mb_utf8 = FALSE;
4513 if (mb_l > 1)
4514 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004515 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 /* Overlong encoded ASCII or ASCII with composing char
4517 * is displayed normally, except a NUL. */
4518 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004519 {
4520 c = mb_c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004521#ifdef FEAT_LINEBREAK
Bram Moolenaar88e76882017-02-27 20:33:46 +01004522 c0 = mb_c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004523#endif
Bram Moolenaar88e76882017-02-27 20:33:46 +01004524 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004526
4527 /* At start of the line we can have a composing char.
4528 * Draw it as a space with a composing char. */
4529 if (utf_iscomposing(mb_c))
4530 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004531 int i;
4532
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004533 for (i = Screen_mco - 1; i > 0; --i)
4534 u8cc[i] = u8cc[i - 1];
4535 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004536 mb_c = ' ';
4537 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 }
4539
4540 if ((mb_l == 1 && c >= 0x80)
4541 || (mb_l >= 1 && mb_c == 0)
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004542 || (mb_l > 1 && (!vim_isprintc(mb_c))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 {
4544 /*
4545 * Illegal UTF-8 byte: display as <xx>.
4546 * Non-BMP character : display as ? or fullwidth ?.
4547 */
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004548 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004549# ifdef FEAT_RIGHTLEFT
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004550 if (wp->w_p_rl) /* reverse */
4551 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004552# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 p_extra = extra;
4554 c = *p_extra;
4555 mb_c = mb_ptr2char_adv(&p_extra);
4556 mb_utf8 = (c >= 0x80);
4557 n_extra = (int)STRLEN(p_extra);
4558 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004559 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 if (area_attr == 0 && search_attr == 0)
4561 {
4562 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004563 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 saved_attr2 = char_attr; /* save current attr */
4565 }
4566 }
4567 else if (mb_l == 0) /* at the NUL at end-of-line */
4568 mb_l = 1;
4569#ifdef FEAT_ARABIC
4570 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4571 {
4572 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004573 int pc, pc1, nc;
4574 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575
4576 /* The idea of what is the previous and next
4577 * character depends on 'rightleft'. */
4578 if (wp->w_p_rl)
4579 {
4580 pc = prev_c;
4581 pc1 = prev_c1;
4582 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004583 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 }
4585 else
4586 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004587 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004589 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 }
4591 prev_c = mb_c;
4592
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004593 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 }
4595 else
4596 prev_c = mb_c;
4597#endif
4598 }
4599 else /* enc_dbcs */
4600 {
4601 mb_l = MB_BYTE2LEN(c);
4602 if (mb_l == 0) /* at the NUL at end-of-line */
4603 mb_l = 1;
4604 else if (mb_l > 1)
4605 {
4606 /* We assume a second byte below 32 is illegal.
4607 * Hopefully this is OK for all double-byte encodings!
4608 */
4609 if (ptr[1] >= 32)
4610 mb_c = (c << 8) + ptr[1];
4611 else
4612 {
4613 if (ptr[1] == NUL)
4614 {
4615 /* head byte at end of line */
4616 mb_l = 1;
4617 transchar_nonprint(extra, c);
4618 }
4619 else
4620 {
4621 /* illegal tail byte */
4622 mb_l = 2;
4623 STRCPY(extra, "XX");
4624 }
4625 p_extra = extra;
4626 n_extra = (int)STRLEN(extra) - 1;
4627 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004628 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 c = *p_extra++;
4630 if (area_attr == 0 && search_attr == 0)
4631 {
4632 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004633 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 saved_attr2 = char_attr; /* save current attr */
4635 }
4636 mb_c = c;
4637 }
4638 }
4639 }
4640 /* If a double-width char doesn't fit display a '>' in the
4641 * last column; the character is displayed at the start of the
4642 * next line. */
4643 if ((
4644# ifdef FEAT_RIGHTLEFT
4645 wp->w_p_rl ? (col <= 0) :
4646# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004647 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 && (*mb_char2cells)(mb_c) == 2)
4649 {
4650 c = '>';
4651 mb_c = c;
4652 mb_utf8 = FALSE;
4653 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004654 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02004655 // Put pointer back so that the character will be
4656 // displayed at the start of the next line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 --ptr;
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02004658#ifdef FEAT_CONCEAL
4659 did_decrement_ptr = TRUE;
4660#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 }
4662 else if (*ptr != NUL)
4663 ptr += mb_l - 1;
4664
4665 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004666 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004667 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004668 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004671 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004672 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 c = ' ';
4674 if (area_attr == 0 && search_attr == 0)
4675 {
4676 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004677 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 saved_attr2 = char_attr; /* save current attr */
4679 }
4680 mb_c = c;
4681 mb_utf8 = FALSE;
4682 mb_l = 1;
4683 }
4684
4685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 ++ptr;
4687
4688 if (extra_check)
4689 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004690#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004691 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004692#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004693
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004694#ifdef FEAT_TERMINAL
4695 if (get_term_attr)
4696 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02004697 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004698
4699 if (!attr_pri)
4700 char_attr = syntax_attr;
4701 else
4702 char_attr = hl_combine_attr(syntax_attr, char_attr);
4703 }
4704#endif
4705
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004706#ifdef FEAT_SYN_HL
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004707 // Get syntax attribute, unless still at the start of the line
4708 // (double-wide char that doesn't fit).
Bram Moolenaar217ad922005-03-20 22:37:15 +00004709 v = (long)(ptr - line);
4710 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 {
4712 /* Get the syntax attribute for the character. If there
4713 * is an error, disable syntax highlighting. */
4714 save_did_emsg = did_emsg;
4715 did_emsg = FALSE;
4716
Bram Moolenaar217ad922005-03-20 22:37:15 +00004717 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004718# ifdef FEAT_SPELL
4719 has_spell ? &can_spell :
4720# endif
4721 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722
4723 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004724 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004725 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004726 has_syntax = FALSE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004727 syntax_attr = 0;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 else
4730 did_emsg = save_did_emsg;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004731
4732 // combine syntax attribute with 'wincolor'
4733 if (win_attr != 0)
4734 syntax_attr = hl_combine_attr(win_attr, syntax_attr);
4735
Bram Moolenaar017ba072019-09-14 21:01:23 +02004736# ifdef SYN_TIME_LIMIT
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004737 if (wp->w_s->b_syn_slow)
4738 has_syntax = FALSE;
Bram Moolenaar017ba072019-09-14 21:01:23 +02004739# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740
4741 /* Need to get the line again, a multi-line regexp may
4742 * have made it invalid. */
4743 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4744 ptr = line + v;
4745
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004746# ifdef FEAT_TEXT_PROP
Bram Moolenaarde24a872019-05-05 15:48:00 +02004747 // Text properties overrule syntax highlighting or combine.
4748 if (text_prop_attr == 0 || text_prop_combine)
4749# endif
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004750 {
Bram Moolenaarde24a872019-05-05 15:48:00 +02004751 int comb_attr = syntax_attr;
4752# ifdef FEAT_TEXT_PROP
4753 comb_attr = hl_combine_attr(text_prop_attr, comb_attr);
4754# endif
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004755 if (!attr_pri)
Bram Moolenaar017ba072019-09-14 21:01:23 +02004756 {
4757#ifdef FEAT_SYN_HL
4758 if (cul_attr)
4759 char_attr = hl_combine_attr(
4760 comb_attr, cul_attr);
4761 else
4762#endif
4763 char_attr = comb_attr;
4764 }
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004765 else
Bram Moolenaarde24a872019-05-05 15:48:00 +02004766 char_attr = hl_combine_attr(comb_attr, char_attr);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004767 }
Bram Moolenaarc095b282010-07-20 22:33:34 +02004768# ifdef FEAT_CONCEAL
4769 /* no concealing past the end of the line, it interferes
4770 * with line highlighting */
4771 if (c == NUL)
4772 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004773 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004774 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004775# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004777#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004778
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004779#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004780 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004781 * Only do this when there is no syntax highlighting, the
4782 * @Spell cluster is not used or the current syntax item
4783 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004784 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004785 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004786 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004787 if (c != 0 && (
4788# ifdef FEAT_SYN_HL
4789 !has_syntax ||
4790# endif
4791 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004792 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004793 char_u *prev_ptr, *p;
4794 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004795 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaare7566042005-06-17 22:00:15 +00004796 if (has_mbyte)
4797 {
4798 prev_ptr = ptr - mb_l;
4799 v -= mb_l - 1;
4800 }
4801 else
Bram Moolenaare7566042005-06-17 22:00:15 +00004802 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004803
4804 /* Use nextline[] if possible, it has the start of the
4805 * next line concatenated. */
4806 if ((prev_ptr - line) - nextlinecol >= 0)
4807 p = nextline + (prev_ptr - line) - nextlinecol;
4808 else
4809 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004810 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004811 len = spell_check(wp, p, &spell_hlf, &cap_col,
4812 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004813 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004814
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004815 /* In Insert mode only highlight a word that
4816 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004817 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004818 && (State & INSERT) != 0
4819 && wp->w_cursor.lnum == lnum
4820 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004821 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004822 && wp->w_cursor.col < (colnr_T)word_end)
4823 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004824 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004825 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004826 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004827
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004828 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004829 && (p - nextline) + len > nextline_idx)
4830 {
4831 /* Remember that the good word continues at the
4832 * start of the next line. */
4833 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004834 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004835 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004836
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004837 /* Turn index into actual attributes. */
4838 if (spell_hlf != HLF_COUNT)
4839 spell_attr = highlight_attr[spell_hlf];
4840
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004841 if (cap_col > 0)
4842 {
4843 if (p != prev_ptr
4844 && (p - nextline) + cap_col >= nextline_idx)
4845 {
4846 /* Remember that the word in the next line
4847 * must start with a capital. */
4848 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004849 cap_col = (int)((p - nextline) + cap_col
4850 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004851 }
4852 else
4853 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004854 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004855 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004856 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004857 }
4858 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004859 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004860 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004861 char_attr = hl_combine_attr(char_attr, spell_attr);
4862 else
4863 char_attr = hl_combine_attr(spell_attr, char_attr);
4864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865#endif
4866#ifdef FEAT_LINEBREAK
4867 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004868 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004870 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004871 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 {
Bram Moolenaar4df70292015-03-21 14:20:16 +01004873 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004874 char_u *p = ptr - (mb_off + 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004875
Bram Moolenaar597a4222014-06-25 14:39:50 +02004876 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004877 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004878 NULL) - 1;
Bram Moolenaar02631462017-09-22 15:20:32 +02004879 if (c == TAB && n_extra + col > wp->w_width)
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004880# ifdef FEAT_VARTABS
Bram Moolenaara87b72c2018-06-25 21:24:51 +02004881 n_extra = tabstop_padding(vcol, wp->w_buffer->b_p_ts,
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004882 wp->w_buffer->b_p_vts_array) - 1;
4883# else
Bram Moolenaara3650912014-11-19 13:21:57 +01004884 n_extra = (int)wp->w_buffer->b_p_ts
4885 - vcol % (int)wp->w_buffer->b_p_ts - 1;
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004886# endif
Bram Moolenaara3650912014-11-19 13:21:57 +01004887
Bram Moolenaar4df70292015-03-21 14:20:16 +01004888 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004889 c_final = NUL;
Bram Moolenaar1c465442017-03-12 20:10:05 +01004890 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004891 {
4892#ifdef FEAT_CONCEAL
4893 if (c == TAB)
4894 /* See "Tab alignment" below. */
4895 FIX_FOR_BOGUSCOLS;
4896#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004897 if (!wp->w_p_list)
4898 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004899 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 }
4901#endif
4902
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004903 // 'list': Change char 160 to lcs_nbsp and space to lcs_space.
4904 // But not when the character is followed by a composing
4905 // character (use mb_l to check that).
4906 if (wp->w_p_list
4907 && ((((c == 160 && mb_l == 1)
4908 || (mb_utf8
4909 && ((mb_c == 160 && mb_l == 2)
4910 || (mb_c == 0x202f && mb_l == 3))))
4911 && lcs_nbsp)
4912 || (c == ' '
4913 && mb_l == 1
4914 && lcs_space
4915 && ptr - line <= trailcol)))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004916 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004917 c = (c == ' ') ? lcs_space : lcs_nbsp;
4918 if (area_attr == 0 && search_attr == 0)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004919 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004920 n_attr = 1;
4921 extra_attr = HL_ATTR(HLF_8);
4922 saved_attr2 = char_attr; /* save current attr */
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004923 }
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004924 mb_c = c;
4925 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004926 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004927 mb_utf8 = TRUE;
4928 u8cc[0] = 0;
4929 c = 0xc0;
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004930 }
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004931 else
4932 mb_utf8 = FALSE;
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004933 }
4934
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4936 {
4937 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004938 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 {
4940 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004941 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 saved_attr2 = char_attr; /* save current attr */
4943 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004945 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 {
4947 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004948 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004949 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 }
4951 else
4952 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 }
4954 }
4955
4956 /*
4957 * Handling of non-printable characters.
4958 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004959 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 {
4961 /*
4962 * when getting a character from the file, we may have to
4963 * turn it into something else on the way to putting it
4964 * into "ScreenLines".
4965 */
4966 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4967 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004968 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004969 long vcol_adjusted = vcol; /* removed showbreak length */
4970#ifdef FEAT_LINEBREAK
4971 /* only adjust the tab_len, when at the first column
4972 * after the showbreak value was drawn */
4973 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4974 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4975#endif
Bram Moolenaar69cbbec2019-08-17 14:10:56 +02004976 // tab amount depends on current column
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004977#ifdef FEAT_VARTABS
4978 tab_len = tabstop_padding(vcol_adjusted,
4979 wp->w_buffer->b_p_ts,
4980 wp->w_buffer->b_p_vts_array) - 1;
4981#else
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004982 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004983 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4984#endif
Bram Moolenaard574ea22015-01-14 19:35:14 +01004985
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004986#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004987 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004988#endif
Bram Moolenaar69cbbec2019-08-17 14:10:56 +02004989 // tab amount depends on current column
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004990 n_extra = tab_len;
4991#ifdef FEAT_LINEBREAK
4992 else
4993 {
Bram Moolenaar69cbbec2019-08-17 14:10:56 +02004994 char_u *p;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01004995 int len;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004996 int i;
4997 int saved_nextra = n_extra;
4998
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004999#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005000 if (vcol_off > 0)
Bram Moolenaar69cbbec2019-08-17 14:10:56 +02005001 // there are characters to conceal
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005002 tab_len += vcol_off;
Bram Moolenaar69cbbec2019-08-17 14:10:56 +02005003 // boguscols before FIX_FOR_BOGUSCOLS macro from above
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005004 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
5005 && n_extra > tab_len)
5006 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005007#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005008
Bram Moolenaar69cbbec2019-08-17 14:10:56 +02005009 // if n_extra > 0, it gives the number of chars, to
5010 // use for a tab, else we need to calculate the width
5011 // for a tab
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005012 len = (tab_len * mb_char2len(lcs_tab2));
5013 if (n_extra > 0)
5014 len += n_extra - tab_len;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005015 c = lcs_tab1;
Bram Moolenaar964b3742019-05-24 18:54:09 +02005016 p = alloc(len + 1);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005017 vim_memset(p, ' ', len);
5018 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005019 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005020 p_extra_free = p;
5021 for (i = 0; i < tab_len; i++)
5022 {
Bram Moolenaar69cbbec2019-08-17 14:10:56 +02005023 int lcs = lcs_tab2;
5024
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02005025 if (*p == NUL)
5026 {
5027 tab_len = i;
5028 break;
5029 }
Bram Moolenaar69cbbec2019-08-17 14:10:56 +02005030
5031 // if lcs_tab3 is given, need to change the char
5032 // for tab
5033 if (lcs_tab3 && i == tab_len - 1)
5034 lcs = lcs_tab3;
5035 mb_char2bytes(lcs, p);
5036 p += mb_char2len(lcs);
5037 n_extra += mb_char2len(lcs)
5038 - (saved_nextra > 0 ? 1 : 0);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005039 }
5040 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005041#ifdef FEAT_CONCEAL
Bram Moolenaar69cbbec2019-08-17 14:10:56 +02005042 // n_extra will be increased by FIX_FOX_BOGUSCOLS
5043 // macro below, so need to adjust for that here
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005044 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005045 n_extra -= vcol_off;
5046#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005047 }
5048#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005049#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005050 {
5051 int vc_saved = vcol_off;
5052
5053 /* Tab alignment should be identical regardless of
5054 * 'conceallevel' value. So tab compensates of all
5055 * previous concealed characters, and thus resets
5056 * vcol_off and boguscols accumulated so far in the
5057 * line. Note that the tab can be longer than
5058 * 'tabstop' when there are concealed characters. */
5059 FIX_FOR_BOGUSCOLS;
5060
5061 /* Make sure, the highlighting for the tab char will be
5062 * correctly set further below (effectively reverts the
5063 * FIX_FOR_BOGSUCOLS macro */
5064 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005065 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005066 tab_len += vc_saved;
5067 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005068#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 if (wp->w_p_list)
5071 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01005072 c = (n_extra == 0 && lcs_tab3) ? lcs_tab3 : lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005073#ifdef FEAT_LINEBREAK
5074 if (wp->w_p_lbr)
5075 c_extra = NUL; /* using p_extra from above */
5076 else
5077#endif
5078 c_extra = lcs_tab2;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005079 c_final = lcs_tab3;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005080 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005081 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 saved_attr2 = char_attr; /* save current attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005084 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 {
5086 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005087 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005088 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 }
5091 else
5092 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01005093 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 c_extra = ' ';
5095 c = ' ';
5096 }
5097 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005098 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02005099 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005100 || ((fromcol >= 0 || fromcol_prev >= 0)
5101 && tocol > vcol
5102 && VIsual_mode != Ctrl_V
5103 && (
5104# ifdef FEAT_RIGHTLEFT
5105 wp->w_p_rl ? (col >= 0) :
5106# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005107 (col < wp->w_width))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005108 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005109 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005110 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02005111 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005113 /* Display a '$' after the line or highlight an extra
5114 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115#if defined(FEAT_DIFF) || defined(LINE_ATTR)
5116 /* For a diff line the highlighting continues after the
5117 * "$". */
5118 if (
5119# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005120 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121# ifdef LINE_ATTR
5122 &&
5123# endif
5124# endif
5125# ifdef LINE_ATTR
5126 line_attr == 0
5127# endif
5128 )
5129#endif
5130 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 /* In virtualedit, visual selections may extend
5132 * beyond end of line. */
5133 if (area_highlighting && virtual_active()
5134 && tocol != MAXCOL && vcol < tocol)
5135 n_extra = 0;
5136 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 {
5138 p_extra = at_end_str;
5139 n_extra = 1;
5140 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005141 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 }
5143 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02005144 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005145 c = lcs_eol;
5146 else
5147 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 lcs_eol_one = -1;
5149 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005150 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005152 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 n_attr = 1;
5154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005156 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 {
5158 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005159 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005160 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 }
5162 else
5163 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 }
5165 else if (c != NUL)
5166 {
5167 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02005168 if (n_extra == 0)
5169 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170#ifdef FEAT_RIGHTLEFT
5171 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
5172 rl_mirror(p_extra); /* reverse "<12>" */
5173#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005175 c_final = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005176#ifdef FEAT_LINEBREAK
5177 if (wp->w_p_lbr)
5178 {
5179 char_u *p;
5180
5181 c = *p_extra;
Bram Moolenaar964b3742019-05-24 18:54:09 +02005182 p = alloc(n_extra + 1);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005183 vim_memset(p, ' ', n_extra);
5184 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5185 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005186 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005187 p_extra_free = p_extra = p;
5188 }
5189 else
5190#endif
5191 {
5192 n_extra = byte2cells(c) - 1;
5193 c = *p_extra++;
5194 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005195 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 {
5197 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005198 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 saved_attr2 = char_attr; /* save current attr */
5200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 else if (VIsual_active
5204 && (VIsual_mode == Ctrl_V
5205 || VIsual_mode == 'v')
5206 && virtual_active()
5207 && tocol != MAXCOL
5208 && vcol < tocol
5209 && (
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005210#ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 wp->w_p_rl ? (col >= 0) :
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005212#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005213 (col < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 {
5215 c = ' ';
5216 --ptr; /* put it back at the NUL */
5217 }
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005218#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 else if ((
5220# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005221 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005223# ifdef FEAT_TERMINAL
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005224 win_attr != 0 ||
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005225# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 ) && (
5228# ifdef FEAT_RIGHTLEFT
5229 wp->w_p_rl ? (col >= 0) :
5230# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005231 (col
5232# ifdef FEAT_CONCEAL
5233 - boguscols
5234# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005235 < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 {
5237 /* Highlight until the right side of the window */
5238 c = ' ';
5239 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005240
5241 /* Remember we do the char for line highlighting. */
5242 ++did_line_attr;
5243
5244 /* don't do search HL for the rest of the line */
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005245 if (line_attr != 0 && char_attr == search_attr
5246 && (did_line_attr > 1
5247 || (wp->w_p_list && lcs_eol > 0)))
Bram Moolenaar91170f82006-05-05 21:15:17 +00005248 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249# ifdef FEAT_DIFF
5250 if (diff_hlf == HLF_TXD)
5251 {
5252 diff_hlf = HLF_CHD;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005253 if (vi_attr == 0 || char_attr != vi_attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005254 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005255 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005256 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaar017ba072019-09-14 21:01:23 +02005257 && wp->w_p_culopt_flags != CULOPT_NBR
5258 && (!cul_screenline
5259 || (vcol >= left_curline_col
5260 && vcol <= right_curline_col)))
5261 char_attr = hl_combine_attr(
5262 char_attr, HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 }
5265# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005266# ifdef FEAT_TERMINAL
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005267 if (win_attr != 0)
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005268 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005269 char_attr = win_attr;
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005270 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar017ba072019-09-14 21:01:23 +02005271 {
5272 if (!cul_screenline || (vcol >= left_curline_col
5273 && vcol <= right_curline_col))
5274 char_attr = hl_combine_attr(
5275 char_attr, HL_ATTR(HLF_CUL));
5276 }
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02005277 else if (line_attr)
5278 char_attr = hl_combine_attr(char_attr, line_attr);
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005279 }
5280# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 }
5282#endif
5283 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005284
5285#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005286 if ( wp->w_p_cole > 0
5287 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaarab62c192019-03-30 16:39:05 +01005288 conceal_cursor_line(wp))
5289 && ((syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005290 && !(lnum_in_visual_area
5291 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005292 {
5293 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005294 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005295 && (syn_get_sub_char() != NUL || match_conc
5296 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005297 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005298 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005299 /* First time at this concealed item: display one
5300 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005301 if (match_conc)
5302 c = match_conc;
5303 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005304 c = syn_get_sub_char();
5305 else if (lcs_conceal != NUL)
5306 c = lcs_conceal;
5307 else
5308 c = ' ';
5309
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005310 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005311
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005312 if (n_extra > 0)
5313 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005314 vcol += n_extra;
5315 if (wp->w_p_wrap && n_extra > 0)
5316 {
5317# ifdef FEAT_RIGHTLEFT
5318 if (wp->w_p_rl)
5319 {
5320 col -= n_extra;
5321 boguscols -= n_extra;
5322 }
5323 else
5324# endif
5325 {
5326 boguscols += n_extra;
5327 col += n_extra;
5328 }
5329 }
5330 n_extra = 0;
5331 n_attr = 0;
5332 }
5333 else if (n_skip == 0)
5334 {
5335 is_concealing = TRUE;
5336 n_skip = 1;
5337 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005338 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005339 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005340 {
5341 mb_utf8 = TRUE;
5342 u8cc[0] = 0;
5343 c = 0xc0;
5344 }
5345 else
5346 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005347 }
5348 else
5349 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005350 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005351 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005352 }
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02005353
5354 if (n_skip > 0 && did_decrement_ptr)
5355 // not showing the '>', put pointer back to avoid getting stuck
5356 ++ptr;
5357
5358#endif // FEAT_CONCEAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 }
5360
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005361#ifdef FEAT_CONCEAL
5362 /* In the cursor line and we may be concealing characters: correct
5363 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005364 if (!did_wcol && draw_state == WL_LINE
5365 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005366 && conceal_cursor_line(wp)
5367 && (int)wp->w_virtcol <= vcol + n_skip)
5368 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005369# ifdef FEAT_RIGHTLEFT
5370 if (wp->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02005371 wp->w_wcol = wp->w_width - col + boguscols - 1;
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005372 else
5373# endif
5374 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005375 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005376 did_wcol = TRUE;
Bram Moolenaar5babc6e2019-09-14 21:55:51 +02005377 curwin->w_valid |= VALID_WCOL|VALID_WROW|VALID_VIRTCOL;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005378 }
5379#endif
5380
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 /* Don't override visual selection highlighting. */
5382 if (n_attr > 0
5383 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005384 && !attr_pri)
Bram Moolenaar017ba072019-09-14 21:01:23 +02005385 {
Bram Moolenaar0571f3d2019-09-14 22:33:47 +02005386#ifdef LINE_ATTR
Bram Moolenaar017ba072019-09-14 21:01:23 +02005387 if (line_attr)
5388 char_attr = hl_combine_attr(extra_attr, line_attr);
5389 else
Bram Moolenaar0571f3d2019-09-14 22:33:47 +02005390#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02005391 char_attr = extra_attr;
5392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393
Bram Moolenaar81695252004-12-29 20:58:21 +00005394#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 /* XIM don't send preedit_start and preedit_end, but they send
5396 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5397 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005398 if (p_imst == IM_ON_THE_SPOT
5399 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005400 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 && (State & INSERT)
5402 && !p_imdisable
5403 && im_is_preediting()
5404 && draw_state == WL_LINE)
5405 {
5406 colnr_T tcol;
5407
5408 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005409 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 else
5411 tcol = preedit_end_col;
5412 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5413 {
5414 if (feedback_old_attr < 0)
5415 {
5416 feedback_col = 0;
5417 feedback_old_attr = char_attr;
5418 }
5419 char_attr = im_get_feedback_attr(feedback_col);
5420 if (char_attr < 0)
5421 char_attr = feedback_old_attr;
5422 feedback_col++;
5423 }
5424 else if (feedback_old_attr >= 0)
5425 {
5426 char_attr = feedback_old_attr;
5427 feedback_old_attr = -1;
5428 feedback_col = 0;
5429 }
5430 }
5431#endif
5432 /*
5433 * Handle the case where we are in column 0 but not on the first
5434 * character of the line and the user wants us to show us a
5435 * special character (via 'listchars' option "precedes:<char>".
5436 */
5437 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005438 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5440#ifdef FEAT_DIFF
5441 && filler_todo <= 0
5442#endif
5443 && draw_state > WL_NR
5444 && c != NUL)
5445 {
5446 c = lcs_prec;
5447 lcs_prec_todo = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005448 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5449 {
5450 /* Double-width character being overwritten by the "precedes"
5451 * character, need to fill up half the character. */
5452 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005453 c_final = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005454 n_extra = 1;
5455 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005456 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005459 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 {
5461 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005462 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005463 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 }
5465 else
5466 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005467 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 {
5469 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005470 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 n_attr3 = 1;
5472 }
5473 }
5474
5475 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005476 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 */
Bram Moolenaarf914a332019-07-20 15:09:56 +02005478 if ((c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005479#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005480 || did_line_attr == 1
5481#endif
Bram Moolenaarf914a332019-07-20 15:09:56 +02005482 ) && eol_hl_off == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005484#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarbbca7732019-07-24 18:13:16 +02005485 // flag to indicate whether prevcol equals startcol of search_hl or
5486 // one of the matches
5487 int prevcol_hl_flag = get_prevcol_hl_flag(wp, &search_hl,
5488 (long)(ptr - line) - (c == NUL));
Bram Moolenaar91170f82006-05-05 21:15:17 +00005489#endif
Bram Moolenaarbbca7732019-07-24 18:13:16 +02005490 // Invert at least one char, used for Visual and empty line or
5491 // highlight match at end of line. If it's beyond the last
5492 // char on the screen, just overwrite that one (tricky!) Not
5493 // needed when a '$' was displayed for 'list'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005495 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005496 && (VIsual_mode != Ctrl_V
5497 || lnum == VIsual.lnum
5498 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005499 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarbbca7732019-07-24 18:13:16 +02005501 // highlight 'hlsearch' match at end of line
5502 || (prevcol_hl_flag
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005503# ifdef FEAT_SYN_HL
5504 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
5505 && !(wp == curwin && VIsual_active))
5506# endif
5507# ifdef FEAT_DIFF
5508 && diff_hlf == (hlf_T)0
5509# endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005510# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005511 && did_line_attr <= 1
5512# endif
5513 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514#endif
5515 ))
5516 {
5517 int n = 0;
5518
5519#ifdef FEAT_RIGHTLEFT
5520 if (wp->w_p_rl)
5521 {
5522 if (col < 0)
5523 n = 1;
5524 }
5525 else
5526#endif
5527 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005528 if (col >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 n = -1;
5530 }
5531 if (n != 0)
5532 {
5533 /* At the window boundary, highlight the last character
5534 * instead (better than nothing). */
5535 off += n;
5536 col += n;
5537 }
5538 else
5539 {
5540 /* Add a blank character to highlight. */
5541 ScreenLines[off] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 if (enc_utf8)
5543 ScreenLinesUC[off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 }
5545#ifdef FEAT_SEARCH_EXTRA
5546 if (area_attr == 0)
5547 {
Bram Moolenaarbbca7732019-07-24 18:13:16 +02005548 // Use attributes from match with highest priority among
5549 // 'search_hl' and the match list.
5550 get_search_match_hl(wp, &search_hl,
5551 (long)(ptr - line), &char_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 }
5553#endif
5554 ScreenAttrs[off] = char_attr;
5555#ifdef FEAT_RIGHTLEFT
5556 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005557 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005559 --off;
5560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 else
5562#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005563 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005565 ++off;
5566 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005567 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005568 eol_hl_off = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571
Bram Moolenaar91170f82006-05-05 21:15:17 +00005572 /*
5573 * At end of the text line.
5574 */
5575 if (c == NUL)
5576 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005577#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005578 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005579 if (wp->w_p_wrap)
5580 v = wp->w_skipcol;
5581 else
5582 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005583
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005584 /* check if line ends before left margin */
5585 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005586 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005587#ifdef FEAT_CONCEAL
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01005588 // Get rid of the boguscols now, we want to draw until the right
5589 // edge for 'cursorcolumn'.
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005590 col -= boguscols;
5591 boguscols = 0;
5592#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005593
5594 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005595 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005596
5597 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005598 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5599 && (int)wp->w_virtcol <
Bram Moolenaar02631462017-09-22 15:20:32 +02005600 wp->w_width * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005601 && lnum != wp->w_cursor.lnum)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005602 || draw_color_col
5603 || win_attr != 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005604# ifdef FEAT_RIGHTLEFT
5605 && !wp->w_p_rl
5606# endif
5607 )
5608 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005609 int rightmost_vcol = 0;
5610 int i;
5611
5612 if (wp->w_p_cuc)
5613 rightmost_vcol = wp->w_virtcol;
5614 if (draw_color_col)
5615 /* determine rightmost colorcolumn to possibly draw */
5616 for (i = 0; color_cols[i] >= 0; ++i)
5617 if (rightmost_vcol < color_cols[i])
5618 rightmost_vcol = color_cols[i];
5619
Bram Moolenaar02631462017-09-22 15:20:32 +02005620 while (col < wp->w_width)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005621 {
5622 ScreenLines[off] = ' ';
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005623 if (enc_utf8)
5624 ScreenLinesUC[off] = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005625 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005626 if (draw_color_col)
5627 draw_color_col = advance_color_col(VCOL_HLC,
5628 &color_cols);
5629
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005630 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005631 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005632 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005633 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005634 else
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005635 ScreenAttrs[off++] = win_attr;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005636
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005637 if (VCOL_HLC >= rightmost_vcol && win_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005638 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005639
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005640 ++vcol;
5641 }
5642 }
5643#endif
5644
Bram Moolenaar53f81742017-09-22 14:35:51 +02005645 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005646 (int)wp->w_width, screen_line_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 row++;
5648
5649 /*
5650 * Update w_cline_height and w_cline_folded if the cursor line was
5651 * updated (saves a call to plines() later).
5652 */
5653 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5654 {
5655 curwin->w_cline_row = startrow;
5656 curwin->w_cline_height = row - startrow;
5657#ifdef FEAT_FOLDING
5658 curwin->w_cline_folded = FALSE;
5659#endif
5660 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5661 }
5662
5663 break;
5664 }
5665
Bram Moolenaara5c6a0b2019-05-08 20:20:46 +02005666 // Show "extends" character from 'listchars' if beyond the line end and
5667 // 'list' is set.
5668 if (lcs_ext != NUL
5669 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670 && !wp->w_p_wrap
5671#ifdef FEAT_DIFF
5672 && filler_todo <= 0
5673#endif
5674 && (
5675#ifdef FEAT_RIGHTLEFT
5676 wp->w_p_rl ? col == 0 :
5677#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005678 col == wp->w_width - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005680 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5682 {
5683 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005684 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005686 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 {
5688 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005689 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005690 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 }
5692 else
5693 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 }
5695
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005696#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005697 /* advance to the next 'colorcolumn' */
5698 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005699 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005700
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005701 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005702 * highlight the cursor position itself.
5703 * Also highlight the 'colorcolumn' if it is different than
5704 * 'cursorcolumn' */
5705 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005706 if (draw_state == WL_LINE && !lnum_in_visual_area
5707 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005708 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005709 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005710 && lnum != wp->w_cursor.lnum)
5711 {
5712 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005713 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005714 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005715 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005716 {
5717 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005718 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005719 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005720 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005721#endif
5722
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 /*
5724 * Store character to be displayed.
5725 * Skip characters that are left of the screen for 'nowrap'.
5726 */
5727 vcol_prev = vcol;
5728 if (draw_state < WL_LINE || n_skip <= 0)
5729 {
5730 /*
5731 * Store the character.
5732 */
Bram Moolenaara12a1612019-01-24 16:39:02 +01005733#if defined(FEAT_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5735 {
5736 /* A double-wide character is: put first halve in left cell. */
5737 --off;
5738 --col;
5739 }
5740#endif
5741 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005743 {
5744 if ((mb_c & 0xff00) == 0x8e00)
5745 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 else if (enc_utf8)
5749 {
5750 if (mb_utf8)
5751 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005752 int i;
5753
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005755 if ((c & 0xff) == 0)
5756 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005757 for (i = 0; i < Screen_mco; ++i)
5758 {
5759 ScreenLinesC[i][off] = u8cc[i];
5760 if (u8cc[i] == 0)
5761 break;
5762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763 }
5764 else
5765 ScreenLinesUC[off] = 0;
5766 }
5767 if (multi_attr)
5768 {
5769 ScreenAttrs[off] = multi_attr;
5770 multi_attr = 0;
5771 }
5772 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 ScreenAttrs[off] = char_attr;
5774
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5776 {
5777 /* Need to fill two screen columns. */
5778 ++off;
5779 ++col;
5780 if (enc_utf8)
5781 /* UTF-8: Put a 0 in the second screen char. */
5782 ScreenLines[off] = 0;
5783 else
5784 /* DBCS: Put second byte in the second screen char. */
5785 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005786 if (draw_state > WL_NR
5787#ifdef FEAT_DIFF
5788 && filler_todo <= 0
5789#endif
5790 )
5791 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792 /* When "tocol" is halfway a character, set it to the end of
5793 * the character, otherwise highlighting won't stop. */
5794 if (tocol == vcol)
5795 ++tocol;
5796#ifdef FEAT_RIGHTLEFT
5797 if (wp->w_p_rl)
5798 {
5799 /* now it's time to backup one cell */
5800 --off;
5801 --col;
5802 }
5803#endif
5804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805#ifdef FEAT_RIGHTLEFT
5806 if (wp->w_p_rl)
5807 {
5808 --off;
5809 --col;
5810 }
5811 else
5812#endif
5813 {
5814 ++off;
5815 ++col;
5816 }
5817 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005818#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005819 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005820 {
5821 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005822 ++vcol_off;
5823 if (n_extra > 0)
5824 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005825 if (wp->w_p_wrap)
5826 {
5827 /*
5828 * Special voodoo required if 'wrap' is on.
5829 *
5830 * Advance the column indicator to force the line
5831 * drawing to wrap early. This will make the line
5832 * take up the same screen space when parts are concealed,
5833 * so that cursor line computations aren't messed up.
5834 *
5835 * To avoid the fictitious advance of 'col' causing
5836 * trailing junk to be written out of the screen line
5837 * we are building, 'boguscols' keeps track of the number
5838 * of bad columns we have advanced.
5839 */
5840 if (n_extra > 0)
5841 {
5842 vcol += n_extra;
5843# ifdef FEAT_RIGHTLEFT
5844 if (wp->w_p_rl)
5845 {
5846 col -= n_extra;
5847 boguscols -= n_extra;
5848 }
5849 else
5850# endif
5851 {
5852 col += n_extra;
5853 boguscols += n_extra;
5854 }
5855 n_extra = 0;
5856 n_attr = 0;
5857 }
5858
5859
Bram Moolenaar860cae12010-06-05 23:22:07 +02005860 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5861 {
5862 /* Need to fill two screen columns. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01005863# ifdef FEAT_RIGHTLEFT
Bram Moolenaar860cae12010-06-05 23:22:07 +02005864 if (wp->w_p_rl)
5865 {
5866 --boguscols;
5867 --col;
5868 }
5869 else
Bram Moolenaara12a1612019-01-24 16:39:02 +01005870# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02005871 {
5872 ++boguscols;
5873 ++col;
5874 }
5875 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005876
5877# ifdef FEAT_RIGHTLEFT
5878 if (wp->w_p_rl)
5879 {
5880 --boguscols;
5881 --col;
5882 }
5883 else
5884# endif
5885 {
5886 ++boguscols;
5887 ++col;
5888 }
5889 }
5890 else
5891 {
5892 if (n_extra > 0)
5893 {
5894 vcol += n_extra;
5895 n_extra = 0;
5896 n_attr = 0;
5897 }
5898 }
5899
5900 }
5901#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902 else
5903 --n_skip;
5904
Bram Moolenaar64486672010-05-16 15:46:46 +02005905 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5906 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005907 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908#ifdef FEAT_DIFF
5909 && filler_todo <= 0
5910#endif
5911 )
5912 ++vcol;
5913
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005914#ifdef FEAT_SYN_HL
5915 if (vcol_save_attr >= 0)
5916 char_attr = vcol_save_attr;
5917#endif
5918
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 /* restore attributes after "predeces" in 'listchars' */
5920 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5921 char_attr = saved_attr3;
5922
5923 /* restore attributes after last 'listchars' or 'number' char */
5924 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5925 char_attr = saved_attr2;
5926
5927 /*
5928 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005929 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930 */
5931 if ((
5932#ifdef FEAT_RIGHTLEFT
5933 wp->w_p_rl ? (col < 0) :
5934#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005935 (col >= wp->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 && (*ptr != NUL
5937#ifdef FEAT_DIFF
5938 || filler_todo > 0
5939#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005940 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5942 )
5943 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005944#ifdef FEAT_CONCEAL
Bram Moolenaar53f81742017-09-22 14:35:51 +02005945 screen_line(screen_row, wp->w_wincol, col - boguscols,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005946 (int)wp->w_width, screen_line_flags);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005947 boguscols = 0;
5948#else
Bram Moolenaar53f81742017-09-22 14:35:51 +02005949 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005950 (int)wp->w_width, screen_line_flags);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005951#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 ++row;
5953 ++screen_row;
5954
5955 /* When not wrapping and finished diff lines, or when displayed
5956 * '$' and highlighting until last column, break here. */
5957 if ((!wp->w_p_wrap
5958#ifdef FEAT_DIFF
5959 && filler_todo <= 0
5960#endif
5961 ) || lcs_eol_one == -1)
5962 break;
5963
5964 /* When the window is too narrow draw all "@" lines. */
5965 if (draw_state != WL_LINE
5966#ifdef FEAT_DIFF
5967 && filler_todo <= 0
5968#endif
5969 )
5970 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01005971 win_draw_end(wp, '@', ' ', TRUE, row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973 row = endrow;
5974 }
5975
5976 /* When line got too long for screen break here. */
5977 if (row == endrow)
5978 {
5979 ++row;
5980 break;
5981 }
5982
5983 if (screen_cur_row == screen_row - 1
5984#ifdef FEAT_DIFF
5985 && filler_todo <= 0
5986#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005987 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988 {
5989 /* Remember that the line wraps, used for modeless copy. */
5990 LineWraps[screen_row - 1] = TRUE;
5991
5992 /*
5993 * Special trick to make copy/paste of wrapped lines work with
5994 * xterm/screen: write an extra character beyond the end of
5995 * the line. This will work with all terminal types
5996 * (regardless of the xn,am settings).
5997 * Only do this on a fast tty.
5998 * Only do this if the cursor is on the current line
5999 * (something has been written in it).
6000 * Don't do this for the GUI.
6001 * Don't do this for double-width characters.
6002 * Don't do this for a window not at the right screen border.
6003 */
6004 if (p_tf
6005#ifdef FEAT_GUI
6006 && !gui.in_use
6007#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00006009 && ((*mb_off2cells)(LineOffset[screen_row],
6010 LineOffset[screen_row] + screen_Columns)
6011 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00006013 + (int)Columns - 2,
6014 LineOffset[screen_row] + screen_Columns)
Bram Moolenaara12a1612019-01-24 16:39:02 +01006015 == 2)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016 {
6017 /* First make sure we are at the end of the screen line,
6018 * then output the same character again to let the
6019 * terminal know about the wrap. If the terminal doesn't
6020 * auto-wrap, we overwrite the character. */
Bram Moolenaar02631462017-09-22 15:20:32 +02006021 if (screen_cur_col != wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022 screen_char(LineOffset[screen_row - 1]
6023 + (unsigned)Columns - 1,
6024 screen_row - 1, (int)(Columns - 1));
6025
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026 /* When there is a multi-byte character, just output a
6027 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00006028 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
6029 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030 out_char(' ');
6031 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032 out_char(ScreenLines[LineOffset[screen_row - 1]
6033 + (Columns - 1)]);
6034 /* force a redraw of the first char on the next line */
6035 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
6036 screen_start(); /* don't know where cursor is now */
6037 }
6038 }
6039
6040 col = 0;
6041 off = (unsigned)(current_ScreenLine - ScreenLines);
6042#ifdef FEAT_RIGHTLEFT
6043 if (wp->w_p_rl)
6044 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006045 col = wp->w_width - 1; /* col is not used if breaking! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046 off += col;
6047 }
6048#endif
6049
6050 /* reset the drawing state for the start of a wrapped line */
6051 draw_state = WL_START;
6052 saved_n_extra = n_extra;
6053 saved_p_extra = p_extra;
6054 saved_c_extra = c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01006055 saved_c_final = c_final;
Bram Moolenaar017ba072019-09-14 21:01:23 +02006056#ifdef FEAT_SYN_HL
6057 if (!(cul_screenline
6058# ifdef FEAT_DIFF
6059 && diff_hlf == (hlf_T)0)
6060# endif
6061 )
6062 saved_char_attr = char_attr;
6063 else
6064#endif
6065 saved_char_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 n_extra = 0;
6067 lcs_prec_todo = lcs_prec;
6068#ifdef FEAT_LINEBREAK
6069# ifdef FEAT_DIFF
6070 if (filler_todo <= 0)
6071# endif
6072 need_showbreak = TRUE;
6073#endif
6074#ifdef FEAT_DIFF
6075 --filler_todo;
6076 /* When the filler lines are actually below the last line of the
6077 * file, don't draw the line itself, break here. */
6078 if (filler_todo == 0 && wp->w_botfill)
6079 break;
6080#endif
6081 }
6082
6083 } /* for every character in the line */
6084
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006085#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006086 /* After an empty line check first word for capital. */
6087 if (*skipwhite(line) == NUL)
6088 {
6089 capcol_lnum = lnum + 1;
6090 cap_col = 0;
6091 }
6092#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006093#ifdef FEAT_TEXT_PROP
6094 vim_free(text_props);
6095 vim_free(text_prop_idxs);
6096#endif
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006097
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01006098 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099 return row;
6100}
6101
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006102/*
6103 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006104 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006105 */
6106 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006107comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006108{
6109 int i;
6110
6111 for (i = 0; i < Screen_mco; ++i)
6112 {
6113 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
6114 return TRUE;
6115 if (ScreenLinesC[i][off_from] == 0)
6116 break;
6117 }
6118 return FALSE;
6119}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006120
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121/*
6122 * Check whether the given character needs redrawing:
6123 * - the (first byte of the) character is different
6124 * - the attributes are different
6125 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006126 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127 */
6128 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006129char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130{
6131 if (cols > 0
6132 && ((ScreenLines[off_from] != ScreenLines[off_to]
6133 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 || (enc_dbcs != 0
6135 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
6136 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
6137 ? ScreenLines2[off_from] != ScreenLines2[off_to]
6138 : (cols > 1 && ScreenLines[off_from + 1]
6139 != ScreenLines[off_to + 1])))
6140 || (enc_utf8
6141 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6142 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006143 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006144 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6145 && ScreenLines[off_from + 1]
Bram Moolenaara12a1612019-01-24 16:39:02 +01006146 != ScreenLines[off_to + 1])))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147 return TRUE;
6148 return FALSE;
6149}
6150
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006151#if defined(FEAT_TERMINAL) || defined(PROTO)
6152/*
6153 * Return the index in ScreenLines[] for the current screen line.
6154 */
6155 int
6156screen_get_current_line_off()
6157{
6158 return (int)(current_ScreenLine - ScreenLines);
6159}
6160#endif
6161
Bram Moolenaarc662ec92019-06-23 00:15:57 +02006162#ifdef FEAT_TEXT_PROP
6163/*
6164 * Return TRUE if this position has a higher level popup or this cell is
6165 * transparent in the current popup.
6166 */
6167 static int
6168blocked_by_popup(int row, int col)
6169{
6170 int off;
6171
6172 if (!popup_visible)
6173 return FALSE;
6174 off = row * screen_Columns + col;
6175 return popup_mask[off] > screen_zindex || popup_transparent[off];
6176}
6177#endif
6178
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179/*
6180 * Move one "cooked" screen line to the screen, but only the characters that
6181 * have actually changed. Handle insert/delete character.
6182 * "coloff" gives the first column on the screen for this line.
6183 * "endcol" gives the columns where valid characters are.
6184 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6185 * needs to be cleared, negative otherwise.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006186 * "flags" can have bits:
6187 * SLF_POPUP popup window
6188 * SLF_RIGHTLEFT rightleft window:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6190 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6191 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006192 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006193screen_line(
6194 int row,
6195 int coloff,
6196 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006197 int clear_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006198 int flags UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199{
6200 unsigned off_from;
6201 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006202 unsigned max_off_from;
6203 unsigned max_off_to;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 int force = FALSE; /* force update rest of the line */
6207 int redraw_this /* bool: does character need redraw? */
6208#ifdef FEAT_GUI
6209 = TRUE /* For GUI when while-loop empty */
6210#endif
6211 ;
6212 int redraw_next; /* redraw_this for next character */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 int clear_next = FALSE;
6214 int char_cells; /* 1: normal char */
6215 /* 2: occupies two display cells */
6216# define CHAR_CELLS char_cells
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006218 /* Check for illegal row and col, just in case. */
6219 if (row >= Rows)
6220 row = Rows - 1;
6221 if (endcol > Columns)
6222 endcol = Columns;
6223
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224# ifdef FEAT_CLIPBOARD
6225 clip_may_clear_selection(row, row);
6226# endif
6227
6228 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6229 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006230 max_off_from = off_from + screen_Columns;
6231 max_off_to = LineOffset[row] + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232
6233#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006234 if (flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 {
6236 /* Clear rest first, because it's left of the text. */
6237 if (clear_width > 0)
6238 {
6239 while (col <= endcol && ScreenLines[off_to] == ' '
6240 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006241 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242 {
6243 ++off_to;
6244 ++col;
6245 }
6246 if (col <= endcol)
6247 screen_fill(row, row + 1, col + coloff,
6248 endcol + coloff + 1, ' ', ' ', 0);
6249 }
6250 col = endcol + 1;
6251 off_to = LineOffset[row] + col + coloff;
6252 off_from += col;
6253 endcol = (clear_width > 0 ? clear_width : -clear_width);
6254 }
6255#endif /* FEAT_RIGHTLEFT */
6256
6257 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6258
6259 while (col < endcol)
6260 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006262 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 else
6264 char_cells = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265
6266 redraw_this = redraw_next;
6267 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6268 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6269
6270#ifdef FEAT_GUI
6271 /* If the next character was bold, then redraw the current character to
6272 * remove any pixels that might have spilt over into us. This only
6273 * happens in the GUI.
6274 */
6275 if (redraw_next && gui.in_use)
6276 {
6277 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006278 if (hl > HL_ALL)
6279 hl = syn_attr2attr(hl);
6280 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281 redraw_this = TRUE;
6282 }
6283#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02006284#ifdef FEAT_TEXT_PROP
Bram Moolenaarc662ec92019-06-23 00:15:57 +02006285 if (blocked_by_popup(row, col + coloff))
Bram Moolenaar33796b32019-06-08 16:01:13 +02006286 redraw_this = FALSE;
6287#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288 if (redraw_this)
6289 {
6290 /*
6291 * Special handling when 'xs' termcap flag set (hpterm):
6292 * Attributes for characters are stored at the position where the
6293 * cursor is when writing the highlighting code. The
6294 * start-highlighting code must be written with the cursor on the
6295 * first highlighted character. The stop-highlighting code must
6296 * be written with the cursor just after the last highlighted
6297 * character.
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01006298 * Overwriting a character doesn't remove its highlighting. Need
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299 * to clear the rest of the line, and force redrawing it
6300 * completely.
6301 */
6302 if ( p_wiv
6303 && !force
6304#ifdef FEAT_GUI
6305 && !gui.in_use
6306#endif
6307 && ScreenAttrs[off_to] != 0
6308 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6309 {
6310 /*
6311 * Need to remove highlighting attributes here.
6312 */
6313 windgoto(row, col + coloff);
6314 out_str(T_CE); /* clear rest of this screen line */
6315 screen_start(); /* don't know where cursor is now */
6316 force = TRUE; /* force redraw of rest of the line */
6317 redraw_next = TRUE; /* or else next char would miss out */
6318
6319 /*
6320 * If the previous character was highlighted, need to stop
6321 * highlighting at this character.
6322 */
6323 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6324 {
6325 screen_attr = ScreenAttrs[off_to - 1];
6326 term_windgoto(row, col + coloff);
6327 screen_stop_highlight();
6328 }
6329 else
6330 screen_attr = 0; /* highlighting has stopped */
6331 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332 if (enc_dbcs != 0)
6333 {
6334 /* Check if overwriting a double-byte with a single-byte or
6335 * the other way around requires another character to be
6336 * redrawn. For UTF-8 this isn't needed, because comparing
6337 * ScreenLinesUC[] is sufficient. */
6338 if (char_cells == 1
6339 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006340 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 {
6342 /* Writing a single-cell character over a double-cell
6343 * character: need to redraw the next cell. */
6344 ScreenLines[off_to + 1] = 0;
6345 redraw_next = TRUE;
6346 }
6347 else if (char_cells == 2
6348 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006349 && (*mb_off2cells)(off_to, max_off_to) == 1
6350 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351 {
6352 /* Writing the second half of a double-cell character over
6353 * a double-cell character: need to redraw the second
6354 * cell. */
6355 ScreenLines[off_to + 2] = 0;
6356 redraw_next = TRUE;
6357 }
6358
6359 if (enc_dbcs == DBCS_JPNU)
6360 ScreenLines2[off_to] = ScreenLines2[off_from];
6361 }
6362 /* When writing a single-width character over a double-width
6363 * character and at the end of the redrawn text, need to clear out
6364 * the right halve of the old character.
6365 * Also required when writing the right halve of a double-width
6366 * char over the left halve of an existing one. */
6367 if (has_mbyte && col + char_cells == endcol
6368 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006369 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006371 && (*mb_off2cells)(off_to, max_off_to) == 1
6372 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373 clear_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374
6375 ScreenLines[off_to] = ScreenLines[off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376 if (enc_utf8)
6377 {
6378 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6379 if (ScreenLinesUC[off_from] != 0)
6380 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006381 int i;
6382
6383 for (i = 0; i < Screen_mco; ++i)
6384 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385 }
6386 }
6387 if (char_cells == 2)
6388 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389
6390#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006391 /* The bold trick makes a single column of pixels appear in the
6392 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393 * character should be redrawn too. This happens for our own GUI
6394 * and for some xterms. */
6395 if (
6396# ifdef FEAT_GUI
6397 gui.in_use
6398# endif
6399# if defined(FEAT_GUI) && defined(UNIX)
6400 ||
6401# endif
6402# ifdef UNIX
6403 term_is_xterm
6404# endif
6405 )
6406 {
6407 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006408 if (hl > HL_ALL)
6409 hl = syn_attr2attr(hl);
6410 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411 redraw_next = TRUE;
6412 }
6413#endif
6414 ScreenAttrs[off_to] = ScreenAttrs[off_from];
Bram Moolenaara12a1612019-01-24 16:39:02 +01006415
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006416 /* For simplicity set the attributes of second half of a
6417 * double-wide character equal to the first half. */
6418 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006420
6421 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 screen_char(off_to, row, col + coloff);
6425 }
6426 else if ( p_wiv
6427#ifdef FEAT_GUI
6428 && !gui.in_use
6429#endif
6430 && col + coloff > 0)
6431 {
6432 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6433 {
6434 /*
6435 * Don't output stop-highlight when moving the cursor, it will
6436 * stop the highlighting when it should continue.
6437 */
6438 screen_attr = 0;
6439 }
6440 else if (screen_attr != 0)
6441 screen_stop_highlight();
6442 }
6443
6444 off_to += CHAR_CELLS;
6445 off_from += CHAR_CELLS;
6446 col += CHAR_CELLS;
6447 }
6448
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449 if (clear_next)
6450 {
6451 /* Clear the second half of a double-wide character of which the left
6452 * half was overwritten with a single-wide character. */
6453 ScreenLines[off_to] = ' ';
6454 if (enc_utf8)
6455 ScreenLinesUC[off_to] = 0;
6456 screen_char(off_to, row, col + coloff);
6457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458
6459 if (clear_width > 0
6460#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006461 && !(flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462#endif
6463 )
6464 {
6465#ifdef FEAT_GUI
6466 int startCol = col;
6467#endif
6468
6469 /* blank out the rest of the line */
6470 while (col < clear_width && ScreenLines[off_to] == ' '
6471 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006472 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473 {
6474 ++off_to;
6475 ++col;
6476 }
6477 if (col < clear_width)
6478 {
6479#ifdef FEAT_GUI
6480 /*
6481 * In the GUI, clearing the rest of the line may leave pixels
6482 * behind if the first character cleared was bold. Some bold
6483 * fonts spill over the left. In this case we redraw the previous
6484 * character too. If we didn't skip any blanks above, then we
6485 * only redraw if the character wasn't already redrawn anyway.
6486 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006487 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488 {
6489 hl = ScreenAttrs[off_to];
6490 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006491 {
6492 int prev_cells = 1;
Bram Moolenaara12a1612019-01-24 16:39:02 +01006493
Bram Moolenaar9c697322006-10-09 20:11:17 +00006494 if (enc_utf8)
6495 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6496 * that its width is 2. */
6497 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6498 else if (enc_dbcs != 0)
6499 {
6500 /* find previous character by counting from first
6501 * column and get its width. */
6502 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006503 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006504
6505 while (off < off_to)
6506 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006507 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006508 off += prev_cells;
6509 }
6510 }
6511
6512 if (enc_dbcs != 0 && prev_cells > 1)
6513 screen_char_2(off_to - prev_cells, row,
6514 col + coloff - prev_cells);
6515 else
Bram Moolenaar9c697322006-10-09 20:11:17 +00006516 screen_char(off_to - prev_cells, row,
6517 col + coloff - prev_cells);
6518 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519 }
6520#endif
6521 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6522 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523 off_to += clear_width - col;
6524 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525 }
6526 }
6527
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006528 if (clear_width > 0
6529#ifdef FEAT_TEXT_PROP
6530 && !(flags & SLF_POPUP) // no separator for popup window
6531#endif
6532 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006534 // For a window that has a right neighbor, draw the separator char
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006535 // right of the window contents. But not on top of a popup window.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006536 if (coloff + col < Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006538#ifdef FEAT_TEXT_PROP
Bram Moolenaarc662ec92019-06-23 00:15:57 +02006539 if (!blocked_by_popup(row, col + coloff))
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006540#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006542 int c;
6543
6544 c = fillchar_vsep(&hl);
6545 if (ScreenLines[off_to] != (schar_T)c
6546 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6547 != (c >= 0x80 ? c : 0))
6548 || ScreenAttrs[off_to] != hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006550 ScreenLines[off_to] = c;
6551 ScreenAttrs[off_to] = hl;
6552 if (enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006554 if (c >= 0x80)
6555 {
6556 ScreenLinesUC[off_to] = c;
6557 ScreenLinesC[0][off_to] = 0;
6558 }
6559 else
6560 ScreenLinesUC[off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561 }
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006562 screen_char(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564 }
6565 }
6566 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567 LineWraps[row] = FALSE;
6568 }
6569}
6570
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006571#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006573 * Mirror text "str" for right-left displaying.
6574 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006576 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006577rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578{
6579 char_u *p1, *p2;
6580 int t;
6581
6582 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6583 {
6584 t = *p1;
6585 *p1 = *p2;
6586 *p2 = t;
6587 }
6588}
6589#endif
6590
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591/*
6592 * mark all status lines for redraw; used after first :cd
6593 */
6594 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006595status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596{
6597 win_T *wp;
6598
Bram Moolenaar29323592016-07-24 22:04:11 +02006599 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600 if (wp->w_status_height)
6601 {
6602 wp->w_redr_status = TRUE;
6603 redraw_later(VALID);
6604 }
6605}
6606
6607/*
6608 * mark all status lines of the current buffer for redraw
6609 */
6610 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006611status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612{
6613 win_T *wp;
6614
Bram Moolenaar29323592016-07-24 22:04:11 +02006615 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6617 {
6618 wp->w_redr_status = TRUE;
6619 redraw_later(VALID);
6620 }
6621}
6622
6623/*
6624 * Redraw all status lines that need to be redrawn.
6625 */
6626 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006627redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628{
6629 win_T *wp;
6630
Bram Moolenaar29323592016-07-24 22:04:11 +02006631 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006633 win_redr_status(wp, FALSE);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006634 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006635 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637
Bram Moolenaar4033c552017-09-16 20:54:51 +02006638#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639/*
6640 * Redraw all status lines at the bottom of frame "frp".
6641 */
6642 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006643win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644{
6645 if (frp->fr_layout == FR_LEAF)
6646 frp->fr_win->w_redr_status = TRUE;
6647 else if (frp->fr_layout == FR_ROW)
6648 {
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006649 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650 win_redraw_last_status(frp);
6651 }
6652 else /* frp->fr_layout == FR_COL */
6653 {
6654 frp = frp->fr_child;
6655 while (frp->fr_next != NULL)
6656 frp = frp->fr_next;
6657 win_redraw_last_status(frp);
6658 }
6659}
6660#endif
6661
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662/*
6663 * Draw the verticap separator right of window "wp" starting with line "row".
6664 */
6665 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006666draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667{
6668 int hl;
6669 int c;
6670
6671 if (wp->w_vsep_width)
6672 {
6673 /* draw the vertical separator right of this window */
6674 c = fillchar_vsep(&hl);
6675 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6676 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6677 c, ' ', hl);
6678 }
6679}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680
6681#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006682static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683
6684/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006685 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686 */
6687 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006688status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689{
6690 int len = 0;
6691
6692#ifdef FEAT_MENU
6693 int emenu = (xp->xp_context == EXPAND_MENUS
6694 || xp->xp_context == EXPAND_MENUNAMES);
6695
6696 /* Check for menu separators - replace with '|'. */
6697 if (emenu && menu_is_separator(s))
6698 return 1;
6699#endif
6700
6701 while (*s != NUL)
6702 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006703 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006704 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006705 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706 }
6707
6708 return len;
6709}
6710
6711/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006712 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006713 * These are backslashes used for escaping. Do show backslashes in help tags.
6714 */
6715 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006716skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006717{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006718 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006719#ifdef FEAT_MENU
6720 || ((xp->xp_context == EXPAND_MENUS
6721 || xp->xp_context == EXPAND_MENUNAMES)
6722 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6723#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006724 )
6725 {
6726#ifndef BACKSLASH_IN_FILENAME
6727 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6728 return 2;
6729#endif
6730 return 1;
6731 }
6732 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006733}
6734
6735/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736 * Show wildchar matches in the status line.
6737 * Show at least the "match" item.
6738 * We start at item 'first_match' in the list and show all matches that fit.
6739 *
6740 * If inversion is possible we use it. Else '=' characters are used.
6741 */
6742 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006743win_redr_status_matches(
6744 expand_T *xp,
6745 int num_matches,
6746 char_u **matches, /* list of matches */
6747 int match,
6748 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749{
6750#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6751 int row;
6752 char_u *buf;
6753 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006754 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755 int fillchar;
6756 int attr;
6757 int i;
6758 int highlight = TRUE;
6759 char_u *selstart = NULL;
6760 int selstart_col = 0;
6761 char_u *selend = NULL;
6762 static int first_match = 0;
6763 int add_left = FALSE;
6764 char_u *s;
6765#ifdef FEAT_MENU
6766 int emenu;
6767#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769
6770 if (matches == NULL) /* interrupted completion? */
6771 return;
6772
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006773 if (has_mbyte)
Bram Moolenaar964b3742019-05-24 18:54:09 +02006774 buf = alloc(Columns * MB_MAXBYTES + 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006775 else
Bram Moolenaar964b3742019-05-24 18:54:09 +02006776 buf = alloc(Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777 if (buf == NULL)
6778 return;
6779
6780 if (match == -1) /* don't show match but original text */
6781 {
6782 match = 0;
6783 highlight = FALSE;
6784 }
6785 /* count 1 for the ending ">" */
6786 clen = status_match_len(xp, L_MATCH(match)) + 3;
6787 if (match == 0)
6788 first_match = 0;
6789 else if (match < first_match)
6790 {
6791 /* jumping left, as far as we can go */
6792 first_match = match;
6793 add_left = TRUE;
6794 }
6795 else
6796 {
6797 /* check if match fits on the screen */
6798 for (i = first_match; i < match; ++i)
6799 clen += status_match_len(xp, L_MATCH(i)) + 2;
6800 if (first_match > 0)
6801 clen += 2;
6802 /* jumping right, put match at the left */
6803 if ((long)clen > Columns)
6804 {
6805 first_match = match;
6806 /* if showing the last match, we can add some on the left */
6807 clen = 2;
6808 for (i = match; i < num_matches; ++i)
6809 {
6810 clen += status_match_len(xp, L_MATCH(i)) + 2;
6811 if ((long)clen >= Columns)
6812 break;
6813 }
6814 if (i == num_matches)
6815 add_left = TRUE;
6816 }
6817 }
6818 if (add_left)
6819 while (first_match > 0)
6820 {
6821 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6822 if ((long)clen >= Columns)
6823 break;
6824 --first_match;
6825 }
6826
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006827 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828
6829 if (first_match == 0)
6830 {
6831 *buf = NUL;
6832 len = 0;
6833 }
6834 else
6835 {
6836 STRCPY(buf, "< ");
6837 len = 2;
6838 }
6839 clen = len;
6840
6841 i = first_match;
6842 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6843 {
6844 if (i == match)
6845 {
6846 selstart = buf + len;
6847 selstart_col = clen;
6848 }
6849
6850 s = L_MATCH(i);
6851 /* Check for menu separators - replace with '|' */
6852#ifdef FEAT_MENU
6853 emenu = (xp->xp_context == EXPAND_MENUS
6854 || xp->xp_context == EXPAND_MENUNAMES);
6855 if (emenu && menu_is_separator(s))
6856 {
6857 STRCPY(buf + len, transchar('|'));
6858 l = (int)STRLEN(buf + len);
6859 len += l;
6860 clen += l;
6861 }
6862 else
6863#endif
6864 for ( ; *s != NUL; ++s)
6865 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006866 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867 clen += ptr2cells(s);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006868 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 {
6870 STRNCPY(buf + len, s, l);
6871 s += l - 1;
6872 len += l;
6873 }
6874 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875 {
6876 STRCPY(buf + len, transchar_byte(*s));
6877 len += (int)STRLEN(buf + len);
6878 }
6879 }
6880 if (i == match)
6881 selend = buf + len;
6882
6883 *(buf + len++) = ' ';
6884 *(buf + len++) = ' ';
6885 clen += 2;
6886 if (++i == num_matches)
6887 break;
6888 }
6889
6890 if (i != num_matches)
6891 {
6892 *(buf + len++) = '>';
6893 ++clen;
6894 }
6895
6896 buf[len] = NUL;
6897
6898 row = cmdline_row - 1;
6899 if (row >= 0)
6900 {
6901 if (wild_menu_showing == 0)
6902 {
6903 if (msg_scrolled > 0)
6904 {
6905 /* Put the wildmenu just above the command line. If there is
6906 * no room, scroll the screen one line up. */
6907 if (cmdline_row == Rows - 1)
6908 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02006909 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910 ++msg_scrolled;
6911 }
6912 else
6913 {
6914 ++cmdline_row;
6915 ++row;
6916 }
6917 wild_menu_showing = WM_SCROLLED;
6918 }
6919 else
6920 {
6921 /* Create status line if needed by setting 'laststatus' to 2.
6922 * Set 'winminheight' to zero to avoid that the window is
6923 * resized. */
6924 if (lastwin->w_status_height == 0)
6925 {
6926 save_p_ls = p_ls;
6927 save_p_wmh = p_wmh;
6928 p_ls = 2;
6929 p_wmh = 0;
6930 last_status(FALSE);
6931 }
6932 wild_menu_showing = WM_SHOWN;
6933 }
6934 }
6935
6936 screen_puts(buf, row, 0, attr);
6937 if (selstart != NULL && highlight)
6938 {
6939 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006940 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941 }
6942
6943 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6944 }
6945
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947 vim_free(buf);
6948}
6949#endif
6950
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951/*
6952 * Redraw the status line of window wp.
6953 *
6954 * If inversion is possible we use it. Else '=' characters are used.
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006955 * If "ignore_pum" is TRUE, also redraw statusline when the popup menu is
6956 * displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957 */
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006958 static void
Bram Moolenaar829adb72018-06-24 19:24:03 +02006959win_redr_status(win_T *wp, int ignore_pum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960{
6961 int row;
6962 char_u *p;
6963 int len;
6964 int fillchar;
6965 int attr;
6966 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006967 static int busy = FALSE;
6968
6969 /* It's possible to get here recursively when 'statusline' (indirectly)
6970 * invokes ":redrawstatus". Simply ignore the call then. */
6971 if (busy)
6972 return;
6973 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974
6975 wp->w_redr_status = FALSE;
6976 if (wp->w_status_height == 0)
6977 {
6978 /* no status line, can only be last window */
6979 redraw_cmdline = TRUE;
6980 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006981 else if (!redrawing()
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006982 // don't update status line when popup menu is visible and may be
6983 // drawn over it, unless it will be redrawn later
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006984 || (!ignore_pum && pum_visible()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985 {
6986 /* Don't redraw right now, do it later. */
6987 wp->w_redr_status = TRUE;
6988 }
6989#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006990 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 {
6992 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006993 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 }
6995#endif
6996 else
6997 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006998 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007000 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 p = NameBuff;
7002 len = (int)STRLEN(p);
7003
Bram Moolenaard85f2712017-07-28 21:51:57 +02007004 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005#ifdef FEAT_QUICKFIX
7006 || wp->w_p_pvw
7007#endif
7008 || bufIsChanged(wp->w_buffer)
7009 || wp->w_buffer->b_p_ro)
7010 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02007011 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00007013 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 len += (int)STRLEN(p + len);
7015 }
7016#ifdef FEAT_QUICKFIX
7017 if (wp->w_p_pvw)
7018 {
7019 STRCPY(p + len, _("[Preview]"));
7020 len += (int)STRLEN(p + len);
7021 }
7022#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02007023 if (bufIsChanged(wp->w_buffer)
7024#ifdef FEAT_TERMINAL
7025 && !bt_terminal(wp->w_buffer)
7026#endif
7027 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028 {
7029 STRCPY(p + len, "[+]");
7030 len += 3;
7031 }
7032 if (wp->w_buffer->b_p_ro)
7033 {
Bram Moolenaar23584032013-06-07 20:17:11 +02007034 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01007035 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036 }
7037
Bram Moolenaar02631462017-09-22 15:20:32 +02007038 this_ru_col = ru_col - (Columns - wp->w_width);
7039 if (this_ru_col < (wp->w_width + 1) / 2)
7040 this_ru_col = (wp->w_width + 1) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041 if (this_ru_col <= 1)
7042 {
7043 p = (char_u *)"<"; /* No room for file name! */
7044 len = 1;
7045 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01007046 else if (has_mbyte)
7047 {
7048 int clen = 0, i;
7049
7050 /* Count total number of display cells. */
7051 clen = mb_string2cells(p, -1);
7052
7053 /* Find first character that will fit.
7054 * Going from start to end is much faster for DBCS. */
7055 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
7056 i += (*mb_ptr2len)(p + i))
7057 clen -= (*mb_ptr2cells)(p + i);
7058 len = clen;
7059 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060 {
Bram Moolenaara12a1612019-01-24 16:39:02 +01007061 p = p + i - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 *p = '<';
Bram Moolenaara12a1612019-01-24 16:39:02 +01007063 ++len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 }
7065
Bram Moolenaara12a1612019-01-24 16:39:02 +01007066 }
7067 else if (len > this_ru_col - 1)
7068 {
7069 p += len - (this_ru_col - 1);
7070 *p = '<';
7071 len = this_ru_col - 1;
7072 }
7073
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar53f81742017-09-22 14:35:51 +02007075 screen_puts(p, row, wp->w_wincol, attr);
7076 screen_fill(row, row + 1, len + wp->w_wincol,
7077 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007079 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
7081 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007082 - 1 + wp->w_wincol), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083
7084#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +02007085 win_redr_ruler(wp, TRUE, ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086#endif
7087 }
7088
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089 /*
7090 * May need to draw the character below the vertical separator.
7091 */
7092 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
7093 {
7094 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007095 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 else
7097 fillchar = fillchar_vsep(&attr);
7098 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
7099 attr);
7100 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007101 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102}
7103
Bram Moolenaar238a5642006-02-21 22:12:05 +00007104#ifdef FEAT_STL_OPT
7105/*
7106 * Redraw the status line according to 'statusline' and take care of any
7107 * errors encountered.
7108 */
7109 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007110redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00007111{
Bram Moolenaar362f3562009-11-03 16:20:34 +00007112 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02007113 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007114
7115 /* When called recursively return. This can happen when the statusline
7116 * contains an expression that triggers a redraw. */
7117 if (entered)
7118 return;
7119 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007120
Bram Moolenaara742e082016-04-05 21:10:38 +02007121 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007122 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02007123 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007124 {
7125 /* When there is an error disable the statusline, otherwise the
7126 * display is messed up with errors and a redraw triggers the problem
7127 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007128 set_string_option_direct((char_u *)"statusline", -1,
7129 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007130 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007131 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007132 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007133 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007134}
7135#endif
7136
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137/*
7138 * Return TRUE if the status line of window "wp" is connected to the status
7139 * line of the window right of it. If not, then it's a vertical separator.
7140 * Only call if (wp->w_vsep_width != 0).
7141 */
7142 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007143stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144{
7145 frame_T *fr;
7146
7147 fr = wp->w_frame;
7148 while (fr->fr_parent != NULL)
7149 {
7150 if (fr->fr_parent->fr_layout == FR_COL)
7151 {
7152 if (fr->fr_next != NULL)
7153 break;
7154 }
7155 else
7156 {
7157 if (fr->fr_next != NULL)
7158 return TRUE;
7159 }
7160 fr = fr->fr_parent;
7161 }
7162 return FALSE;
7163}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166/*
7167 * Get the value to show for the language mappings, active 'keymap'.
7168 */
7169 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007170get_keymap_str(
7171 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007172 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007173 char_u *buf, /* buffer for the result */
7174 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175{
7176 char_u *p;
7177
7178 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7179 return FALSE;
7180
7181 {
7182#ifdef FEAT_EVAL
7183 buf_T *old_curbuf = curbuf;
7184 win_T *old_curwin = curwin;
7185 char_u *s;
7186
7187 curbuf = wp->w_buffer;
7188 curwin = wp;
7189 STRCPY(buf, "b:keymap_name"); /* must be writable */
7190 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007191 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 --emsg_skip;
7193 curbuf = old_curbuf;
7194 curwin = old_curwin;
7195 if (p == NULL || *p == NUL)
7196#endif
7197 {
7198#ifdef FEAT_KEYMAP
7199 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7200 p = wp->w_buffer->b_p_keymap;
7201 else
7202#endif
7203 p = (char_u *)"lang";
7204 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007205 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206 buf[0] = NUL;
7207#ifdef FEAT_EVAL
7208 vim_free(s);
7209#endif
7210 }
7211 return buf[0] != NUL;
7212}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213
7214#if defined(FEAT_STL_OPT) || defined(PROTO)
7215/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007216 * Redraw the status line or ruler of window "wp".
7217 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218 */
7219 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007220win_redr_custom(
7221 win_T *wp,
7222 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007224 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225 int attr;
7226 int curattr;
7227 int row;
7228 int col = 0;
7229 int maxwidth;
7230 int width;
7231 int n;
7232 int len;
7233 int fillchar;
7234 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007235 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007237 struct stl_hlrec hltab[STL_MAX_ITEM];
7238 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007239 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007240 win_T *ewp;
7241 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242
Bram Moolenaar1d633412013-12-11 15:52:01 +01007243 /* There is a tiny chance that this gets called recursively: When
7244 * redrawing a status line triggers redrawing the ruler or tabline.
7245 * Avoid trouble by not allowing recursion. */
7246 if (entered)
7247 return;
7248 entered = TRUE;
7249
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007251 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007253 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007254 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007255 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007256 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007257 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007258 maxwidth = Columns;
7259# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007260 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007261# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007263 else
7264 {
7265 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007266 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02007267 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007268
7269 if (draw_ruler)
7270 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007271 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007272 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007273 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007274 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007275 if (*++stl == '-')
7276 stl++;
7277 if (atoi((char *)stl))
7278 while (VIM_ISDIGIT(*stl))
7279 stl++;
7280 if (*stl++ != '(')
7281 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007282 }
Bram Moolenaar02631462017-09-22 15:20:32 +02007283 col = ru_col - (Columns - wp->w_width);
7284 if (col < (wp->w_width + 1) / 2)
7285 col = (wp->w_width + 1) / 2;
7286 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007287 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007288 {
7289 row = Rows - 1;
7290 --maxwidth; /* writing in last column may cause scrolling */
7291 fillchar = ' ';
7292 attr = 0;
7293 }
7294
7295# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007296 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007297# endif
7298 }
7299 else
7300 {
7301 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007302 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007303 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007304 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007305# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007306 use_sandbox = was_set_insecurely((char_u *)"statusline",
7307 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007308# endif
7309 }
7310
Bram Moolenaar53f81742017-09-22 14:35:51 +02007311 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007312 }
7313
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007315 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316
Bram Moolenaar61452852011-02-01 18:01:11 +01007317 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7318 * the cursor away and back. */
7319 ewp = wp == NULL ? curwin : wp;
7320 p_crb_save = ewp->w_p_crb;
7321 ewp->w_p_crb = FALSE;
7322
Bram Moolenaar362f3562009-11-03 16:20:34 +00007323 /* Make a copy, because the statusline may include a function call that
7324 * might change the option value and free the memory. */
7325 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007326 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007327 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007328 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007329 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007330 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007332 /* Make all characters printable. */
7333 p = transstr(buf);
7334 if (p != NULL)
7335 {
7336 vim_strncpy(buf, p, sizeof(buf) - 1);
7337 vim_free(p);
7338 }
7339
7340 /* fill up with "fillchar" */
7341 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007342 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344 len += (*mb_char2bytes)(fillchar, buf + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345 ++width;
7346 }
7347 buf[len] = NUL;
7348
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007349 /*
7350 * Draw each snippet with the specified highlighting.
7351 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352 curattr = attr;
7353 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007354 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007356 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357 screen_puts_len(p, len, row, col, curattr);
7358 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007359 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007361 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007363 else if (hltab[n].userhl < 0)
7364 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007365#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007366 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7367 && wp->w_status_height != 0)
7368 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007369 else if (wp != NULL && bt_terminal(wp->w_buffer)
7370 && wp->w_status_height != 0)
7371 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02007372#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007373 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007374 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007376 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377 }
7378 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007379
7380 if (wp == NULL)
7381 {
7382 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7383 col = 0;
7384 len = 0;
7385 p = buf;
7386 fillchar = 0;
7387 for (n = 0; tabtab[n].start != NULL; n++)
7388 {
7389 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7390 while (col < len)
7391 TabPageIdxs[col++] = fillchar;
7392 p = tabtab[n].start;
7393 fillchar = tabtab[n].userhl;
7394 }
7395 while (col < Columns)
7396 TabPageIdxs[col++] = fillchar;
7397 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007398
7399theend:
7400 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401}
7402
7403#endif /* FEAT_STL_OPT */
7404
7405/*
7406 * Output a single character directly to the screen and update ScreenLines.
7407 */
7408 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007409screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411 char_u buf[MB_MAXBYTES + 1];
7412
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007413 if (has_mbyte)
7414 buf[(*mb_char2bytes)(c, buf)] = NUL;
7415 else
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007416 {
7417 buf[0] = c;
7418 buf[1] = NUL;
7419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 screen_puts(buf, row, col, attr);
7421}
7422
7423/*
7424 * Get a single character directly from ScreenLines into "bytes[]".
7425 * Also return its attribute in *attrp;
7426 */
7427 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007428screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429{
7430 unsigned off;
7431
7432 /* safety check */
7433 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7434 {
7435 off = LineOffset[row] + col;
7436 *attrp = ScreenAttrs[off];
7437 bytes[0] = ScreenLines[off];
7438 bytes[1] = NUL;
7439
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440 if (enc_utf8 && ScreenLinesUC[off] != 0)
7441 bytes[utfc_char2bytes(off, bytes)] = NUL;
7442 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7443 {
7444 bytes[0] = ScreenLines[off];
7445 bytes[1] = ScreenLines2[off];
7446 bytes[2] = NUL;
7447 }
7448 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7449 {
7450 bytes[1] = ScreenLines[off + 1];
7451 bytes[2] = NUL;
7452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453 }
7454}
7455
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007456/*
7457 * Return TRUE if composing characters for screen posn "off" differs from
7458 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007459 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007460 */
7461 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007462screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007463{
7464 int i;
7465
7466 for (i = 0; i < Screen_mco; ++i)
7467 {
7468 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7469 return TRUE;
7470 if (u8cc[i] == 0)
7471 break;
7472 }
7473 return FALSE;
7474}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007475
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476/*
7477 * Put string '*text' on the screen at position 'row' and 'col', with
7478 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7479 * Note: only outputs within one row, message is truncated at screen boundary!
7480 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7481 */
7482 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007483screen_puts(
7484 char_u *text,
7485 int row,
7486 int col,
7487 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488{
7489 screen_puts_len(text, -1, row, col, attr);
7490}
7491
7492/*
7493 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7494 * a NUL.
7495 */
7496 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007497screen_puts_len(
7498 char_u *text,
7499 int textlen,
7500 int row,
7501 int col,
7502 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503{
7504 unsigned off;
7505 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007506 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507 int c;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007508 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509 int mbyte_blen = 1;
7510 int mbyte_cells = 1;
7511 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007512 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513 int clear_next_cell = FALSE;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007514#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007516 int pc, nc, nc1;
7517 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007519 int force_redraw_this;
7520 int force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007521 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522
Bram Moolenaar0b4c9ed2019-06-03 22:04:23 +02007523 // Safety check. The check for negative row and column is to fix issue
7524 // #4102. TODO: find out why row/col could be negative.
7525 if (ScreenLines == NULL
7526 || row >= screen_Rows || row < 0
7527 || col >= screen_Columns || col < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007529 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530
Bram Moolenaarc236c162008-07-13 17:41:49 +00007531 /* When drawing over the right halve of a double-wide char clear out the
7532 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007533 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaara12a1612019-01-24 16:39:02 +01007534#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00007535 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01007536#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007537 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007538 {
7539 ScreenLines[off - 1] = ' ';
7540 ScreenAttrs[off - 1] = 0;
7541 if (enc_utf8)
7542 {
7543 ScreenLinesUC[off - 1] = 0;
7544 ScreenLinesC[0][off - 1] = 0;
7545 }
7546 /* redraw the previous cell, make it empty */
7547 screen_char(off - 1, row, col - 1);
7548 /* force the cell at "col" to be redrawn */
7549 force_redraw_next = TRUE;
7550 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007551
Bram Moolenaar367329b2007-08-30 11:53:22 +00007552 max_off = LineOffset[row] + screen_Columns;
Bram Moolenaara064ac82007-08-05 18:10:54 +00007553 while (col < screen_Columns
7554 && (len < 0 || (int)(ptr - text) < len)
7555 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556 {
7557 c = *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558 /* check if this is the first byte of a multibyte */
7559 if (has_mbyte)
7560 {
7561 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007562 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007564 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7566 mbyte_cells = 1;
7567 else if (enc_dbcs != 0)
7568 mbyte_cells = mbyte_blen;
7569 else /* enc_utf8 */
7570 {
7571 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007572 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573 (int)((text + len) - ptr));
7574 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007575 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007577#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7579 {
7580 /* Do Arabic shaping. */
7581 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7582 {
7583 /* Past end of string to be displayed. */
7584 nc = NUL;
7585 nc1 = NUL;
7586 }
7587 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007588 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007589 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7590 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007591 nc1 = pcc[0];
7592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593 pc = prev_c;
7594 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007595 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596 }
7597 else
7598 prev_c = u8c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007599#endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007600 if (col + mbyte_cells > screen_Columns)
7601 {
7602 /* Only 1 cell left, but character requires 2 cells:
7603 * display a '>' in the last column to avoid wrapping. */
7604 c = '>';
7605 mbyte_cells = 1;
7606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607 }
7608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007610 force_redraw_this = force_redraw_next;
7611 force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007612
7613 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007614 || (mbyte_cells == 2
7615 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7616 || (enc_dbcs == DBCS_JPNU
7617 && c == 0x8e
7618 && ScreenLines2[off] != ptr[1])
7619 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007620 && (ScreenLinesUC[off] !=
7621 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7622 || (ScreenLinesUC[off] != 0
7623 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007625 || exmode_active;
7626
Bram Moolenaar24a5ac52019-06-08 19:01:18 +02007627 if ((need_redraw || force_redraw_this)
7628#ifdef FEAT_TEXT_PROP
Bram Moolenaarc662ec92019-06-23 00:15:57 +02007629 && !blocked_by_popup(row, col)
Bram Moolenaar24a5ac52019-06-08 19:01:18 +02007630#endif
7631 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632 {
7633#if defined(FEAT_GUI) || defined(UNIX)
7634 /* The bold trick makes a single row of pixels appear in the next
7635 * character. When a bold character is removed, the next
7636 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007637 * and for some xterms. */
7638 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639# ifdef FEAT_GUI
7640 gui.in_use
7641# endif
7642# if defined(FEAT_GUI) && defined(UNIX)
7643 ||
7644# endif
7645# ifdef UNIX
7646 term_is_xterm
7647# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007648 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007650 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007652 if (n > HL_ALL)
7653 n = syn_attr2attr(n);
7654 if (n & HL_BOLD)
7655 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656 }
7657#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658 /* When at the end of the text and overwriting a two-cell
7659 * character with a one-cell character, need to clear the next
7660 * cell. Also when overwriting the left halve of a two-cell char
7661 * with the right halve of a two-cell char. Do this only once
7662 * (mb_off2cells() may return 2 on the right halve). */
7663 if (clear_next_cell)
7664 clear_next_cell = FALSE;
7665 else if (has_mbyte
7666 && (len < 0 ? ptr[mbyte_blen] == NUL
7667 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007668 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007670 && (*mb_off2cells)(off, max_off) == 1
7671 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672 clear_next_cell = TRUE;
7673
7674 /* Make sure we never leave a second byte of a double-byte behind,
7675 * it confuses mb_off2cells(). */
7676 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007677 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007679 && (*mb_off2cells)(off, max_off) == 1
7680 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681 ScreenLines[off + mbyte_blen] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682 ScreenLines[off] = c;
7683 ScreenAttrs[off] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 if (enc_utf8)
7685 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007686 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687 ScreenLinesUC[off] = 0;
7688 else
7689 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007690 int i;
7691
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007693 for (i = 0; i < Screen_mco; ++i)
7694 {
7695 ScreenLinesC[i][off] = u8cc[i];
7696 if (u8cc[i] == 0)
7697 break;
7698 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699 }
7700 if (mbyte_cells == 2)
7701 {
7702 ScreenLines[off + 1] = 0;
7703 ScreenAttrs[off + 1] = attr;
7704 }
7705 screen_char(off, row, col);
7706 }
7707 else if (mbyte_cells == 2)
7708 {
7709 ScreenLines[off + 1] = ptr[1];
7710 ScreenAttrs[off + 1] = attr;
7711 screen_char_2(off, row, col);
7712 }
7713 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7714 {
7715 ScreenLines2[off] = ptr[1];
7716 screen_char(off, row, col);
7717 }
7718 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719 screen_char(off, row, col);
7720 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721 if (has_mbyte)
7722 {
7723 off += mbyte_cells;
7724 col += mbyte_cells;
7725 ptr += mbyte_blen;
7726 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007727 {
7728 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007730 len = -1;
7731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732 }
7733 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734 {
7735 ++off;
7736 ++col;
7737 ++ptr;
7738 }
7739 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007740
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007741 /* If we detected the next character needs to be redrawn, but the text
7742 * doesn't extend up to there, update the character here. */
7743 if (force_redraw_next && col < screen_Columns)
7744 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007745 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7746 screen_char_2(off, row, col);
7747 else
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007748 screen_char(off, row, col);
7749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750}
7751
7752#ifdef FEAT_SEARCH_EXTRA
7753/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007754 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755 */
7756 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007757start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758{
7759 if (p_hls && !no_hlsearch)
7760 {
7761 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007762 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007763# ifdef FEAT_RELTIME
7764 /* Set the time limit to 'redrawtime'. */
7765 profile_setlimit(p_rdt, &search_hl.tm);
7766# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767 }
7768}
7769
7770/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007771 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772 */
7773 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007774end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775{
7776 if (search_hl.rm.regprog != NULL)
7777 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007778 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779 search_hl.rm.regprog = NULL;
7780 }
7781}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02007782#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02007783
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007785screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786{
7787 attrentry_T *aep = NULL;
7788
7789 screen_attr = attr;
7790 if (full_screen
Bram Moolenaar4f974752019-02-17 17:44:42 +01007791#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792 && termcap_active
7793#endif
7794 )
7795 {
7796#ifdef FEAT_GUI
7797 if (gui.in_use)
7798 {
7799 char buf[20];
7800
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007801 /* The GUI handles this internally. */
7802 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803 OUT_STR(buf);
7804 }
7805 else
7806#endif
7807 {
7808 if (attr > HL_ALL) /* special HL attr. */
7809 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007810 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811 aep = syn_cterm_attr2entry(attr);
7812 else
7813 aep = syn_term_attr2entry(attr);
7814 if (aep == NULL) /* did ":syntax clear" */
7815 attr = 0;
7816 else
7817 attr = aep->ae_attr;
7818 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01007819 if ((attr & HL_BOLD) && *T_MD != NUL) /* bold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007820 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007821 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007822#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007823 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
7824 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
7825 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007826#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007827 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007828 /* If the Normal FG color has BOLD attribute and the new HL
7829 * has a FG color defined, clear BOLD. */
7830 out_str(T_ME);
Bram Moolenaar45a00002017-12-22 21:12:34 +01007831 if ((attr & HL_STANDOUT) && *T_SO != NUL) /* standout */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832 out_str(T_SO);
Bram Moolenaar45a00002017-12-22 21:12:34 +01007833 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) /* undercurl */
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01007834 out_str(T_UCS);
7835 if (((attr & HL_UNDERLINE) /* underline or undercurl */
Bram Moolenaar45a00002017-12-22 21:12:34 +01007836 || ((attr & HL_UNDERCURL) && *T_UCS == NUL))
7837 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838 out_str(T_US);
Bram Moolenaar45a00002017-12-22 21:12:34 +01007839 if ((attr & HL_ITALIC) && *T_CZH != NUL) /* italic */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840 out_str(T_CZH);
Bram Moolenaar45a00002017-12-22 21:12:34 +01007841 if ((attr & HL_INVERSE) && *T_MR != NUL) /* inverse (reverse) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842 out_str(T_MR);
Bram Moolenaar45a00002017-12-22 21:12:34 +01007843 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) /* strike */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02007844 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845
7846 /*
7847 * Output the color or start string after bold etc., in case the
7848 * bold etc. override the color setting.
7849 */
7850 if (aep != NULL)
7851 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007852#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007853 /* When 'termguicolors' is set but fg or bg is unset,
7854 * fall back to the cterm colors. This helps for SpellBad,
7855 * where the GUI uses a red undercurl. */
7856 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007858 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007859 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007860 }
7861 else
7862#endif
7863 if (t_colors > 1)
7864 {
7865 if (aep->ae_u.cterm.fg_color)
7866 term_fg_color(aep->ae_u.cterm.fg_color - 1);
7867 }
7868#ifdef FEAT_TERMGUICOLORS
7869 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
7870 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007871 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007872 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873 }
7874 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007875#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007876 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007878 if (aep->ae_u.cterm.bg_color)
7879 term_bg_color(aep->ae_u.cterm.bg_color - 1);
7880 }
7881
Bram Moolenaarf708ac52018-03-12 21:48:32 +01007882 if (!IS_CTERM)
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007883 {
7884 if (aep->ae_u.term.start != NULL)
7885 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 }
7887 }
7888 }
7889 }
7890}
7891
7892 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007893screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894{
7895 int do_ME = FALSE; /* output T_ME code */
7896
7897 if (screen_attr != 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01007898#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899 && termcap_active
7900#endif
7901 )
7902 {
7903#ifdef FEAT_GUI
7904 if (gui.in_use)
7905 {
7906 char buf[20];
7907
7908 /* use internal GUI code */
7909 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
7910 OUT_STR(buf);
7911 }
7912 else
7913#endif
7914 {
7915 if (screen_attr > HL_ALL) /* special HL attr. */
7916 {
7917 attrentry_T *aep;
7918
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007919 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920 {
7921 /*
7922 * Assume that t_me restores the original colors!
7923 */
7924 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007925 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007926#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007927 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
7928 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
7929 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007930#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007931 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007932#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007933 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
7934 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
7935 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007936#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007937 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938 do_ME = TRUE;
7939 }
7940 else
7941 {
7942 aep = syn_term_attr2entry(screen_attr);
7943 if (aep != NULL && aep->ae_u.term.stop != NULL)
7944 {
7945 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
7946 do_ME = TRUE;
7947 else
7948 out_str(aep->ae_u.term.stop);
7949 }
7950 }
7951 if (aep == NULL) /* did ":syntax clear" */
7952 screen_attr = 0;
7953 else
7954 screen_attr = aep->ae_attr;
7955 }
7956
7957 /*
7958 * Often all ending-codes are equal to T_ME. Avoid outputting the
7959 * same sequence several times.
7960 */
7961 if (screen_attr & HL_STANDOUT)
7962 {
7963 if (STRCMP(T_SE, T_ME) == 0)
7964 do_ME = TRUE;
7965 else
7966 out_str(T_SE);
7967 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01007968 if ((screen_attr & HL_UNDERCURL) && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01007969 {
7970 if (STRCMP(T_UCE, T_ME) == 0)
7971 do_ME = TRUE;
7972 else
7973 out_str(T_UCE);
7974 }
7975 if ((screen_attr & HL_UNDERLINE)
Bram Moolenaar45a00002017-12-22 21:12:34 +01007976 || ((screen_attr & HL_UNDERCURL) && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977 {
7978 if (STRCMP(T_UE, T_ME) == 0)
7979 do_ME = TRUE;
7980 else
7981 out_str(T_UE);
7982 }
7983 if (screen_attr & HL_ITALIC)
7984 {
7985 if (STRCMP(T_CZR, T_ME) == 0)
7986 do_ME = TRUE;
7987 else
7988 out_str(T_CZR);
7989 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02007990 if (screen_attr & HL_STRIKETHROUGH)
7991 {
7992 if (STRCMP(T_STE, T_ME) == 0)
7993 do_ME = TRUE;
7994 else
7995 out_str(T_STE);
7996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
7998 out_str(T_ME);
7999
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008000#ifdef FEAT_TERMGUICOLORS
8001 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008003 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008004 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008005 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008006 term_bg_rgb_color(cterm_normal_bg_gui_color);
8007 }
8008 else
8009#endif
8010 {
8011 if (t_colors > 1)
8012 {
8013 /* set Normal cterm colors */
8014 if (cterm_normal_fg_color != 0)
8015 term_fg_color(cterm_normal_fg_color - 1);
8016 if (cterm_normal_bg_color != 0)
8017 term_bg_color(cterm_normal_bg_color - 1);
8018 if (cterm_normal_fg_bold)
8019 out_str(T_MD);
8020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021 }
8022 }
8023 }
8024 screen_attr = 0;
8025}
8026
8027/*
8028 * Reset the colors for a cterm. Used when leaving Vim.
8029 * The machine specific code may override this again.
8030 */
8031 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008032reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008034 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035 {
8036 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008037#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008038 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8039 || cterm_normal_bg_gui_color != INVALCOLOR)
8040 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008041#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008043#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044 {
8045 out_str(T_OP);
8046 screen_attr = -1;
8047 }
8048 if (cterm_normal_fg_bold)
8049 {
8050 out_str(T_ME);
8051 screen_attr = -1;
8052 }
8053 }
8054}
8055
8056/*
8057 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8058 * using the attributes from ScreenAttrs["off"].
8059 */
8060 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008061screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062{
8063 int attr;
8064
8065 /* Check for illegal values, just in case (could happen just after
8066 * resizing). */
8067 if (row >= screen_Rows || col >= screen_Columns)
8068 return;
8069
Bram Moolenaar33796b32019-06-08 16:01:13 +02008070 // Skip if under the popup menu.
8071 // Popup windows with zindex higher than POPUPMENU_ZINDEX go on top.
8072 if (pum_under_menu(row, col)
Bram Moolenaare2c453d2019-08-21 14:37:09 +02008073#ifdef FEAT_TEXT_PROP
Bram Moolenaar33796b32019-06-08 16:01:13 +02008074 && screen_zindex <= POPUPMENU_ZINDEX
Bram Moolenaare2c453d2019-08-21 14:37:09 +02008075#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02008076 )
Bram Moolenaarae654382019-01-17 21:09:05 +01008077 return;
Bram Moolenaar33796b32019-06-08 16:01:13 +02008078#ifdef FEAT_TEXT_PROP
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008079 if (blocked_by_popup(row, col))
Bram Moolenaar33796b32019-06-08 16:01:13 +02008080 return;
8081#endif
8082
Bram Moolenaar494838a2015-02-10 19:20:37 +01008083 /* Outputting a character in the last cell on the screen may scroll the
8084 * screen up. Only do it when the "xn" termcap property is set, otherwise
8085 * mark the character invalid (update it when scrolled up). */
8086 if (*T_XN == NUL
8087 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088#ifdef FEAT_RIGHTLEFT
8089 /* account for first command-line character in rightleft mode */
8090 && !cmdmsg_rl
8091#endif
8092 )
8093 {
8094 ScreenAttrs[off] = (sattr_T)-1;
8095 return;
8096 }
8097
8098 /*
8099 * Stop highlighting first, so it's easier to move the cursor.
8100 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 if (screen_char_attr != 0)
8102 attr = screen_char_attr;
8103 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104 attr = ScreenAttrs[off];
8105 if (screen_attr != attr)
8106 screen_stop_highlight();
8107
8108 windgoto(row, col);
8109
8110 if (screen_attr != attr)
8111 screen_start_highlight(attr);
8112
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113 if (enc_utf8 && ScreenLinesUC[off] != 0)
8114 {
8115 char_u buf[MB_MAXBYTES + 1];
8116
Bram Moolenaarcb070082016-04-02 22:14:51 +02008117 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008118 {
8119 if (*p_ambw == 'd'
Bram Moolenaara12a1612019-01-24 16:39:02 +01008120#ifdef FEAT_GUI
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008121 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008122#endif
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008123 )
8124 {
8125 /* Clear the two screen cells. If the character is actually
8126 * single width it won't change the second cell. */
8127 out_str((char_u *)" ");
8128 term_windgoto(row, col);
8129 }
8130 /* not sure where the cursor is after drawing the ambiguous width
8131 * character */
Bram Moolenaarcb070082016-04-02 22:14:51 +02008132 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008133 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02008134 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008136
8137 /* Convert the UTF-8 character to bytes and write it. */
8138 buf[utfc_char2bytes(off, buf)] = NUL;
8139 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 }
8141 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145 /* double-byte character in single-width cell */
8146 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8147 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148 }
8149
8150 screen_cur_col++;
8151}
8152
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153/*
8154 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8155 * on the screen at position 'row' and 'col'.
8156 * The attributes of the first byte is used for all. This is required to
8157 * output the two bytes of a double-byte character with nothing in between.
8158 */
8159 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008160screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161{
8162 /* Check for illegal values (could be wrong when screen was resized). */
8163 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8164 return;
8165
8166 /* Outputting the last character on the screen may scrollup the screen.
8167 * Don't to it! Mark the character invalid (update it when scrolled up) */
8168 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8169 {
8170 ScreenAttrs[off] = (sattr_T)-1;
8171 return;
8172 }
8173
8174 /* Output the first byte normally (positions the cursor), then write the
8175 * second byte directly. */
8176 screen_char(off, row, col);
8177 out_char(ScreenLines[off + 1]);
8178 ++screen_cur_col;
8179}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181/*
8182 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8183 * This uses the contents of ScreenLines[] and doesn't change it.
8184 */
8185 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008186screen_draw_rectangle(
8187 int row,
8188 int col,
8189 int height,
8190 int width,
8191 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192{
8193 int r, c;
8194 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008195 int max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008197 /* Can't use ScreenLines unless initialized */
8198 if (ScreenLines == NULL)
8199 return;
8200
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201 if (invert)
8202 screen_char_attr = HL_INVERSE;
8203 for (r = row; r < row + height; ++r)
8204 {
8205 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008206 max_off = off + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008207 for (c = col; c < col + width; ++c)
8208 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00008209 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210 {
8211 screen_char_2(off + c, r, c);
8212 ++c;
8213 }
8214 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215 {
8216 screen_char(off + c, r, c);
Bram Moolenaar367329b2007-08-30 11:53:22 +00008217 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 ++c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219 }
8220 }
8221 }
8222 screen_char_attr = 0;
8223}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225/*
8226 * Redraw the characters for a vertically split window.
8227 */
8228 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008229redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230{
8231 int col;
8232 int width;
8233
8234# ifdef FEAT_CLIPBOARD
8235 clip_may_clear_selection(row, end - 1);
8236# endif
8237
8238 if (wp == NULL)
8239 {
8240 col = 0;
8241 width = Columns;
8242 }
8243 else
8244 {
8245 col = wp->w_wincol;
8246 width = wp->w_width;
8247 }
8248 screen_draw_rectangle(row, col, end - row, width, FALSE);
8249}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008251 static void
8252space_to_screenline(int off, int attr)
8253{
8254 ScreenLines[off] = ' ';
8255 ScreenAttrs[off] = attr;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008256 if (enc_utf8)
8257 ScreenLinesUC[off] = 0;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008258}
8259
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260/*
8261 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8262 * with character 'c1' in first column followed by 'c2' in the other columns.
8263 * Use attributes 'attr'.
8264 */
8265 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008266screen_fill(
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008267 int start_row,
8268 int end_row,
8269 int start_col,
8270 int end_col,
8271 int c1,
8272 int c2,
8273 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008275 int row;
8276 int col;
8277 int off;
8278 int end_off;
8279 int did_delete;
8280 int c;
8281 int norm_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008283 int force_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284#endif
8285
8286 if (end_row > screen_Rows) /* safety check */
8287 end_row = screen_Rows;
8288 if (end_col > screen_Columns) /* safety check */
8289 end_col = screen_Columns;
8290 if (ScreenLines == NULL
8291 || start_row >= end_row
8292 || start_col >= end_col) /* nothing to do */
8293 return;
8294
8295 /* it's a "normal" terminal when not in a GUI or cterm */
8296 norm_term = (
8297#ifdef FEAT_GUI
8298 !gui.in_use &&
8299#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008300 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301 for (row = start_row; row < end_row; ++row)
8302 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008303 if (has_mbyte
Bram Moolenaara12a1612019-01-24 16:39:02 +01008304#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00008305 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008306#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00008307 )
8308 {
8309 /* When drawing over the right halve of a double-wide char clear
8310 * out the left halve. When drawing over the left halve of a
8311 * double wide-char clear out the right halve. Only needed in a
8312 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008313 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008314 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008315 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008316 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008317 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 /*
8319 * Try to use delete-line termcap code, when no attributes or in a
8320 * "normal" terminal, where a bold/italic space is just a
8321 * space.
8322 */
8323 did_delete = FALSE;
8324 if (c2 == ' '
8325 && end_col == Columns
8326 && can_clear(T_CE)
8327 && (attr == 0
8328 || (norm_term
8329 && attr <= HL_ALL
8330 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8331 {
8332 /*
8333 * check if we really need to clear something
8334 */
8335 col = start_col;
8336 if (c1 != ' ') /* don't clear first char */
8337 ++col;
8338
8339 off = LineOffset[row] + col;
8340 end_off = LineOffset[row] + end_col;
8341
8342 /* skip blanks (used often, keep it fast!) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343 if (enc_utf8)
8344 while (off < end_off && ScreenLines[off] == ' '
8345 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8346 ++off;
8347 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 while (off < end_off && ScreenLines[off] == ' '
8349 && ScreenAttrs[off] == 0)
8350 ++off;
8351 if (off < end_off) /* something to be cleared */
8352 {
8353 col = off - LineOffset[row];
8354 screen_stop_highlight();
8355 term_windgoto(row, col);/* clear rest of this screen line */
8356 out_str(T_CE);
8357 screen_start(); /* don't know where cursor is now */
8358 col = end_col - col;
8359 while (col--) /* clear chars in ScreenLines */
8360 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008361 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362 ++off;
8363 }
8364 }
8365 did_delete = TRUE; /* the chars are cleared now */
8366 }
8367
8368 off = LineOffset[row] + start_col;
8369 c = c1;
8370 for (col = start_col; col < end_col; ++col)
8371 {
Bram Moolenaar33796b32019-06-08 16:01:13 +02008372 if ((ScreenLines[off] != c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008373 || (enc_utf8 && (int)ScreenLinesUC[off]
8374 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375 || ScreenAttrs[off] != attr
8376#if defined(FEAT_GUI) || defined(UNIX)
8377 || force_next
8378#endif
8379 )
Bram Moolenaar33796b32019-06-08 16:01:13 +02008380#ifdef FEAT_TEXT_PROP
8381 // Skip if under a(nother) popup.
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008382 && !blocked_by_popup(row, col)
Bram Moolenaar33796b32019-06-08 16:01:13 +02008383#endif
8384 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385 {
8386#if defined(FEAT_GUI) || defined(UNIX)
8387 /* The bold trick may make a single row of pixels appear in
8388 * the next character. When a bold character is removed, the
8389 * next character should be redrawn too. This happens for our
8390 * own GUI and for some xterms. */
8391 if (
8392# ifdef FEAT_GUI
8393 gui.in_use
8394# endif
8395# if defined(FEAT_GUI) && defined(UNIX)
8396 ||
8397# endif
8398# ifdef UNIX
8399 term_is_xterm
8400# endif
8401 )
8402 {
8403 if (ScreenLines[off] != ' '
8404 && (ScreenAttrs[off] > HL_ALL
8405 || ScreenAttrs[off] & HL_BOLD))
8406 force_next = TRUE;
8407 else
8408 force_next = FALSE;
8409 }
8410#endif
8411 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412 if (enc_utf8)
8413 {
8414 if (c >= 0x80)
8415 {
8416 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008417 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418 }
8419 else
8420 ScreenLinesUC[off] = 0;
8421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422 ScreenAttrs[off] = attr;
8423 if (!did_delete || c != ' ')
8424 screen_char(off, row, col);
8425 }
8426 ++off;
8427 if (col == start_col)
8428 {
8429 if (did_delete)
8430 break;
8431 c = c2;
8432 }
8433 }
8434 if (end_col == Columns)
8435 LineWraps[row] = FALSE;
8436 if (row == Rows - 1) /* overwritten the command line */
8437 {
8438 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02008439 if (start_col == 0 && end_col == Columns
8440 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008441 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008442 if (start_col == 0)
8443 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444 }
8445 }
8446}
8447
8448/*
8449 * Check if there should be a delay. Used before clearing or redrawing the
8450 * screen or the command line.
8451 */
8452 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008453check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454{
8455 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8456 && !did_wait_return
8457 && emsg_silent == 0)
8458 {
8459 out_flush();
8460 ui_delay(1000L, TRUE);
8461 emsg_on_display = FALSE;
8462 if (check_msg_scroll)
8463 msg_scroll = FALSE;
8464 }
8465}
8466
8467/*
Bram Moolenaarca57ab52019-04-13 14:53:16 +02008468 * Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect.
8469 */
8470 static void
8471clear_TabPageIdxs(void)
8472{
8473 int scol;
8474
8475 for (scol = 0; scol < Columns; ++scol)
8476 TabPageIdxs[scol] = 0;
8477}
8478
8479/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008481 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482 * Returns TRUE if there is a valid screen to write to.
8483 * Returns FALSE when starting up and screen not initialized yet.
8484 */
8485 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008486screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008488 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489 return (ScreenLines != NULL);
8490}
8491
8492/*
8493 * Resize the shell to Rows and Columns.
8494 * Allocate ScreenLines[] and associated items.
8495 *
8496 * There may be some time between setting Rows and Columns and (re)allocating
8497 * ScreenLines[]. This happens when starting up and when (manually) changing
8498 * the shell size. Always use screen_Rows and screen_Columns to access items
8499 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8500 * final size of the shell is needed.
8501 */
8502 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008503screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504{
8505 int new_row, old_row;
8506#ifdef FEAT_GUI
8507 int old_Rows;
8508#endif
8509 win_T *wp;
8510 int outofmem = FALSE;
8511 int len;
8512 schar_T *new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008514 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008516 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 sattr_T *new_ScreenAttrs;
8518 unsigned *new_LineOffset;
8519 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008520 short *new_TabPageIdxs;
Bram Moolenaar33796b32019-06-08 16:01:13 +02008521#ifdef FEAT_TEXT_PROP
8522 short *new_popup_mask;
Bram Moolenaar4c063a02019-06-10 21:24:12 +02008523 short *new_popup_mask_next;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008524 char *new_popup_transparent;
Bram Moolenaar33796b32019-06-08 16:01:13 +02008525#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00008526 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008528 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008529 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008531retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532 /*
8533 * Allocation of the screen buffers is done only when the size changes and
8534 * when Rows and Columns have been set and we have started doing full
8535 * screen stuff.
8536 */
8537 if ((ScreenLines != NULL
8538 && Rows == screen_Rows
8539 && Columns == screen_Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540 && enc_utf8 == (ScreenLinesUC != NULL)
8541 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaara12a1612019-01-24 16:39:02 +01008542 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543 || Rows == 0
8544 || Columns == 0
8545 || (!full_screen && ScreenLines == NULL))
8546 return;
8547
8548 /*
8549 * It's possible that we produce an out-of-memory message below, which
8550 * will cause this function to be called again. To break the loop, just
8551 * return here.
8552 */
8553 if (entered)
8554 return;
8555 entered = TRUE;
8556
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008557 /*
8558 * Note that the window sizes are updated before reallocating the arrays,
8559 * thus we must not redraw here!
8560 */
8561 ++RedrawingDisabled;
8562
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563 win_new_shellsize(); /* fit the windows in the new sized shell */
8564
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565 comp_col(); /* recompute columns for shown command and ruler */
8566
8567 /*
8568 * We're changing the size of the screen.
8569 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8570 * - Move lines from the old arrays into the new arrays, clear extra
8571 * lines (unless the screen is going to be cleared).
8572 * - Free the old arrays.
8573 *
8574 * If anything fails, make ScreenLines NULL, so we don't do anything!
8575 * Continuing with the old ScreenLines may result in a crash, because the
8576 * size is wrong.
8577 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008578 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008580 if (aucmd_win != NULL)
8581 win_free_lsize(aucmd_win);
Bram Moolenaar8caaf822019-06-01 18:11:22 +02008582#ifdef FEAT_TEXT_PROP
8583 // global popup windows
8584 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
8585 win_free_lsize(wp);
8586 // tab-local popup windows
8587 FOR_ALL_TABPAGES(tp)
8588 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
8589 win_free_lsize(wp);
8590#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008592 new_ScreenLines = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
Bram Moolenaar216b7102010-03-23 13:56:59 +01008593 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594 if (enc_utf8)
8595 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008596 new_ScreenLinesUC = LALLOC_MULT(u8char_T, (Rows + 1) * Columns);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008597 for (i = 0; i < p_mco; ++i)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008598 new_ScreenLinesC[i] = LALLOC_CLEAR_MULT(u8char_T,
8599 (Rows + 1) * Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600 }
8601 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008602 new_ScreenLines2 = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
8603 new_ScreenAttrs = LALLOC_MULT(sattr_T, (Rows + 1) * Columns);
8604 new_LineOffset = LALLOC_MULT(unsigned, Rows);
8605 new_LineWraps = LALLOC_MULT(char_u, Rows);
8606 new_TabPageIdxs = LALLOC_MULT(short, Columns);
Bram Moolenaar33796b32019-06-08 16:01:13 +02008607#ifdef FEAT_TEXT_PROP
8608 new_popup_mask = LALLOC_MULT(short, Rows * Columns);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02008609 new_popup_mask_next = LALLOC_MULT(short, Rows * Columns);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008610 new_popup_transparent = LALLOC_MULT(char, Rows * Columns);
Bram Moolenaar33796b32019-06-08 16:01:13 +02008611#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008613 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614 {
8615 if (win_alloc_lines(wp) == FAIL)
8616 {
8617 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008618 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619 }
8620 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008621 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8622 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008623 outofmem = TRUE;
Bram Moolenaar8caaf822019-06-01 18:11:22 +02008624#ifdef FEAT_TEXT_PROP
8625 // global popup windows
8626 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
8627 if (win_alloc_lines(wp) == FAIL)
8628 {
8629 outofmem = TRUE;
8630 goto give_up;
8631 }
8632 // tab-local popup windows
8633 FOR_ALL_TABPAGES(tp)
8634 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
8635 if (win_alloc_lines(wp) == FAIL)
8636 {
8637 outofmem = TRUE;
8638 goto give_up;
8639 }
8640#endif
8641
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008642give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008644 for (i = 0; i < p_mco; ++i)
8645 if (new_ScreenLinesC[i] == NULL)
8646 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647 if (new_ScreenLines == NULL
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008648 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008649 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650 || new_ScreenAttrs == NULL
8651 || new_LineOffset == NULL
8652 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008653 || new_TabPageIdxs == NULL
Bram Moolenaar33796b32019-06-08 16:01:13 +02008654#ifdef FEAT_TEXT_PROP
8655 || new_popup_mask == NULL
Bram Moolenaar4c063a02019-06-10 21:24:12 +02008656 || new_popup_mask_next == NULL
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008657 || new_popup_transparent == NULL
Bram Moolenaar33796b32019-06-08 16:01:13 +02008658#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008659 || outofmem)
8660 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008661 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008662 {
8663 /* guess the size */
8664 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8665
8666 /* Remember we did this to avoid getting outofmem messages over
8667 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008668 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008669 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01008670 VIM_CLEAR(new_ScreenLines);
Bram Moolenaard23a8232018-02-10 18:45:26 +01008671 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008672 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01008673 VIM_CLEAR(new_ScreenLinesC[i]);
8674 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaard23a8232018-02-10 18:45:26 +01008675 VIM_CLEAR(new_ScreenAttrs);
8676 VIM_CLEAR(new_LineOffset);
8677 VIM_CLEAR(new_LineWraps);
8678 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar33796b32019-06-08 16:01:13 +02008679#ifdef FEAT_TEXT_PROP
8680 VIM_CLEAR(new_popup_mask);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02008681 VIM_CLEAR(new_popup_mask_next);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008682 VIM_CLEAR(new_popup_transparent);
Bram Moolenaar33796b32019-06-08 16:01:13 +02008683#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684 }
8685 else
8686 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008687 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008688
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689 for (new_row = 0; new_row < Rows; ++new_row)
8690 {
8691 new_LineOffset[new_row] = new_row * Columns;
8692 new_LineWraps[new_row] = FALSE;
8693
8694 /*
8695 * If the screen is not going to be cleared, copy as much as
8696 * possible from the old screen to the new one and clear the rest
8697 * (used when resizing the window at the "--more--" prompt or when
8698 * executing an external command, for the GUI).
8699 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008700 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701 {
8702 (void)vim_memset(new_ScreenLines + new_row * Columns,
8703 ' ', (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704 if (enc_utf8)
8705 {
8706 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8707 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008708 for (i = 0; i < p_mco; ++i)
8709 (void)vim_memset(new_ScreenLinesC[i]
8710 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711 0, (size_t)Columns * sizeof(u8char_T));
8712 }
8713 if (enc_dbcs == DBCS_JPNU)
8714 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8715 0, (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8717 0, (size_t)Columns * sizeof(sattr_T));
8718 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008719 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008720 {
8721 if (screen_Columns < Columns)
8722 len = screen_Columns;
8723 else
8724 len = Columns;
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008725 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008726 * may be invalid now. Also when p_mco changes. */
8727 if (!(enc_utf8 && ScreenLinesUC == NULL)
8728 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008729 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8730 ScreenLines + LineOffset[old_row],
8731 (size_t)len * sizeof(schar_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008732 if (enc_utf8 && ScreenLinesUC != NULL
8733 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008734 {
8735 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8736 ScreenLinesUC + LineOffset[old_row],
8737 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008738 for (i = 0; i < p_mco; ++i)
8739 mch_memmove(new_ScreenLinesC[i]
8740 + new_LineOffset[new_row],
8741 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008742 (size_t)len * sizeof(u8char_T));
8743 }
8744 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8745 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8746 ScreenLines2 + LineOffset[old_row],
8747 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8749 ScreenAttrs + LineOffset[old_row],
8750 (size_t)len * sizeof(sattr_T));
8751 }
8752 }
8753 }
8754 /* Use the last line of the screen for the current line. */
8755 current_ScreenLine = new_ScreenLines + Rows * Columns;
Bram Moolenaar6ace95e2019-08-13 23:09:49 +02008756
8757#ifdef FEAT_TEXT_PROP
8758 vim_memset(new_popup_mask, 0, Rows * Columns * sizeof(short));
8759 vim_memset(new_popup_transparent, 0, Rows * Columns * sizeof(char));
8760#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761 }
8762
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008763 free_screenlines();
8764
Bram Moolenaar6ace95e2019-08-13 23:09:49 +02008765 // NOTE: this may result in all pointers to become NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008766 ScreenLines = new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008768 for (i = 0; i < p_mco; ++i)
8769 ScreenLinesC[i] = new_ScreenLinesC[i];
8770 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771 ScreenLines2 = new_ScreenLines2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772 ScreenAttrs = new_ScreenAttrs;
8773 LineOffset = new_LineOffset;
8774 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008775 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar33796b32019-06-08 16:01:13 +02008776#ifdef FEAT_TEXT_PROP
8777 popup_mask = new_popup_mask;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008778 popup_mask_next = new_popup_mask_next;
8779 popup_transparent = new_popup_transparent;
Bram Moolenaar33796b32019-06-08 16:01:13 +02008780 popup_mask_refresh = TRUE;
8781#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782
8783 /* It's important that screen_Rows and screen_Columns reflect the actual
8784 * size of ScreenLines[]. Set them before calling anything. */
8785#ifdef FEAT_GUI
8786 old_Rows = screen_Rows;
8787#endif
8788 screen_Rows = Rows;
8789 screen_Columns = Columns;
8790
8791 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008792 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793 screenclear2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794#ifdef FEAT_GUI
8795 else if (gui.in_use
8796 && !gui.starting
8797 && ScreenLines != NULL
8798 && old_Rows != Rows)
8799 {
8800 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8801 /*
8802 * Adjust the position of the cursor, for when executing an external
8803 * command.
8804 */
8805 if (msg_row >= Rows) /* Rows got smaller */
8806 msg_row = Rows - 1; /* put cursor at last row */
8807 else if (Rows > old_Rows) /* Rows got bigger */
8808 msg_row += Rows - old_Rows; /* put cursor in same place */
8809 if (msg_col >= Columns) /* Columns got smaller */
8810 msg_col = Columns - 1; /* put cursor at last column */
8811 }
8812#endif
Bram Moolenaarca57ab52019-04-13 14:53:16 +02008813 clear_TabPageIdxs();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008816 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008817
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008818 /*
8819 * Do not apply autocommands more than 3 times to avoid an endless loop
8820 * in case applying autocommands always changes Rows or Columns.
8821 */
8822 if (starting == 0 && ++retry_count <= 3)
8823 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008824 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008825 /* In rare cases, autocommands may have altered Rows or Columns,
8826 * jump back to check if we need to allocate the screen again. */
8827 goto retry;
8828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829}
8830
8831 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008832free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008833{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008834 int i;
8835
Bram Moolenaar33796b32019-06-08 16:01:13 +02008836 VIM_CLEAR(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008837 for (i = 0; i < Screen_mco; ++i)
Bram Moolenaar33796b32019-06-08 16:01:13 +02008838 VIM_CLEAR(ScreenLinesC[i]);
8839 VIM_CLEAR(ScreenLines2);
8840 VIM_CLEAR(ScreenLines);
8841 VIM_CLEAR(ScreenAttrs);
8842 VIM_CLEAR(LineOffset);
8843 VIM_CLEAR(LineWraps);
8844 VIM_CLEAR(TabPageIdxs);
8845#ifdef FEAT_TEXT_PROP
8846 VIM_CLEAR(popup_mask);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02008847 VIM_CLEAR(popup_mask_next);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008848 VIM_CLEAR(popup_transparent);
Bram Moolenaar33796b32019-06-08 16:01:13 +02008849#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008850}
8851
8852 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008853screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854{
8855 check_for_delay(FALSE);
8856 screenalloc(FALSE); /* allocate screen buffers if size changed */
8857 screenclear2(); /* clear the screen */
8858}
8859
8860 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008861screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862{
8863 int i;
8864
8865 if (starting == NO_SCREEN || ScreenLines == NULL
8866#ifdef FEAT_GUI
8867 || (gui.in_use && gui.starting)
8868#endif
8869 )
8870 return;
8871
8872#ifdef FEAT_GUI
8873 if (!gui.in_use)
8874#endif
8875 screen_attr = -1; /* force setting the Normal colors */
8876 screen_stop_highlight(); /* don't want highlighting here */
8877
8878#ifdef FEAT_CLIPBOARD
8879 /* disable selection without redrawing it */
8880 clip_scroll_selection(9999);
8881#endif
8882
8883 /* blank out ScreenLines */
8884 for (i = 0; i < Rows; ++i)
8885 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02008886 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887 LineWraps[i] = FALSE;
8888 }
8889
8890 if (can_clear(T_CL))
8891 {
8892 out_str(T_CL); /* clear the display */
8893 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008894 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895 }
8896 else
8897 {
8898 /* can't clear the screen, mark all chars with invalid attributes */
8899 for (i = 0; i < Rows; ++i)
8900 lineinvalid(LineOffset[i], (int)Columns);
8901 clear_cmdline = TRUE;
8902 }
8903
8904 screen_cleared = TRUE; /* can use contents of ScreenLines now */
8905
8906 win_rest_invalid(firstwin);
8907 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008908 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909 if (must_redraw == CLEAR) /* no need to clear again */
8910 must_redraw = NOT_VALID;
8911 compute_cmdrow();
8912 msg_row = cmdline_row; /* put cursor on last line for messages */
8913 msg_col = 0;
8914 screen_start(); /* don't know where cursor is now */
8915 msg_scrolled = 0; /* can't scroll back */
8916 msg_didany = FALSE;
8917 msg_didout = FALSE;
8918}
8919
8920/*
8921 * Clear one line in ScreenLines.
8922 */
8923 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02008924lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925{
8926 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927 if (enc_utf8)
8928 (void)vim_memset(ScreenLinesUC + off, 0,
8929 (size_t)width * sizeof(u8char_T));
Bram Moolenaarcfce7172017-08-17 20:31:48 +02008930 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931}
8932
8933/*
8934 * Mark one line in ScreenLines invalid by setting the attributes to an
8935 * invalid value.
8936 */
8937 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008938lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939{
8940 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
8941}
8942
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943/*
8944 * Copy part of a Screenline for vertically split window "wp".
8945 */
8946 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008947linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948{
8949 unsigned off_to = LineOffset[to] + wp->w_wincol;
8950 unsigned off_from = LineOffset[from] + wp->w_wincol;
8951
8952 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
8953 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008954 if (enc_utf8)
8955 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008956 int i;
8957
Bram Moolenaar071d4272004-06-13 20:20:40 +00008958 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
8959 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008960 for (i = 0; i < p_mco; ++i)
8961 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
8962 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008963 }
8964 if (enc_dbcs == DBCS_JPNU)
8965 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
8966 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
8968 wp->w_width * sizeof(sattr_T));
8969}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970
8971/*
8972 * Return TRUE if clearing with term string "p" would work.
8973 * It can't work when the string is empty or it won't set the right background.
Bram Moolenaar33796b32019-06-08 16:01:13 +02008974 * Don't clear to end-of-line when there are popups, it may cause flicker.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008975 */
8976 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008977can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978{
8979 return (*p != NUL && (t_colors <= 1
8980#ifdef FEAT_GUI
8981 || gui.in_use
8982#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008983#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008984 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02008985 || (!p_tgc && cterm_normal_bg_color == 0)
8986#else
8987 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008988#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02008989 || *T_UT != NUL)
8990#ifdef FEAT_TEXT_PROP
8991 && !(p == T_CE && popup_visible)
8992#endif
8993 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00008994}
8995
8996/*
8997 * Reset cursor position. Use whenever cursor was moved because of outputting
8998 * something directly to the screen (shell commands) or a terminal control
8999 * code.
9000 */
9001 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009002screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009003{
9004 screen_cur_row = screen_cur_col = 9999;
9005}
9006
9007/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009008 * Move the cursor to position "row","col" in the screen.
9009 * This tries to find the most efficient way to move, minimizing the number of
9010 * characters sent to the terminal.
9011 */
9012 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009013windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009014{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009015 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009016 int i;
9017 int plan;
9018 int cost;
9019 int wouldbe_col;
9020 int noinvcurs;
9021 char_u *bs;
9022 int goto_cost;
9023 int attr;
9024
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009025#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009026#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9027
9028#define PLAN_LE 1
9029#define PLAN_CR 2
9030#define PLAN_NL 3
9031#define PLAN_WRITE 4
9032 /* Can't use ScreenLines unless initialized */
9033 if (ScreenLines == NULL)
9034 return;
9035
9036 if (col != screen_cur_col || row != screen_cur_row)
9037 {
9038 /* Check for valid position. */
9039 if (row < 0) /* window without text lines? */
9040 row = 0;
9041 if (row >= screen_Rows)
9042 row = screen_Rows - 1;
9043 if (col >= screen_Columns)
9044 col = screen_Columns - 1;
9045
9046 /* check if no cursor movement is allowed in highlight mode */
9047 if (screen_attr && *T_MS == NUL)
9048 noinvcurs = HIGHL_COST;
9049 else
9050 noinvcurs = 0;
9051 goto_cost = GOTO_COST + noinvcurs;
9052
9053 /*
9054 * Plan how to do the positioning:
9055 * 1. Use CR to move it to column 0, same row.
9056 * 2. Use T_LE to move it a few columns to the left.
9057 * 3. Use NL to move a few lines down, column 0.
9058 * 4. Move a few columns to the right with T_ND or by writing chars.
9059 *
9060 * Don't do this if the cursor went beyond the last column, the cursor
9061 * position is unknown then (some terminals wrap, some don't )
9062 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009063 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064 * characters to move the cursor to the right.
9065 */
9066 if (row >= screen_cur_row && screen_cur_col < Columns)
9067 {
9068 /*
9069 * If the cursor is in the same row, bigger col, we can use CR
9070 * or T_LE.
9071 */
9072 bs = NULL; /* init for GCC */
9073 attr = screen_attr;
9074 if (row == screen_cur_row && col < screen_cur_col)
9075 {
9076 /* "le" is preferred over "bc", because "bc" is obsolete */
9077 if (*T_LE)
9078 bs = T_LE; /* "cursor left" */
9079 else
9080 bs = T_BC; /* "backspace character (old) */
9081 if (*bs)
9082 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9083 else
9084 cost = 999;
9085 if (col + 1 < cost) /* using CR is less characters */
9086 {
9087 plan = PLAN_CR;
9088 wouldbe_col = 0;
9089 cost = 1; /* CR is just one character */
9090 }
9091 else
9092 {
9093 plan = PLAN_LE;
9094 wouldbe_col = col;
9095 }
9096 if (noinvcurs) /* will stop highlighting */
9097 {
9098 cost += noinvcurs;
9099 attr = 0;
9100 }
9101 }
9102
9103 /*
9104 * If the cursor is above where we want to be, we can use CR LF.
9105 */
9106 else if (row > screen_cur_row)
9107 {
9108 plan = PLAN_NL;
9109 wouldbe_col = 0;
9110 cost = (row - screen_cur_row) * 2; /* CR LF */
9111 if (noinvcurs) /* will stop highlighting */
9112 {
9113 cost += noinvcurs;
9114 attr = 0;
9115 }
9116 }
9117
9118 /*
9119 * If the cursor is in the same row, smaller col, just use write.
9120 */
9121 else
9122 {
9123 plan = PLAN_WRITE;
9124 wouldbe_col = screen_cur_col;
9125 cost = 0;
9126 }
9127
9128 /*
9129 * Check if any characters that need to be written have the
9130 * correct attributes. Also avoid UTF-8 characters.
9131 */
9132 i = col - wouldbe_col;
9133 if (i > 0)
9134 cost += i;
9135 if (cost < goto_cost && i > 0)
9136 {
9137 /*
9138 * Check if the attributes are correct without additionally
9139 * stopping highlighting.
9140 */
9141 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9142 while (i && *p++ == attr)
9143 --i;
9144 if (i != 0)
9145 {
9146 /*
9147 * Try if it works when highlighting is stopped here.
9148 */
9149 if (*--p == 0)
9150 {
9151 cost += noinvcurs;
9152 while (i && *p++ == 0)
9153 --i;
9154 }
9155 if (i != 0)
9156 cost = 999; /* different attributes, don't do it */
9157 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158 if (enc_utf8)
9159 {
9160 /* Don't use an UTF-8 char for positioning, it's slow. */
9161 for (i = wouldbe_col; i < col; ++i)
9162 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9163 {
9164 cost = 999;
9165 break;
9166 }
9167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168 }
9169
9170 /*
9171 * We can do it without term_windgoto()!
9172 */
9173 if (cost < goto_cost)
9174 {
9175 if (plan == PLAN_LE)
9176 {
9177 if (noinvcurs)
9178 screen_stop_highlight();
9179 while (screen_cur_col > col)
9180 {
9181 out_str(bs);
9182 --screen_cur_col;
9183 }
9184 }
9185 else if (plan == PLAN_CR)
9186 {
9187 if (noinvcurs)
9188 screen_stop_highlight();
9189 out_char('\r');
9190 screen_cur_col = 0;
9191 }
9192 else if (plan == PLAN_NL)
9193 {
9194 if (noinvcurs)
9195 screen_stop_highlight();
9196 while (screen_cur_row < row)
9197 {
9198 out_char('\n');
9199 ++screen_cur_row;
9200 }
9201 screen_cur_col = 0;
9202 }
9203
9204 i = col - screen_cur_col;
9205 if (i > 0)
9206 {
9207 /*
9208 * Use cursor-right if it's one character only. Avoids
9209 * removing a line of pixels from the last bold char, when
9210 * using the bold trick in the GUI.
9211 */
9212 if (T_ND[0] != NUL && T_ND[1] == NUL)
9213 {
9214 while (i-- > 0)
9215 out_char(*T_ND);
9216 }
9217 else
9218 {
9219 int off;
9220
9221 off = LineOffset[row] + screen_cur_col;
9222 while (i-- > 0)
9223 {
9224 if (ScreenAttrs[off] != screen_attr)
9225 screen_stop_highlight();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009226 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009227 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009228 if (enc_dbcs == DBCS_JPNU
9229 && ScreenLines[off] == 0x8e)
9230 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009231 ++off;
9232 }
9233 }
9234 }
9235 }
9236 }
9237 else
9238 cost = 999;
9239
9240 if (cost >= goto_cost)
9241 {
9242 if (noinvcurs)
9243 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009244 if (row == screen_cur_row && (col > screen_cur_col)
9245 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246 term_cursor_right(col - screen_cur_col);
9247 else
9248 term_windgoto(row, col);
9249 }
9250 screen_cur_row = row;
9251 screen_cur_col = col;
9252 }
9253}
9254
9255/*
9256 * Set cursor to its position in the current window.
9257 */
9258 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009259setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009260{
Bram Moolenaar987723e2018-03-06 11:43:04 +01009261 setcursor_mayforce(FALSE);
9262}
9263
9264/*
9265 * Set cursor to its position in the current window.
9266 * When "force" is TRUE also when not redrawing.
9267 */
9268 void
9269setcursor_mayforce(int force)
9270{
9271 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009272 {
9273 validate_cursor();
9274 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009275 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009276#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009277 /* With 'rightleft' set and the cursor on a double-wide
9278 * character, position it on the leftmost column. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01009279 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol
9280 - ((has_mbyte
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009281 && (*mb_ptr2cells)(ml_get_cursor()) == 2
Bram Moolenaara12a1612019-01-24 16:39:02 +01009282 && vim_isprintc(gchar_cursor())) ? 2 : 1)) :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009283#endif
9284 curwin->w_wcol));
9285 }
9286}
9287
9288
9289/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009290 * Insert 'line_count' lines at 'row' in window 'wp'.
9291 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9292 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009293 * scrolling.
9294 * Returns FAIL if the lines are not inserted, OK for success.
9295 */
9296 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009297win_ins_lines(
9298 win_T *wp,
9299 int row,
9300 int line_count,
9301 int invalid,
9302 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009303{
9304 int did_delete;
9305 int nextrow;
9306 int lastrow;
9307 int retval;
9308
9309 if (invalid)
9310 wp->w_lines_valid = 0;
9311
9312 if (wp->w_height < 5)
9313 return FAIL;
9314
9315 if (line_count > wp->w_height - row)
9316 line_count = wp->w_height - row;
9317
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009318 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009319 if (retval != MAYBE)
9320 return retval;
9321
9322 /*
9323 * If there is a next window or a status line, we first try to delete the
9324 * lines at the bottom to avoid messing what is after the window.
Bram Moolenaarc363fe12019-08-04 18:13:46 +02009325 * If this fails and there are following windows, don't do anything to
9326 * avoid messing up those windows, better just redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009327 */
9328 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009329 if (wp->w_next != NULL || wp->w_status_height)
9330 {
9331 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009332 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009333 did_delete = TRUE;
9334 else if (wp->w_next)
9335 return FAIL;
9336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009337 /*
9338 * if no lines deleted, blank the lines that will end up below the window
9339 */
9340 if (!did_delete)
9341 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009342 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009343 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02009344 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009345 lastrow = nextrow + line_count;
9346 if (lastrow > Rows)
9347 lastrow = Rows;
9348 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009349 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009350 ' ', ' ', 0);
9351 }
9352
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009353 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009354 == FAIL)
9355 {
Bram Moolenaarc363fe12019-08-04 18:13:46 +02009356 // deletion will have messed up other windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00009357 if (did_delete)
9358 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009359 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009360 win_rest_invalid(W_NEXT(wp));
9361 }
9362 return FAIL;
9363 }
9364
9365 return OK;
9366}
9367
9368/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009369 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009370 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9371 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9372 * scrolling
9373 * Return OK for success, FAIL if the lines are not deleted.
9374 */
9375 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009376win_del_lines(
9377 win_T *wp,
9378 int row,
9379 int line_count,
9380 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009381 int mayclear,
9382 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009383{
9384 int retval;
9385
9386 if (invalid)
9387 wp->w_lines_valid = 0;
9388
9389 if (line_count > wp->w_height - row)
9390 line_count = wp->w_height - row;
9391
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009392 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009393 if (retval != MAYBE)
9394 return retval;
9395
9396 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009397 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009398 return FAIL;
9399
Bram Moolenaar071d4272004-06-13 20:20:40 +00009400 /*
9401 * If there are windows or status lines below, try to put them at the
9402 * correct place. If we can't do that, they have to be redrawn.
9403 */
9404 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9405 {
9406 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009407 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009408 {
9409 wp->w_redr_status = TRUE;
9410 win_rest_invalid(wp->w_next);
9411 }
9412 }
9413 /*
9414 * If this is the last window and there is no status line, redraw the
9415 * command line later.
9416 */
9417 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009418 redraw_cmdline = TRUE;
9419 return OK;
9420}
9421
9422/*
9423 * Common code for win_ins_lines() and win_del_lines().
9424 * Returns OK or FAIL when the work has been done.
9425 * Returns MAYBE when not finished yet.
9426 */
9427 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009428win_do_lines(
9429 win_T *wp,
9430 int row,
9431 int line_count,
9432 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009433 int del,
9434 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009435{
9436 int retval;
9437
9438 if (!redrawing() || line_count <= 0)
9439 return FAIL;
9440
Bram Moolenaar33796b32019-06-08 16:01:13 +02009441 // When inserting lines would result in loss of command output, just redraw
9442 // the lines.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009443 if (no_win_do_lines_ins && !del)
9444 return FAIL;
9445
Bram Moolenaar33796b32019-06-08 16:01:13 +02009446 // only a few lines left: redraw is faster
Bram Moolenaar4033c552017-09-16 20:54:51 +02009447 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009448 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009449 if (!no_win_do_lines_ins)
Bram Moolenaar33796b32019-06-08 16:01:13 +02009450 screenclear(); // will set wp->w_lines_valid to 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00009451 return FAIL;
9452 }
9453
Bram Moolenaar33796b32019-06-08 16:01:13 +02009454#ifdef FEAT_TEXT_PROP
Bram Moolenaar4c063a02019-06-10 21:24:12 +02009455 // this doesn't work when there are popups visible
Bram Moolenaar33796b32019-06-08 16:01:13 +02009456 if (popup_visible)
9457 return FAIL;
9458#endif
9459
9460 // Delete all remaining lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461 if (row + line_count >= wp->w_height)
9462 {
9463 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009464 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009465 ' ', ' ', 0);
9466 return OK;
9467 }
9468
9469 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009470 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009471 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009472 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009473 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009474 if (!no_win_do_lines_ins)
9475 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009476
9477 /*
9478 * If the terminal can set a scroll region, use that.
9479 * Always do this in a vertically split window. This will redraw from
9480 * ScreenLines[] when t_CV isn't defined. That's faster than using
9481 * win_line().
9482 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009483 * a character in the lower right corner of the scroll region may cause a
9484 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485 */
Bram Moolenaar02631462017-09-22 15:20:32 +02009486 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009487 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009488 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009489 scroll_region_set(wp, row);
9490 if (del)
9491 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009492 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009493 else
9494 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009495 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009496 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497 scroll_region_reset();
9498 return retval;
9499 }
9500
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9502 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503
9504 return MAYBE;
9505}
9506
9507/*
9508 * window 'wp' and everything after it is messed up, mark it for redraw
9509 */
9510 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009511win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009512{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009513 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009514 {
9515 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009516 wp->w_redr_status = TRUE;
9517 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009518 }
9519 redraw_cmdline = TRUE;
9520}
9521
9522/*
9523 * The rest of the routines in this file perform screen manipulations. The
9524 * given operation is performed physically on the screen. The corresponding
9525 * change is also made to the internal screen image. In this way, the editor
9526 * anticipates the effect of editing changes on the appearance of the screen.
9527 * That way, when we call screenupdate a complete redraw isn't usually
9528 * necessary. Another advantage is that we can keep adding code to anticipate
9529 * screen changes, and in the meantime, everything still works.
9530 */
9531
9532/*
9533 * types for inserting or deleting lines
9534 */
9535#define USE_T_CAL 1
9536#define USE_T_CDL 2
9537#define USE_T_AL 3
9538#define USE_T_CE 4
9539#define USE_T_DL 5
9540#define USE_T_SR 6
9541#define USE_NL 7
9542#define USE_T_CD 8
9543#define USE_REDRAW 9
9544
9545/*
9546 * insert lines on the screen and update ScreenLines[]
9547 * 'end' is the line after the scrolled part. Normally it is Rows.
9548 * When scrolling region used 'off' is the offset from the top for the region.
9549 * 'row' and 'end' are relative to the start of the region.
9550 *
9551 * return FAIL for failure, OK for success.
9552 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009553 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009554screen_ins_lines(
9555 int off,
9556 int row,
9557 int line_count,
9558 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009559 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01009560 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561{
9562 int i;
9563 int j;
9564 unsigned temp;
9565 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009566 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009567 int type;
9568 int result_empty;
9569 int can_ce = can_clear(T_CE);
9570
9571 /*
9572 * FAIL if
9573 * - there is no valid screen
9574 * - the screen has to be redrawn completely
9575 * - the line count is less than one
9576 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009577 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar33796b32019-06-08 16:01:13 +02009578 * - there is a popup window
Bram Moolenaar071d4272004-06-13 20:20:40 +00009579 */
Bram Moolenaar33796b32019-06-08 16:01:13 +02009580 if (!screen_valid(TRUE)
9581 || line_count <= 0 || line_count > p_ttyscroll
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009582#ifdef FEAT_CLIPBOARD
9583 || (clip_star.state != SELECT_CLEARED
9584 && redrawing_for_callback > 0)
9585#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02009586#ifdef FEAT_TEXT_PROP
9587 || popup_visible
9588#endif
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009589 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009590 return FAIL;
9591
9592 /*
9593 * There are seven ways to insert lines:
9594 * 0. When in a vertically split window and t_CV isn't set, redraw the
9595 * characters from ScreenLines[].
9596 * 1. Use T_CD (clear to end of display) if it exists and the result of
9597 * the insert is just empty lines
9598 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9599 * present or line_count > 1. It looks better if we do all the inserts
9600 * at once.
9601 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9602 * insert is just empty lines and T_CE is not present or line_count >
9603 * 1.
9604 * 4. Use T_AL (insert line) if it exists.
9605 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9606 * just empty lines.
9607 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9608 * just empty lines.
9609 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9610 * the 'da' flag is not set or we have clear line capability.
9611 * 8. redraw the characters from ScreenLines[].
9612 *
9613 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9614 * the scrollbar for the window. It does have insert line, use that if it
9615 * exists.
9616 */
9617 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009618 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9619 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009620 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009621 type = USE_T_CD;
9622 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9623 type = USE_T_CAL;
9624 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9625 type = USE_T_CDL;
9626 else if (*T_AL != NUL)
9627 type = USE_T_AL;
9628 else if (can_ce && result_empty)
9629 type = USE_T_CE;
9630 else if (*T_DL != NUL && result_empty)
9631 type = USE_T_DL;
9632 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9633 type = USE_T_SR;
9634 else
9635 return FAIL;
9636
9637 /*
9638 * For clearing the lines screen_del_lines() is used. This will also take
9639 * care of t_db if necessary.
9640 */
9641 if (type == USE_T_CD || type == USE_T_CDL ||
9642 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009643 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009644
9645 /*
9646 * If text is retained below the screen, first clear or delete as many
9647 * lines at the bottom of the window as are about to be inserted so that
9648 * the deleted lines won't later surface during a screen_del_lines.
9649 */
9650 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009651 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009652
9653#ifdef FEAT_CLIPBOARD
9654 /* Remove a modeless selection when inserting lines halfway the screen
9655 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009656 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009657 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009658 else
9659 clip_scroll_selection(-line_count);
9660#endif
9661
Bram Moolenaar071d4272004-06-13 20:20:40 +00009662#ifdef FEAT_GUI
9663 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9664 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009665 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009666#endif
9667
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009668 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
9669 cursor_col = wp->w_wincol;
9670
Bram Moolenaar071d4272004-06-13 20:20:40 +00009671 if (*T_CCS != NUL) /* cursor relative to region */
9672 cursor_row = row;
9673 else
9674 cursor_row = row + off;
9675
9676 /*
9677 * Shift LineOffset[] line_count down to reflect the inserted lines.
9678 * Clear the inserted lines in ScreenLines[].
9679 */
9680 row += off;
9681 end += off;
9682 for (i = 0; i < line_count; ++i)
9683 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009684 if (wp != NULL && wp->w_width != Columns)
9685 {
9686 /* need to copy part of a line */
9687 j = end - 1 - i;
9688 while ((j -= line_count) >= row)
9689 linecopy(j + line_count, j, wp);
9690 j += line_count;
9691 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009692 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
9693 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009694 else
9695 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9696 LineWraps[j] = FALSE;
9697 }
9698 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009699 {
9700 j = end - 1 - i;
9701 temp = LineOffset[j];
9702 while ((j -= line_count) >= row)
9703 {
9704 LineOffset[j + line_count] = LineOffset[j];
9705 LineWraps[j + line_count] = LineWraps[j];
9706 }
9707 LineOffset[j + line_count] = temp;
9708 LineWraps[j + line_count] = FALSE;
9709 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009710 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009711 else
9712 lineinvalid(temp, (int)Columns);
9713 }
9714 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009715
9716 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009717 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009718 if (clear_attr != 0)
9719 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009720
Bram Moolenaar071d4272004-06-13 20:20:40 +00009721 /* redraw the characters */
9722 if (type == USE_REDRAW)
9723 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02009724 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009725 {
9726 term_append_lines(line_count);
9727 screen_start(); /* don't know where cursor is now */
9728 }
9729 else
9730 {
9731 for (i = 0; i < line_count; i++)
9732 {
9733 if (type == USE_T_AL)
9734 {
9735 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009736 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009737 out_str(T_AL);
9738 }
9739 else /* type == USE_T_SR */
9740 out_str(T_SR);
9741 screen_start(); /* don't know where cursor is now */
9742 }
9743 }
9744
9745 /*
9746 * With scroll-reverse and 'da' flag set we need to clear the lines that
9747 * have been scrolled down into the region.
9748 */
9749 if (type == USE_T_SR && *T_DA)
9750 {
9751 for (i = 0; i < line_count; ++i)
9752 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009753 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009754 out_str(T_CE);
9755 screen_start(); /* don't know where cursor is now */
9756 }
9757 }
9758
9759#ifdef FEAT_GUI
9760 gui_can_update_cursor();
9761 if (gui.in_use)
9762 out_flush(); /* always flush after a scroll */
9763#endif
9764 return OK;
9765}
9766
9767/*
Bram Moolenaar107abd22016-08-12 14:08:25 +02009768 * Delete lines on the screen and update ScreenLines[].
9769 * "end" is the line after the scrolled part. Normally it is Rows.
9770 * When scrolling region used "off" is the offset from the top for the region.
9771 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009772 *
9773 * Return OK for success, FAIL if the lines are not deleted.
9774 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009776screen_del_lines(
9777 int off,
9778 int row,
9779 int line_count,
9780 int end,
9781 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009782 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +01009783 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009784{
9785 int j;
9786 int i;
9787 unsigned temp;
9788 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009789 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009790 int cursor_end;
9791 int result_empty; /* result is empty until end of region */
9792 int can_delete; /* deleting line codes can be used */
9793 int type;
9794
9795 /*
9796 * FAIL if
9797 * - there is no valid screen
9798 * - the screen has to be redrawn completely
9799 * - the line count is less than one
9800 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009801 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009802 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009803 if (!screen_valid(TRUE) || line_count <= 0
9804 || (!force && line_count > p_ttyscroll)
9805#ifdef FEAT_CLIPBOARD
9806 || (clip_star.state != SELECT_CLEARED
9807 && redrawing_for_callback > 0)
9808#endif
9809 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009810 return FAIL;
9811
9812 /*
9813 * Check if the rest of the current region will become empty.
9814 */
9815 result_empty = row + line_count >= end;
9816
9817 /*
9818 * We can delete lines only when 'db' flag not set or when 'ce' option
9819 * available.
9820 */
9821 can_delete = (*T_DB == NUL || can_clear(T_CE));
9822
9823 /*
9824 * There are six ways to delete lines:
9825 * 0. When in a vertically split window and t_CV isn't set, redraw the
9826 * characters from ScreenLines[].
9827 * 1. Use T_CD if it exists and the result is empty.
9828 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9829 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9830 * none of the other ways work.
9831 * 4. Use T_CE (erase line) if the result is empty.
9832 * 5. Use T_DL (delete line) if it exists.
9833 * 6. redraw the characters from ScreenLines[].
9834 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009835 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9836 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009837 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009838 type = USE_T_CD;
9839#if defined(__BEOS__) && defined(BEOS_DR8)
9840 /*
9841 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9842 * its internal termcap... this works okay for tests which test *T_DB !=
9843 * NUL. It has the disadvantage that the user cannot use any :set t_*
9844 * command to get T_DB (back) to empty_option, only :set term=... will do
9845 * the trick...
9846 * Anyway, this hack will hopefully go away with the next OS release.
9847 * (Olaf Seibert)
9848 */
9849 else if (row == 0 && T_DB == empty_option
9850 && (line_count == 1 || *T_CDL == NUL))
9851#else
9852 else if (row == 0 && (
9853#ifndef AMIGA
9854 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9855 * up, so use delete-line command */
9856 line_count == 1 ||
9857#endif
9858 *T_CDL == NUL))
9859#endif
9860 type = USE_NL;
9861 else if (*T_CDL != NUL && line_count > 1 && can_delete)
9862 type = USE_T_CDL;
9863 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +02009864 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009865 type = USE_T_CE;
9866 else if (*T_DL != NUL && can_delete)
9867 type = USE_T_DL;
9868 else if (*T_CDL != NUL && can_delete)
9869 type = USE_T_CDL;
9870 else
9871 return FAIL;
9872
9873#ifdef FEAT_CLIPBOARD
9874 /* Remove a modeless selection when deleting lines halfway the screen or
9875 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009876 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009877 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009878 else
9879 clip_scroll_selection(line_count);
9880#endif
9881
Bram Moolenaar071d4272004-06-13 20:20:40 +00009882#ifdef FEAT_GUI
9883 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9884 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009885 gui_dont_update_cursor(gui.cursor_row >= row + off
9886 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009887#endif
9888
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009889 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
9890 cursor_col = wp->w_wincol;
9891
Bram Moolenaar071d4272004-06-13 20:20:40 +00009892 if (*T_CCS != NUL) /* cursor relative to region */
9893 {
9894 cursor_row = row;
9895 cursor_end = end;
9896 }
9897 else
9898 {
9899 cursor_row = row + off;
9900 cursor_end = end + off;
9901 }
9902
9903 /*
9904 * Now shift LineOffset[] line_count up to reflect the deleted lines.
9905 * Clear the inserted lines in ScreenLines[].
9906 */
9907 row += off;
9908 end += off;
9909 for (i = 0; i < line_count; ++i)
9910 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009911 if (wp != NULL && wp->w_width != Columns)
9912 {
9913 /* need to copy part of a line */
9914 j = row + i;
9915 while ((j += line_count) <= end - 1)
9916 linecopy(j - line_count, j, wp);
9917 j -= line_count;
9918 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009919 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
9920 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009921 else
9922 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9923 LineWraps[j] = FALSE;
9924 }
9925 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009926 {
9927 /* whole width, moving the line pointers is faster */
9928 j = row + i;
9929 temp = LineOffset[j];
9930 while ((j += line_count) <= end - 1)
9931 {
9932 LineOffset[j - line_count] = LineOffset[j];
9933 LineWraps[j - line_count] = LineWraps[j];
9934 }
9935 LineOffset[j - line_count] = temp;
9936 LineWraps[j - line_count] = FALSE;
9937 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009938 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009939 else
9940 lineinvalid(temp, (int)Columns);
9941 }
9942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009943
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009944 if (screen_attr != clear_attr)
9945 screen_stop_highlight();
9946 if (clear_attr != 0)
9947 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009948
Bram Moolenaar071d4272004-06-13 20:20:40 +00009949 /* redraw the characters */
9950 if (type == USE_REDRAW)
9951 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02009952 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009953 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009954 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009955 out_str(T_CD);
9956 screen_start(); /* don't know where cursor is now */
9957 }
9958 else if (type == USE_T_CDL)
9959 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009960 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009961 term_delete_lines(line_count);
9962 screen_start(); /* don't know where cursor is now */
9963 }
9964 /*
9965 * Deleting lines at top of the screen or scroll region: Just scroll
9966 * the whole screen (scroll region) up by outputting newlines on the
9967 * last line.
9968 */
9969 else if (type == USE_NL)
9970 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009971 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009972 for (i = line_count; --i >= 0; )
9973 out_char('\n'); /* cursor will remain on same line */
9974 }
9975 else
9976 {
9977 for (i = line_count; --i >= 0; )
9978 {
9979 if (type == USE_T_DL)
9980 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009981 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009982 out_str(T_DL); /* delete a line */
9983 }
9984 else /* type == USE_T_CE */
9985 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009986 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009987 out_str(T_CE); /* erase a line */
9988 }
9989 screen_start(); /* don't know where cursor is now */
9990 }
9991 }
9992
9993 /*
9994 * If the 'db' flag is set, we need to clear the lines that have been
9995 * scrolled up at the bottom of the region.
9996 */
9997 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
9998 {
9999 for (i = line_count; i > 0; --i)
10000 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010001 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010002 out_str(T_CE); /* erase a line */
10003 screen_start(); /* don't know where cursor is now */
10004 }
10005 }
10006
10007#ifdef FEAT_GUI
10008 gui_can_update_cursor();
10009 if (gui.in_use)
10010 out_flush(); /* always flush after a scroll */
10011#endif
10012
10013 return OK;
10014}
10015
10016/*
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010017 * Return TRUE when postponing displaying the mode message: when not redrawing
10018 * or inside a mapping.
10019 */
10020 int
10021skip_showmode()
10022{
10023 // Call char_avail() only when we are going to show something, because it
10024 // takes a bit of time. redrawing() may also call char_avail_avail().
10025 if (global_busy
10026 || msg_silent != 0
10027 || !redrawing()
10028 || (char_avail() && !KeyTyped))
10029 {
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010030 redraw_mode = TRUE; // show mode later
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010031 return TRUE;
10032 }
10033 return FALSE;
10034}
10035
10036/*
Bram Moolenaar81226e02018-02-20 21:44:45 +010010037 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010038 *
10039 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10040 * If clear_cmdline is FALSE there may be a message there that needs to be
10041 * cleared only if a mode is shown.
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010042 * If redraw_mode is TRUE show or clear the mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010043 * Return the length of the message (0 if no message).
10044 */
10045 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010046showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010047{
10048 int need_clear;
10049 int length = 0;
10050 int do_mode;
10051 int attr;
10052 int nwr_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010053 int sub_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010054
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010055 do_mode = ((p_smd && msg_silent == 0)
10056 && ((State & INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +020010057 || restart_edit != NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010058 || VIsual_active));
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010059 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010060 {
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010061 if (skip_showmode())
10062 return 0; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +000010063
10064 nwr_save = need_wait_return;
10065
10066 /* wait a bit before overwriting an important message */
10067 check_for_delay(FALSE);
10068
10069 /* if the cmdline is more than one line high, erase top lines */
10070 need_clear = clear_cmdline;
10071 if (clear_cmdline && cmdline_row < Rows - 1)
10072 msg_clr_cmdline(); /* will reset clear_cmdline */
10073
10074 /* Position on the last line in the window, column 0 */
10075 msg_pos_mode();
10076 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010077 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010078 if (do_mode)
10079 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010080 msg_puts_attr("--", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010081#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010082 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010083# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010084 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010085# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010086 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010087# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010088 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010089# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar32526b32019-01-19 17:43:09 +010010090 msg_puts_attr(" IM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010091# else
Bram Moolenaar32526b32019-01-19 17:43:09 +010010092 msg_puts_attr(" XIM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010093# endif
10094#endif
10095#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10096 if (gui.in_use)
10097 {
10098 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010099 {
10100 /* HANGUL */
10101 if (enc_utf8)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010102 msg_puts_attr(" \355\225\234\352\270\200", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010103 else
Bram Moolenaar32526b32019-01-19 17:43:09 +010010104 msg_puts_attr(" \307\321\261\333", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010106 }
10107#endif
Bram Moolenaarea389e92014-05-28 21:40:52 +020010108 /* CTRL-X in Insert mode */
10109 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010110 {
10111 /* These messages can get long, avoid a wrap in a narrow
10112 * window. Prefer showing edit_submode_extra. */
10113 length = (Rows - msg_row) * Columns - 3;
10114 if (edit_submode_extra != NULL)
10115 length -= vim_strsize(edit_submode_extra);
10116 if (length > 0)
10117 {
10118 if (edit_submode_pre != NULL)
10119 length -= vim_strsize(edit_submode_pre);
10120 if (length - vim_strsize(edit_submode) > 0)
10121 {
10122 if (edit_submode_pre != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010123 msg_puts_attr((char *)edit_submode_pre, attr);
10124 msg_puts_attr((char *)edit_submode, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010125 }
10126 if (edit_submode_extra != NULL)
10127 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010128 msg_puts_attr(" ", attr); /* add a space in between */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010129 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010130 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010131 else
10132 sub_attr = attr;
Bram Moolenaar32526b32019-01-19 17:43:09 +010010133 msg_puts_attr((char *)edit_submode_extra, sub_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010134 }
10135 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010136 }
10137 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010138 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010139 if (State & VREPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010140 msg_puts_attr(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010141 else if (State & REPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010142 msg_puts_attr(_(" REPLACE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010143 else if (State & INSERT)
10144 {
10145#ifdef FEAT_RIGHTLEFT
10146 if (p_ri)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010147 msg_puts_attr(_(" REVERSE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010148#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +010010149 msg_puts_attr(_(" INSERT"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010150 }
Bram Moolenaar942b4542018-06-17 16:23:34 +020010151 else if (restart_edit == 'I' || restart_edit == 'A')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010152 msg_puts_attr(_(" (insert)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010153 else if (restart_edit == 'R')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010154 msg_puts_attr(_(" (replace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010155 else if (restart_edit == 'V')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010156 msg_puts_attr(_(" (vreplace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010157#ifdef FEAT_RIGHTLEFT
10158 if (p_hkmap)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010159 msg_puts_attr(_(" Hebrew"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010160#endif
10161#ifdef FEAT_KEYMAP
10162 if (State & LANGMAP)
10163 {
10164# ifdef FEAT_ARABIC
10165 if (curwin->w_p_arab)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010166 msg_puts_attr(_(" Arabic"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010167 else
10168# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010169 if (get_keymap_str(curwin, (char_u *)" (%s)",
10170 NameBuff, MAXPATHL))
Bram Moolenaar32526b32019-01-19 17:43:09 +010010171 msg_puts_attr((char *)NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010172 }
10173#endif
10174 if ((State & INSERT) && p_paste)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010175 msg_puts_attr(_(" (paste)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010176
Bram Moolenaar071d4272004-06-13 20:20:40 +000010177 if (VIsual_active)
10178 {
10179 char *p;
10180
10181 /* Don't concatenate separate words to avoid translation
10182 * problems. */
10183 switch ((VIsual_select ? 4 : 0)
10184 + (VIsual_mode == Ctrl_V) * 2
10185 + (VIsual_mode == 'V'))
10186 {
10187 case 0: p = N_(" VISUAL"); break;
10188 case 1: p = N_(" VISUAL LINE"); break;
10189 case 2: p = N_(" VISUAL BLOCK"); break;
10190 case 4: p = N_(" SELECT"); break;
10191 case 5: p = N_(" SELECT LINE"); break;
10192 default: p = N_(" SELECT BLOCK"); break;
10193 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010194 msg_puts_attr(_(p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010195 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010196 msg_puts_attr(" --", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010197 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010198
Bram Moolenaar071d4272004-06-13 20:20:40 +000010199 need_clear = TRUE;
10200 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010201 if (reg_recording != 0
Bram Moolenaare2c453d2019-08-21 14:37:09 +020010202 && edit_submode == NULL) // otherwise it gets too long
Bram Moolenaar071d4272004-06-13 20:20:40 +000010203 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010204 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010205 need_clear = TRUE;
10206 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010207
10208 mode_displayed = TRUE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010209 if (need_clear || clear_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010210 msg_clr_eos();
10211 msg_didout = FALSE; /* overwrite this message */
10212 length = msg_col;
10213 msg_col = 0;
10214 need_wait_return = nwr_save; /* never ask for hit-return for this */
10215 }
10216 else if (clear_cmdline && msg_silent == 0)
10217 /* Clear the whole command line. Will reset "clear_cmdline". */
10218 msg_clr_cmdline();
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010219 else if (redraw_mode)
10220 {
10221 msg_pos_mode();
10222 msg_clr_eos();
10223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010224
10225#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010226 /* In Visual mode the size of the selected area must be redrawn. */
10227 if (VIsual_active)
10228 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010229
10230 /* If the last window has no status line, the ruler is after the mode
10231 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010232 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +020010233 win_redr_ruler(lastwin, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010234#endif
10235 redraw_cmdline = FALSE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010236 redraw_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010237 clear_cmdline = FALSE;
10238
10239 return length;
10240}
10241
10242/*
10243 * Position for a mode message.
10244 */
10245 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010246msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010247{
10248 msg_col = 0;
10249 msg_row = Rows - 1;
10250}
10251
10252/*
10253 * Delete mode message. Used when ESC is typed which is expected to end
10254 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010255 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010256 */
10257 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010258unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010259{
10260 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010261 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010262 */
10263 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10264 redraw_cmdline = TRUE; /* delete mode later */
10265 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010266 clearmode();
10267}
10268
10269/*
10270 * Clear the mode message.
10271 */
10272 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010273clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010274{
Bram Moolenaar2abad542018-05-19 14:43:45 +020010275 int save_msg_row = msg_row;
10276 int save_msg_col = msg_col;
10277
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010278 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010279 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010280 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010281 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +020010282
10283 msg_col = save_msg_col;
10284 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010285}
10286
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010287 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010288recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010289{
Bram Moolenaar32526b32019-01-19 17:43:09 +010010290 msg_puts_attr(_("recording"), attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010291 if (!shortmess(SHM_RECORDING))
10292 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010293 char s[4];
10294
10295 sprintf(s, " @%c", reg_recording);
10296 msg_puts_attr(s, attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010297 }
10298}
10299
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010300/*
10301 * Draw the tab pages line at the top of the Vim window.
10302 */
Bram Moolenaare12bab32019-01-08 22:02:56 +010010303 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010304draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010305{
10306 int tabcount = 0;
10307 tabpage_T *tp;
10308 int tabwidth;
10309 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010310 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010311 int attr;
10312 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010313 win_T *cwp;
10314 int wincount;
10315 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010316 int c;
10317 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010318 int attr_sel = HL_ATTR(HLF_TPS);
10319 int attr_nosel = HL_ATTR(HLF_TP);
10320 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010321 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010322 int room;
10323 int use_sep_chars = (t_colors < 8
10324#ifdef FEAT_GUI
10325 && !gui.in_use
10326#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010327#ifdef FEAT_TERMGUICOLORS
10328 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010329#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010330 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010331
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010332 if (ScreenLines == NULL)
10333 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010334 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010335
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010336#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010337 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010338 if (gui_use_tabline())
10339 {
10340 gui_update_tabline();
10341 return;
10342 }
10343#endif
10344
10345 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010346 return;
10347
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010348#if defined(FEAT_STL_OPT)
Bram Moolenaarca57ab52019-04-13 14:53:16 +020010349 clear_TabPageIdxs();
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010350
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010351 /* Use the 'tabline' option if it's set. */
10352 if (*p_tal != NUL)
10353 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010354 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010355
10356 /* Check for an error. If there is one we would loop in redrawing the
10357 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010358 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010359 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010360 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010361 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010362 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010363 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010364 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010365 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010366#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010367 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010368 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010369 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010370
Bram Moolenaar238a5642006-02-21 22:12:05 +000010371 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10372 if (tabwidth < 6)
10373 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010374
Bram Moolenaar238a5642006-02-21 22:12:05 +000010375 attr = attr_nosel;
10376 tabcount = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010377 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10378 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010379 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010380 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010381
Bram Moolenaar238a5642006-02-21 22:12:05 +000010382 if (tp->tp_topframe == topframe)
10383 attr = attr_sel;
10384 if (use_sep_chars && col > 0)
10385 screen_putchar('|', 0, col++, attr);
10386
10387 if (tp->tp_topframe != topframe)
10388 attr = attr_nosel;
10389
10390 screen_putchar(' ', 0, col++, attr);
10391
10392 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010393 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010394 cwp = curwin;
10395 wp = firstwin;
10396 }
10397 else
10398 {
10399 cwp = tp->tp_curwin;
10400 wp = tp->tp_firstwin;
10401 }
10402
10403 modified = FALSE;
10404 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10405 if (bufIsChanged(wp->w_buffer))
10406 modified = TRUE;
10407 if (modified || wincount > 1)
10408 {
10409 if (wincount > 1)
10410 {
10411 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010412 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010413 if (col + len >= Columns - 3)
10414 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010415 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010416#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010417 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010418#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010419 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010420#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010421 );
10422 col += len;
10423 }
10424 if (modified)
10425 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10426 screen_putchar(' ', 0, col++, attr);
10427 }
10428
10429 room = scol - col + tabwidth - 1;
10430 if (room > 0)
10431 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010432 /* Get buffer name in NameBuff[] */
10433 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010434 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010435 len = vim_strsize(NameBuff);
10436 p = NameBuff;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010437 if (has_mbyte)
10438 while (len > room)
10439 {
10440 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010441 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010442 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010010443 else if (len > room)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010444 {
10445 p += len - room;
10446 len = room;
10447 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010448 if (len > Columns - col - 1)
10449 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010450
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010451 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010452 col += len;
10453 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010454 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010455
10456 /* Store the tab page number in TabPageIdxs[], so that
10457 * jump_to_mouse() knows where each one is. */
10458 ++tabcount;
10459 while (scol < col)
10460 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010461 }
10462
Bram Moolenaar238a5642006-02-21 22:12:05 +000010463 if (use_sep_chars)
10464 c = '_';
10465 else
10466 c = ' ';
10467 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010468
10469 /* Put an "X" for closing the current tab if there are several. */
10470 if (first_tabpage->tp_next != NULL)
10471 {
10472 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10473 TabPageIdxs[Columns - 1] = -999;
10474 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010475 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010476
10477 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10478 * set. */
10479 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010480}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010481
10482/*
10483 * Get buffer name for "buf" into NameBuff[].
10484 * Takes care of special buffer names and translates special characters.
10485 */
10486 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010487get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010488{
10489 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010490 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010491 else
10492 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10493 trans_characters(NameBuff, MAXPATHL);
10494}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010495
Bram Moolenaar071d4272004-06-13 20:20:40 +000010496/*
10497 * Get the character to use in a status line. Get its attributes in "*attr".
10498 */
10499 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010500fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010501{
10502 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010503
10504#ifdef FEAT_TERMINAL
10505 if (bt_terminal(wp->w_buffer))
10506 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010507 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010508 {
10509 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010510 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010511 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010512 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010513 {
10514 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010515 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010516 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010517 }
10518 else
10519#endif
10520 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010521 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010522 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010523 fill = fill_stl;
10524 }
10525 else
10526 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010527 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010528 fill = fill_stlnc;
10529 }
10530 /* Use fill when there is highlighting, and highlighting of current
10531 * window differs, or the fillchars differ, or this is not the
10532 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010533 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010534 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010535 || (fill_stl != fill_stlnc)))
10536 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010537 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010538 return '^';
10539 return '=';
10540}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010541
Bram Moolenaar071d4272004-06-13 20:20:40 +000010542/*
10543 * Get the character to use in a separator between vertically split windows.
10544 * Get its attributes in "*attr".
10545 */
10546 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010547fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010548{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010549 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010550 if (*attr == 0 && fill_vert == ' ')
10551 return '|';
10552 else
10553 return fill_vert;
10554}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010555
10556/*
10557 * Return TRUE if redrawing should currently be done.
10558 */
10559 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010560redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010561{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010562#ifdef FEAT_EVAL
10563 if (disable_redraw_for_testing)
10564 return 0;
10565 else
10566#endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +020010567 return ((!RedrawingDisabled
10568#ifdef FEAT_EVAL
10569 || ignore_redraw_flag_for_testing
10570#endif
10571 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010572}
10573
10574/*
10575 * Return TRUE if printing messages should currently be done.
10576 */
10577 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010578messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010579{
10580 return (!(p_lz && char_avail() && !KeyTyped));
10581}
10582
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010583#ifdef FEAT_MENU
10584/*
10585 * Draw the window toolbar.
10586 */
10587 static void
10588redraw_win_toolbar(win_T *wp)
10589{
10590 vimmenu_T *menu;
10591 int item_idx = 0;
10592 int item_count = 0;
10593 int col = 0;
10594 int next_col;
10595 int off = (int)(current_ScreenLine - ScreenLines);
10596 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
10597 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
10598
10599 vim_free(wp->w_winbar_items);
10600 for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
10601 ++item_count;
Bram Moolenaarc799fe22019-05-28 23:08:19 +020010602 wp->w_winbar_items = ALLOC_CLEAR_MULT(winbar_item_T, item_count + 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010603
10604 /* TODO: use fewer spaces if there is not enough room */
10605 for (menu = wp->w_winbar->children;
Bram Moolenaar02631462017-09-22 15:20:32 +020010606 menu != NULL && col < wp->w_width; menu = menu->next)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010607 {
10608 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010609 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010610 break;
10611 if (col > 1)
10612 {
10613 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010614 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010615 break;
10616 }
10617
10618 wp->w_winbar_items[item_idx].wb_startcol = col;
10619 space_to_screenline(off + col, button_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010620 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010621 break;
10622
10623 next_col = text_to_screenline(wp, menu->name, col);
10624 while (col < next_col)
10625 {
10626 ScreenAttrs[off + col] = button_attr;
10627 ++col;
10628 }
10629 wp->w_winbar_items[item_idx].wb_endcol = col;
10630 wp->w_winbar_items[item_idx].wb_menu = menu;
10631 ++item_idx;
10632
Bram Moolenaar02631462017-09-22 15:20:32 +020010633 if (col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010634 break;
10635 space_to_screenline(off + col, button_attr);
10636 ++col;
10637 }
Bram Moolenaar02631462017-09-22 15:20:32 +020010638 while (col < wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010639 {
10640 space_to_screenline(off + col, fill_attr);
10641 ++col;
10642 }
10643 wp->w_winbar_items[item_idx].wb_menu = NULL; /* end marker */
10644
Bram Moolenaar02631462017-09-22 15:20:32 +020010645 screen_line(wp->w_winrow, wp->w_wincol, (int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +020010646 (int)wp->w_width, 0);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010647}
10648#endif
Bram Moolenaar491ac282018-06-17 14:47:55 +020010649
Bram Moolenaar071d4272004-06-13 20:20:40 +000010650/*
10651 * Show current status info in ruler and various other places
10652 * If always is FALSE, only show ruler if position has changed.
10653 */
10654 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010655showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010656{
10657 if (!always && !redrawing())
10658 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010659 if (pum_visible())
10660 {
10661 /* Don't redraw right now, do it later. */
10662 curwin->w_redr_status = TRUE;
10663 return;
10664 }
Bram Moolenaar4033c552017-09-16 20:54:51 +020010665#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010666 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar362f3562009-11-03 16:20:34 +000010667 redraw_custom_statusline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010668 else
10669#endif
10670#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +020010671 win_redr_ruler(curwin, always, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010672#endif
10673
10674#ifdef FEAT_TITLE
10675 if (need_maketitle
10676# ifdef FEAT_STL_OPT
10677 || (p_icon && (stl_syntax & STL_IN_ICON))
10678 || (p_title && (stl_syntax & STL_IN_TITLE))
10679# endif
10680 )
10681 maketitle();
10682#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010683 /* Redraw the tab pages line if needed. */
10684 if (redraw_tabline)
10685 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010686}
10687
10688#ifdef FEAT_CMDL_INFO
10689 static void
Bram Moolenaar491ac282018-06-17 14:47:55 +020010690win_redr_ruler(win_T *wp, int always, int ignore_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010691{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010692#define RULER_BUF_LEN 70
10693 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010694 int row;
10695 int fillchar;
10696 int attr;
10697 int empty_line = FALSE;
10698 colnr_T virtcol;
10699 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010700 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010701 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010702 int this_ru_col;
10703 int off = 0;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +010010704 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010705
10706 /* If 'ruler' off or redrawing disabled, don't do anything */
10707 if (!p_ru)
10708 return;
10709
10710 /*
10711 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10712 * after deleting lines, before cursor.lnum is corrected.
10713 */
10714 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10715 return;
10716
Bram Moolenaare2c453d2019-08-21 14:37:09 +020010717 // Don't draw the ruler while doing insert-completion, it might overwrite
10718 // the (long) mode message.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010719 if (wp == lastwin && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010720 if (edit_submode != NULL)
10721 return;
Bram Moolenaar491ac282018-06-17 14:47:55 +020010722 // Don't draw the ruler when the popup menu is visible, it may overlap.
10723 // Except when the popup menu will be redrawn anyway.
10724 if (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010725 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010726
10727#ifdef FEAT_STL_OPT
10728 if (*p_ruf)
10729 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010730 int save_called_emsg = called_emsg;
10731
10732 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010733 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010734 if (called_emsg)
10735 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010736 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010737 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010738 return;
10739 }
10740#endif
10741
10742 /*
10743 * Check if not in Insert mode and the line is empty (will show "0-1").
10744 */
10745 if (!(State & INSERT)
10746 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10747 empty_line = TRUE;
10748
10749 /*
10750 * Only draw the ruler when something changed.
10751 */
10752 validate_virtcol_win(wp);
10753 if ( redraw_cmdline
10754 || always
10755 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10756 || wp->w_cursor.col != wp->w_ru_cursor.col
10757 || wp->w_virtcol != wp->w_ru_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +000010758 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
Bram Moolenaar071d4272004-06-13 20:20:40 +000010759 || wp->w_topline != wp->w_ru_topline
10760 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10761#ifdef FEAT_DIFF
10762 || wp->w_topfill != wp->w_ru_topfill
10763#endif
10764 || empty_line != wp->w_ru_empty)
10765 {
10766 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010767 if (wp->w_status_height)
10768 {
10769 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010770 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar53f81742017-09-22 14:35:51 +020010771 off = wp->w_wincol;
Bram Moolenaar02631462017-09-22 15:20:32 +020010772 width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010773 }
10774 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010775 {
10776 row = Rows - 1;
10777 fillchar = ' ';
10778 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010779 width = Columns;
10780 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010781 }
10782
10783 /* In list mode virtcol needs to be recomputed */
10784 virtcol = wp->w_virtcol;
10785 if (wp->w_p_list && lcs_tab1 == NUL)
10786 {
10787 wp->w_p_list = FALSE;
10788 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10789 wp->w_p_list = TRUE;
10790 }
10791
10792 /*
10793 * Some sprintfs return the length, some return a pointer.
10794 * To avoid portability problems we use strlen() here.
10795 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010796 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010797 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10798 ? 0L
10799 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010800 len = STRLEN(buffer);
10801 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010802 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10803 (int)virtcol + 1);
10804
10805 /*
10806 * Add a "50%" if there is room for it.
10807 * On the last line, don't print in the last column (scrolls the
10808 * screen up on some terminals).
10809 */
10810 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010811 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010812 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010813 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010814 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010815 this_ru_col = ru_col - (Columns - width);
10816 if (this_ru_col < 0)
10817 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010818 /* Never use more than half the window/screen width, leave the other
10819 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010820 if (this_ru_col < (width + 1) / 2)
10821 this_ru_col = (width + 1) / 2;
10822 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010823 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010010824 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010825 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010826 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010827 if (has_mbyte)
10828 i += (*mb_char2bytes)(fillchar, buffer + i);
10829 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010830 buffer[i++] = fillchar;
10831 ++o;
10832 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010833 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010834 }
10835 /* Truncate at window boundary. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010836 if (has_mbyte)
10837 {
10838 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010839 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010840 {
10841 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010842 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010843 {
10844 buffer[i] = NUL;
10845 break;
10846 }
10847 }
10848 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010010849 else if (this_ru_col + (int)STRLEN(buffer) > width)
Bram Moolenaar4033c552017-09-16 20:54:51 +020010850 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010851
Bram Moolenaar4033c552017-09-16 20:54:51 +020010852 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010853 i = redraw_cmdline;
10854 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020010855 this_ru_col + off + (int)STRLEN(buffer),
10856 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010857 fillchar, fillchar, attr);
10858 /* don't redraw the cmdline because of showing the ruler */
10859 redraw_cmdline = i;
10860 wp->w_ru_cursor = wp->w_cursor;
10861 wp->w_ru_virtcol = wp->w_virtcol;
10862 wp->w_ru_empty = empty_line;
10863 wp->w_ru_topline = wp->w_topline;
10864 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10865#ifdef FEAT_DIFF
10866 wp->w_ru_topfill = wp->w_topfill;
10867#endif
10868 }
10869}
10870#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010871
Bram Moolenaare677df82019-09-02 22:31:11 +020010872/*
10873 * Compute columns for ruler and shown command. 'sc_col' is also used to
10874 * decide what the maximum length of a message on the status line can be.
10875 * If there is a status line for the last window, 'sc_col' is independent
10876 * of 'ru_col'.
10877 */
10878
10879#define COL_RULER 17 // columns needed by standard ruler
10880
10881 void
10882comp_col(void)
10883{
10884#if defined(FEAT_CMDL_INFO)
10885 int last_has_status = (p_ls == 2 || (p_ls == 1 && !ONE_WINDOW));
10886
10887 sc_col = 0;
10888 ru_col = 0;
10889 if (p_ru)
10890 {
10891# ifdef FEAT_STL_OPT
10892 ru_col = (ru_wid ? ru_wid : COL_RULER) + 1;
10893# else
10894 ru_col = COL_RULER + 1;
10895# endif
10896 // no last status line, adjust sc_col
10897 if (!last_has_status)
10898 sc_col = ru_col;
10899 }
10900 if (p_sc)
10901 {
10902 sc_col += SHOWCMD_COLS;
10903 if (!p_ru || last_has_status) // no need for separating space
10904 ++sc_col;
10905 }
10906 sc_col = Columns - sc_col;
10907 ru_col = Columns - ru_col;
10908 if (sc_col <= 0) // screen too narrow, will become a mess
10909 sc_col = 1;
10910 if (ru_col <= 0)
10911 ru_col = 1;
10912#else
10913 sc_col = Columns;
10914 ru_col = Columns;
10915#endif
10916#ifdef FEAT_EVAL
10917 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
10918#endif
10919}
10920
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010921#if defined(FEAT_LINEBREAK) || defined(PROTO)
10922/*
Bram Moolenaar64486672010-05-16 15:46:46 +020010923 * Return the width of the 'number' and 'relativenumber' column.
10924 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010925 * Otherwise it depends on 'numberwidth' and the line count.
10926 */
10927 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010928number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010929{
10930 int n;
10931 linenr_T lnum;
10932
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020010933 if (wp->w_p_rnu && !wp->w_p_nu)
10934 /* cursor line shows "0" */
10935 lnum = wp->w_height;
10936 else
10937 /* cursor line shows absolute line number */
10938 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020010939
Bram Moolenaar6b314672015-03-20 15:42:10 +010010940 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010941 return wp->w_nrwidth_width;
10942 wp->w_nrwidth_line_count = lnum;
10943
10944 n = 0;
10945 do
10946 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010947 lnum /= 10;
10948 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010949 } while (lnum > 0);
10950
10951 /* 'numberwidth' gives the minimal width plus one */
10952 if (n < wp->w_p_nuw - 1)
10953 n = wp->w_p_nuw - 1;
10954
Bram Moolenaare4b407f2019-07-04 11:59:28 +020010955# ifdef FEAT_SIGNS
10956 // If 'signcolumn' is set to 'number' and there is a sign to display, then
10957 // the minimal width for the number column is 2.
10958 if (n < 2 && (wp->w_buffer->b_signlist != NULL)
10959 && (*wp->w_p_scl == 'n' && *(wp->w_p_scl + 1) == 'u'))
10960 n = 2;
10961# endif
10962
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010963 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010010964 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010965 return n;
10966}
10967#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010968
Bram Moolenaar113e1072019-01-20 15:30:40 +010010969#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010970/*
10971 * Return the current cursor column. This is the actual position on the
10972 * screen. First column is 0.
10973 */
10974 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010975screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010976{
10977 return screen_cur_col;
10978}
10979
10980/*
10981 * Return the current cursor row. This is the actual position on the screen.
10982 * First row is 0.
10983 */
10984 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010985screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010986{
10987 return screen_cur_row;
10988}
Bram Moolenaar113e1072019-01-20 15:30:40 +010010989#endif
Bram Moolenaare677df82019-09-02 22:31:11 +020010990
10991/*
10992 * Handle setting 'listchars' or 'fillchars'.
10993 * Returns error message, NULL if it's OK.
10994 */
10995 char *
10996set_chars_option(char_u **varp)
10997{
10998 int round, i, len, entries;
10999 char_u *p, *s;
11000 int c1 = 0, c2 = 0, c3 = 0;
11001 struct charstab
11002 {
11003 int *cp;
11004 char *name;
11005 };
11006 static struct charstab filltab[] =
11007 {
11008 {&fill_stl, "stl"},
11009 {&fill_stlnc, "stlnc"},
11010 {&fill_vert, "vert"},
11011 {&fill_fold, "fold"},
11012 {&fill_diff, "diff"},
11013 };
11014 static struct charstab lcstab[] =
11015 {
11016 {&lcs_eol, "eol"},
11017 {&lcs_ext, "extends"},
11018 {&lcs_nbsp, "nbsp"},
11019 {&lcs_prec, "precedes"},
11020 {&lcs_space, "space"},
11021 {&lcs_tab2, "tab"},
11022 {&lcs_trail, "trail"},
11023#ifdef FEAT_CONCEAL
11024 {&lcs_conceal, "conceal"},
11025#else
11026 {NULL, "conceal"},
11027#endif
11028 };
11029 struct charstab *tab;
11030
11031 if (varp == &p_lcs)
11032 {
11033 tab = lcstab;
11034 entries = sizeof(lcstab) / sizeof(struct charstab);
11035 }
11036 else
11037 {
11038 tab = filltab;
11039 entries = sizeof(filltab) / sizeof(struct charstab);
11040 }
11041
11042 // first round: check for valid value, second round: assign values
11043 for (round = 0; round <= 1; ++round)
11044 {
11045 if (round > 0)
11046 {
11047 // After checking that the value is valid: set defaults: space for
11048 // 'fillchars', NUL for 'listchars'
11049 for (i = 0; i < entries; ++i)
11050 if (tab[i].cp != NULL)
11051 *(tab[i].cp) = (varp == &p_lcs ? NUL : ' ');
11052
11053 if (varp == &p_lcs)
11054 {
11055 lcs_tab1 = NUL;
11056 lcs_tab3 = NUL;
11057 }
11058 else
11059 fill_diff = '-';
11060 }
11061 p = *varp;
11062 while (*p)
11063 {
11064 for (i = 0; i < entries; ++i)
11065 {
11066 len = (int)STRLEN(tab[i].name);
11067 if (STRNCMP(p, tab[i].name, len) == 0
11068 && p[len] == ':'
11069 && p[len + 1] != NUL)
11070 {
11071 c2 = c3 = 0;
11072 s = p + len + 1;
11073 c1 = mb_ptr2char_adv(&s);
11074 if (mb_char2cells(c1) > 1)
11075 continue;
11076 if (tab[i].cp == &lcs_tab2)
11077 {
11078 if (*s == NUL)
11079 continue;
11080 c2 = mb_ptr2char_adv(&s);
11081 if (mb_char2cells(c2) > 1)
11082 continue;
11083 if (!(*s == ',' || *s == NUL))
11084 {
11085 c3 = mb_ptr2char_adv(&s);
11086 if (mb_char2cells(c3) > 1)
11087 continue;
11088 }
11089 }
11090
11091 if (*s == ',' || *s == NUL)
11092 {
11093 if (round)
11094 {
11095 if (tab[i].cp == &lcs_tab2)
11096 {
11097 lcs_tab1 = c1;
11098 lcs_tab2 = c2;
11099 lcs_tab3 = c3;
11100 }
11101 else if (tab[i].cp != NULL)
11102 *(tab[i].cp) = c1;
11103
11104 }
11105 p = s;
11106 break;
11107 }
11108 }
11109 }
11110
11111 if (i == entries)
11112 return e_invarg;
11113 if (*p == ',')
11114 ++p;
11115 }
11116 }
11117
11118 return NULL; // no error
11119}
Bram Moolenaar017ba072019-09-14 21:01:23 +020011120
11121#ifdef FEAT_SYN_HL
11122/*
11123 * Used when 'cursorlineopt' contains "screenline": compute the margins between
11124 * which the highlighting is used.
11125 */
11126 static void
11127margin_columns_win(win_T *wp, int *left_col, int *right_col)
11128{
11129 // cache previous calculations depending on w_virtcol
11130 static int saved_w_virtcol;
11131 static win_T *prev_wp;
11132 static int prev_left_col;
11133 static int prev_right_col;
11134 static int prev_col_off;
11135
11136 int cur_col_off = win_col_off(wp);
11137 int width1;
11138 int width2;
11139
11140 if (saved_w_virtcol == wp->w_virtcol
11141 && prev_wp == wp && prev_col_off == cur_col_off)
11142 {
11143 *right_col = prev_right_col;
11144 *left_col = prev_left_col;
11145 return;
11146 }
11147
11148 width1 = wp->w_width - cur_col_off;
11149 width2 = width1 + win_col_off2(wp);
11150
11151 *left_col = 0;
11152 *right_col = width1;
11153
11154 if (wp->w_virtcol >= (colnr_T)width1)
11155 *right_col = width1 + ((wp->w_virtcol - width1) / width2 + 1) * width2;
11156 if (wp->w_virtcol >= (colnr_T)width1 && width2 > 0)
11157 *left_col = (wp->w_virtcol - width1) / width2 * width2 + width1;
11158
11159 // cache values
11160 prev_left_col = *left_col;
11161 prev_right_col = *right_col;
11162 prev_wp = wp;
11163 saved_w_virtcol = wp->w_virtcol;
11164 prev_col_off = cur_col_off;
11165}
11166#endif