blob: fec1081c261c69b022162c21606d8d107bc40bc8 [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 {
5386 if (line_attr)
5387 char_attr = hl_combine_attr(extra_attr, line_attr);
5388 else
5389 char_attr = extra_attr;
5390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391
Bram Moolenaar81695252004-12-29 20:58:21 +00005392#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 /* XIM don't send preedit_start and preedit_end, but they send
5394 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5395 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005396 if (p_imst == IM_ON_THE_SPOT
5397 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005398 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399 && (State & INSERT)
5400 && !p_imdisable
5401 && im_is_preediting()
5402 && draw_state == WL_LINE)
5403 {
5404 colnr_T tcol;
5405
5406 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005407 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 else
5409 tcol = preedit_end_col;
5410 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5411 {
5412 if (feedback_old_attr < 0)
5413 {
5414 feedback_col = 0;
5415 feedback_old_attr = char_attr;
5416 }
5417 char_attr = im_get_feedback_attr(feedback_col);
5418 if (char_attr < 0)
5419 char_attr = feedback_old_attr;
5420 feedback_col++;
5421 }
5422 else if (feedback_old_attr >= 0)
5423 {
5424 char_attr = feedback_old_attr;
5425 feedback_old_attr = -1;
5426 feedback_col = 0;
5427 }
5428 }
5429#endif
5430 /*
5431 * Handle the case where we are in column 0 but not on the first
5432 * character of the line and the user wants us to show us a
5433 * special character (via 'listchars' option "precedes:<char>".
5434 */
5435 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005436 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5438#ifdef FEAT_DIFF
5439 && filler_todo <= 0
5440#endif
5441 && draw_state > WL_NR
5442 && c != NUL)
5443 {
5444 c = lcs_prec;
5445 lcs_prec_todo = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005446 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5447 {
5448 /* Double-width character being overwritten by the "precedes"
5449 * character, need to fill up half the character. */
5450 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005451 c_final = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005452 n_extra = 1;
5453 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005454 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005457 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 {
5459 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005460 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005461 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 }
5463 else
5464 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005465 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 {
5467 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005468 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 n_attr3 = 1;
5470 }
5471 }
5472
5473 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005474 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 */
Bram Moolenaarf914a332019-07-20 15:09:56 +02005476 if ((c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005477#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005478 || did_line_attr == 1
5479#endif
Bram Moolenaarf914a332019-07-20 15:09:56 +02005480 ) && eol_hl_off == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005482#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarbbca7732019-07-24 18:13:16 +02005483 // flag to indicate whether prevcol equals startcol of search_hl or
5484 // one of the matches
5485 int prevcol_hl_flag = get_prevcol_hl_flag(wp, &search_hl,
5486 (long)(ptr - line) - (c == NUL));
Bram Moolenaar91170f82006-05-05 21:15:17 +00005487#endif
Bram Moolenaarbbca7732019-07-24 18:13:16 +02005488 // Invert at least one char, used for Visual and empty line or
5489 // highlight match at end of line. If it's beyond the last
5490 // char on the screen, just overwrite that one (tricky!) Not
5491 // needed when a '$' was displayed for 'list'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005493 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005494 && (VIsual_mode != Ctrl_V
5495 || lnum == VIsual.lnum
5496 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005497 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarbbca7732019-07-24 18:13:16 +02005499 // highlight 'hlsearch' match at end of line
5500 || (prevcol_hl_flag
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005501# ifdef FEAT_SYN_HL
5502 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
5503 && !(wp == curwin && VIsual_active))
5504# endif
5505# ifdef FEAT_DIFF
5506 && diff_hlf == (hlf_T)0
5507# endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005508# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005509 && did_line_attr <= 1
5510# endif
5511 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512#endif
5513 ))
5514 {
5515 int n = 0;
5516
5517#ifdef FEAT_RIGHTLEFT
5518 if (wp->w_p_rl)
5519 {
5520 if (col < 0)
5521 n = 1;
5522 }
5523 else
5524#endif
5525 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005526 if (col >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 n = -1;
5528 }
5529 if (n != 0)
5530 {
5531 /* At the window boundary, highlight the last character
5532 * instead (better than nothing). */
5533 off += n;
5534 col += n;
5535 }
5536 else
5537 {
5538 /* Add a blank character to highlight. */
5539 ScreenLines[off] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540 if (enc_utf8)
5541 ScreenLinesUC[off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 }
5543#ifdef FEAT_SEARCH_EXTRA
5544 if (area_attr == 0)
5545 {
Bram Moolenaarbbca7732019-07-24 18:13:16 +02005546 // Use attributes from match with highest priority among
5547 // 'search_hl' and the match list.
5548 get_search_match_hl(wp, &search_hl,
5549 (long)(ptr - line), &char_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 }
5551#endif
5552 ScreenAttrs[off] = char_attr;
5553#ifdef FEAT_RIGHTLEFT
5554 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005555 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005557 --off;
5558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 else
5560#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005561 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005563 ++off;
5564 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005565 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005566 eol_hl_off = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569
Bram Moolenaar91170f82006-05-05 21:15:17 +00005570 /*
5571 * At end of the text line.
5572 */
5573 if (c == NUL)
5574 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005575#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005576 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005577 if (wp->w_p_wrap)
5578 v = wp->w_skipcol;
5579 else
5580 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005581
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005582 /* check if line ends before left margin */
5583 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005584 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005585#ifdef FEAT_CONCEAL
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01005586 // Get rid of the boguscols now, we want to draw until the right
5587 // edge for 'cursorcolumn'.
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005588 col -= boguscols;
5589 boguscols = 0;
5590#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005591
5592 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005593 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005594
5595 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005596 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5597 && (int)wp->w_virtcol <
Bram Moolenaar02631462017-09-22 15:20:32 +02005598 wp->w_width * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005599 && lnum != wp->w_cursor.lnum)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005600 || draw_color_col
5601 || win_attr != 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005602# ifdef FEAT_RIGHTLEFT
5603 && !wp->w_p_rl
5604# endif
5605 )
5606 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005607 int rightmost_vcol = 0;
5608 int i;
5609
5610 if (wp->w_p_cuc)
5611 rightmost_vcol = wp->w_virtcol;
5612 if (draw_color_col)
5613 /* determine rightmost colorcolumn to possibly draw */
5614 for (i = 0; color_cols[i] >= 0; ++i)
5615 if (rightmost_vcol < color_cols[i])
5616 rightmost_vcol = color_cols[i];
5617
Bram Moolenaar02631462017-09-22 15:20:32 +02005618 while (col < wp->w_width)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005619 {
5620 ScreenLines[off] = ' ';
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005621 if (enc_utf8)
5622 ScreenLinesUC[off] = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005623 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005624 if (draw_color_col)
5625 draw_color_col = advance_color_col(VCOL_HLC,
5626 &color_cols);
5627
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005628 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005629 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005630 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005631 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005632 else
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005633 ScreenAttrs[off++] = win_attr;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005634
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005635 if (VCOL_HLC >= rightmost_vcol && win_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005636 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005637
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005638 ++vcol;
5639 }
5640 }
5641#endif
5642
Bram Moolenaar53f81742017-09-22 14:35:51 +02005643 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005644 (int)wp->w_width, screen_line_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 row++;
5646
5647 /*
5648 * Update w_cline_height and w_cline_folded if the cursor line was
5649 * updated (saves a call to plines() later).
5650 */
5651 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5652 {
5653 curwin->w_cline_row = startrow;
5654 curwin->w_cline_height = row - startrow;
5655#ifdef FEAT_FOLDING
5656 curwin->w_cline_folded = FALSE;
5657#endif
5658 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5659 }
5660
5661 break;
5662 }
5663
Bram Moolenaara5c6a0b2019-05-08 20:20:46 +02005664 // Show "extends" character from 'listchars' if beyond the line end and
5665 // 'list' is set.
5666 if (lcs_ext != NUL
5667 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 && !wp->w_p_wrap
5669#ifdef FEAT_DIFF
5670 && filler_todo <= 0
5671#endif
5672 && (
5673#ifdef FEAT_RIGHTLEFT
5674 wp->w_p_rl ? col == 0 :
5675#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005676 col == wp->w_width - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005678 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5680 {
5681 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005682 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005684 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 {
5686 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005687 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005688 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 }
5690 else
5691 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 }
5693
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005694#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005695 /* advance to the next 'colorcolumn' */
5696 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005697 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005698
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005699 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005700 * highlight the cursor position itself.
5701 * Also highlight the 'colorcolumn' if it is different than
5702 * 'cursorcolumn' */
5703 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005704 if (draw_state == WL_LINE && !lnum_in_visual_area
5705 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005706 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005707 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005708 && lnum != wp->w_cursor.lnum)
5709 {
5710 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005711 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005712 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005713 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005714 {
5715 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005716 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005717 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005718 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005719#endif
5720
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 /*
5722 * Store character to be displayed.
5723 * Skip characters that are left of the screen for 'nowrap'.
5724 */
5725 vcol_prev = vcol;
5726 if (draw_state < WL_LINE || n_skip <= 0)
5727 {
5728 /*
5729 * Store the character.
5730 */
Bram Moolenaara12a1612019-01-24 16:39:02 +01005731#if defined(FEAT_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5733 {
5734 /* A double-wide character is: put first halve in left cell. */
5735 --off;
5736 --col;
5737 }
5738#endif
5739 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005741 {
5742 if ((mb_c & 0xff00) == 0x8e00)
5743 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 else if (enc_utf8)
5747 {
5748 if (mb_utf8)
5749 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005750 int i;
5751
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005753 if ((c & 0xff) == 0)
5754 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005755 for (i = 0; i < Screen_mco; ++i)
5756 {
5757 ScreenLinesC[i][off] = u8cc[i];
5758 if (u8cc[i] == 0)
5759 break;
5760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 }
5762 else
5763 ScreenLinesUC[off] = 0;
5764 }
5765 if (multi_attr)
5766 {
5767 ScreenAttrs[off] = multi_attr;
5768 multi_attr = 0;
5769 }
5770 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 ScreenAttrs[off] = char_attr;
5772
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5774 {
5775 /* Need to fill two screen columns. */
5776 ++off;
5777 ++col;
5778 if (enc_utf8)
5779 /* UTF-8: Put a 0 in the second screen char. */
5780 ScreenLines[off] = 0;
5781 else
5782 /* DBCS: Put second byte in the second screen char. */
5783 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005784 if (draw_state > WL_NR
5785#ifdef FEAT_DIFF
5786 && filler_todo <= 0
5787#endif
5788 )
5789 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 /* When "tocol" is halfway a character, set it to the end of
5791 * the character, otherwise highlighting won't stop. */
5792 if (tocol == vcol)
5793 ++tocol;
5794#ifdef FEAT_RIGHTLEFT
5795 if (wp->w_p_rl)
5796 {
5797 /* now it's time to backup one cell */
5798 --off;
5799 --col;
5800 }
5801#endif
5802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803#ifdef FEAT_RIGHTLEFT
5804 if (wp->w_p_rl)
5805 {
5806 --off;
5807 --col;
5808 }
5809 else
5810#endif
5811 {
5812 ++off;
5813 ++col;
5814 }
5815 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005816#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005817 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005818 {
5819 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005820 ++vcol_off;
5821 if (n_extra > 0)
5822 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005823 if (wp->w_p_wrap)
5824 {
5825 /*
5826 * Special voodoo required if 'wrap' is on.
5827 *
5828 * Advance the column indicator to force the line
5829 * drawing to wrap early. This will make the line
5830 * take up the same screen space when parts are concealed,
5831 * so that cursor line computations aren't messed up.
5832 *
5833 * To avoid the fictitious advance of 'col' causing
5834 * trailing junk to be written out of the screen line
5835 * we are building, 'boguscols' keeps track of the number
5836 * of bad columns we have advanced.
5837 */
5838 if (n_extra > 0)
5839 {
5840 vcol += n_extra;
5841# ifdef FEAT_RIGHTLEFT
5842 if (wp->w_p_rl)
5843 {
5844 col -= n_extra;
5845 boguscols -= n_extra;
5846 }
5847 else
5848# endif
5849 {
5850 col += n_extra;
5851 boguscols += n_extra;
5852 }
5853 n_extra = 0;
5854 n_attr = 0;
5855 }
5856
5857
Bram Moolenaar860cae12010-06-05 23:22:07 +02005858 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5859 {
5860 /* Need to fill two screen columns. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01005861# ifdef FEAT_RIGHTLEFT
Bram Moolenaar860cae12010-06-05 23:22:07 +02005862 if (wp->w_p_rl)
5863 {
5864 --boguscols;
5865 --col;
5866 }
5867 else
Bram Moolenaara12a1612019-01-24 16:39:02 +01005868# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02005869 {
5870 ++boguscols;
5871 ++col;
5872 }
5873 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005874
5875# ifdef FEAT_RIGHTLEFT
5876 if (wp->w_p_rl)
5877 {
5878 --boguscols;
5879 --col;
5880 }
5881 else
5882# endif
5883 {
5884 ++boguscols;
5885 ++col;
5886 }
5887 }
5888 else
5889 {
5890 if (n_extra > 0)
5891 {
5892 vcol += n_extra;
5893 n_extra = 0;
5894 n_attr = 0;
5895 }
5896 }
5897
5898 }
5899#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900 else
5901 --n_skip;
5902
Bram Moolenaar64486672010-05-16 15:46:46 +02005903 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5904 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005905 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906#ifdef FEAT_DIFF
5907 && filler_todo <= 0
5908#endif
5909 )
5910 ++vcol;
5911
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005912#ifdef FEAT_SYN_HL
5913 if (vcol_save_attr >= 0)
5914 char_attr = vcol_save_attr;
5915#endif
5916
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917 /* restore attributes after "predeces" in 'listchars' */
5918 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5919 char_attr = saved_attr3;
5920
5921 /* restore attributes after last 'listchars' or 'number' char */
5922 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5923 char_attr = saved_attr2;
5924
5925 /*
5926 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005927 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 */
5929 if ((
5930#ifdef FEAT_RIGHTLEFT
5931 wp->w_p_rl ? (col < 0) :
5932#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005933 (col >= wp->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 && (*ptr != NUL
5935#ifdef FEAT_DIFF
5936 || filler_todo > 0
5937#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005938 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5940 )
5941 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005942#ifdef FEAT_CONCEAL
Bram Moolenaar53f81742017-09-22 14:35:51 +02005943 screen_line(screen_row, wp->w_wincol, col - boguscols,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005944 (int)wp->w_width, screen_line_flags);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005945 boguscols = 0;
5946#else
Bram Moolenaar53f81742017-09-22 14:35:51 +02005947 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005948 (int)wp->w_width, screen_line_flags);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005949#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950 ++row;
5951 ++screen_row;
5952
5953 /* When not wrapping and finished diff lines, or when displayed
5954 * '$' and highlighting until last column, break here. */
5955 if ((!wp->w_p_wrap
5956#ifdef FEAT_DIFF
5957 && filler_todo <= 0
5958#endif
5959 ) || lcs_eol_one == -1)
5960 break;
5961
5962 /* When the window is too narrow draw all "@" lines. */
5963 if (draw_state != WL_LINE
5964#ifdef FEAT_DIFF
5965 && filler_todo <= 0
5966#endif
5967 )
5968 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01005969 win_draw_end(wp, '@', ' ', TRUE, row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971 row = endrow;
5972 }
5973
5974 /* When line got too long for screen break here. */
5975 if (row == endrow)
5976 {
5977 ++row;
5978 break;
5979 }
5980
5981 if (screen_cur_row == screen_row - 1
5982#ifdef FEAT_DIFF
5983 && filler_todo <= 0
5984#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005985 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 {
5987 /* Remember that the line wraps, used for modeless copy. */
5988 LineWraps[screen_row - 1] = TRUE;
5989
5990 /*
5991 * Special trick to make copy/paste of wrapped lines work with
5992 * xterm/screen: write an extra character beyond the end of
5993 * the line. This will work with all terminal types
5994 * (regardless of the xn,am settings).
5995 * Only do this on a fast tty.
5996 * Only do this if the cursor is on the current line
5997 * (something has been written in it).
5998 * Don't do this for the GUI.
5999 * Don't do this for double-width characters.
6000 * Don't do this for a window not at the right screen border.
6001 */
6002 if (p_tf
6003#ifdef FEAT_GUI
6004 && !gui.in_use
6005#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00006007 && ((*mb_off2cells)(LineOffset[screen_row],
6008 LineOffset[screen_row] + screen_Columns)
6009 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00006011 + (int)Columns - 2,
6012 LineOffset[screen_row] + screen_Columns)
Bram Moolenaara12a1612019-01-24 16:39:02 +01006013 == 2)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014 {
6015 /* First make sure we are at the end of the screen line,
6016 * then output the same character again to let the
6017 * terminal know about the wrap. If the terminal doesn't
6018 * auto-wrap, we overwrite the character. */
Bram Moolenaar02631462017-09-22 15:20:32 +02006019 if (screen_cur_col != wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020 screen_char(LineOffset[screen_row - 1]
6021 + (unsigned)Columns - 1,
6022 screen_row - 1, (int)(Columns - 1));
6023
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024 /* When there is a multi-byte character, just output a
6025 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00006026 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
6027 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028 out_char(' ');
6029 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030 out_char(ScreenLines[LineOffset[screen_row - 1]
6031 + (Columns - 1)]);
6032 /* force a redraw of the first char on the next line */
6033 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
6034 screen_start(); /* don't know where cursor is now */
6035 }
6036 }
6037
6038 col = 0;
6039 off = (unsigned)(current_ScreenLine - ScreenLines);
6040#ifdef FEAT_RIGHTLEFT
6041 if (wp->w_p_rl)
6042 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006043 col = wp->w_width - 1; /* col is not used if breaking! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044 off += col;
6045 }
6046#endif
6047
6048 /* reset the drawing state for the start of a wrapped line */
6049 draw_state = WL_START;
6050 saved_n_extra = n_extra;
6051 saved_p_extra = p_extra;
6052 saved_c_extra = c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01006053 saved_c_final = c_final;
Bram Moolenaar017ba072019-09-14 21:01:23 +02006054#ifdef FEAT_SYN_HL
6055 if (!(cul_screenline
6056# ifdef FEAT_DIFF
6057 && diff_hlf == (hlf_T)0)
6058# endif
6059 )
6060 saved_char_attr = char_attr;
6061 else
6062#endif
6063 saved_char_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 n_extra = 0;
6065 lcs_prec_todo = lcs_prec;
6066#ifdef FEAT_LINEBREAK
6067# ifdef FEAT_DIFF
6068 if (filler_todo <= 0)
6069# endif
6070 need_showbreak = TRUE;
6071#endif
6072#ifdef FEAT_DIFF
6073 --filler_todo;
6074 /* When the filler lines are actually below the last line of the
6075 * file, don't draw the line itself, break here. */
6076 if (filler_todo == 0 && wp->w_botfill)
6077 break;
6078#endif
6079 }
6080
6081 } /* for every character in the line */
6082
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006083#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006084 /* After an empty line check first word for capital. */
6085 if (*skipwhite(line) == NUL)
6086 {
6087 capcol_lnum = lnum + 1;
6088 cap_col = 0;
6089 }
6090#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006091#ifdef FEAT_TEXT_PROP
6092 vim_free(text_props);
6093 vim_free(text_prop_idxs);
6094#endif
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006095
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01006096 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097 return row;
6098}
6099
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006100/*
6101 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006102 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006103 */
6104 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006105comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006106{
6107 int i;
6108
6109 for (i = 0; i < Screen_mco; ++i)
6110 {
6111 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
6112 return TRUE;
6113 if (ScreenLinesC[i][off_from] == 0)
6114 break;
6115 }
6116 return FALSE;
6117}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006118
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119/*
6120 * Check whether the given character needs redrawing:
6121 * - the (first byte of the) character is different
6122 * - the attributes are different
6123 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006124 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 */
6126 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006127char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128{
6129 if (cols > 0
6130 && ((ScreenLines[off_from] != ScreenLines[off_to]
6131 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132 || (enc_dbcs != 0
6133 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
6134 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
6135 ? ScreenLines2[off_from] != ScreenLines2[off_to]
6136 : (cols > 1 && ScreenLines[off_from + 1]
6137 != ScreenLines[off_to + 1])))
6138 || (enc_utf8
6139 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6140 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006141 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006142 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6143 && ScreenLines[off_from + 1]
Bram Moolenaara12a1612019-01-24 16:39:02 +01006144 != ScreenLines[off_to + 1])))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 return TRUE;
6146 return FALSE;
6147}
6148
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006149#if defined(FEAT_TERMINAL) || defined(PROTO)
6150/*
6151 * Return the index in ScreenLines[] for the current screen line.
6152 */
6153 int
6154screen_get_current_line_off()
6155{
6156 return (int)(current_ScreenLine - ScreenLines);
6157}
6158#endif
6159
Bram Moolenaarc662ec92019-06-23 00:15:57 +02006160#ifdef FEAT_TEXT_PROP
6161/*
6162 * Return TRUE if this position has a higher level popup or this cell is
6163 * transparent in the current popup.
6164 */
6165 static int
6166blocked_by_popup(int row, int col)
6167{
6168 int off;
6169
6170 if (!popup_visible)
6171 return FALSE;
6172 off = row * screen_Columns + col;
6173 return popup_mask[off] > screen_zindex || popup_transparent[off];
6174}
6175#endif
6176
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177/*
6178 * Move one "cooked" screen line to the screen, but only the characters that
6179 * have actually changed. Handle insert/delete character.
6180 * "coloff" gives the first column on the screen for this line.
6181 * "endcol" gives the columns where valid characters are.
6182 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6183 * needs to be cleared, negative otherwise.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006184 * "flags" can have bits:
6185 * SLF_POPUP popup window
6186 * SLF_RIGHTLEFT rightleft window:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6188 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6189 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006190 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006191screen_line(
6192 int row,
6193 int coloff,
6194 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006195 int clear_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006196 int flags UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197{
6198 unsigned off_from;
6199 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006200 unsigned max_off_from;
6201 unsigned max_off_to;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 int force = FALSE; /* force update rest of the line */
6205 int redraw_this /* bool: does character need redraw? */
6206#ifdef FEAT_GUI
6207 = TRUE /* For GUI when while-loop empty */
6208#endif
6209 ;
6210 int redraw_next; /* redraw_this for next character */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211 int clear_next = FALSE;
6212 int char_cells; /* 1: normal char */
6213 /* 2: occupies two display cells */
6214# define CHAR_CELLS char_cells
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006216 /* Check for illegal row and col, just in case. */
6217 if (row >= Rows)
6218 row = Rows - 1;
6219 if (endcol > Columns)
6220 endcol = Columns;
6221
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222# ifdef FEAT_CLIPBOARD
6223 clip_may_clear_selection(row, row);
6224# endif
6225
6226 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6227 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006228 max_off_from = off_from + screen_Columns;
6229 max_off_to = LineOffset[row] + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230
6231#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006232 if (flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 {
6234 /* Clear rest first, because it's left of the text. */
6235 if (clear_width > 0)
6236 {
6237 while (col <= endcol && ScreenLines[off_to] == ' '
6238 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006239 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 {
6241 ++off_to;
6242 ++col;
6243 }
6244 if (col <= endcol)
6245 screen_fill(row, row + 1, col + coloff,
6246 endcol + coloff + 1, ' ', ' ', 0);
6247 }
6248 col = endcol + 1;
6249 off_to = LineOffset[row] + col + coloff;
6250 off_from += col;
6251 endcol = (clear_width > 0 ? clear_width : -clear_width);
6252 }
6253#endif /* FEAT_RIGHTLEFT */
6254
6255 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6256
6257 while (col < endcol)
6258 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006260 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261 else
6262 char_cells = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263
6264 redraw_this = redraw_next;
6265 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6266 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6267
6268#ifdef FEAT_GUI
6269 /* If the next character was bold, then redraw the current character to
6270 * remove any pixels that might have spilt over into us. This only
6271 * happens in the GUI.
6272 */
6273 if (redraw_next && gui.in_use)
6274 {
6275 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006276 if (hl > HL_ALL)
6277 hl = syn_attr2attr(hl);
6278 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 redraw_this = TRUE;
6280 }
6281#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02006282#ifdef FEAT_TEXT_PROP
Bram Moolenaarc662ec92019-06-23 00:15:57 +02006283 if (blocked_by_popup(row, col + coloff))
Bram Moolenaar33796b32019-06-08 16:01:13 +02006284 redraw_this = FALSE;
6285#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 if (redraw_this)
6287 {
6288 /*
6289 * Special handling when 'xs' termcap flag set (hpterm):
6290 * Attributes for characters are stored at the position where the
6291 * cursor is when writing the highlighting code. The
6292 * start-highlighting code must be written with the cursor on the
6293 * first highlighted character. The stop-highlighting code must
6294 * be written with the cursor just after the last highlighted
6295 * character.
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01006296 * Overwriting a character doesn't remove its highlighting. Need
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297 * to clear the rest of the line, and force redrawing it
6298 * completely.
6299 */
6300 if ( p_wiv
6301 && !force
6302#ifdef FEAT_GUI
6303 && !gui.in_use
6304#endif
6305 && ScreenAttrs[off_to] != 0
6306 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6307 {
6308 /*
6309 * Need to remove highlighting attributes here.
6310 */
6311 windgoto(row, col + coloff);
6312 out_str(T_CE); /* clear rest of this screen line */
6313 screen_start(); /* don't know where cursor is now */
6314 force = TRUE; /* force redraw of rest of the line */
6315 redraw_next = TRUE; /* or else next char would miss out */
6316
6317 /*
6318 * If the previous character was highlighted, need to stop
6319 * highlighting at this character.
6320 */
6321 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6322 {
6323 screen_attr = ScreenAttrs[off_to - 1];
6324 term_windgoto(row, col + coloff);
6325 screen_stop_highlight();
6326 }
6327 else
6328 screen_attr = 0; /* highlighting has stopped */
6329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330 if (enc_dbcs != 0)
6331 {
6332 /* Check if overwriting a double-byte with a single-byte or
6333 * the other way around requires another character to be
6334 * redrawn. For UTF-8 this isn't needed, because comparing
6335 * ScreenLinesUC[] is sufficient. */
6336 if (char_cells == 1
6337 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006338 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339 {
6340 /* Writing a single-cell character over a double-cell
6341 * character: need to redraw the next cell. */
6342 ScreenLines[off_to + 1] = 0;
6343 redraw_next = TRUE;
6344 }
6345 else if (char_cells == 2
6346 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006347 && (*mb_off2cells)(off_to, max_off_to) == 1
6348 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349 {
6350 /* Writing the second half of a double-cell character over
6351 * a double-cell character: need to redraw the second
6352 * cell. */
6353 ScreenLines[off_to + 2] = 0;
6354 redraw_next = TRUE;
6355 }
6356
6357 if (enc_dbcs == DBCS_JPNU)
6358 ScreenLines2[off_to] = ScreenLines2[off_from];
6359 }
6360 /* When writing a single-width character over a double-width
6361 * character and at the end of the redrawn text, need to clear out
6362 * the right halve of the old character.
6363 * Also required when writing the right halve of a double-width
6364 * char over the left halve of an existing one. */
6365 if (has_mbyte && col + char_cells == endcol
6366 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006367 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006369 && (*mb_off2cells)(off_to, max_off_to) == 1
6370 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371 clear_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372
6373 ScreenLines[off_to] = ScreenLines[off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 if (enc_utf8)
6375 {
6376 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6377 if (ScreenLinesUC[off_from] != 0)
6378 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006379 int i;
6380
6381 for (i = 0; i < Screen_mco; ++i)
6382 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 }
6384 }
6385 if (char_cells == 2)
6386 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387
6388#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006389 /* The bold trick makes a single column of pixels appear in the
6390 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391 * character should be redrawn too. This happens for our own GUI
6392 * and for some xterms. */
6393 if (
6394# ifdef FEAT_GUI
6395 gui.in_use
6396# endif
6397# if defined(FEAT_GUI) && defined(UNIX)
6398 ||
6399# endif
6400# ifdef UNIX
6401 term_is_xterm
6402# endif
6403 )
6404 {
6405 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006406 if (hl > HL_ALL)
6407 hl = syn_attr2attr(hl);
6408 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409 redraw_next = TRUE;
6410 }
6411#endif
6412 ScreenAttrs[off_to] = ScreenAttrs[off_from];
Bram Moolenaara12a1612019-01-24 16:39:02 +01006413
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006414 /* For simplicity set the attributes of second half of a
6415 * double-wide character equal to the first half. */
6416 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006418
6419 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422 screen_char(off_to, row, col + coloff);
6423 }
6424 else if ( p_wiv
6425#ifdef FEAT_GUI
6426 && !gui.in_use
6427#endif
6428 && col + coloff > 0)
6429 {
6430 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6431 {
6432 /*
6433 * Don't output stop-highlight when moving the cursor, it will
6434 * stop the highlighting when it should continue.
6435 */
6436 screen_attr = 0;
6437 }
6438 else if (screen_attr != 0)
6439 screen_stop_highlight();
6440 }
6441
6442 off_to += CHAR_CELLS;
6443 off_from += CHAR_CELLS;
6444 col += CHAR_CELLS;
6445 }
6446
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 if (clear_next)
6448 {
6449 /* Clear the second half of a double-wide character of which the left
6450 * half was overwritten with a single-wide character. */
6451 ScreenLines[off_to] = ' ';
6452 if (enc_utf8)
6453 ScreenLinesUC[off_to] = 0;
6454 screen_char(off_to, row, col + coloff);
6455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456
6457 if (clear_width > 0
6458#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006459 && !(flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460#endif
6461 )
6462 {
6463#ifdef FEAT_GUI
6464 int startCol = col;
6465#endif
6466
6467 /* blank out the rest of the line */
6468 while (col < clear_width && ScreenLines[off_to] == ' '
6469 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006470 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471 {
6472 ++off_to;
6473 ++col;
6474 }
6475 if (col < clear_width)
6476 {
6477#ifdef FEAT_GUI
6478 /*
6479 * In the GUI, clearing the rest of the line may leave pixels
6480 * behind if the first character cleared was bold. Some bold
6481 * fonts spill over the left. In this case we redraw the previous
6482 * character too. If we didn't skip any blanks above, then we
6483 * only redraw if the character wasn't already redrawn anyway.
6484 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006485 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486 {
6487 hl = ScreenAttrs[off_to];
6488 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006489 {
6490 int prev_cells = 1;
Bram Moolenaara12a1612019-01-24 16:39:02 +01006491
Bram Moolenaar9c697322006-10-09 20:11:17 +00006492 if (enc_utf8)
6493 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6494 * that its width is 2. */
6495 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6496 else if (enc_dbcs != 0)
6497 {
6498 /* find previous character by counting from first
6499 * column and get its width. */
6500 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006501 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006502
6503 while (off < off_to)
6504 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006505 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006506 off += prev_cells;
6507 }
6508 }
6509
6510 if (enc_dbcs != 0 && prev_cells > 1)
6511 screen_char_2(off_to - prev_cells, row,
6512 col + coloff - prev_cells);
6513 else
Bram Moolenaar9c697322006-10-09 20:11:17 +00006514 screen_char(off_to - prev_cells, row,
6515 col + coloff - prev_cells);
6516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517 }
6518#endif
6519 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6520 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521 off_to += clear_width - col;
6522 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523 }
6524 }
6525
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006526 if (clear_width > 0
6527#ifdef FEAT_TEXT_PROP
6528 && !(flags & SLF_POPUP) // no separator for popup window
6529#endif
6530 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006532 // For a window that has a right neighbor, draw the separator char
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006533 // right of the window contents. But not on top of a popup window.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006534 if (coloff + col < Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006536#ifdef FEAT_TEXT_PROP
Bram Moolenaarc662ec92019-06-23 00:15:57 +02006537 if (!blocked_by_popup(row, col + coloff))
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006538#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006540 int c;
6541
6542 c = fillchar_vsep(&hl);
6543 if (ScreenLines[off_to] != (schar_T)c
6544 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6545 != (c >= 0x80 ? c : 0))
6546 || ScreenAttrs[off_to] != hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006548 ScreenLines[off_to] = c;
6549 ScreenAttrs[off_to] = hl;
6550 if (enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006552 if (c >= 0x80)
6553 {
6554 ScreenLinesUC[off_to] = c;
6555 ScreenLinesC[0][off_to] = 0;
6556 }
6557 else
6558 ScreenLinesUC[off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559 }
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006560 screen_char(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562 }
6563 }
6564 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565 LineWraps[row] = FALSE;
6566 }
6567}
6568
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006569#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006571 * Mirror text "str" for right-left displaying.
6572 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006574 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006575rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576{
6577 char_u *p1, *p2;
6578 int t;
6579
6580 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6581 {
6582 t = *p1;
6583 *p1 = *p2;
6584 *p2 = t;
6585 }
6586}
6587#endif
6588
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589/*
6590 * mark all status lines for redraw; used after first :cd
6591 */
6592 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006593status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594{
6595 win_T *wp;
6596
Bram Moolenaar29323592016-07-24 22:04:11 +02006597 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598 if (wp->w_status_height)
6599 {
6600 wp->w_redr_status = TRUE;
6601 redraw_later(VALID);
6602 }
6603}
6604
6605/*
6606 * mark all status lines of the current buffer for redraw
6607 */
6608 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006609status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610{
6611 win_T *wp;
6612
Bram Moolenaar29323592016-07-24 22:04:11 +02006613 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6615 {
6616 wp->w_redr_status = TRUE;
6617 redraw_later(VALID);
6618 }
6619}
6620
6621/*
6622 * Redraw all status lines that need to be redrawn.
6623 */
6624 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006625redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626{
6627 win_T *wp;
6628
Bram Moolenaar29323592016-07-24 22:04:11 +02006629 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006631 win_redr_status(wp, FALSE);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006632 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006633 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635
Bram Moolenaar4033c552017-09-16 20:54:51 +02006636#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637/*
6638 * Redraw all status lines at the bottom of frame "frp".
6639 */
6640 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006641win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642{
6643 if (frp->fr_layout == FR_LEAF)
6644 frp->fr_win->w_redr_status = TRUE;
6645 else if (frp->fr_layout == FR_ROW)
6646 {
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006647 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 win_redraw_last_status(frp);
6649 }
6650 else /* frp->fr_layout == FR_COL */
6651 {
6652 frp = frp->fr_child;
6653 while (frp->fr_next != NULL)
6654 frp = frp->fr_next;
6655 win_redraw_last_status(frp);
6656 }
6657}
6658#endif
6659
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660/*
6661 * Draw the verticap separator right of window "wp" starting with line "row".
6662 */
6663 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006664draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665{
6666 int hl;
6667 int c;
6668
6669 if (wp->w_vsep_width)
6670 {
6671 /* draw the vertical separator right of this window */
6672 c = fillchar_vsep(&hl);
6673 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6674 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6675 c, ' ', hl);
6676 }
6677}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678
6679#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006680static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681
6682/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006683 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684 */
6685 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006686status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687{
6688 int len = 0;
6689
6690#ifdef FEAT_MENU
6691 int emenu = (xp->xp_context == EXPAND_MENUS
6692 || xp->xp_context == EXPAND_MENUNAMES);
6693
6694 /* Check for menu separators - replace with '|'. */
6695 if (emenu && menu_is_separator(s))
6696 return 1;
6697#endif
6698
6699 while (*s != NUL)
6700 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006701 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006702 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006703 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704 }
6705
6706 return len;
6707}
6708
6709/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006710 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006711 * These are backslashes used for escaping. Do show backslashes in help tags.
6712 */
6713 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006714skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006715{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006716 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006717#ifdef FEAT_MENU
6718 || ((xp->xp_context == EXPAND_MENUS
6719 || xp->xp_context == EXPAND_MENUNAMES)
6720 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6721#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006722 )
6723 {
6724#ifndef BACKSLASH_IN_FILENAME
6725 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6726 return 2;
6727#endif
6728 return 1;
6729 }
6730 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006731}
6732
6733/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734 * Show wildchar matches in the status line.
6735 * Show at least the "match" item.
6736 * We start at item 'first_match' in the list and show all matches that fit.
6737 *
6738 * If inversion is possible we use it. Else '=' characters are used.
6739 */
6740 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006741win_redr_status_matches(
6742 expand_T *xp,
6743 int num_matches,
6744 char_u **matches, /* list of matches */
6745 int match,
6746 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747{
6748#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6749 int row;
6750 char_u *buf;
6751 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006752 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753 int fillchar;
6754 int attr;
6755 int i;
6756 int highlight = TRUE;
6757 char_u *selstart = NULL;
6758 int selstart_col = 0;
6759 char_u *selend = NULL;
6760 static int first_match = 0;
6761 int add_left = FALSE;
6762 char_u *s;
6763#ifdef FEAT_MENU
6764 int emenu;
6765#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767
6768 if (matches == NULL) /* interrupted completion? */
6769 return;
6770
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006771 if (has_mbyte)
Bram Moolenaar964b3742019-05-24 18:54:09 +02006772 buf = alloc(Columns * MB_MAXBYTES + 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006773 else
Bram Moolenaar964b3742019-05-24 18:54:09 +02006774 buf = alloc(Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775 if (buf == NULL)
6776 return;
6777
6778 if (match == -1) /* don't show match but original text */
6779 {
6780 match = 0;
6781 highlight = FALSE;
6782 }
6783 /* count 1 for the ending ">" */
6784 clen = status_match_len(xp, L_MATCH(match)) + 3;
6785 if (match == 0)
6786 first_match = 0;
6787 else if (match < first_match)
6788 {
6789 /* jumping left, as far as we can go */
6790 first_match = match;
6791 add_left = TRUE;
6792 }
6793 else
6794 {
6795 /* check if match fits on the screen */
6796 for (i = first_match; i < match; ++i)
6797 clen += status_match_len(xp, L_MATCH(i)) + 2;
6798 if (first_match > 0)
6799 clen += 2;
6800 /* jumping right, put match at the left */
6801 if ((long)clen > Columns)
6802 {
6803 first_match = match;
6804 /* if showing the last match, we can add some on the left */
6805 clen = 2;
6806 for (i = match; i < num_matches; ++i)
6807 {
6808 clen += status_match_len(xp, L_MATCH(i)) + 2;
6809 if ((long)clen >= Columns)
6810 break;
6811 }
6812 if (i == num_matches)
6813 add_left = TRUE;
6814 }
6815 }
6816 if (add_left)
6817 while (first_match > 0)
6818 {
6819 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6820 if ((long)clen >= Columns)
6821 break;
6822 --first_match;
6823 }
6824
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006825 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826
6827 if (first_match == 0)
6828 {
6829 *buf = NUL;
6830 len = 0;
6831 }
6832 else
6833 {
6834 STRCPY(buf, "< ");
6835 len = 2;
6836 }
6837 clen = len;
6838
6839 i = first_match;
6840 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6841 {
6842 if (i == match)
6843 {
6844 selstart = buf + len;
6845 selstart_col = clen;
6846 }
6847
6848 s = L_MATCH(i);
6849 /* Check for menu separators - replace with '|' */
6850#ifdef FEAT_MENU
6851 emenu = (xp->xp_context == EXPAND_MENUS
6852 || xp->xp_context == EXPAND_MENUNAMES);
6853 if (emenu && menu_is_separator(s))
6854 {
6855 STRCPY(buf + len, transchar('|'));
6856 l = (int)STRLEN(buf + len);
6857 len += l;
6858 clen += l;
6859 }
6860 else
6861#endif
6862 for ( ; *s != NUL; ++s)
6863 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006864 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 clen += ptr2cells(s);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006866 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867 {
6868 STRNCPY(buf + len, s, l);
6869 s += l - 1;
6870 len += l;
6871 }
6872 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873 {
6874 STRCPY(buf + len, transchar_byte(*s));
6875 len += (int)STRLEN(buf + len);
6876 }
6877 }
6878 if (i == match)
6879 selend = buf + len;
6880
6881 *(buf + len++) = ' ';
6882 *(buf + len++) = ' ';
6883 clen += 2;
6884 if (++i == num_matches)
6885 break;
6886 }
6887
6888 if (i != num_matches)
6889 {
6890 *(buf + len++) = '>';
6891 ++clen;
6892 }
6893
6894 buf[len] = NUL;
6895
6896 row = cmdline_row - 1;
6897 if (row >= 0)
6898 {
6899 if (wild_menu_showing == 0)
6900 {
6901 if (msg_scrolled > 0)
6902 {
6903 /* Put the wildmenu just above the command line. If there is
6904 * no room, scroll the screen one line up. */
6905 if (cmdline_row == Rows - 1)
6906 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02006907 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 ++msg_scrolled;
6909 }
6910 else
6911 {
6912 ++cmdline_row;
6913 ++row;
6914 }
6915 wild_menu_showing = WM_SCROLLED;
6916 }
6917 else
6918 {
6919 /* Create status line if needed by setting 'laststatus' to 2.
6920 * Set 'winminheight' to zero to avoid that the window is
6921 * resized. */
6922 if (lastwin->w_status_height == 0)
6923 {
6924 save_p_ls = p_ls;
6925 save_p_wmh = p_wmh;
6926 p_ls = 2;
6927 p_wmh = 0;
6928 last_status(FALSE);
6929 }
6930 wild_menu_showing = WM_SHOWN;
6931 }
6932 }
6933
6934 screen_puts(buf, row, 0, attr);
6935 if (selstart != NULL && highlight)
6936 {
6937 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006938 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939 }
6940
6941 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6942 }
6943
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 vim_free(buf);
6946}
6947#endif
6948
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949/*
6950 * Redraw the status line of window wp.
6951 *
6952 * If inversion is possible we use it. Else '=' characters are used.
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006953 * If "ignore_pum" is TRUE, also redraw statusline when the popup menu is
6954 * displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955 */
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006956 static void
Bram Moolenaar829adb72018-06-24 19:24:03 +02006957win_redr_status(win_T *wp, int ignore_pum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958{
6959 int row;
6960 char_u *p;
6961 int len;
6962 int fillchar;
6963 int attr;
6964 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006965 static int busy = FALSE;
6966
6967 /* It's possible to get here recursively when 'statusline' (indirectly)
6968 * invokes ":redrawstatus". Simply ignore the call then. */
6969 if (busy)
6970 return;
6971 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972
6973 wp->w_redr_status = FALSE;
6974 if (wp->w_status_height == 0)
6975 {
6976 /* no status line, can only be last window */
6977 redraw_cmdline = TRUE;
6978 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006979 else if (!redrawing()
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006980 // don't update status line when popup menu is visible and may be
6981 // drawn over it, unless it will be redrawn later
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006982 || (!ignore_pum && pum_visible()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 {
6984 /* Don't redraw right now, do it later. */
6985 wp->w_redr_status = TRUE;
6986 }
6987#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006988 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 {
6990 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006991 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 }
6993#endif
6994 else
6995 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006996 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006998 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999 p = NameBuff;
7000 len = (int)STRLEN(p);
7001
Bram Moolenaard85f2712017-07-28 21:51:57 +02007002 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003#ifdef FEAT_QUICKFIX
7004 || wp->w_p_pvw
7005#endif
7006 || bufIsChanged(wp->w_buffer)
7007 || wp->w_buffer->b_p_ro)
7008 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02007009 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00007011 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012 len += (int)STRLEN(p + len);
7013 }
7014#ifdef FEAT_QUICKFIX
7015 if (wp->w_p_pvw)
7016 {
7017 STRCPY(p + len, _("[Preview]"));
7018 len += (int)STRLEN(p + len);
7019 }
7020#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02007021 if (bufIsChanged(wp->w_buffer)
7022#ifdef FEAT_TERMINAL
7023 && !bt_terminal(wp->w_buffer)
7024#endif
7025 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 {
7027 STRCPY(p + len, "[+]");
7028 len += 3;
7029 }
7030 if (wp->w_buffer->b_p_ro)
7031 {
Bram Moolenaar23584032013-06-07 20:17:11 +02007032 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01007033 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034 }
7035
Bram Moolenaar02631462017-09-22 15:20:32 +02007036 this_ru_col = ru_col - (Columns - wp->w_width);
7037 if (this_ru_col < (wp->w_width + 1) / 2)
7038 this_ru_col = (wp->w_width + 1) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039 if (this_ru_col <= 1)
7040 {
7041 p = (char_u *)"<"; /* No room for file name! */
7042 len = 1;
7043 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01007044 else if (has_mbyte)
7045 {
7046 int clen = 0, i;
7047
7048 /* Count total number of display cells. */
7049 clen = mb_string2cells(p, -1);
7050
7051 /* Find first character that will fit.
7052 * Going from start to end is much faster for DBCS. */
7053 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
7054 i += (*mb_ptr2len)(p + i))
7055 clen -= (*mb_ptr2cells)(p + i);
7056 len = clen;
7057 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 {
Bram Moolenaara12a1612019-01-24 16:39:02 +01007059 p = p + i - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060 *p = '<';
Bram Moolenaara12a1612019-01-24 16:39:02 +01007061 ++len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 }
7063
Bram Moolenaara12a1612019-01-24 16:39:02 +01007064 }
7065 else if (len > this_ru_col - 1)
7066 {
7067 p += len - (this_ru_col - 1);
7068 *p = '<';
7069 len = this_ru_col - 1;
7070 }
7071
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar53f81742017-09-22 14:35:51 +02007073 screen_puts(p, row, wp->w_wincol, attr);
7074 screen_fill(row, row + 1, len + wp->w_wincol,
7075 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007077 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
7079 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007080 - 1 + wp->w_wincol), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081
7082#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +02007083 win_redr_ruler(wp, TRUE, ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084#endif
7085 }
7086
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087 /*
7088 * May need to draw the character below the vertical separator.
7089 */
7090 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
7091 {
7092 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007093 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094 else
7095 fillchar = fillchar_vsep(&attr);
7096 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
7097 attr);
7098 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007099 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100}
7101
Bram Moolenaar238a5642006-02-21 22:12:05 +00007102#ifdef FEAT_STL_OPT
7103/*
7104 * Redraw the status line according to 'statusline' and take care of any
7105 * errors encountered.
7106 */
7107 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007108redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00007109{
Bram Moolenaar362f3562009-11-03 16:20:34 +00007110 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02007111 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007112
7113 /* When called recursively return. This can happen when the statusline
7114 * contains an expression that triggers a redraw. */
7115 if (entered)
7116 return;
7117 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007118
Bram Moolenaara742e082016-04-05 21:10:38 +02007119 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007120 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02007121 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007122 {
7123 /* When there is an error disable the statusline, otherwise the
7124 * display is messed up with errors and a redraw triggers the problem
7125 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007126 set_string_option_direct((char_u *)"statusline", -1,
7127 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007128 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007129 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007130 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007131 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007132}
7133#endif
7134
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135/*
7136 * Return TRUE if the status line of window "wp" is connected to the status
7137 * line of the window right of it. If not, then it's a vertical separator.
7138 * Only call if (wp->w_vsep_width != 0).
7139 */
7140 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007141stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142{
7143 frame_T *fr;
7144
7145 fr = wp->w_frame;
7146 while (fr->fr_parent != NULL)
7147 {
7148 if (fr->fr_parent->fr_layout == FR_COL)
7149 {
7150 if (fr->fr_next != NULL)
7151 break;
7152 }
7153 else
7154 {
7155 if (fr->fr_next != NULL)
7156 return TRUE;
7157 }
7158 fr = fr->fr_parent;
7159 }
7160 return FALSE;
7161}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164/*
7165 * Get the value to show for the language mappings, active 'keymap'.
7166 */
7167 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007168get_keymap_str(
7169 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007170 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007171 char_u *buf, /* buffer for the result */
7172 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173{
7174 char_u *p;
7175
7176 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7177 return FALSE;
7178
7179 {
7180#ifdef FEAT_EVAL
7181 buf_T *old_curbuf = curbuf;
7182 win_T *old_curwin = curwin;
7183 char_u *s;
7184
7185 curbuf = wp->w_buffer;
7186 curwin = wp;
7187 STRCPY(buf, "b:keymap_name"); /* must be writable */
7188 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007189 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 --emsg_skip;
7191 curbuf = old_curbuf;
7192 curwin = old_curwin;
7193 if (p == NULL || *p == NUL)
7194#endif
7195 {
7196#ifdef FEAT_KEYMAP
7197 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7198 p = wp->w_buffer->b_p_keymap;
7199 else
7200#endif
7201 p = (char_u *)"lang";
7202 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007203 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 buf[0] = NUL;
7205#ifdef FEAT_EVAL
7206 vim_free(s);
7207#endif
7208 }
7209 return buf[0] != NUL;
7210}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211
7212#if defined(FEAT_STL_OPT) || defined(PROTO)
7213/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007214 * Redraw the status line or ruler of window "wp".
7215 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 */
7217 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007218win_redr_custom(
7219 win_T *wp,
7220 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007222 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223 int attr;
7224 int curattr;
7225 int row;
7226 int col = 0;
7227 int maxwidth;
7228 int width;
7229 int n;
7230 int len;
7231 int fillchar;
7232 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007233 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007235 struct stl_hlrec hltab[STL_MAX_ITEM];
7236 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007237 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007238 win_T *ewp;
7239 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240
Bram Moolenaar1d633412013-12-11 15:52:01 +01007241 /* There is a tiny chance that this gets called recursively: When
7242 * redrawing a status line triggers redrawing the ruler or tabline.
7243 * Avoid trouble by not allowing recursion. */
7244 if (entered)
7245 return;
7246 entered = TRUE;
7247
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007249 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007251 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007252 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007253 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007254 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007255 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007256 maxwidth = Columns;
7257# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007258 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007259# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007261 else
7262 {
7263 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007264 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02007265 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007266
7267 if (draw_ruler)
7268 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007269 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007270 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007271 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007272 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007273 if (*++stl == '-')
7274 stl++;
7275 if (atoi((char *)stl))
7276 while (VIM_ISDIGIT(*stl))
7277 stl++;
7278 if (*stl++ != '(')
7279 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007280 }
Bram Moolenaar02631462017-09-22 15:20:32 +02007281 col = ru_col - (Columns - wp->w_width);
7282 if (col < (wp->w_width + 1) / 2)
7283 col = (wp->w_width + 1) / 2;
7284 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007285 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007286 {
7287 row = Rows - 1;
7288 --maxwidth; /* writing in last column may cause scrolling */
7289 fillchar = ' ';
7290 attr = 0;
7291 }
7292
7293# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007294 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007295# endif
7296 }
7297 else
7298 {
7299 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007300 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007301 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007302 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007303# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007304 use_sandbox = was_set_insecurely((char_u *)"statusline",
7305 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007306# endif
7307 }
7308
Bram Moolenaar53f81742017-09-22 14:35:51 +02007309 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007310 }
7311
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007313 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314
Bram Moolenaar61452852011-02-01 18:01:11 +01007315 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7316 * the cursor away and back. */
7317 ewp = wp == NULL ? curwin : wp;
7318 p_crb_save = ewp->w_p_crb;
7319 ewp->w_p_crb = FALSE;
7320
Bram Moolenaar362f3562009-11-03 16:20:34 +00007321 /* Make a copy, because the statusline may include a function call that
7322 * might change the option value and free the memory. */
7323 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007324 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007325 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007326 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007327 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007328 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007330 /* Make all characters printable. */
7331 p = transstr(buf);
7332 if (p != NULL)
7333 {
7334 vim_strncpy(buf, p, sizeof(buf) - 1);
7335 vim_free(p);
7336 }
7337
7338 /* fill up with "fillchar" */
7339 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007340 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342 len += (*mb_char2bytes)(fillchar, buf + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343 ++width;
7344 }
7345 buf[len] = NUL;
7346
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007347 /*
7348 * Draw each snippet with the specified highlighting.
7349 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350 curattr = attr;
7351 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007352 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007354 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 screen_puts_len(p, len, row, col, curattr);
7356 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007357 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007359 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007361 else if (hltab[n].userhl < 0)
7362 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007363#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007364 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7365 && wp->w_status_height != 0)
7366 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007367 else if (wp != NULL && bt_terminal(wp->w_buffer)
7368 && wp->w_status_height != 0)
7369 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02007370#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007371 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007372 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007374 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 }
7376 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007377
7378 if (wp == NULL)
7379 {
7380 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7381 col = 0;
7382 len = 0;
7383 p = buf;
7384 fillchar = 0;
7385 for (n = 0; tabtab[n].start != NULL; n++)
7386 {
7387 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7388 while (col < len)
7389 TabPageIdxs[col++] = fillchar;
7390 p = tabtab[n].start;
7391 fillchar = tabtab[n].userhl;
7392 }
7393 while (col < Columns)
7394 TabPageIdxs[col++] = fillchar;
7395 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007396
7397theend:
7398 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399}
7400
7401#endif /* FEAT_STL_OPT */
7402
7403/*
7404 * Output a single character directly to the screen and update ScreenLines.
7405 */
7406 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007407screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 char_u buf[MB_MAXBYTES + 1];
7410
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007411 if (has_mbyte)
7412 buf[(*mb_char2bytes)(c, buf)] = NUL;
7413 else
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007414 {
7415 buf[0] = c;
7416 buf[1] = NUL;
7417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418 screen_puts(buf, row, col, attr);
7419}
7420
7421/*
7422 * Get a single character directly from ScreenLines into "bytes[]".
7423 * Also return its attribute in *attrp;
7424 */
7425 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007426screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427{
7428 unsigned off;
7429
7430 /* safety check */
7431 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7432 {
7433 off = LineOffset[row] + col;
7434 *attrp = ScreenAttrs[off];
7435 bytes[0] = ScreenLines[off];
7436 bytes[1] = NUL;
7437
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438 if (enc_utf8 && ScreenLinesUC[off] != 0)
7439 bytes[utfc_char2bytes(off, bytes)] = NUL;
7440 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7441 {
7442 bytes[0] = ScreenLines[off];
7443 bytes[1] = ScreenLines2[off];
7444 bytes[2] = NUL;
7445 }
7446 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7447 {
7448 bytes[1] = ScreenLines[off + 1];
7449 bytes[2] = NUL;
7450 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451 }
7452}
7453
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007454/*
7455 * Return TRUE if composing characters for screen posn "off" differs from
7456 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007457 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007458 */
7459 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007460screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007461{
7462 int i;
7463
7464 for (i = 0; i < Screen_mco; ++i)
7465 {
7466 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7467 return TRUE;
7468 if (u8cc[i] == 0)
7469 break;
7470 }
7471 return FALSE;
7472}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007473
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474/*
7475 * Put string '*text' on the screen at position 'row' and 'col', with
7476 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7477 * Note: only outputs within one row, message is truncated at screen boundary!
7478 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7479 */
7480 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007481screen_puts(
7482 char_u *text,
7483 int row,
7484 int col,
7485 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486{
7487 screen_puts_len(text, -1, row, col, attr);
7488}
7489
7490/*
7491 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7492 * a NUL.
7493 */
7494 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007495screen_puts_len(
7496 char_u *text,
7497 int textlen,
7498 int row,
7499 int col,
7500 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501{
7502 unsigned off;
7503 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007504 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505 int c;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007506 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507 int mbyte_blen = 1;
7508 int mbyte_cells = 1;
7509 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007510 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511 int clear_next_cell = FALSE;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007512#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007514 int pc, nc, nc1;
7515 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007517 int force_redraw_this;
7518 int force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007519 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520
Bram Moolenaar0b4c9ed2019-06-03 22:04:23 +02007521 // Safety check. The check for negative row and column is to fix issue
7522 // #4102. TODO: find out why row/col could be negative.
7523 if (ScreenLines == NULL
7524 || row >= screen_Rows || row < 0
7525 || col >= screen_Columns || col < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007527 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528
Bram Moolenaarc236c162008-07-13 17:41:49 +00007529 /* When drawing over the right halve of a double-wide char clear out the
7530 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007531 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaara12a1612019-01-24 16:39:02 +01007532#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00007533 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01007534#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007535 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007536 {
7537 ScreenLines[off - 1] = ' ';
7538 ScreenAttrs[off - 1] = 0;
7539 if (enc_utf8)
7540 {
7541 ScreenLinesUC[off - 1] = 0;
7542 ScreenLinesC[0][off - 1] = 0;
7543 }
7544 /* redraw the previous cell, make it empty */
7545 screen_char(off - 1, row, col - 1);
7546 /* force the cell at "col" to be redrawn */
7547 force_redraw_next = TRUE;
7548 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007549
Bram Moolenaar367329b2007-08-30 11:53:22 +00007550 max_off = LineOffset[row] + screen_Columns;
Bram Moolenaara064ac82007-08-05 18:10:54 +00007551 while (col < screen_Columns
7552 && (len < 0 || (int)(ptr - text) < len)
7553 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554 {
7555 c = *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556 /* check if this is the first byte of a multibyte */
7557 if (has_mbyte)
7558 {
7559 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007560 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007562 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7564 mbyte_cells = 1;
7565 else if (enc_dbcs != 0)
7566 mbyte_cells = mbyte_blen;
7567 else /* enc_utf8 */
7568 {
7569 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007570 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571 (int)((text + len) - ptr));
7572 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007573 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007575#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7577 {
7578 /* Do Arabic shaping. */
7579 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7580 {
7581 /* Past end of string to be displayed. */
7582 nc = NUL;
7583 nc1 = NUL;
7584 }
7585 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007586 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007587 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7588 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007589 nc1 = pcc[0];
7590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 pc = prev_c;
7592 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007593 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007594 }
7595 else
7596 prev_c = u8c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007597#endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007598 if (col + mbyte_cells > screen_Columns)
7599 {
7600 /* Only 1 cell left, but character requires 2 cells:
7601 * display a '>' in the last column to avoid wrapping. */
7602 c = '>';
7603 mbyte_cells = 1;
7604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605 }
7606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007608 force_redraw_this = force_redraw_next;
7609 force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007610
7611 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612 || (mbyte_cells == 2
7613 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7614 || (enc_dbcs == DBCS_JPNU
7615 && c == 0x8e
7616 && ScreenLines2[off] != ptr[1])
7617 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007618 && (ScreenLinesUC[off] !=
7619 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7620 || (ScreenLinesUC[off] != 0
7621 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007623 || exmode_active;
7624
Bram Moolenaar24a5ac52019-06-08 19:01:18 +02007625 if ((need_redraw || force_redraw_this)
7626#ifdef FEAT_TEXT_PROP
Bram Moolenaarc662ec92019-06-23 00:15:57 +02007627 && !blocked_by_popup(row, col)
Bram Moolenaar24a5ac52019-06-08 19:01:18 +02007628#endif
7629 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630 {
7631#if defined(FEAT_GUI) || defined(UNIX)
7632 /* The bold trick makes a single row of pixels appear in the next
7633 * character. When a bold character is removed, the next
7634 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007635 * and for some xterms. */
7636 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637# ifdef FEAT_GUI
7638 gui.in_use
7639# endif
7640# if defined(FEAT_GUI) && defined(UNIX)
7641 ||
7642# endif
7643# ifdef UNIX
7644 term_is_xterm
7645# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007646 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007648 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007650 if (n > HL_ALL)
7651 n = syn_attr2attr(n);
7652 if (n & HL_BOLD)
7653 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654 }
7655#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656 /* When at the end of the text and overwriting a two-cell
7657 * character with a one-cell character, need to clear the next
7658 * cell. Also when overwriting the left halve of a two-cell char
7659 * with the right halve of a two-cell char. Do this only once
7660 * (mb_off2cells() may return 2 on the right halve). */
7661 if (clear_next_cell)
7662 clear_next_cell = FALSE;
7663 else if (has_mbyte
7664 && (len < 0 ? ptr[mbyte_blen] == NUL
7665 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007666 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007668 && (*mb_off2cells)(off, max_off) == 1
7669 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670 clear_next_cell = TRUE;
7671
7672 /* Make sure we never leave a second byte of a double-byte behind,
7673 * it confuses mb_off2cells(). */
7674 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007675 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007677 && (*mb_off2cells)(off, max_off) == 1
7678 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679 ScreenLines[off + mbyte_blen] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680 ScreenLines[off] = c;
7681 ScreenAttrs[off] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682 if (enc_utf8)
7683 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007684 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685 ScreenLinesUC[off] = 0;
7686 else
7687 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007688 int i;
7689
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007691 for (i = 0; i < Screen_mco; ++i)
7692 {
7693 ScreenLinesC[i][off] = u8cc[i];
7694 if (u8cc[i] == 0)
7695 break;
7696 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697 }
7698 if (mbyte_cells == 2)
7699 {
7700 ScreenLines[off + 1] = 0;
7701 ScreenAttrs[off + 1] = attr;
7702 }
7703 screen_char(off, row, col);
7704 }
7705 else if (mbyte_cells == 2)
7706 {
7707 ScreenLines[off + 1] = ptr[1];
7708 ScreenAttrs[off + 1] = attr;
7709 screen_char_2(off, row, col);
7710 }
7711 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7712 {
7713 ScreenLines2[off] = ptr[1];
7714 screen_char(off, row, col);
7715 }
7716 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717 screen_char(off, row, col);
7718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719 if (has_mbyte)
7720 {
7721 off += mbyte_cells;
7722 col += mbyte_cells;
7723 ptr += mbyte_blen;
7724 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007725 {
7726 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007728 len = -1;
7729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730 }
7731 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732 {
7733 ++off;
7734 ++col;
7735 ++ptr;
7736 }
7737 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007738
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007739 /* If we detected the next character needs to be redrawn, but the text
7740 * doesn't extend up to there, update the character here. */
7741 if (force_redraw_next && col < screen_Columns)
7742 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007743 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7744 screen_char_2(off, row, col);
7745 else
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007746 screen_char(off, row, col);
7747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748}
7749
7750#ifdef FEAT_SEARCH_EXTRA
7751/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007752 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753 */
7754 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007755start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756{
7757 if (p_hls && !no_hlsearch)
7758 {
7759 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007760 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007761# ifdef FEAT_RELTIME
7762 /* Set the time limit to 'redrawtime'. */
7763 profile_setlimit(p_rdt, &search_hl.tm);
7764# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765 }
7766}
7767
7768/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007769 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770 */
7771 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007772end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773{
7774 if (search_hl.rm.regprog != NULL)
7775 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007776 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777 search_hl.rm.regprog = NULL;
7778 }
7779}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02007780#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02007781
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007783screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784{
7785 attrentry_T *aep = NULL;
7786
7787 screen_attr = attr;
7788 if (full_screen
Bram Moolenaar4f974752019-02-17 17:44:42 +01007789#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790 && termcap_active
7791#endif
7792 )
7793 {
7794#ifdef FEAT_GUI
7795 if (gui.in_use)
7796 {
7797 char buf[20];
7798
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007799 /* The GUI handles this internally. */
7800 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801 OUT_STR(buf);
7802 }
7803 else
7804#endif
7805 {
7806 if (attr > HL_ALL) /* special HL attr. */
7807 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007808 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809 aep = syn_cterm_attr2entry(attr);
7810 else
7811 aep = syn_term_attr2entry(attr);
7812 if (aep == NULL) /* did ":syntax clear" */
7813 attr = 0;
7814 else
7815 attr = aep->ae_attr;
7816 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01007817 if ((attr & HL_BOLD) && *T_MD != NUL) /* bold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007819 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007820#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007821 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
7822 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
7823 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007824#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007825 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007826 /* If the Normal FG color has BOLD attribute and the new HL
7827 * has a FG color defined, clear BOLD. */
7828 out_str(T_ME);
Bram Moolenaar45a00002017-12-22 21:12:34 +01007829 if ((attr & HL_STANDOUT) && *T_SO != NUL) /* standout */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 out_str(T_SO);
Bram Moolenaar45a00002017-12-22 21:12:34 +01007831 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) /* undercurl */
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01007832 out_str(T_UCS);
7833 if (((attr & HL_UNDERLINE) /* underline or undercurl */
Bram Moolenaar45a00002017-12-22 21:12:34 +01007834 || ((attr & HL_UNDERCURL) && *T_UCS == NUL))
7835 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 out_str(T_US);
Bram Moolenaar45a00002017-12-22 21:12:34 +01007837 if ((attr & HL_ITALIC) && *T_CZH != NUL) /* italic */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838 out_str(T_CZH);
Bram Moolenaar45a00002017-12-22 21:12:34 +01007839 if ((attr & HL_INVERSE) && *T_MR != NUL) /* inverse (reverse) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840 out_str(T_MR);
Bram Moolenaar45a00002017-12-22 21:12:34 +01007841 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) /* strike */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02007842 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843
7844 /*
7845 * Output the color or start string after bold etc., in case the
7846 * bold etc. override the color setting.
7847 */
7848 if (aep != NULL)
7849 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007850#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007851 /* When 'termguicolors' is set but fg or bg is unset,
7852 * fall back to the cterm colors. This helps for SpellBad,
7853 * where the GUI uses a red undercurl. */
7854 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007856 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007857 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007858 }
7859 else
7860#endif
7861 if (t_colors > 1)
7862 {
7863 if (aep->ae_u.cterm.fg_color)
7864 term_fg_color(aep->ae_u.cterm.fg_color - 1);
7865 }
7866#ifdef FEAT_TERMGUICOLORS
7867 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
7868 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007869 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007870 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871 }
7872 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007873#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007874 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007876 if (aep->ae_u.cterm.bg_color)
7877 term_bg_color(aep->ae_u.cterm.bg_color - 1);
7878 }
7879
Bram Moolenaarf708ac52018-03-12 21:48:32 +01007880 if (!IS_CTERM)
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007881 {
7882 if (aep->ae_u.term.start != NULL)
7883 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884 }
7885 }
7886 }
7887 }
7888}
7889
7890 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007891screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892{
7893 int do_ME = FALSE; /* output T_ME code */
7894
7895 if (screen_attr != 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01007896#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897 && termcap_active
7898#endif
7899 )
7900 {
7901#ifdef FEAT_GUI
7902 if (gui.in_use)
7903 {
7904 char buf[20];
7905
7906 /* use internal GUI code */
7907 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
7908 OUT_STR(buf);
7909 }
7910 else
7911#endif
7912 {
7913 if (screen_attr > HL_ALL) /* special HL attr. */
7914 {
7915 attrentry_T *aep;
7916
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007917 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918 {
7919 /*
7920 * Assume that t_me restores the original colors!
7921 */
7922 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007923 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007924#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007925 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
7926 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
7927 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007928#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007929 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007930#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007931 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
7932 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
7933 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007934#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007935 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936 do_ME = TRUE;
7937 }
7938 else
7939 {
7940 aep = syn_term_attr2entry(screen_attr);
7941 if (aep != NULL && aep->ae_u.term.stop != NULL)
7942 {
7943 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
7944 do_ME = TRUE;
7945 else
7946 out_str(aep->ae_u.term.stop);
7947 }
7948 }
7949 if (aep == NULL) /* did ":syntax clear" */
7950 screen_attr = 0;
7951 else
7952 screen_attr = aep->ae_attr;
7953 }
7954
7955 /*
7956 * Often all ending-codes are equal to T_ME. Avoid outputting the
7957 * same sequence several times.
7958 */
7959 if (screen_attr & HL_STANDOUT)
7960 {
7961 if (STRCMP(T_SE, T_ME) == 0)
7962 do_ME = TRUE;
7963 else
7964 out_str(T_SE);
7965 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01007966 if ((screen_attr & HL_UNDERCURL) && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01007967 {
7968 if (STRCMP(T_UCE, T_ME) == 0)
7969 do_ME = TRUE;
7970 else
7971 out_str(T_UCE);
7972 }
7973 if ((screen_attr & HL_UNDERLINE)
Bram Moolenaar45a00002017-12-22 21:12:34 +01007974 || ((screen_attr & HL_UNDERCURL) && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975 {
7976 if (STRCMP(T_UE, T_ME) == 0)
7977 do_ME = TRUE;
7978 else
7979 out_str(T_UE);
7980 }
7981 if (screen_attr & HL_ITALIC)
7982 {
7983 if (STRCMP(T_CZR, T_ME) == 0)
7984 do_ME = TRUE;
7985 else
7986 out_str(T_CZR);
7987 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02007988 if (screen_attr & HL_STRIKETHROUGH)
7989 {
7990 if (STRCMP(T_STE, T_ME) == 0)
7991 do_ME = TRUE;
7992 else
7993 out_str(T_STE);
7994 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
7996 out_str(T_ME);
7997
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007998#ifdef FEAT_TERMGUICOLORS
7999 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008001 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008002 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008003 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008004 term_bg_rgb_color(cterm_normal_bg_gui_color);
8005 }
8006 else
8007#endif
8008 {
8009 if (t_colors > 1)
8010 {
8011 /* set Normal cterm colors */
8012 if (cterm_normal_fg_color != 0)
8013 term_fg_color(cterm_normal_fg_color - 1);
8014 if (cterm_normal_bg_color != 0)
8015 term_bg_color(cterm_normal_bg_color - 1);
8016 if (cterm_normal_fg_bold)
8017 out_str(T_MD);
8018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019 }
8020 }
8021 }
8022 screen_attr = 0;
8023}
8024
8025/*
8026 * Reset the colors for a cterm. Used when leaving Vim.
8027 * The machine specific code may override this again.
8028 */
8029 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008030reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008032 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033 {
8034 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008035#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008036 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8037 || cterm_normal_bg_gui_color != INVALCOLOR)
8038 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008039#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008041#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042 {
8043 out_str(T_OP);
8044 screen_attr = -1;
8045 }
8046 if (cterm_normal_fg_bold)
8047 {
8048 out_str(T_ME);
8049 screen_attr = -1;
8050 }
8051 }
8052}
8053
8054/*
8055 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8056 * using the attributes from ScreenAttrs["off"].
8057 */
8058 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008059screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060{
8061 int attr;
8062
8063 /* Check for illegal values, just in case (could happen just after
8064 * resizing). */
8065 if (row >= screen_Rows || col >= screen_Columns)
8066 return;
8067
Bram Moolenaar33796b32019-06-08 16:01:13 +02008068 // Skip if under the popup menu.
8069 // Popup windows with zindex higher than POPUPMENU_ZINDEX go on top.
8070 if (pum_under_menu(row, col)
Bram Moolenaare2c453d2019-08-21 14:37:09 +02008071#ifdef FEAT_TEXT_PROP
Bram Moolenaar33796b32019-06-08 16:01:13 +02008072 && screen_zindex <= POPUPMENU_ZINDEX
Bram Moolenaare2c453d2019-08-21 14:37:09 +02008073#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02008074 )
Bram Moolenaarae654382019-01-17 21:09:05 +01008075 return;
Bram Moolenaar33796b32019-06-08 16:01:13 +02008076#ifdef FEAT_TEXT_PROP
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008077 if (blocked_by_popup(row, col))
Bram Moolenaar33796b32019-06-08 16:01:13 +02008078 return;
8079#endif
8080
Bram Moolenaar494838a2015-02-10 19:20:37 +01008081 /* Outputting a character in the last cell on the screen may scroll the
8082 * screen up. Only do it when the "xn" termcap property is set, otherwise
8083 * mark the character invalid (update it when scrolled up). */
8084 if (*T_XN == NUL
8085 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086#ifdef FEAT_RIGHTLEFT
8087 /* account for first command-line character in rightleft mode */
8088 && !cmdmsg_rl
8089#endif
8090 )
8091 {
8092 ScreenAttrs[off] = (sattr_T)-1;
8093 return;
8094 }
8095
8096 /*
8097 * Stop highlighting first, so it's easier to move the cursor.
8098 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 if (screen_char_attr != 0)
8100 attr = screen_char_attr;
8101 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102 attr = ScreenAttrs[off];
8103 if (screen_attr != attr)
8104 screen_stop_highlight();
8105
8106 windgoto(row, col);
8107
8108 if (screen_attr != attr)
8109 screen_start_highlight(attr);
8110
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111 if (enc_utf8 && ScreenLinesUC[off] != 0)
8112 {
8113 char_u buf[MB_MAXBYTES + 1];
8114
Bram Moolenaarcb070082016-04-02 22:14:51 +02008115 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008116 {
8117 if (*p_ambw == 'd'
Bram Moolenaara12a1612019-01-24 16:39:02 +01008118#ifdef FEAT_GUI
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008119 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008120#endif
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008121 )
8122 {
8123 /* Clear the two screen cells. If the character is actually
8124 * single width it won't change the second cell. */
8125 out_str((char_u *)" ");
8126 term_windgoto(row, col);
8127 }
8128 /* not sure where the cursor is after drawing the ambiguous width
8129 * character */
Bram Moolenaarcb070082016-04-02 22:14:51 +02008130 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008131 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02008132 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008134
8135 /* Convert the UTF-8 character to bytes and write it. */
8136 buf[utfc_char2bytes(off, buf)] = NUL;
8137 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 }
8139 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 /* double-byte character in single-width cell */
8144 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8145 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146 }
8147
8148 screen_cur_col++;
8149}
8150
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151/*
8152 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8153 * on the screen at position 'row' and 'col'.
8154 * The attributes of the first byte is used for all. This is required to
8155 * output the two bytes of a double-byte character with nothing in between.
8156 */
8157 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008158screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159{
8160 /* Check for illegal values (could be wrong when screen was resized). */
8161 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8162 return;
8163
8164 /* Outputting the last character on the screen may scrollup the screen.
8165 * Don't to it! Mark the character invalid (update it when scrolled up) */
8166 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8167 {
8168 ScreenAttrs[off] = (sattr_T)-1;
8169 return;
8170 }
8171
8172 /* Output the first byte normally (positions the cursor), then write the
8173 * second byte directly. */
8174 screen_char(off, row, col);
8175 out_char(ScreenLines[off + 1]);
8176 ++screen_cur_col;
8177}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179/*
8180 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8181 * This uses the contents of ScreenLines[] and doesn't change it.
8182 */
8183 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008184screen_draw_rectangle(
8185 int row,
8186 int col,
8187 int height,
8188 int width,
8189 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190{
8191 int r, c;
8192 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008193 int max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008194
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008195 /* Can't use ScreenLines unless initialized */
8196 if (ScreenLines == NULL)
8197 return;
8198
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 if (invert)
8200 screen_char_attr = HL_INVERSE;
8201 for (r = row; r < row + height; ++r)
8202 {
8203 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008204 max_off = off + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008205 for (c = col; c < col + width; ++c)
8206 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00008207 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208 {
8209 screen_char_2(off + c, r, c);
8210 ++c;
8211 }
8212 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213 {
8214 screen_char(off + c, r, c);
Bram Moolenaar367329b2007-08-30 11:53:22 +00008215 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 ++c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217 }
8218 }
8219 }
8220 screen_char_attr = 0;
8221}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223/*
8224 * Redraw the characters for a vertically split window.
8225 */
8226 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008227redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228{
8229 int col;
8230 int width;
8231
8232# ifdef FEAT_CLIPBOARD
8233 clip_may_clear_selection(row, end - 1);
8234# endif
8235
8236 if (wp == NULL)
8237 {
8238 col = 0;
8239 width = Columns;
8240 }
8241 else
8242 {
8243 col = wp->w_wincol;
8244 width = wp->w_width;
8245 }
8246 screen_draw_rectangle(row, col, end - row, width, FALSE);
8247}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008249 static void
8250space_to_screenline(int off, int attr)
8251{
8252 ScreenLines[off] = ' ';
8253 ScreenAttrs[off] = attr;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008254 if (enc_utf8)
8255 ScreenLinesUC[off] = 0;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008256}
8257
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258/*
8259 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8260 * with character 'c1' in first column followed by 'c2' in the other columns.
8261 * Use attributes 'attr'.
8262 */
8263 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008264screen_fill(
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008265 int start_row,
8266 int end_row,
8267 int start_col,
8268 int end_col,
8269 int c1,
8270 int c2,
8271 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008273 int row;
8274 int col;
8275 int off;
8276 int end_off;
8277 int did_delete;
8278 int c;
8279 int norm_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008281 int force_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282#endif
8283
8284 if (end_row > screen_Rows) /* safety check */
8285 end_row = screen_Rows;
8286 if (end_col > screen_Columns) /* safety check */
8287 end_col = screen_Columns;
8288 if (ScreenLines == NULL
8289 || start_row >= end_row
8290 || start_col >= end_col) /* nothing to do */
8291 return;
8292
8293 /* it's a "normal" terminal when not in a GUI or cterm */
8294 norm_term = (
8295#ifdef FEAT_GUI
8296 !gui.in_use &&
8297#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008298 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299 for (row = start_row; row < end_row; ++row)
8300 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008301 if (has_mbyte
Bram Moolenaara12a1612019-01-24 16:39:02 +01008302#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00008303 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008304#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00008305 )
8306 {
8307 /* When drawing over the right halve of a double-wide char clear
8308 * out the left halve. When drawing over the left halve of a
8309 * double wide-char clear out the right halve. Only needed in a
8310 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008311 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008312 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008313 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008314 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316 /*
8317 * Try to use delete-line termcap code, when no attributes or in a
8318 * "normal" terminal, where a bold/italic space is just a
8319 * space.
8320 */
8321 did_delete = FALSE;
8322 if (c2 == ' '
8323 && end_col == Columns
8324 && can_clear(T_CE)
8325 && (attr == 0
8326 || (norm_term
8327 && attr <= HL_ALL
8328 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8329 {
8330 /*
8331 * check if we really need to clear something
8332 */
8333 col = start_col;
8334 if (c1 != ' ') /* don't clear first char */
8335 ++col;
8336
8337 off = LineOffset[row] + col;
8338 end_off = LineOffset[row] + end_col;
8339
8340 /* skip blanks (used often, keep it fast!) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341 if (enc_utf8)
8342 while (off < end_off && ScreenLines[off] == ' '
8343 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8344 ++off;
8345 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 while (off < end_off && ScreenLines[off] == ' '
8347 && ScreenAttrs[off] == 0)
8348 ++off;
8349 if (off < end_off) /* something to be cleared */
8350 {
8351 col = off - LineOffset[row];
8352 screen_stop_highlight();
8353 term_windgoto(row, col);/* clear rest of this screen line */
8354 out_str(T_CE);
8355 screen_start(); /* don't know where cursor is now */
8356 col = end_col - col;
8357 while (col--) /* clear chars in ScreenLines */
8358 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008359 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360 ++off;
8361 }
8362 }
8363 did_delete = TRUE; /* the chars are cleared now */
8364 }
8365
8366 off = LineOffset[row] + start_col;
8367 c = c1;
8368 for (col = start_col; col < end_col; ++col)
8369 {
Bram Moolenaar33796b32019-06-08 16:01:13 +02008370 if ((ScreenLines[off] != c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008371 || (enc_utf8 && (int)ScreenLinesUC[off]
8372 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373 || ScreenAttrs[off] != attr
8374#if defined(FEAT_GUI) || defined(UNIX)
8375 || force_next
8376#endif
8377 )
Bram Moolenaar33796b32019-06-08 16:01:13 +02008378#ifdef FEAT_TEXT_PROP
8379 // Skip if under a(nother) popup.
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008380 && !blocked_by_popup(row, col)
Bram Moolenaar33796b32019-06-08 16:01:13 +02008381#endif
8382 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383 {
8384#if defined(FEAT_GUI) || defined(UNIX)
8385 /* The bold trick may make a single row of pixels appear in
8386 * the next character. When a bold character is removed, the
8387 * next character should be redrawn too. This happens for our
8388 * own GUI and for some xterms. */
8389 if (
8390# ifdef FEAT_GUI
8391 gui.in_use
8392# endif
8393# if defined(FEAT_GUI) && defined(UNIX)
8394 ||
8395# endif
8396# ifdef UNIX
8397 term_is_xterm
8398# endif
8399 )
8400 {
8401 if (ScreenLines[off] != ' '
8402 && (ScreenAttrs[off] > HL_ALL
8403 || ScreenAttrs[off] & HL_BOLD))
8404 force_next = TRUE;
8405 else
8406 force_next = FALSE;
8407 }
8408#endif
8409 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410 if (enc_utf8)
8411 {
8412 if (c >= 0x80)
8413 {
8414 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008415 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416 }
8417 else
8418 ScreenLinesUC[off] = 0;
8419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420 ScreenAttrs[off] = attr;
8421 if (!did_delete || c != ' ')
8422 screen_char(off, row, col);
8423 }
8424 ++off;
8425 if (col == start_col)
8426 {
8427 if (did_delete)
8428 break;
8429 c = c2;
8430 }
8431 }
8432 if (end_col == Columns)
8433 LineWraps[row] = FALSE;
8434 if (row == Rows - 1) /* overwritten the command line */
8435 {
8436 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02008437 if (start_col == 0 && end_col == Columns
8438 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008440 if (start_col == 0)
8441 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442 }
8443 }
8444}
8445
8446/*
8447 * Check if there should be a delay. Used before clearing or redrawing the
8448 * screen or the command line.
8449 */
8450 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008451check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452{
8453 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8454 && !did_wait_return
8455 && emsg_silent == 0)
8456 {
8457 out_flush();
8458 ui_delay(1000L, TRUE);
8459 emsg_on_display = FALSE;
8460 if (check_msg_scroll)
8461 msg_scroll = FALSE;
8462 }
8463}
8464
8465/*
Bram Moolenaarca57ab52019-04-13 14:53:16 +02008466 * Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect.
8467 */
8468 static void
8469clear_TabPageIdxs(void)
8470{
8471 int scol;
8472
8473 for (scol = 0; scol < Columns; ++scol)
8474 TabPageIdxs[scol] = 0;
8475}
8476
8477/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008479 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480 * Returns TRUE if there is a valid screen to write to.
8481 * Returns FALSE when starting up and screen not initialized yet.
8482 */
8483 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008484screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008486 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487 return (ScreenLines != NULL);
8488}
8489
8490/*
8491 * Resize the shell to Rows and Columns.
8492 * Allocate ScreenLines[] and associated items.
8493 *
8494 * There may be some time between setting Rows and Columns and (re)allocating
8495 * ScreenLines[]. This happens when starting up and when (manually) changing
8496 * the shell size. Always use screen_Rows and screen_Columns to access items
8497 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8498 * final size of the shell is needed.
8499 */
8500 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008501screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502{
8503 int new_row, old_row;
8504#ifdef FEAT_GUI
8505 int old_Rows;
8506#endif
8507 win_T *wp;
8508 int outofmem = FALSE;
8509 int len;
8510 schar_T *new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008512 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008514 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515 sattr_T *new_ScreenAttrs;
8516 unsigned *new_LineOffset;
8517 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008518 short *new_TabPageIdxs;
Bram Moolenaar33796b32019-06-08 16:01:13 +02008519#ifdef FEAT_TEXT_PROP
8520 short *new_popup_mask;
Bram Moolenaar4c063a02019-06-10 21:24:12 +02008521 short *new_popup_mask_next;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008522 char *new_popup_transparent;
Bram Moolenaar33796b32019-06-08 16:01:13 +02008523#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00008524 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008526 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008527 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008529retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530 /*
8531 * Allocation of the screen buffers is done only when the size changes and
8532 * when Rows and Columns have been set and we have started doing full
8533 * screen stuff.
8534 */
8535 if ((ScreenLines != NULL
8536 && Rows == screen_Rows
8537 && Columns == screen_Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538 && enc_utf8 == (ScreenLinesUC != NULL)
8539 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaara12a1612019-01-24 16:39:02 +01008540 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541 || Rows == 0
8542 || Columns == 0
8543 || (!full_screen && ScreenLines == NULL))
8544 return;
8545
8546 /*
8547 * It's possible that we produce an out-of-memory message below, which
8548 * will cause this function to be called again. To break the loop, just
8549 * return here.
8550 */
8551 if (entered)
8552 return;
8553 entered = TRUE;
8554
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008555 /*
8556 * Note that the window sizes are updated before reallocating the arrays,
8557 * thus we must not redraw here!
8558 */
8559 ++RedrawingDisabled;
8560
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561 win_new_shellsize(); /* fit the windows in the new sized shell */
8562
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563 comp_col(); /* recompute columns for shown command and ruler */
8564
8565 /*
8566 * We're changing the size of the screen.
8567 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8568 * - Move lines from the old arrays into the new arrays, clear extra
8569 * lines (unless the screen is going to be cleared).
8570 * - Free the old arrays.
8571 *
8572 * If anything fails, make ScreenLines NULL, so we don't do anything!
8573 * Continuing with the old ScreenLines may result in a crash, because the
8574 * size is wrong.
8575 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008576 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008578 if (aucmd_win != NULL)
8579 win_free_lsize(aucmd_win);
Bram Moolenaar8caaf822019-06-01 18:11:22 +02008580#ifdef FEAT_TEXT_PROP
8581 // global popup windows
8582 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
8583 win_free_lsize(wp);
8584 // tab-local popup windows
8585 FOR_ALL_TABPAGES(tp)
8586 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
8587 win_free_lsize(wp);
8588#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008590 new_ScreenLines = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
Bram Moolenaar216b7102010-03-23 13:56:59 +01008591 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592 if (enc_utf8)
8593 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008594 new_ScreenLinesUC = LALLOC_MULT(u8char_T, (Rows + 1) * Columns);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008595 for (i = 0; i < p_mco; ++i)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008596 new_ScreenLinesC[i] = LALLOC_CLEAR_MULT(u8char_T,
8597 (Rows + 1) * Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008598 }
8599 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008600 new_ScreenLines2 = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
8601 new_ScreenAttrs = LALLOC_MULT(sattr_T, (Rows + 1) * Columns);
8602 new_LineOffset = LALLOC_MULT(unsigned, Rows);
8603 new_LineWraps = LALLOC_MULT(char_u, Rows);
8604 new_TabPageIdxs = LALLOC_MULT(short, Columns);
Bram Moolenaar33796b32019-06-08 16:01:13 +02008605#ifdef FEAT_TEXT_PROP
8606 new_popup_mask = LALLOC_MULT(short, Rows * Columns);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02008607 new_popup_mask_next = LALLOC_MULT(short, Rows * Columns);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008608 new_popup_transparent = LALLOC_MULT(char, Rows * Columns);
Bram Moolenaar33796b32019-06-08 16:01:13 +02008609#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008611 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612 {
8613 if (win_alloc_lines(wp) == FAIL)
8614 {
8615 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008616 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617 }
8618 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008619 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8620 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008621 outofmem = TRUE;
Bram Moolenaar8caaf822019-06-01 18:11:22 +02008622#ifdef FEAT_TEXT_PROP
8623 // global popup windows
8624 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
8625 if (win_alloc_lines(wp) == FAIL)
8626 {
8627 outofmem = TRUE;
8628 goto give_up;
8629 }
8630 // tab-local popup windows
8631 FOR_ALL_TABPAGES(tp)
8632 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
8633 if (win_alloc_lines(wp) == FAIL)
8634 {
8635 outofmem = TRUE;
8636 goto give_up;
8637 }
8638#endif
8639
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008640give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008642 for (i = 0; i < p_mco; ++i)
8643 if (new_ScreenLinesC[i] == NULL)
8644 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645 if (new_ScreenLines == NULL
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008646 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648 || new_ScreenAttrs == NULL
8649 || new_LineOffset == NULL
8650 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008651 || new_TabPageIdxs == NULL
Bram Moolenaar33796b32019-06-08 16:01:13 +02008652#ifdef FEAT_TEXT_PROP
8653 || new_popup_mask == NULL
Bram Moolenaar4c063a02019-06-10 21:24:12 +02008654 || new_popup_mask_next == NULL
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008655 || new_popup_transparent == NULL
Bram Moolenaar33796b32019-06-08 16:01:13 +02008656#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008657 || outofmem)
8658 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008659 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008660 {
8661 /* guess the size */
8662 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8663
8664 /* Remember we did this to avoid getting outofmem messages over
8665 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008666 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008667 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01008668 VIM_CLEAR(new_ScreenLines);
Bram Moolenaard23a8232018-02-10 18:45:26 +01008669 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008670 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01008671 VIM_CLEAR(new_ScreenLinesC[i]);
8672 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaard23a8232018-02-10 18:45:26 +01008673 VIM_CLEAR(new_ScreenAttrs);
8674 VIM_CLEAR(new_LineOffset);
8675 VIM_CLEAR(new_LineWraps);
8676 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar33796b32019-06-08 16:01:13 +02008677#ifdef FEAT_TEXT_PROP
8678 VIM_CLEAR(new_popup_mask);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02008679 VIM_CLEAR(new_popup_mask_next);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008680 VIM_CLEAR(new_popup_transparent);
Bram Moolenaar33796b32019-06-08 16:01:13 +02008681#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008682 }
8683 else
8684 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008685 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008686
Bram Moolenaar071d4272004-06-13 20:20:40 +00008687 for (new_row = 0; new_row < Rows; ++new_row)
8688 {
8689 new_LineOffset[new_row] = new_row * Columns;
8690 new_LineWraps[new_row] = FALSE;
8691
8692 /*
8693 * If the screen is not going to be cleared, copy as much as
8694 * possible from the old screen to the new one and clear the rest
8695 * (used when resizing the window at the "--more--" prompt or when
8696 * executing an external command, for the GUI).
8697 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008698 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699 {
8700 (void)vim_memset(new_ScreenLines + new_row * Columns,
8701 ' ', (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702 if (enc_utf8)
8703 {
8704 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8705 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008706 for (i = 0; i < p_mco; ++i)
8707 (void)vim_memset(new_ScreenLinesC[i]
8708 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709 0, (size_t)Columns * sizeof(u8char_T));
8710 }
8711 if (enc_dbcs == DBCS_JPNU)
8712 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8713 0, (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8715 0, (size_t)Columns * sizeof(sattr_T));
8716 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008717 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718 {
8719 if (screen_Columns < Columns)
8720 len = screen_Columns;
8721 else
8722 len = Columns;
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008723 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008724 * may be invalid now. Also when p_mco changes. */
8725 if (!(enc_utf8 && ScreenLinesUC == NULL)
8726 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008727 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8728 ScreenLines + LineOffset[old_row],
8729 (size_t)len * sizeof(schar_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008730 if (enc_utf8 && ScreenLinesUC != NULL
8731 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008732 {
8733 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8734 ScreenLinesUC + LineOffset[old_row],
8735 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008736 for (i = 0; i < p_mco; ++i)
8737 mch_memmove(new_ScreenLinesC[i]
8738 + new_LineOffset[new_row],
8739 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740 (size_t)len * sizeof(u8char_T));
8741 }
8742 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8743 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8744 ScreenLines2 + LineOffset[old_row],
8745 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8747 ScreenAttrs + LineOffset[old_row],
8748 (size_t)len * sizeof(sattr_T));
8749 }
8750 }
8751 }
8752 /* Use the last line of the screen for the current line. */
8753 current_ScreenLine = new_ScreenLines + Rows * Columns;
Bram Moolenaar6ace95e2019-08-13 23:09:49 +02008754
8755#ifdef FEAT_TEXT_PROP
8756 vim_memset(new_popup_mask, 0, Rows * Columns * sizeof(short));
8757 vim_memset(new_popup_transparent, 0, Rows * Columns * sizeof(char));
8758#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008759 }
8760
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008761 free_screenlines();
8762
Bram Moolenaar6ace95e2019-08-13 23:09:49 +02008763 // NOTE: this may result in all pointers to become NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764 ScreenLines = new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008765 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008766 for (i = 0; i < p_mco; ++i)
8767 ScreenLinesC[i] = new_ScreenLinesC[i];
8768 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769 ScreenLines2 = new_ScreenLines2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008770 ScreenAttrs = new_ScreenAttrs;
8771 LineOffset = new_LineOffset;
8772 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008773 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar33796b32019-06-08 16:01:13 +02008774#ifdef FEAT_TEXT_PROP
8775 popup_mask = new_popup_mask;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008776 popup_mask_next = new_popup_mask_next;
8777 popup_transparent = new_popup_transparent;
Bram Moolenaar33796b32019-06-08 16:01:13 +02008778 popup_mask_refresh = TRUE;
8779#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780
8781 /* It's important that screen_Rows and screen_Columns reflect the actual
8782 * size of ScreenLines[]. Set them before calling anything. */
8783#ifdef FEAT_GUI
8784 old_Rows = screen_Rows;
8785#endif
8786 screen_Rows = Rows;
8787 screen_Columns = Columns;
8788
8789 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008790 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008791 screenclear2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008792#ifdef FEAT_GUI
8793 else if (gui.in_use
8794 && !gui.starting
8795 && ScreenLines != NULL
8796 && old_Rows != Rows)
8797 {
8798 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8799 /*
8800 * Adjust the position of the cursor, for when executing an external
8801 * command.
8802 */
8803 if (msg_row >= Rows) /* Rows got smaller */
8804 msg_row = Rows - 1; /* put cursor at last row */
8805 else if (Rows > old_Rows) /* Rows got bigger */
8806 msg_row += Rows - old_Rows; /* put cursor in same place */
8807 if (msg_col >= Columns) /* Columns got smaller */
8808 msg_col = Columns - 1; /* put cursor at last column */
8809 }
8810#endif
Bram Moolenaarca57ab52019-04-13 14:53:16 +02008811 clear_TabPageIdxs();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008812
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008814 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008815
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008816 /*
8817 * Do not apply autocommands more than 3 times to avoid an endless loop
8818 * in case applying autocommands always changes Rows or Columns.
8819 */
8820 if (starting == 0 && ++retry_count <= 3)
8821 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008822 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008823 /* In rare cases, autocommands may have altered Rows or Columns,
8824 * jump back to check if we need to allocate the screen again. */
8825 goto retry;
8826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827}
8828
8829 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008830free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008831{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008832 int i;
8833
Bram Moolenaar33796b32019-06-08 16:01:13 +02008834 VIM_CLEAR(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008835 for (i = 0; i < Screen_mco; ++i)
Bram Moolenaar33796b32019-06-08 16:01:13 +02008836 VIM_CLEAR(ScreenLinesC[i]);
8837 VIM_CLEAR(ScreenLines2);
8838 VIM_CLEAR(ScreenLines);
8839 VIM_CLEAR(ScreenAttrs);
8840 VIM_CLEAR(LineOffset);
8841 VIM_CLEAR(LineWraps);
8842 VIM_CLEAR(TabPageIdxs);
8843#ifdef FEAT_TEXT_PROP
8844 VIM_CLEAR(popup_mask);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02008845 VIM_CLEAR(popup_mask_next);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008846 VIM_CLEAR(popup_transparent);
Bram Moolenaar33796b32019-06-08 16:01:13 +02008847#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008848}
8849
8850 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008851screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852{
8853 check_for_delay(FALSE);
8854 screenalloc(FALSE); /* allocate screen buffers if size changed */
8855 screenclear2(); /* clear the screen */
8856}
8857
8858 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008859screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860{
8861 int i;
8862
8863 if (starting == NO_SCREEN || ScreenLines == NULL
8864#ifdef FEAT_GUI
8865 || (gui.in_use && gui.starting)
8866#endif
8867 )
8868 return;
8869
8870#ifdef FEAT_GUI
8871 if (!gui.in_use)
8872#endif
8873 screen_attr = -1; /* force setting the Normal colors */
8874 screen_stop_highlight(); /* don't want highlighting here */
8875
8876#ifdef FEAT_CLIPBOARD
8877 /* disable selection without redrawing it */
8878 clip_scroll_selection(9999);
8879#endif
8880
8881 /* blank out ScreenLines */
8882 for (i = 0; i < Rows; ++i)
8883 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02008884 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885 LineWraps[i] = FALSE;
8886 }
8887
8888 if (can_clear(T_CL))
8889 {
8890 out_str(T_CL); /* clear the display */
8891 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008892 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893 }
8894 else
8895 {
8896 /* can't clear the screen, mark all chars with invalid attributes */
8897 for (i = 0; i < Rows; ++i)
8898 lineinvalid(LineOffset[i], (int)Columns);
8899 clear_cmdline = TRUE;
8900 }
8901
8902 screen_cleared = TRUE; /* can use contents of ScreenLines now */
8903
8904 win_rest_invalid(firstwin);
8905 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008906 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008907 if (must_redraw == CLEAR) /* no need to clear again */
8908 must_redraw = NOT_VALID;
8909 compute_cmdrow();
8910 msg_row = cmdline_row; /* put cursor on last line for messages */
8911 msg_col = 0;
8912 screen_start(); /* don't know where cursor is now */
8913 msg_scrolled = 0; /* can't scroll back */
8914 msg_didany = FALSE;
8915 msg_didout = FALSE;
8916}
8917
8918/*
8919 * Clear one line in ScreenLines.
8920 */
8921 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02008922lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923{
8924 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925 if (enc_utf8)
8926 (void)vim_memset(ScreenLinesUC + off, 0,
8927 (size_t)width * sizeof(u8char_T));
Bram Moolenaarcfce7172017-08-17 20:31:48 +02008928 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008929}
8930
8931/*
8932 * Mark one line in ScreenLines invalid by setting the attributes to an
8933 * invalid value.
8934 */
8935 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008936lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937{
8938 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
8939}
8940
Bram Moolenaar071d4272004-06-13 20:20:40 +00008941/*
8942 * Copy part of a Screenline for vertically split window "wp".
8943 */
8944 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008945linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946{
8947 unsigned off_to = LineOffset[to] + wp->w_wincol;
8948 unsigned off_from = LineOffset[from] + wp->w_wincol;
8949
8950 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
8951 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952 if (enc_utf8)
8953 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008954 int i;
8955
Bram Moolenaar071d4272004-06-13 20:20:40 +00008956 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
8957 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008958 for (i = 0; i < p_mco; ++i)
8959 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
8960 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961 }
8962 if (enc_dbcs == DBCS_JPNU)
8963 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
8964 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008965 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
8966 wp->w_width * sizeof(sattr_T));
8967}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968
8969/*
8970 * Return TRUE if clearing with term string "p" would work.
8971 * It can't work when the string is empty or it won't set the right background.
Bram Moolenaar33796b32019-06-08 16:01:13 +02008972 * Don't clear to end-of-line when there are popups, it may cause flicker.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008973 */
8974 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008975can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976{
8977 return (*p != NUL && (t_colors <= 1
8978#ifdef FEAT_GUI
8979 || gui.in_use
8980#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008981#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008982 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02008983 || (!p_tgc && cterm_normal_bg_color == 0)
8984#else
8985 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008986#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02008987 || *T_UT != NUL)
8988#ifdef FEAT_TEXT_PROP
8989 && !(p == T_CE && popup_visible)
8990#endif
8991 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00008992}
8993
8994/*
8995 * Reset cursor position. Use whenever cursor was moved because of outputting
8996 * something directly to the screen (shell commands) or a terminal control
8997 * code.
8998 */
8999 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009000screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009001{
9002 screen_cur_row = screen_cur_col = 9999;
9003}
9004
9005/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009006 * Move the cursor to position "row","col" in the screen.
9007 * This tries to find the most efficient way to move, minimizing the number of
9008 * characters sent to the terminal.
9009 */
9010 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009011windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009012{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009013 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009014 int i;
9015 int plan;
9016 int cost;
9017 int wouldbe_col;
9018 int noinvcurs;
9019 char_u *bs;
9020 int goto_cost;
9021 int attr;
9022
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009023#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009024#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9025
9026#define PLAN_LE 1
9027#define PLAN_CR 2
9028#define PLAN_NL 3
9029#define PLAN_WRITE 4
9030 /* Can't use ScreenLines unless initialized */
9031 if (ScreenLines == NULL)
9032 return;
9033
9034 if (col != screen_cur_col || row != screen_cur_row)
9035 {
9036 /* Check for valid position. */
9037 if (row < 0) /* window without text lines? */
9038 row = 0;
9039 if (row >= screen_Rows)
9040 row = screen_Rows - 1;
9041 if (col >= screen_Columns)
9042 col = screen_Columns - 1;
9043
9044 /* check if no cursor movement is allowed in highlight mode */
9045 if (screen_attr && *T_MS == NUL)
9046 noinvcurs = HIGHL_COST;
9047 else
9048 noinvcurs = 0;
9049 goto_cost = GOTO_COST + noinvcurs;
9050
9051 /*
9052 * Plan how to do the positioning:
9053 * 1. Use CR to move it to column 0, same row.
9054 * 2. Use T_LE to move it a few columns to the left.
9055 * 3. Use NL to move a few lines down, column 0.
9056 * 4. Move a few columns to the right with T_ND or by writing chars.
9057 *
9058 * Don't do this if the cursor went beyond the last column, the cursor
9059 * position is unknown then (some terminals wrap, some don't )
9060 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009061 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062 * characters to move the cursor to the right.
9063 */
9064 if (row >= screen_cur_row && screen_cur_col < Columns)
9065 {
9066 /*
9067 * If the cursor is in the same row, bigger col, we can use CR
9068 * or T_LE.
9069 */
9070 bs = NULL; /* init for GCC */
9071 attr = screen_attr;
9072 if (row == screen_cur_row && col < screen_cur_col)
9073 {
9074 /* "le" is preferred over "bc", because "bc" is obsolete */
9075 if (*T_LE)
9076 bs = T_LE; /* "cursor left" */
9077 else
9078 bs = T_BC; /* "backspace character (old) */
9079 if (*bs)
9080 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9081 else
9082 cost = 999;
9083 if (col + 1 < cost) /* using CR is less characters */
9084 {
9085 plan = PLAN_CR;
9086 wouldbe_col = 0;
9087 cost = 1; /* CR is just one character */
9088 }
9089 else
9090 {
9091 plan = PLAN_LE;
9092 wouldbe_col = col;
9093 }
9094 if (noinvcurs) /* will stop highlighting */
9095 {
9096 cost += noinvcurs;
9097 attr = 0;
9098 }
9099 }
9100
9101 /*
9102 * If the cursor is above where we want to be, we can use CR LF.
9103 */
9104 else if (row > screen_cur_row)
9105 {
9106 plan = PLAN_NL;
9107 wouldbe_col = 0;
9108 cost = (row - screen_cur_row) * 2; /* CR LF */
9109 if (noinvcurs) /* will stop highlighting */
9110 {
9111 cost += noinvcurs;
9112 attr = 0;
9113 }
9114 }
9115
9116 /*
9117 * If the cursor is in the same row, smaller col, just use write.
9118 */
9119 else
9120 {
9121 plan = PLAN_WRITE;
9122 wouldbe_col = screen_cur_col;
9123 cost = 0;
9124 }
9125
9126 /*
9127 * Check if any characters that need to be written have the
9128 * correct attributes. Also avoid UTF-8 characters.
9129 */
9130 i = col - wouldbe_col;
9131 if (i > 0)
9132 cost += i;
9133 if (cost < goto_cost && i > 0)
9134 {
9135 /*
9136 * Check if the attributes are correct without additionally
9137 * stopping highlighting.
9138 */
9139 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9140 while (i && *p++ == attr)
9141 --i;
9142 if (i != 0)
9143 {
9144 /*
9145 * Try if it works when highlighting is stopped here.
9146 */
9147 if (*--p == 0)
9148 {
9149 cost += noinvcurs;
9150 while (i && *p++ == 0)
9151 --i;
9152 }
9153 if (i != 0)
9154 cost = 999; /* different attributes, don't do it */
9155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156 if (enc_utf8)
9157 {
9158 /* Don't use an UTF-8 char for positioning, it's slow. */
9159 for (i = wouldbe_col; i < col; ++i)
9160 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9161 {
9162 cost = 999;
9163 break;
9164 }
9165 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009166 }
9167
9168 /*
9169 * We can do it without term_windgoto()!
9170 */
9171 if (cost < goto_cost)
9172 {
9173 if (plan == PLAN_LE)
9174 {
9175 if (noinvcurs)
9176 screen_stop_highlight();
9177 while (screen_cur_col > col)
9178 {
9179 out_str(bs);
9180 --screen_cur_col;
9181 }
9182 }
9183 else if (plan == PLAN_CR)
9184 {
9185 if (noinvcurs)
9186 screen_stop_highlight();
9187 out_char('\r');
9188 screen_cur_col = 0;
9189 }
9190 else if (plan == PLAN_NL)
9191 {
9192 if (noinvcurs)
9193 screen_stop_highlight();
9194 while (screen_cur_row < row)
9195 {
9196 out_char('\n');
9197 ++screen_cur_row;
9198 }
9199 screen_cur_col = 0;
9200 }
9201
9202 i = col - screen_cur_col;
9203 if (i > 0)
9204 {
9205 /*
9206 * Use cursor-right if it's one character only. Avoids
9207 * removing a line of pixels from the last bold char, when
9208 * using the bold trick in the GUI.
9209 */
9210 if (T_ND[0] != NUL && T_ND[1] == NUL)
9211 {
9212 while (i-- > 0)
9213 out_char(*T_ND);
9214 }
9215 else
9216 {
9217 int off;
9218
9219 off = LineOffset[row] + screen_cur_col;
9220 while (i-- > 0)
9221 {
9222 if (ScreenAttrs[off] != screen_attr)
9223 screen_stop_highlight();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009224 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009225 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009226 if (enc_dbcs == DBCS_JPNU
9227 && ScreenLines[off] == 0x8e)
9228 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009229 ++off;
9230 }
9231 }
9232 }
9233 }
9234 }
9235 else
9236 cost = 999;
9237
9238 if (cost >= goto_cost)
9239 {
9240 if (noinvcurs)
9241 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009242 if (row == screen_cur_row && (col > screen_cur_col)
9243 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009244 term_cursor_right(col - screen_cur_col);
9245 else
9246 term_windgoto(row, col);
9247 }
9248 screen_cur_row = row;
9249 screen_cur_col = col;
9250 }
9251}
9252
9253/*
9254 * Set cursor to its position in the current window.
9255 */
9256 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009257setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009258{
Bram Moolenaar987723e2018-03-06 11:43:04 +01009259 setcursor_mayforce(FALSE);
9260}
9261
9262/*
9263 * Set cursor to its position in the current window.
9264 * When "force" is TRUE also when not redrawing.
9265 */
9266 void
9267setcursor_mayforce(int force)
9268{
9269 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009270 {
9271 validate_cursor();
9272 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009273 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009274#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009275 /* With 'rightleft' set and the cursor on a double-wide
9276 * character, position it on the leftmost column. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01009277 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol
9278 - ((has_mbyte
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009279 && (*mb_ptr2cells)(ml_get_cursor()) == 2
Bram Moolenaara12a1612019-01-24 16:39:02 +01009280 && vim_isprintc(gchar_cursor())) ? 2 : 1)) :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009281#endif
9282 curwin->w_wcol));
9283 }
9284}
9285
9286
9287/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009288 * Insert 'line_count' lines at 'row' in window 'wp'.
9289 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9290 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009291 * scrolling.
9292 * Returns FAIL if the lines are not inserted, OK for success.
9293 */
9294 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009295win_ins_lines(
9296 win_T *wp,
9297 int row,
9298 int line_count,
9299 int invalid,
9300 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009301{
9302 int did_delete;
9303 int nextrow;
9304 int lastrow;
9305 int retval;
9306
9307 if (invalid)
9308 wp->w_lines_valid = 0;
9309
9310 if (wp->w_height < 5)
9311 return FAIL;
9312
9313 if (line_count > wp->w_height - row)
9314 line_count = wp->w_height - row;
9315
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009316 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009317 if (retval != MAYBE)
9318 return retval;
9319
9320 /*
9321 * If there is a next window or a status line, we first try to delete the
9322 * lines at the bottom to avoid messing what is after the window.
Bram Moolenaarc363fe12019-08-04 18:13:46 +02009323 * If this fails and there are following windows, don't do anything to
9324 * avoid messing up those windows, better just redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009325 */
9326 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009327 if (wp->w_next != NULL || wp->w_status_height)
9328 {
9329 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009330 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009331 did_delete = TRUE;
9332 else if (wp->w_next)
9333 return FAIL;
9334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009335 /*
9336 * if no lines deleted, blank the lines that will end up below the window
9337 */
9338 if (!did_delete)
9339 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009340 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009341 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02009342 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009343 lastrow = nextrow + line_count;
9344 if (lastrow > Rows)
9345 lastrow = Rows;
9346 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009347 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009348 ' ', ' ', 0);
9349 }
9350
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009351 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009352 == FAIL)
9353 {
Bram Moolenaarc363fe12019-08-04 18:13:46 +02009354 // deletion will have messed up other windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00009355 if (did_delete)
9356 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009357 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009358 win_rest_invalid(W_NEXT(wp));
9359 }
9360 return FAIL;
9361 }
9362
9363 return OK;
9364}
9365
9366/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009367 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009368 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9369 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9370 * scrolling
9371 * Return OK for success, FAIL if the lines are not deleted.
9372 */
9373 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009374win_del_lines(
9375 win_T *wp,
9376 int row,
9377 int line_count,
9378 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009379 int mayclear,
9380 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009381{
9382 int retval;
9383
9384 if (invalid)
9385 wp->w_lines_valid = 0;
9386
9387 if (line_count > wp->w_height - row)
9388 line_count = wp->w_height - row;
9389
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009390 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009391 if (retval != MAYBE)
9392 return retval;
9393
9394 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009395 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009396 return FAIL;
9397
Bram Moolenaar071d4272004-06-13 20:20:40 +00009398 /*
9399 * If there are windows or status lines below, try to put them at the
9400 * correct place. If we can't do that, they have to be redrawn.
9401 */
9402 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9403 {
9404 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009405 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009406 {
9407 wp->w_redr_status = TRUE;
9408 win_rest_invalid(wp->w_next);
9409 }
9410 }
9411 /*
9412 * If this is the last window and there is no status line, redraw the
9413 * command line later.
9414 */
9415 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009416 redraw_cmdline = TRUE;
9417 return OK;
9418}
9419
9420/*
9421 * Common code for win_ins_lines() and win_del_lines().
9422 * Returns OK or FAIL when the work has been done.
9423 * Returns MAYBE when not finished yet.
9424 */
9425 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009426win_do_lines(
9427 win_T *wp,
9428 int row,
9429 int line_count,
9430 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009431 int del,
9432 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009433{
9434 int retval;
9435
9436 if (!redrawing() || line_count <= 0)
9437 return FAIL;
9438
Bram Moolenaar33796b32019-06-08 16:01:13 +02009439 // When inserting lines would result in loss of command output, just redraw
9440 // the lines.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009441 if (no_win_do_lines_ins && !del)
9442 return FAIL;
9443
Bram Moolenaar33796b32019-06-08 16:01:13 +02009444 // only a few lines left: redraw is faster
Bram Moolenaar4033c552017-09-16 20:54:51 +02009445 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009446 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009447 if (!no_win_do_lines_ins)
Bram Moolenaar33796b32019-06-08 16:01:13 +02009448 screenclear(); // will set wp->w_lines_valid to 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00009449 return FAIL;
9450 }
9451
Bram Moolenaar33796b32019-06-08 16:01:13 +02009452#ifdef FEAT_TEXT_PROP
Bram Moolenaar4c063a02019-06-10 21:24:12 +02009453 // this doesn't work when there are popups visible
Bram Moolenaar33796b32019-06-08 16:01:13 +02009454 if (popup_visible)
9455 return FAIL;
9456#endif
9457
9458 // Delete all remaining lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00009459 if (row + line_count >= wp->w_height)
9460 {
9461 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009462 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009463 ' ', ' ', 0);
9464 return OK;
9465 }
9466
9467 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009468 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009470 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009471 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009472 if (!no_win_do_lines_ins)
9473 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009474
9475 /*
9476 * If the terminal can set a scroll region, use that.
9477 * Always do this in a vertically split window. This will redraw from
9478 * ScreenLines[] when t_CV isn't defined. That's faster than using
9479 * win_line().
9480 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009481 * a character in the lower right corner of the scroll region may cause a
9482 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483 */
Bram Moolenaar02631462017-09-22 15:20:32 +02009484 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009486 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009487 scroll_region_set(wp, row);
9488 if (del)
9489 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009490 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009491 else
9492 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009493 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009495 scroll_region_reset();
9496 return retval;
9497 }
9498
Bram Moolenaar071d4272004-06-13 20:20:40 +00009499 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9500 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501
9502 return MAYBE;
9503}
9504
9505/*
9506 * window 'wp' and everything after it is messed up, mark it for redraw
9507 */
9508 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009509win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009510{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009512 {
9513 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009514 wp->w_redr_status = TRUE;
9515 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009516 }
9517 redraw_cmdline = TRUE;
9518}
9519
9520/*
9521 * The rest of the routines in this file perform screen manipulations. The
9522 * given operation is performed physically on the screen. The corresponding
9523 * change is also made to the internal screen image. In this way, the editor
9524 * anticipates the effect of editing changes on the appearance of the screen.
9525 * That way, when we call screenupdate a complete redraw isn't usually
9526 * necessary. Another advantage is that we can keep adding code to anticipate
9527 * screen changes, and in the meantime, everything still works.
9528 */
9529
9530/*
9531 * types for inserting or deleting lines
9532 */
9533#define USE_T_CAL 1
9534#define USE_T_CDL 2
9535#define USE_T_AL 3
9536#define USE_T_CE 4
9537#define USE_T_DL 5
9538#define USE_T_SR 6
9539#define USE_NL 7
9540#define USE_T_CD 8
9541#define USE_REDRAW 9
9542
9543/*
9544 * insert lines on the screen and update ScreenLines[]
9545 * 'end' is the line after the scrolled part. Normally it is Rows.
9546 * When scrolling region used 'off' is the offset from the top for the region.
9547 * 'row' and 'end' are relative to the start of the region.
9548 *
9549 * return FAIL for failure, OK for success.
9550 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009551 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009552screen_ins_lines(
9553 int off,
9554 int row,
9555 int line_count,
9556 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009557 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01009558 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009559{
9560 int i;
9561 int j;
9562 unsigned temp;
9563 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009564 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009565 int type;
9566 int result_empty;
9567 int can_ce = can_clear(T_CE);
9568
9569 /*
9570 * FAIL if
9571 * - there is no valid screen
9572 * - the screen has to be redrawn completely
9573 * - the line count is less than one
9574 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009575 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar33796b32019-06-08 16:01:13 +02009576 * - there is a popup window
Bram Moolenaar071d4272004-06-13 20:20:40 +00009577 */
Bram Moolenaar33796b32019-06-08 16:01:13 +02009578 if (!screen_valid(TRUE)
9579 || line_count <= 0 || line_count > p_ttyscroll
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009580#ifdef FEAT_CLIPBOARD
9581 || (clip_star.state != SELECT_CLEARED
9582 && redrawing_for_callback > 0)
9583#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02009584#ifdef FEAT_TEXT_PROP
9585 || popup_visible
9586#endif
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009587 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009588 return FAIL;
9589
9590 /*
9591 * There are seven ways to insert lines:
9592 * 0. When in a vertically split window and t_CV isn't set, redraw the
9593 * characters from ScreenLines[].
9594 * 1. Use T_CD (clear to end of display) if it exists and the result of
9595 * the insert is just empty lines
9596 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9597 * present or line_count > 1. It looks better if we do all the inserts
9598 * at once.
9599 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9600 * insert is just empty lines and T_CE is not present or line_count >
9601 * 1.
9602 * 4. Use T_AL (insert line) if it exists.
9603 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9604 * just empty lines.
9605 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9606 * just empty lines.
9607 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9608 * the 'da' flag is not set or we have clear line capability.
9609 * 8. redraw the characters from ScreenLines[].
9610 *
9611 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9612 * the scrollbar for the window. It does have insert line, use that if it
9613 * exists.
9614 */
9615 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009616 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9617 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009618 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009619 type = USE_T_CD;
9620 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9621 type = USE_T_CAL;
9622 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9623 type = USE_T_CDL;
9624 else if (*T_AL != NUL)
9625 type = USE_T_AL;
9626 else if (can_ce && result_empty)
9627 type = USE_T_CE;
9628 else if (*T_DL != NUL && result_empty)
9629 type = USE_T_DL;
9630 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9631 type = USE_T_SR;
9632 else
9633 return FAIL;
9634
9635 /*
9636 * For clearing the lines screen_del_lines() is used. This will also take
9637 * care of t_db if necessary.
9638 */
9639 if (type == USE_T_CD || type == USE_T_CDL ||
9640 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009641 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009642
9643 /*
9644 * If text is retained below the screen, first clear or delete as many
9645 * lines at the bottom of the window as are about to be inserted so that
9646 * the deleted lines won't later surface during a screen_del_lines.
9647 */
9648 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009649 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009650
9651#ifdef FEAT_CLIPBOARD
9652 /* Remove a modeless selection when inserting lines halfway the screen
9653 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009654 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009655 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009656 else
9657 clip_scroll_selection(-line_count);
9658#endif
9659
Bram Moolenaar071d4272004-06-13 20:20:40 +00009660#ifdef FEAT_GUI
9661 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9662 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009663 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664#endif
9665
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009666 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
9667 cursor_col = wp->w_wincol;
9668
Bram Moolenaar071d4272004-06-13 20:20:40 +00009669 if (*T_CCS != NUL) /* cursor relative to region */
9670 cursor_row = row;
9671 else
9672 cursor_row = row + off;
9673
9674 /*
9675 * Shift LineOffset[] line_count down to reflect the inserted lines.
9676 * Clear the inserted lines in ScreenLines[].
9677 */
9678 row += off;
9679 end += off;
9680 for (i = 0; i < line_count; ++i)
9681 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009682 if (wp != NULL && wp->w_width != Columns)
9683 {
9684 /* need to copy part of a line */
9685 j = end - 1 - i;
9686 while ((j -= line_count) >= row)
9687 linecopy(j + line_count, j, wp);
9688 j += line_count;
9689 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009690 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
9691 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009692 else
9693 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9694 LineWraps[j] = FALSE;
9695 }
9696 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009697 {
9698 j = end - 1 - i;
9699 temp = LineOffset[j];
9700 while ((j -= line_count) >= row)
9701 {
9702 LineOffset[j + line_count] = LineOffset[j];
9703 LineWraps[j + line_count] = LineWraps[j];
9704 }
9705 LineOffset[j + line_count] = temp;
9706 LineWraps[j + line_count] = FALSE;
9707 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009708 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009709 else
9710 lineinvalid(temp, (int)Columns);
9711 }
9712 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009713
9714 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009715 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009716 if (clear_attr != 0)
9717 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009718
Bram Moolenaar071d4272004-06-13 20:20:40 +00009719 /* redraw the characters */
9720 if (type == USE_REDRAW)
9721 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02009722 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009723 {
9724 term_append_lines(line_count);
9725 screen_start(); /* don't know where cursor is now */
9726 }
9727 else
9728 {
9729 for (i = 0; i < line_count; i++)
9730 {
9731 if (type == USE_T_AL)
9732 {
9733 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009734 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009735 out_str(T_AL);
9736 }
9737 else /* type == USE_T_SR */
9738 out_str(T_SR);
9739 screen_start(); /* don't know where cursor is now */
9740 }
9741 }
9742
9743 /*
9744 * With scroll-reverse and 'da' flag set we need to clear the lines that
9745 * have been scrolled down into the region.
9746 */
9747 if (type == USE_T_SR && *T_DA)
9748 {
9749 for (i = 0; i < line_count; ++i)
9750 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009751 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009752 out_str(T_CE);
9753 screen_start(); /* don't know where cursor is now */
9754 }
9755 }
9756
9757#ifdef FEAT_GUI
9758 gui_can_update_cursor();
9759 if (gui.in_use)
9760 out_flush(); /* always flush after a scroll */
9761#endif
9762 return OK;
9763}
9764
9765/*
Bram Moolenaar107abd22016-08-12 14:08:25 +02009766 * Delete lines on the screen and update ScreenLines[].
9767 * "end" is the line after the scrolled part. Normally it is Rows.
9768 * When scrolling region used "off" is the offset from the top for the region.
9769 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009770 *
9771 * Return OK for success, FAIL if the lines are not deleted.
9772 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009774screen_del_lines(
9775 int off,
9776 int row,
9777 int line_count,
9778 int end,
9779 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009780 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +01009781 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009782{
9783 int j;
9784 int i;
9785 unsigned temp;
9786 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009787 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788 int cursor_end;
9789 int result_empty; /* result is empty until end of region */
9790 int can_delete; /* deleting line codes can be used */
9791 int type;
9792
9793 /*
9794 * FAIL if
9795 * - there is no valid screen
9796 * - the screen has to be redrawn completely
9797 * - the line count is less than one
9798 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009799 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009800 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009801 if (!screen_valid(TRUE) || line_count <= 0
9802 || (!force && line_count > p_ttyscroll)
9803#ifdef FEAT_CLIPBOARD
9804 || (clip_star.state != SELECT_CLEARED
9805 && redrawing_for_callback > 0)
9806#endif
9807 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009808 return FAIL;
9809
9810 /*
9811 * Check if the rest of the current region will become empty.
9812 */
9813 result_empty = row + line_count >= end;
9814
9815 /*
9816 * We can delete lines only when 'db' flag not set or when 'ce' option
9817 * available.
9818 */
9819 can_delete = (*T_DB == NUL || can_clear(T_CE));
9820
9821 /*
9822 * There are six ways to delete lines:
9823 * 0. When in a vertically split window and t_CV isn't set, redraw the
9824 * characters from ScreenLines[].
9825 * 1. Use T_CD if it exists and the result is empty.
9826 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9827 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9828 * none of the other ways work.
9829 * 4. Use T_CE (erase line) if the result is empty.
9830 * 5. Use T_DL (delete line) if it exists.
9831 * 6. redraw the characters from ScreenLines[].
9832 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009833 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9834 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009835 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009836 type = USE_T_CD;
9837#if defined(__BEOS__) && defined(BEOS_DR8)
9838 /*
9839 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9840 * its internal termcap... this works okay for tests which test *T_DB !=
9841 * NUL. It has the disadvantage that the user cannot use any :set t_*
9842 * command to get T_DB (back) to empty_option, only :set term=... will do
9843 * the trick...
9844 * Anyway, this hack will hopefully go away with the next OS release.
9845 * (Olaf Seibert)
9846 */
9847 else if (row == 0 && T_DB == empty_option
9848 && (line_count == 1 || *T_CDL == NUL))
9849#else
9850 else if (row == 0 && (
9851#ifndef AMIGA
9852 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9853 * up, so use delete-line command */
9854 line_count == 1 ||
9855#endif
9856 *T_CDL == NUL))
9857#endif
9858 type = USE_NL;
9859 else if (*T_CDL != NUL && line_count > 1 && can_delete)
9860 type = USE_T_CDL;
9861 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +02009862 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009863 type = USE_T_CE;
9864 else if (*T_DL != NUL && can_delete)
9865 type = USE_T_DL;
9866 else if (*T_CDL != NUL && can_delete)
9867 type = USE_T_CDL;
9868 else
9869 return FAIL;
9870
9871#ifdef FEAT_CLIPBOARD
9872 /* Remove a modeless selection when deleting lines halfway the screen or
9873 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009874 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009875 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009876 else
9877 clip_scroll_selection(line_count);
9878#endif
9879
Bram Moolenaar071d4272004-06-13 20:20:40 +00009880#ifdef FEAT_GUI
9881 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9882 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009883 gui_dont_update_cursor(gui.cursor_row >= row + off
9884 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009885#endif
9886
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009887 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
9888 cursor_col = wp->w_wincol;
9889
Bram Moolenaar071d4272004-06-13 20:20:40 +00009890 if (*T_CCS != NUL) /* cursor relative to region */
9891 {
9892 cursor_row = row;
9893 cursor_end = end;
9894 }
9895 else
9896 {
9897 cursor_row = row + off;
9898 cursor_end = end + off;
9899 }
9900
9901 /*
9902 * Now shift LineOffset[] line_count up to reflect the deleted lines.
9903 * Clear the inserted lines in ScreenLines[].
9904 */
9905 row += off;
9906 end += off;
9907 for (i = 0; i < line_count; ++i)
9908 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009909 if (wp != NULL && wp->w_width != Columns)
9910 {
9911 /* need to copy part of a line */
9912 j = row + i;
9913 while ((j += line_count) <= end - 1)
9914 linecopy(j - line_count, j, wp);
9915 j -= line_count;
9916 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009917 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
9918 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009919 else
9920 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9921 LineWraps[j] = FALSE;
9922 }
9923 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924 {
9925 /* whole width, moving the line pointers is faster */
9926 j = row + i;
9927 temp = LineOffset[j];
9928 while ((j += line_count) <= end - 1)
9929 {
9930 LineOffset[j - line_count] = LineOffset[j];
9931 LineWraps[j - line_count] = LineWraps[j];
9932 }
9933 LineOffset[j - line_count] = temp;
9934 LineWraps[j - line_count] = FALSE;
9935 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009936 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009937 else
9938 lineinvalid(temp, (int)Columns);
9939 }
9940 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009941
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009942 if (screen_attr != clear_attr)
9943 screen_stop_highlight();
9944 if (clear_attr != 0)
9945 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009946
Bram Moolenaar071d4272004-06-13 20:20:40 +00009947 /* redraw the characters */
9948 if (type == USE_REDRAW)
9949 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02009950 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009951 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009952 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009953 out_str(T_CD);
9954 screen_start(); /* don't know where cursor is now */
9955 }
9956 else if (type == USE_T_CDL)
9957 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009958 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009959 term_delete_lines(line_count);
9960 screen_start(); /* don't know where cursor is now */
9961 }
9962 /*
9963 * Deleting lines at top of the screen or scroll region: Just scroll
9964 * the whole screen (scroll region) up by outputting newlines on the
9965 * last line.
9966 */
9967 else if (type == USE_NL)
9968 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009969 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009970 for (i = line_count; --i >= 0; )
9971 out_char('\n'); /* cursor will remain on same line */
9972 }
9973 else
9974 {
9975 for (i = line_count; --i >= 0; )
9976 {
9977 if (type == USE_T_DL)
9978 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009979 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009980 out_str(T_DL); /* delete a line */
9981 }
9982 else /* type == USE_T_CE */
9983 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009984 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009985 out_str(T_CE); /* erase a line */
9986 }
9987 screen_start(); /* don't know where cursor is now */
9988 }
9989 }
9990
9991 /*
9992 * If the 'db' flag is set, we need to clear the lines that have been
9993 * scrolled up at the bottom of the region.
9994 */
9995 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
9996 {
9997 for (i = line_count; i > 0; --i)
9998 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009999 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010000 out_str(T_CE); /* erase a line */
10001 screen_start(); /* don't know where cursor is now */
10002 }
10003 }
10004
10005#ifdef FEAT_GUI
10006 gui_can_update_cursor();
10007 if (gui.in_use)
10008 out_flush(); /* always flush after a scroll */
10009#endif
10010
10011 return OK;
10012}
10013
10014/*
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010015 * Return TRUE when postponing displaying the mode message: when not redrawing
10016 * or inside a mapping.
10017 */
10018 int
10019skip_showmode()
10020{
10021 // Call char_avail() only when we are going to show something, because it
10022 // takes a bit of time. redrawing() may also call char_avail_avail().
10023 if (global_busy
10024 || msg_silent != 0
10025 || !redrawing()
10026 || (char_avail() && !KeyTyped))
10027 {
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010028 redraw_mode = TRUE; // show mode later
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010029 return TRUE;
10030 }
10031 return FALSE;
10032}
10033
10034/*
Bram Moolenaar81226e02018-02-20 21:44:45 +010010035 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010036 *
10037 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10038 * If clear_cmdline is FALSE there may be a message there that needs to be
10039 * cleared only if a mode is shown.
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010040 * If redraw_mode is TRUE show or clear the mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010041 * Return the length of the message (0 if no message).
10042 */
10043 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010044showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010045{
10046 int need_clear;
10047 int length = 0;
10048 int do_mode;
10049 int attr;
10050 int nwr_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010051 int sub_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010052
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010053 do_mode = ((p_smd && msg_silent == 0)
10054 && ((State & INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +020010055 || restart_edit != NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010056 || VIsual_active));
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010057 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010058 {
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010059 if (skip_showmode())
10060 return 0; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +000010061
10062 nwr_save = need_wait_return;
10063
10064 /* wait a bit before overwriting an important message */
10065 check_for_delay(FALSE);
10066
10067 /* if the cmdline is more than one line high, erase top lines */
10068 need_clear = clear_cmdline;
10069 if (clear_cmdline && cmdline_row < Rows - 1)
10070 msg_clr_cmdline(); /* will reset clear_cmdline */
10071
10072 /* Position on the last line in the window, column 0 */
10073 msg_pos_mode();
10074 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010075 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010076 if (do_mode)
10077 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010078 msg_puts_attr("--", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010079#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010080 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010081# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010082 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010083# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010084 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010085# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010086 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010087# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar32526b32019-01-19 17:43:09 +010010088 msg_puts_attr(" IM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010089# else
Bram Moolenaar32526b32019-01-19 17:43:09 +010010090 msg_puts_attr(" XIM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010091# endif
10092#endif
10093#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10094 if (gui.in_use)
10095 {
10096 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010097 {
10098 /* HANGUL */
10099 if (enc_utf8)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010100 msg_puts_attr(" \355\225\234\352\270\200", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010101 else
Bram Moolenaar32526b32019-01-19 17:43:09 +010010102 msg_puts_attr(" \307\321\261\333", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010104 }
10105#endif
Bram Moolenaarea389e92014-05-28 21:40:52 +020010106 /* CTRL-X in Insert mode */
10107 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010108 {
10109 /* These messages can get long, avoid a wrap in a narrow
10110 * window. Prefer showing edit_submode_extra. */
10111 length = (Rows - msg_row) * Columns - 3;
10112 if (edit_submode_extra != NULL)
10113 length -= vim_strsize(edit_submode_extra);
10114 if (length > 0)
10115 {
10116 if (edit_submode_pre != NULL)
10117 length -= vim_strsize(edit_submode_pre);
10118 if (length - vim_strsize(edit_submode) > 0)
10119 {
10120 if (edit_submode_pre != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010121 msg_puts_attr((char *)edit_submode_pre, attr);
10122 msg_puts_attr((char *)edit_submode, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010123 }
10124 if (edit_submode_extra != NULL)
10125 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010126 msg_puts_attr(" ", attr); /* add a space in between */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010127 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010128 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010129 else
10130 sub_attr = attr;
Bram Moolenaar32526b32019-01-19 17:43:09 +010010131 msg_puts_attr((char *)edit_submode_extra, sub_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010132 }
10133 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010134 }
10135 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010136 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010137 if (State & VREPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010138 msg_puts_attr(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010139 else if (State & REPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010140 msg_puts_attr(_(" REPLACE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010141 else if (State & INSERT)
10142 {
10143#ifdef FEAT_RIGHTLEFT
10144 if (p_ri)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010145 msg_puts_attr(_(" REVERSE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010146#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +010010147 msg_puts_attr(_(" INSERT"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010148 }
Bram Moolenaar942b4542018-06-17 16:23:34 +020010149 else if (restart_edit == 'I' || restart_edit == 'A')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010150 msg_puts_attr(_(" (insert)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010151 else if (restart_edit == 'R')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010152 msg_puts_attr(_(" (replace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010153 else if (restart_edit == 'V')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010154 msg_puts_attr(_(" (vreplace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010155#ifdef FEAT_RIGHTLEFT
10156 if (p_hkmap)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010157 msg_puts_attr(_(" Hebrew"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010158#endif
10159#ifdef FEAT_KEYMAP
10160 if (State & LANGMAP)
10161 {
10162# ifdef FEAT_ARABIC
10163 if (curwin->w_p_arab)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010164 msg_puts_attr(_(" Arabic"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010165 else
10166# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010167 if (get_keymap_str(curwin, (char_u *)" (%s)",
10168 NameBuff, MAXPATHL))
Bram Moolenaar32526b32019-01-19 17:43:09 +010010169 msg_puts_attr((char *)NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010170 }
10171#endif
10172 if ((State & INSERT) && p_paste)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010173 msg_puts_attr(_(" (paste)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010174
Bram Moolenaar071d4272004-06-13 20:20:40 +000010175 if (VIsual_active)
10176 {
10177 char *p;
10178
10179 /* Don't concatenate separate words to avoid translation
10180 * problems. */
10181 switch ((VIsual_select ? 4 : 0)
10182 + (VIsual_mode == Ctrl_V) * 2
10183 + (VIsual_mode == 'V'))
10184 {
10185 case 0: p = N_(" VISUAL"); break;
10186 case 1: p = N_(" VISUAL LINE"); break;
10187 case 2: p = N_(" VISUAL BLOCK"); break;
10188 case 4: p = N_(" SELECT"); break;
10189 case 5: p = N_(" SELECT LINE"); break;
10190 default: p = N_(" SELECT BLOCK"); break;
10191 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010192 msg_puts_attr(_(p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010193 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010194 msg_puts_attr(" --", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010195 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010196
Bram Moolenaar071d4272004-06-13 20:20:40 +000010197 need_clear = TRUE;
10198 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010199 if (reg_recording != 0
Bram Moolenaare2c453d2019-08-21 14:37:09 +020010200 && edit_submode == NULL) // otherwise it gets too long
Bram Moolenaar071d4272004-06-13 20:20:40 +000010201 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010202 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010203 need_clear = TRUE;
10204 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010205
10206 mode_displayed = TRUE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010207 if (need_clear || clear_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010208 msg_clr_eos();
10209 msg_didout = FALSE; /* overwrite this message */
10210 length = msg_col;
10211 msg_col = 0;
10212 need_wait_return = nwr_save; /* never ask for hit-return for this */
10213 }
10214 else if (clear_cmdline && msg_silent == 0)
10215 /* Clear the whole command line. Will reset "clear_cmdline". */
10216 msg_clr_cmdline();
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010217 else if (redraw_mode)
10218 {
10219 msg_pos_mode();
10220 msg_clr_eos();
10221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010222
10223#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010224 /* In Visual mode the size of the selected area must be redrawn. */
10225 if (VIsual_active)
10226 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010227
10228 /* If the last window has no status line, the ruler is after the mode
10229 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010230 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +020010231 win_redr_ruler(lastwin, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010232#endif
10233 redraw_cmdline = FALSE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010234 redraw_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010235 clear_cmdline = FALSE;
10236
10237 return length;
10238}
10239
10240/*
10241 * Position for a mode message.
10242 */
10243 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010244msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010245{
10246 msg_col = 0;
10247 msg_row = Rows - 1;
10248}
10249
10250/*
10251 * Delete mode message. Used when ESC is typed which is expected to end
10252 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010253 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010254 */
10255 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010256unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010257{
10258 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010259 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010260 */
10261 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10262 redraw_cmdline = TRUE; /* delete mode later */
10263 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010264 clearmode();
10265}
10266
10267/*
10268 * Clear the mode message.
10269 */
10270 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010271clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010272{
Bram Moolenaar2abad542018-05-19 14:43:45 +020010273 int save_msg_row = msg_row;
10274 int save_msg_col = msg_col;
10275
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010276 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010277 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010278 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010279 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +020010280
10281 msg_col = save_msg_col;
10282 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010283}
10284
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010285 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010286recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010287{
Bram Moolenaar32526b32019-01-19 17:43:09 +010010288 msg_puts_attr(_("recording"), attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010289 if (!shortmess(SHM_RECORDING))
10290 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010291 char s[4];
10292
10293 sprintf(s, " @%c", reg_recording);
10294 msg_puts_attr(s, attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010295 }
10296}
10297
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010298/*
10299 * Draw the tab pages line at the top of the Vim window.
10300 */
Bram Moolenaare12bab32019-01-08 22:02:56 +010010301 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010302draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010303{
10304 int tabcount = 0;
10305 tabpage_T *tp;
10306 int tabwidth;
10307 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010308 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010309 int attr;
10310 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010311 win_T *cwp;
10312 int wincount;
10313 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010314 int c;
10315 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010316 int attr_sel = HL_ATTR(HLF_TPS);
10317 int attr_nosel = HL_ATTR(HLF_TP);
10318 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010319 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010320 int room;
10321 int use_sep_chars = (t_colors < 8
10322#ifdef FEAT_GUI
10323 && !gui.in_use
10324#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010325#ifdef FEAT_TERMGUICOLORS
10326 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010327#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010328 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010329
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010330 if (ScreenLines == NULL)
10331 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010332 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010333
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010334#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010335 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010336 if (gui_use_tabline())
10337 {
10338 gui_update_tabline();
10339 return;
10340 }
10341#endif
10342
10343 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010344 return;
10345
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010346#if defined(FEAT_STL_OPT)
Bram Moolenaarca57ab52019-04-13 14:53:16 +020010347 clear_TabPageIdxs();
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010348
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010349 /* Use the 'tabline' option if it's set. */
10350 if (*p_tal != NUL)
10351 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010352 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010353
10354 /* Check for an error. If there is one we would loop in redrawing the
10355 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010356 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010357 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010358 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010359 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010360 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010361 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010362 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010363 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010364#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010365 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010366 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010367 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010368
Bram Moolenaar238a5642006-02-21 22:12:05 +000010369 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10370 if (tabwidth < 6)
10371 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010372
Bram Moolenaar238a5642006-02-21 22:12:05 +000010373 attr = attr_nosel;
10374 tabcount = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010375 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10376 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010377 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010378 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010379
Bram Moolenaar238a5642006-02-21 22:12:05 +000010380 if (tp->tp_topframe == topframe)
10381 attr = attr_sel;
10382 if (use_sep_chars && col > 0)
10383 screen_putchar('|', 0, col++, attr);
10384
10385 if (tp->tp_topframe != topframe)
10386 attr = attr_nosel;
10387
10388 screen_putchar(' ', 0, col++, attr);
10389
10390 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010391 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010392 cwp = curwin;
10393 wp = firstwin;
10394 }
10395 else
10396 {
10397 cwp = tp->tp_curwin;
10398 wp = tp->tp_firstwin;
10399 }
10400
10401 modified = FALSE;
10402 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10403 if (bufIsChanged(wp->w_buffer))
10404 modified = TRUE;
10405 if (modified || wincount > 1)
10406 {
10407 if (wincount > 1)
10408 {
10409 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010410 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010411 if (col + len >= Columns - 3)
10412 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010413 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010414#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010415 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010416#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010417 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010418#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010419 );
10420 col += len;
10421 }
10422 if (modified)
10423 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10424 screen_putchar(' ', 0, col++, attr);
10425 }
10426
10427 room = scol - col + tabwidth - 1;
10428 if (room > 0)
10429 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010430 /* Get buffer name in NameBuff[] */
10431 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010432 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010433 len = vim_strsize(NameBuff);
10434 p = NameBuff;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010435 if (has_mbyte)
10436 while (len > room)
10437 {
10438 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010439 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010440 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010010441 else if (len > room)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010442 {
10443 p += len - room;
10444 len = room;
10445 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010446 if (len > Columns - col - 1)
10447 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010448
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010449 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010450 col += len;
10451 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010452 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010453
10454 /* Store the tab page number in TabPageIdxs[], so that
10455 * jump_to_mouse() knows where each one is. */
10456 ++tabcount;
10457 while (scol < col)
10458 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010459 }
10460
Bram Moolenaar238a5642006-02-21 22:12:05 +000010461 if (use_sep_chars)
10462 c = '_';
10463 else
10464 c = ' ';
10465 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010466
10467 /* Put an "X" for closing the current tab if there are several. */
10468 if (first_tabpage->tp_next != NULL)
10469 {
10470 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10471 TabPageIdxs[Columns - 1] = -999;
10472 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010473 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010474
10475 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10476 * set. */
10477 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010478}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010479
10480/*
10481 * Get buffer name for "buf" into NameBuff[].
10482 * Takes care of special buffer names and translates special characters.
10483 */
10484 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010485get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010486{
10487 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010488 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010489 else
10490 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10491 trans_characters(NameBuff, MAXPATHL);
10492}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010493
Bram Moolenaar071d4272004-06-13 20:20:40 +000010494/*
10495 * Get the character to use in a status line. Get its attributes in "*attr".
10496 */
10497 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010498fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010499{
10500 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010501
10502#ifdef FEAT_TERMINAL
10503 if (bt_terminal(wp->w_buffer))
10504 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010505 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010506 {
10507 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010508 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010509 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010510 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010511 {
10512 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010513 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010514 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010515 }
10516 else
10517#endif
10518 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010519 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010520 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010521 fill = fill_stl;
10522 }
10523 else
10524 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010525 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010526 fill = fill_stlnc;
10527 }
10528 /* Use fill when there is highlighting, and highlighting of current
10529 * window differs, or the fillchars differ, or this is not the
10530 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010531 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010532 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010533 || (fill_stl != fill_stlnc)))
10534 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010535 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010536 return '^';
10537 return '=';
10538}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010539
Bram Moolenaar071d4272004-06-13 20:20:40 +000010540/*
10541 * Get the character to use in a separator between vertically split windows.
10542 * Get its attributes in "*attr".
10543 */
10544 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010545fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010546{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010547 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010548 if (*attr == 0 && fill_vert == ' ')
10549 return '|';
10550 else
10551 return fill_vert;
10552}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010553
10554/*
10555 * Return TRUE if redrawing should currently be done.
10556 */
10557 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010558redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010559{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010560#ifdef FEAT_EVAL
10561 if (disable_redraw_for_testing)
10562 return 0;
10563 else
10564#endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +020010565 return ((!RedrawingDisabled
10566#ifdef FEAT_EVAL
10567 || ignore_redraw_flag_for_testing
10568#endif
10569 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010570}
10571
10572/*
10573 * Return TRUE if printing messages should currently be done.
10574 */
10575 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010576messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010577{
10578 return (!(p_lz && char_avail() && !KeyTyped));
10579}
10580
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010581#ifdef FEAT_MENU
10582/*
10583 * Draw the window toolbar.
10584 */
10585 static void
10586redraw_win_toolbar(win_T *wp)
10587{
10588 vimmenu_T *menu;
10589 int item_idx = 0;
10590 int item_count = 0;
10591 int col = 0;
10592 int next_col;
10593 int off = (int)(current_ScreenLine - ScreenLines);
10594 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
10595 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
10596
10597 vim_free(wp->w_winbar_items);
10598 for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
10599 ++item_count;
Bram Moolenaarc799fe22019-05-28 23:08:19 +020010600 wp->w_winbar_items = ALLOC_CLEAR_MULT(winbar_item_T, item_count + 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010601
10602 /* TODO: use fewer spaces if there is not enough room */
10603 for (menu = wp->w_winbar->children;
Bram Moolenaar02631462017-09-22 15:20:32 +020010604 menu != NULL && col < wp->w_width; menu = menu->next)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010605 {
10606 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010607 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010608 break;
10609 if (col > 1)
10610 {
10611 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010612 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010613 break;
10614 }
10615
10616 wp->w_winbar_items[item_idx].wb_startcol = col;
10617 space_to_screenline(off + col, button_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010618 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010619 break;
10620
10621 next_col = text_to_screenline(wp, menu->name, col);
10622 while (col < next_col)
10623 {
10624 ScreenAttrs[off + col] = button_attr;
10625 ++col;
10626 }
10627 wp->w_winbar_items[item_idx].wb_endcol = col;
10628 wp->w_winbar_items[item_idx].wb_menu = menu;
10629 ++item_idx;
10630
Bram Moolenaar02631462017-09-22 15:20:32 +020010631 if (col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010632 break;
10633 space_to_screenline(off + col, button_attr);
10634 ++col;
10635 }
Bram Moolenaar02631462017-09-22 15:20:32 +020010636 while (col < wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010637 {
10638 space_to_screenline(off + col, fill_attr);
10639 ++col;
10640 }
10641 wp->w_winbar_items[item_idx].wb_menu = NULL; /* end marker */
10642
Bram Moolenaar02631462017-09-22 15:20:32 +020010643 screen_line(wp->w_winrow, wp->w_wincol, (int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +020010644 (int)wp->w_width, 0);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010645}
10646#endif
Bram Moolenaar491ac282018-06-17 14:47:55 +020010647
Bram Moolenaar071d4272004-06-13 20:20:40 +000010648/*
10649 * Show current status info in ruler and various other places
10650 * If always is FALSE, only show ruler if position has changed.
10651 */
10652 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010653showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010654{
10655 if (!always && !redrawing())
10656 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010657 if (pum_visible())
10658 {
10659 /* Don't redraw right now, do it later. */
10660 curwin->w_redr_status = TRUE;
10661 return;
10662 }
Bram Moolenaar4033c552017-09-16 20:54:51 +020010663#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010664 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar362f3562009-11-03 16:20:34 +000010665 redraw_custom_statusline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010666 else
10667#endif
10668#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +020010669 win_redr_ruler(curwin, always, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010670#endif
10671
10672#ifdef FEAT_TITLE
10673 if (need_maketitle
10674# ifdef FEAT_STL_OPT
10675 || (p_icon && (stl_syntax & STL_IN_ICON))
10676 || (p_title && (stl_syntax & STL_IN_TITLE))
10677# endif
10678 )
10679 maketitle();
10680#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010681 /* Redraw the tab pages line if needed. */
10682 if (redraw_tabline)
10683 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010684}
10685
10686#ifdef FEAT_CMDL_INFO
10687 static void
Bram Moolenaar491ac282018-06-17 14:47:55 +020010688win_redr_ruler(win_T *wp, int always, int ignore_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010689{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010690#define RULER_BUF_LEN 70
10691 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010692 int row;
10693 int fillchar;
10694 int attr;
10695 int empty_line = FALSE;
10696 colnr_T virtcol;
10697 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010698 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010699 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010700 int this_ru_col;
10701 int off = 0;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +010010702 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010703
10704 /* If 'ruler' off or redrawing disabled, don't do anything */
10705 if (!p_ru)
10706 return;
10707
10708 /*
10709 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10710 * after deleting lines, before cursor.lnum is corrected.
10711 */
10712 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10713 return;
10714
Bram Moolenaare2c453d2019-08-21 14:37:09 +020010715 // Don't draw the ruler while doing insert-completion, it might overwrite
10716 // the (long) mode message.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010717 if (wp == lastwin && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010718 if (edit_submode != NULL)
10719 return;
Bram Moolenaar491ac282018-06-17 14:47:55 +020010720 // Don't draw the ruler when the popup menu is visible, it may overlap.
10721 // Except when the popup menu will be redrawn anyway.
10722 if (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010723 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010724
10725#ifdef FEAT_STL_OPT
10726 if (*p_ruf)
10727 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010728 int save_called_emsg = called_emsg;
10729
10730 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010731 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010732 if (called_emsg)
10733 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010734 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010735 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010736 return;
10737 }
10738#endif
10739
10740 /*
10741 * Check if not in Insert mode and the line is empty (will show "0-1").
10742 */
10743 if (!(State & INSERT)
10744 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10745 empty_line = TRUE;
10746
10747 /*
10748 * Only draw the ruler when something changed.
10749 */
10750 validate_virtcol_win(wp);
10751 if ( redraw_cmdline
10752 || always
10753 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10754 || wp->w_cursor.col != wp->w_ru_cursor.col
10755 || wp->w_virtcol != wp->w_ru_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +000010756 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
Bram Moolenaar071d4272004-06-13 20:20:40 +000010757 || wp->w_topline != wp->w_ru_topline
10758 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10759#ifdef FEAT_DIFF
10760 || wp->w_topfill != wp->w_ru_topfill
10761#endif
10762 || empty_line != wp->w_ru_empty)
10763 {
10764 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010765 if (wp->w_status_height)
10766 {
10767 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010768 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar53f81742017-09-22 14:35:51 +020010769 off = wp->w_wincol;
Bram Moolenaar02631462017-09-22 15:20:32 +020010770 width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010771 }
10772 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010773 {
10774 row = Rows - 1;
10775 fillchar = ' ';
10776 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010777 width = Columns;
10778 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010779 }
10780
10781 /* In list mode virtcol needs to be recomputed */
10782 virtcol = wp->w_virtcol;
10783 if (wp->w_p_list && lcs_tab1 == NUL)
10784 {
10785 wp->w_p_list = FALSE;
10786 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10787 wp->w_p_list = TRUE;
10788 }
10789
10790 /*
10791 * Some sprintfs return the length, some return a pointer.
10792 * To avoid portability problems we use strlen() here.
10793 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010794 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010795 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10796 ? 0L
10797 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010798 len = STRLEN(buffer);
10799 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010800 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10801 (int)virtcol + 1);
10802
10803 /*
10804 * Add a "50%" if there is room for it.
10805 * On the last line, don't print in the last column (scrolls the
10806 * screen up on some terminals).
10807 */
10808 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010809 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010810 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010811 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010812 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010813 this_ru_col = ru_col - (Columns - width);
10814 if (this_ru_col < 0)
10815 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010816 /* Never use more than half the window/screen width, leave the other
10817 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010818 if (this_ru_col < (width + 1) / 2)
10819 this_ru_col = (width + 1) / 2;
10820 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010821 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010010822 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010823 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010824 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010825 if (has_mbyte)
10826 i += (*mb_char2bytes)(fillchar, buffer + i);
10827 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010828 buffer[i++] = fillchar;
10829 ++o;
10830 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010831 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010832 }
10833 /* Truncate at window boundary. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010834 if (has_mbyte)
10835 {
10836 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010837 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010838 {
10839 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010840 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010841 {
10842 buffer[i] = NUL;
10843 break;
10844 }
10845 }
10846 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010010847 else if (this_ru_col + (int)STRLEN(buffer) > width)
Bram Moolenaar4033c552017-09-16 20:54:51 +020010848 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010849
Bram Moolenaar4033c552017-09-16 20:54:51 +020010850 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010851 i = redraw_cmdline;
10852 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020010853 this_ru_col + off + (int)STRLEN(buffer),
10854 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010855 fillchar, fillchar, attr);
10856 /* don't redraw the cmdline because of showing the ruler */
10857 redraw_cmdline = i;
10858 wp->w_ru_cursor = wp->w_cursor;
10859 wp->w_ru_virtcol = wp->w_virtcol;
10860 wp->w_ru_empty = empty_line;
10861 wp->w_ru_topline = wp->w_topline;
10862 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10863#ifdef FEAT_DIFF
10864 wp->w_ru_topfill = wp->w_topfill;
10865#endif
10866 }
10867}
10868#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010869
Bram Moolenaare677df82019-09-02 22:31:11 +020010870/*
10871 * Compute columns for ruler and shown command. 'sc_col' is also used to
10872 * decide what the maximum length of a message on the status line can be.
10873 * If there is a status line for the last window, 'sc_col' is independent
10874 * of 'ru_col'.
10875 */
10876
10877#define COL_RULER 17 // columns needed by standard ruler
10878
10879 void
10880comp_col(void)
10881{
10882#if defined(FEAT_CMDL_INFO)
10883 int last_has_status = (p_ls == 2 || (p_ls == 1 && !ONE_WINDOW));
10884
10885 sc_col = 0;
10886 ru_col = 0;
10887 if (p_ru)
10888 {
10889# ifdef FEAT_STL_OPT
10890 ru_col = (ru_wid ? ru_wid : COL_RULER) + 1;
10891# else
10892 ru_col = COL_RULER + 1;
10893# endif
10894 // no last status line, adjust sc_col
10895 if (!last_has_status)
10896 sc_col = ru_col;
10897 }
10898 if (p_sc)
10899 {
10900 sc_col += SHOWCMD_COLS;
10901 if (!p_ru || last_has_status) // no need for separating space
10902 ++sc_col;
10903 }
10904 sc_col = Columns - sc_col;
10905 ru_col = Columns - ru_col;
10906 if (sc_col <= 0) // screen too narrow, will become a mess
10907 sc_col = 1;
10908 if (ru_col <= 0)
10909 ru_col = 1;
10910#else
10911 sc_col = Columns;
10912 ru_col = Columns;
10913#endif
10914#ifdef FEAT_EVAL
10915 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
10916#endif
10917}
10918
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010919#if defined(FEAT_LINEBREAK) || defined(PROTO)
10920/*
Bram Moolenaar64486672010-05-16 15:46:46 +020010921 * Return the width of the 'number' and 'relativenumber' column.
10922 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010923 * Otherwise it depends on 'numberwidth' and the line count.
10924 */
10925 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010926number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010927{
10928 int n;
10929 linenr_T lnum;
10930
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020010931 if (wp->w_p_rnu && !wp->w_p_nu)
10932 /* cursor line shows "0" */
10933 lnum = wp->w_height;
10934 else
10935 /* cursor line shows absolute line number */
10936 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020010937
Bram Moolenaar6b314672015-03-20 15:42:10 +010010938 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010939 return wp->w_nrwidth_width;
10940 wp->w_nrwidth_line_count = lnum;
10941
10942 n = 0;
10943 do
10944 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010945 lnum /= 10;
10946 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010947 } while (lnum > 0);
10948
10949 /* 'numberwidth' gives the minimal width plus one */
10950 if (n < wp->w_p_nuw - 1)
10951 n = wp->w_p_nuw - 1;
10952
Bram Moolenaare4b407f2019-07-04 11:59:28 +020010953# ifdef FEAT_SIGNS
10954 // If 'signcolumn' is set to 'number' and there is a sign to display, then
10955 // the minimal width for the number column is 2.
10956 if (n < 2 && (wp->w_buffer->b_signlist != NULL)
10957 && (*wp->w_p_scl == 'n' && *(wp->w_p_scl + 1) == 'u'))
10958 n = 2;
10959# endif
10960
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010961 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010010962 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010963 return n;
10964}
10965#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010966
Bram Moolenaar113e1072019-01-20 15:30:40 +010010967#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010968/*
10969 * Return the current cursor column. This is the actual position on the
10970 * screen. First column is 0.
10971 */
10972 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010973screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010974{
10975 return screen_cur_col;
10976}
10977
10978/*
10979 * Return the current cursor row. This is the actual position on the screen.
10980 * First row is 0.
10981 */
10982 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010983screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010984{
10985 return screen_cur_row;
10986}
Bram Moolenaar113e1072019-01-20 15:30:40 +010010987#endif
Bram Moolenaare677df82019-09-02 22:31:11 +020010988
10989/*
10990 * Handle setting 'listchars' or 'fillchars'.
10991 * Returns error message, NULL if it's OK.
10992 */
10993 char *
10994set_chars_option(char_u **varp)
10995{
10996 int round, i, len, entries;
10997 char_u *p, *s;
10998 int c1 = 0, c2 = 0, c3 = 0;
10999 struct charstab
11000 {
11001 int *cp;
11002 char *name;
11003 };
11004 static struct charstab filltab[] =
11005 {
11006 {&fill_stl, "stl"},
11007 {&fill_stlnc, "stlnc"},
11008 {&fill_vert, "vert"},
11009 {&fill_fold, "fold"},
11010 {&fill_diff, "diff"},
11011 };
11012 static struct charstab lcstab[] =
11013 {
11014 {&lcs_eol, "eol"},
11015 {&lcs_ext, "extends"},
11016 {&lcs_nbsp, "nbsp"},
11017 {&lcs_prec, "precedes"},
11018 {&lcs_space, "space"},
11019 {&lcs_tab2, "tab"},
11020 {&lcs_trail, "trail"},
11021#ifdef FEAT_CONCEAL
11022 {&lcs_conceal, "conceal"},
11023#else
11024 {NULL, "conceal"},
11025#endif
11026 };
11027 struct charstab *tab;
11028
11029 if (varp == &p_lcs)
11030 {
11031 tab = lcstab;
11032 entries = sizeof(lcstab) / sizeof(struct charstab);
11033 }
11034 else
11035 {
11036 tab = filltab;
11037 entries = sizeof(filltab) / sizeof(struct charstab);
11038 }
11039
11040 // first round: check for valid value, second round: assign values
11041 for (round = 0; round <= 1; ++round)
11042 {
11043 if (round > 0)
11044 {
11045 // After checking that the value is valid: set defaults: space for
11046 // 'fillchars', NUL for 'listchars'
11047 for (i = 0; i < entries; ++i)
11048 if (tab[i].cp != NULL)
11049 *(tab[i].cp) = (varp == &p_lcs ? NUL : ' ');
11050
11051 if (varp == &p_lcs)
11052 {
11053 lcs_tab1 = NUL;
11054 lcs_tab3 = NUL;
11055 }
11056 else
11057 fill_diff = '-';
11058 }
11059 p = *varp;
11060 while (*p)
11061 {
11062 for (i = 0; i < entries; ++i)
11063 {
11064 len = (int)STRLEN(tab[i].name);
11065 if (STRNCMP(p, tab[i].name, len) == 0
11066 && p[len] == ':'
11067 && p[len + 1] != NUL)
11068 {
11069 c2 = c3 = 0;
11070 s = p + len + 1;
11071 c1 = mb_ptr2char_adv(&s);
11072 if (mb_char2cells(c1) > 1)
11073 continue;
11074 if (tab[i].cp == &lcs_tab2)
11075 {
11076 if (*s == NUL)
11077 continue;
11078 c2 = mb_ptr2char_adv(&s);
11079 if (mb_char2cells(c2) > 1)
11080 continue;
11081 if (!(*s == ',' || *s == NUL))
11082 {
11083 c3 = mb_ptr2char_adv(&s);
11084 if (mb_char2cells(c3) > 1)
11085 continue;
11086 }
11087 }
11088
11089 if (*s == ',' || *s == NUL)
11090 {
11091 if (round)
11092 {
11093 if (tab[i].cp == &lcs_tab2)
11094 {
11095 lcs_tab1 = c1;
11096 lcs_tab2 = c2;
11097 lcs_tab3 = c3;
11098 }
11099 else if (tab[i].cp != NULL)
11100 *(tab[i].cp) = c1;
11101
11102 }
11103 p = s;
11104 break;
11105 }
11106 }
11107 }
11108
11109 if (i == entries)
11110 return e_invarg;
11111 if (*p == ',')
11112 ++p;
11113 }
11114 }
11115
11116 return NULL; // no error
11117}
Bram Moolenaar017ba072019-09-14 21:01:23 +020011118
11119#ifdef FEAT_SYN_HL
11120/*
11121 * Used when 'cursorlineopt' contains "screenline": compute the margins between
11122 * which the highlighting is used.
11123 */
11124 static void
11125margin_columns_win(win_T *wp, int *left_col, int *right_col)
11126{
11127 // cache previous calculations depending on w_virtcol
11128 static int saved_w_virtcol;
11129 static win_T *prev_wp;
11130 static int prev_left_col;
11131 static int prev_right_col;
11132 static int prev_col_off;
11133
11134 int cur_col_off = win_col_off(wp);
11135 int width1;
11136 int width2;
11137
11138 if (saved_w_virtcol == wp->w_virtcol
11139 && prev_wp == wp && prev_col_off == cur_col_off)
11140 {
11141 *right_col = prev_right_col;
11142 *left_col = prev_left_col;
11143 return;
11144 }
11145
11146 width1 = wp->w_width - cur_col_off;
11147 width2 = width1 + win_col_off2(wp);
11148
11149 *left_col = 0;
11150 *right_col = width1;
11151
11152 if (wp->w_virtcol >= (colnr_T)width1)
11153 *right_col = width1 + ((wp->w_virtcol - width1) / width2 + 1) * width2;
11154 if (wp->w_virtcol >= (colnr_T)width1 && width2 > 0)
11155 *left_col = (wp->w_virtcol - width1) / width2 * width2 + width1;
11156
11157 // cache values
11158 prev_left_col = *left_col;
11159 prev_right_col = *right_col;
11160 prev_wp = wp;
11161 saved_w_virtcol = wp->w_virtcol;
11162 prev_col_off = cur_col_off;
11163}
11164#endif