blob: 4ea0cffbd7b6d180c1818f878e41bd15c98985e1 [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;
5377 }
5378#endif
5379
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 /* Don't override visual selection highlighting. */
5381 if (n_attr > 0
5382 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005383 && !attr_pri)
Bram Moolenaar017ba072019-09-14 21:01:23 +02005384 {
5385 if (line_attr)
5386 char_attr = hl_combine_attr(extra_attr, line_attr);
5387 else
5388 char_attr = extra_attr;
5389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390
Bram Moolenaar81695252004-12-29 20:58:21 +00005391#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 /* XIM don't send preedit_start and preedit_end, but they send
5393 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5394 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005395 if (p_imst == IM_ON_THE_SPOT
5396 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005397 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 && (State & INSERT)
5399 && !p_imdisable
5400 && im_is_preediting()
5401 && draw_state == WL_LINE)
5402 {
5403 colnr_T tcol;
5404
5405 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005406 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 else
5408 tcol = preedit_end_col;
5409 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5410 {
5411 if (feedback_old_attr < 0)
5412 {
5413 feedback_col = 0;
5414 feedback_old_attr = char_attr;
5415 }
5416 char_attr = im_get_feedback_attr(feedback_col);
5417 if (char_attr < 0)
5418 char_attr = feedback_old_attr;
5419 feedback_col++;
5420 }
5421 else if (feedback_old_attr >= 0)
5422 {
5423 char_attr = feedback_old_attr;
5424 feedback_old_attr = -1;
5425 feedback_col = 0;
5426 }
5427 }
5428#endif
5429 /*
5430 * Handle the case where we are in column 0 but not on the first
5431 * character of the line and the user wants us to show us a
5432 * special character (via 'listchars' option "precedes:<char>".
5433 */
5434 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005435 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5437#ifdef FEAT_DIFF
5438 && filler_todo <= 0
5439#endif
5440 && draw_state > WL_NR
5441 && c != NUL)
5442 {
5443 c = lcs_prec;
5444 lcs_prec_todo = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005445 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5446 {
5447 /* Double-width character being overwritten by the "precedes"
5448 * character, need to fill up half the character. */
5449 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005450 c_final = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005451 n_extra = 1;
5452 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005453 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005454 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005456 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 {
5458 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005459 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005460 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 }
5462 else
5463 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005464 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 {
5466 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005467 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 n_attr3 = 1;
5469 }
5470 }
5471
5472 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005473 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 */
Bram Moolenaarf914a332019-07-20 15:09:56 +02005475 if ((c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005476#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005477 || did_line_attr == 1
5478#endif
Bram Moolenaarf914a332019-07-20 15:09:56 +02005479 ) && eol_hl_off == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005481#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarbbca7732019-07-24 18:13:16 +02005482 // flag to indicate whether prevcol equals startcol of search_hl or
5483 // one of the matches
5484 int prevcol_hl_flag = get_prevcol_hl_flag(wp, &search_hl,
5485 (long)(ptr - line) - (c == NUL));
Bram Moolenaar91170f82006-05-05 21:15:17 +00005486#endif
Bram Moolenaarbbca7732019-07-24 18:13:16 +02005487 // Invert at least one char, used for Visual and empty line or
5488 // highlight match at end of line. If it's beyond the last
5489 // char on the screen, just overwrite that one (tricky!) Not
5490 // needed when a '$' was displayed for 'list'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005492 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005493 && (VIsual_mode != Ctrl_V
5494 || lnum == VIsual.lnum
5495 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005496 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarbbca7732019-07-24 18:13:16 +02005498 // highlight 'hlsearch' match at end of line
5499 || (prevcol_hl_flag
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005500# ifdef FEAT_SYN_HL
5501 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
5502 && !(wp == curwin && VIsual_active))
5503# endif
5504# ifdef FEAT_DIFF
5505 && diff_hlf == (hlf_T)0
5506# endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005507# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005508 && did_line_attr <= 1
5509# endif
5510 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511#endif
5512 ))
5513 {
5514 int n = 0;
5515
5516#ifdef FEAT_RIGHTLEFT
5517 if (wp->w_p_rl)
5518 {
5519 if (col < 0)
5520 n = 1;
5521 }
5522 else
5523#endif
5524 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005525 if (col >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526 n = -1;
5527 }
5528 if (n != 0)
5529 {
5530 /* At the window boundary, highlight the last character
5531 * instead (better than nothing). */
5532 off += n;
5533 col += n;
5534 }
5535 else
5536 {
5537 /* Add a blank character to highlight. */
5538 ScreenLines[off] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 if (enc_utf8)
5540 ScreenLinesUC[off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 }
5542#ifdef FEAT_SEARCH_EXTRA
5543 if (area_attr == 0)
5544 {
Bram Moolenaarbbca7732019-07-24 18:13:16 +02005545 // Use attributes from match with highest priority among
5546 // 'search_hl' and the match list.
5547 get_search_match_hl(wp, &search_hl,
5548 (long)(ptr - line), &char_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 }
5550#endif
5551 ScreenAttrs[off] = char_attr;
5552#ifdef FEAT_RIGHTLEFT
5553 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005554 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005556 --off;
5557 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 else
5559#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005560 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005562 ++off;
5563 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005564 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005565 eol_hl_off = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568
Bram Moolenaar91170f82006-05-05 21:15:17 +00005569 /*
5570 * At end of the text line.
5571 */
5572 if (c == NUL)
5573 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005574#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005575 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005576 if (wp->w_p_wrap)
5577 v = wp->w_skipcol;
5578 else
5579 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005580
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005581 /* check if line ends before left margin */
5582 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005583 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005584#ifdef FEAT_CONCEAL
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01005585 // Get rid of the boguscols now, we want to draw until the right
5586 // edge for 'cursorcolumn'.
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005587 col -= boguscols;
5588 boguscols = 0;
5589#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005590
5591 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005592 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005593
5594 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005595 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5596 && (int)wp->w_virtcol <
Bram Moolenaar02631462017-09-22 15:20:32 +02005597 wp->w_width * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005598 && lnum != wp->w_cursor.lnum)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005599 || draw_color_col
5600 || win_attr != 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005601# ifdef FEAT_RIGHTLEFT
5602 && !wp->w_p_rl
5603# endif
5604 )
5605 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005606 int rightmost_vcol = 0;
5607 int i;
5608
5609 if (wp->w_p_cuc)
5610 rightmost_vcol = wp->w_virtcol;
5611 if (draw_color_col)
5612 /* determine rightmost colorcolumn to possibly draw */
5613 for (i = 0; color_cols[i] >= 0; ++i)
5614 if (rightmost_vcol < color_cols[i])
5615 rightmost_vcol = color_cols[i];
5616
Bram Moolenaar02631462017-09-22 15:20:32 +02005617 while (col < wp->w_width)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005618 {
5619 ScreenLines[off] = ' ';
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005620 if (enc_utf8)
5621 ScreenLinesUC[off] = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005622 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005623 if (draw_color_col)
5624 draw_color_col = advance_color_col(VCOL_HLC,
5625 &color_cols);
5626
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005627 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005628 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005629 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005630 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005631 else
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005632 ScreenAttrs[off++] = win_attr;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005633
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005634 if (VCOL_HLC >= rightmost_vcol && win_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005635 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005636
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005637 ++vcol;
5638 }
5639 }
5640#endif
5641
Bram Moolenaar53f81742017-09-22 14:35:51 +02005642 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005643 (int)wp->w_width, screen_line_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 row++;
5645
5646 /*
5647 * Update w_cline_height and w_cline_folded if the cursor line was
5648 * updated (saves a call to plines() later).
5649 */
5650 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5651 {
5652 curwin->w_cline_row = startrow;
5653 curwin->w_cline_height = row - startrow;
5654#ifdef FEAT_FOLDING
5655 curwin->w_cline_folded = FALSE;
5656#endif
5657 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5658 }
5659
5660 break;
5661 }
5662
Bram Moolenaara5c6a0b2019-05-08 20:20:46 +02005663 // Show "extends" character from 'listchars' if beyond the line end and
5664 // 'list' is set.
5665 if (lcs_ext != NUL
5666 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 && !wp->w_p_wrap
5668#ifdef FEAT_DIFF
5669 && filler_todo <= 0
5670#endif
5671 && (
5672#ifdef FEAT_RIGHTLEFT
5673 wp->w_p_rl ? col == 0 :
5674#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005675 col == wp->w_width - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005677 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5679 {
5680 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005681 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005683 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 {
5685 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005686 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005687 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 }
5689 else
5690 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 }
5692
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005693#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005694 /* advance to the next 'colorcolumn' */
5695 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005696 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005697
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005698 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005699 * highlight the cursor position itself.
5700 * Also highlight the 'colorcolumn' if it is different than
5701 * 'cursorcolumn' */
5702 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005703 if (draw_state == WL_LINE && !lnum_in_visual_area
5704 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005705 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005706 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005707 && lnum != wp->w_cursor.lnum)
5708 {
5709 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005710 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005711 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005712 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005713 {
5714 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005715 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005716 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005717 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005718#endif
5719
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 /*
5721 * Store character to be displayed.
5722 * Skip characters that are left of the screen for 'nowrap'.
5723 */
5724 vcol_prev = vcol;
5725 if (draw_state < WL_LINE || n_skip <= 0)
5726 {
5727 /*
5728 * Store the character.
5729 */
Bram Moolenaara12a1612019-01-24 16:39:02 +01005730#if defined(FEAT_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5732 {
5733 /* A double-wide character is: put first halve in left cell. */
5734 --off;
5735 --col;
5736 }
5737#endif
5738 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005740 {
5741 if ((mb_c & 0xff00) == 0x8e00)
5742 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 else if (enc_utf8)
5746 {
5747 if (mb_utf8)
5748 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005749 int i;
5750
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005752 if ((c & 0xff) == 0)
5753 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005754 for (i = 0; i < Screen_mco; ++i)
5755 {
5756 ScreenLinesC[i][off] = u8cc[i];
5757 if (u8cc[i] == 0)
5758 break;
5759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 }
5761 else
5762 ScreenLinesUC[off] = 0;
5763 }
5764 if (multi_attr)
5765 {
5766 ScreenAttrs[off] = multi_attr;
5767 multi_attr = 0;
5768 }
5769 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 ScreenAttrs[off] = char_attr;
5771
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5773 {
5774 /* Need to fill two screen columns. */
5775 ++off;
5776 ++col;
5777 if (enc_utf8)
5778 /* UTF-8: Put a 0 in the second screen char. */
5779 ScreenLines[off] = 0;
5780 else
5781 /* DBCS: Put second byte in the second screen char. */
5782 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005783 if (draw_state > WL_NR
5784#ifdef FEAT_DIFF
5785 && filler_todo <= 0
5786#endif
5787 )
5788 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789 /* When "tocol" is halfway a character, set it to the end of
5790 * the character, otherwise highlighting won't stop. */
5791 if (tocol == vcol)
5792 ++tocol;
5793#ifdef FEAT_RIGHTLEFT
5794 if (wp->w_p_rl)
5795 {
5796 /* now it's time to backup one cell */
5797 --off;
5798 --col;
5799 }
5800#endif
5801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802#ifdef FEAT_RIGHTLEFT
5803 if (wp->w_p_rl)
5804 {
5805 --off;
5806 --col;
5807 }
5808 else
5809#endif
5810 {
5811 ++off;
5812 ++col;
5813 }
5814 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005815#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005816 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005817 {
5818 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005819 ++vcol_off;
5820 if (n_extra > 0)
5821 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005822 if (wp->w_p_wrap)
5823 {
5824 /*
5825 * Special voodoo required if 'wrap' is on.
5826 *
5827 * Advance the column indicator to force the line
5828 * drawing to wrap early. This will make the line
5829 * take up the same screen space when parts are concealed,
5830 * so that cursor line computations aren't messed up.
5831 *
5832 * To avoid the fictitious advance of 'col' causing
5833 * trailing junk to be written out of the screen line
5834 * we are building, 'boguscols' keeps track of the number
5835 * of bad columns we have advanced.
5836 */
5837 if (n_extra > 0)
5838 {
5839 vcol += n_extra;
5840# ifdef FEAT_RIGHTLEFT
5841 if (wp->w_p_rl)
5842 {
5843 col -= n_extra;
5844 boguscols -= n_extra;
5845 }
5846 else
5847# endif
5848 {
5849 col += n_extra;
5850 boguscols += n_extra;
5851 }
5852 n_extra = 0;
5853 n_attr = 0;
5854 }
5855
5856
Bram Moolenaar860cae12010-06-05 23:22:07 +02005857 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5858 {
5859 /* Need to fill two screen columns. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01005860# ifdef FEAT_RIGHTLEFT
Bram Moolenaar860cae12010-06-05 23:22:07 +02005861 if (wp->w_p_rl)
5862 {
5863 --boguscols;
5864 --col;
5865 }
5866 else
Bram Moolenaara12a1612019-01-24 16:39:02 +01005867# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02005868 {
5869 ++boguscols;
5870 ++col;
5871 }
5872 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005873
5874# ifdef FEAT_RIGHTLEFT
5875 if (wp->w_p_rl)
5876 {
5877 --boguscols;
5878 --col;
5879 }
5880 else
5881# endif
5882 {
5883 ++boguscols;
5884 ++col;
5885 }
5886 }
5887 else
5888 {
5889 if (n_extra > 0)
5890 {
5891 vcol += n_extra;
5892 n_extra = 0;
5893 n_attr = 0;
5894 }
5895 }
5896
5897 }
5898#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 else
5900 --n_skip;
5901
Bram Moolenaar64486672010-05-16 15:46:46 +02005902 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5903 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005904 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905#ifdef FEAT_DIFF
5906 && filler_todo <= 0
5907#endif
5908 )
5909 ++vcol;
5910
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005911#ifdef FEAT_SYN_HL
5912 if (vcol_save_attr >= 0)
5913 char_attr = vcol_save_attr;
5914#endif
5915
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 /* restore attributes after "predeces" in 'listchars' */
5917 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5918 char_attr = saved_attr3;
5919
5920 /* restore attributes after last 'listchars' or 'number' char */
5921 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5922 char_attr = saved_attr2;
5923
5924 /*
5925 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005926 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927 */
5928 if ((
5929#ifdef FEAT_RIGHTLEFT
5930 wp->w_p_rl ? (col < 0) :
5931#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005932 (col >= wp->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 && (*ptr != NUL
5934#ifdef FEAT_DIFF
5935 || filler_todo > 0
5936#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005937 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5939 )
5940 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005941#ifdef FEAT_CONCEAL
Bram Moolenaar53f81742017-09-22 14:35:51 +02005942 screen_line(screen_row, wp->w_wincol, col - boguscols,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005943 (int)wp->w_width, screen_line_flags);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005944 boguscols = 0;
5945#else
Bram Moolenaar53f81742017-09-22 14:35:51 +02005946 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005947 (int)wp->w_width, screen_line_flags);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005948#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949 ++row;
5950 ++screen_row;
5951
5952 /* When not wrapping and finished diff lines, or when displayed
5953 * '$' and highlighting until last column, break here. */
5954 if ((!wp->w_p_wrap
5955#ifdef FEAT_DIFF
5956 && filler_todo <= 0
5957#endif
5958 ) || lcs_eol_one == -1)
5959 break;
5960
5961 /* When the window is too narrow draw all "@" lines. */
5962 if (draw_state != WL_LINE
5963#ifdef FEAT_DIFF
5964 && filler_todo <= 0
5965#endif
5966 )
5967 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01005968 win_draw_end(wp, '@', ' ', TRUE, row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970 row = endrow;
5971 }
5972
5973 /* When line got too long for screen break here. */
5974 if (row == endrow)
5975 {
5976 ++row;
5977 break;
5978 }
5979
5980 if (screen_cur_row == screen_row - 1
5981#ifdef FEAT_DIFF
5982 && filler_todo <= 0
5983#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005984 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985 {
5986 /* Remember that the line wraps, used for modeless copy. */
5987 LineWraps[screen_row - 1] = TRUE;
5988
5989 /*
5990 * Special trick to make copy/paste of wrapped lines work with
5991 * xterm/screen: write an extra character beyond the end of
5992 * the line. This will work with all terminal types
5993 * (regardless of the xn,am settings).
5994 * Only do this on a fast tty.
5995 * Only do this if the cursor is on the current line
5996 * (something has been written in it).
5997 * Don't do this for the GUI.
5998 * Don't do this for double-width characters.
5999 * Don't do this for a window not at the right screen border.
6000 */
6001 if (p_tf
6002#ifdef FEAT_GUI
6003 && !gui.in_use
6004#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00006006 && ((*mb_off2cells)(LineOffset[screen_row],
6007 LineOffset[screen_row] + screen_Columns)
6008 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00006010 + (int)Columns - 2,
6011 LineOffset[screen_row] + screen_Columns)
Bram Moolenaara12a1612019-01-24 16:39:02 +01006012 == 2)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013 {
6014 /* First make sure we are at the end of the screen line,
6015 * then output the same character again to let the
6016 * terminal know about the wrap. If the terminal doesn't
6017 * auto-wrap, we overwrite the character. */
Bram Moolenaar02631462017-09-22 15:20:32 +02006018 if (screen_cur_col != wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019 screen_char(LineOffset[screen_row - 1]
6020 + (unsigned)Columns - 1,
6021 screen_row - 1, (int)(Columns - 1));
6022
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023 /* When there is a multi-byte character, just output a
6024 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00006025 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
6026 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027 out_char(' ');
6028 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029 out_char(ScreenLines[LineOffset[screen_row - 1]
6030 + (Columns - 1)]);
6031 /* force a redraw of the first char on the next line */
6032 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
6033 screen_start(); /* don't know where cursor is now */
6034 }
6035 }
6036
6037 col = 0;
6038 off = (unsigned)(current_ScreenLine - ScreenLines);
6039#ifdef FEAT_RIGHTLEFT
6040 if (wp->w_p_rl)
6041 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006042 col = wp->w_width - 1; /* col is not used if breaking! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043 off += col;
6044 }
6045#endif
6046
6047 /* reset the drawing state for the start of a wrapped line */
6048 draw_state = WL_START;
6049 saved_n_extra = n_extra;
6050 saved_p_extra = p_extra;
6051 saved_c_extra = c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01006052 saved_c_final = c_final;
Bram Moolenaar017ba072019-09-14 21:01:23 +02006053#ifdef FEAT_SYN_HL
6054 if (!(cul_screenline
6055# ifdef FEAT_DIFF
6056 && diff_hlf == (hlf_T)0)
6057# endif
6058 )
6059 saved_char_attr = char_attr;
6060 else
6061#endif
6062 saved_char_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063 n_extra = 0;
6064 lcs_prec_todo = lcs_prec;
6065#ifdef FEAT_LINEBREAK
6066# ifdef FEAT_DIFF
6067 if (filler_todo <= 0)
6068# endif
6069 need_showbreak = TRUE;
6070#endif
6071#ifdef FEAT_DIFF
6072 --filler_todo;
6073 /* When the filler lines are actually below the last line of the
6074 * file, don't draw the line itself, break here. */
6075 if (filler_todo == 0 && wp->w_botfill)
6076 break;
6077#endif
6078 }
6079
6080 } /* for every character in the line */
6081
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006082#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006083 /* After an empty line check first word for capital. */
6084 if (*skipwhite(line) == NUL)
6085 {
6086 capcol_lnum = lnum + 1;
6087 cap_col = 0;
6088 }
6089#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006090#ifdef FEAT_TEXT_PROP
6091 vim_free(text_props);
6092 vim_free(text_prop_idxs);
6093#endif
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006094
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01006095 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 return row;
6097}
6098
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006099/*
6100 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006101 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006102 */
6103 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006104comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006105{
6106 int i;
6107
6108 for (i = 0; i < Screen_mco; ++i)
6109 {
6110 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
6111 return TRUE;
6112 if (ScreenLinesC[i][off_from] == 0)
6113 break;
6114 }
6115 return FALSE;
6116}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006117
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118/*
6119 * Check whether the given character needs redrawing:
6120 * - the (first byte of the) character is different
6121 * - the attributes are different
6122 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006123 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 */
6125 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006126char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127{
6128 if (cols > 0
6129 && ((ScreenLines[off_from] != ScreenLines[off_to]
6130 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 || (enc_dbcs != 0
6132 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
6133 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
6134 ? ScreenLines2[off_from] != ScreenLines2[off_to]
6135 : (cols > 1 && ScreenLines[off_from + 1]
6136 != ScreenLines[off_to + 1])))
6137 || (enc_utf8
6138 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6139 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006140 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006141 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6142 && ScreenLines[off_from + 1]
Bram Moolenaara12a1612019-01-24 16:39:02 +01006143 != ScreenLines[off_to + 1])))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 return TRUE;
6145 return FALSE;
6146}
6147
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006148#if defined(FEAT_TERMINAL) || defined(PROTO)
6149/*
6150 * Return the index in ScreenLines[] for the current screen line.
6151 */
6152 int
6153screen_get_current_line_off()
6154{
6155 return (int)(current_ScreenLine - ScreenLines);
6156}
6157#endif
6158
Bram Moolenaarc662ec92019-06-23 00:15:57 +02006159#ifdef FEAT_TEXT_PROP
6160/*
6161 * Return TRUE if this position has a higher level popup or this cell is
6162 * transparent in the current popup.
6163 */
6164 static int
6165blocked_by_popup(int row, int col)
6166{
6167 int off;
6168
6169 if (!popup_visible)
6170 return FALSE;
6171 off = row * screen_Columns + col;
6172 return popup_mask[off] > screen_zindex || popup_transparent[off];
6173}
6174#endif
6175
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176/*
6177 * Move one "cooked" screen line to the screen, but only the characters that
6178 * have actually changed. Handle insert/delete character.
6179 * "coloff" gives the first column on the screen for this line.
6180 * "endcol" gives the columns where valid characters are.
6181 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6182 * needs to be cleared, negative otherwise.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006183 * "flags" can have bits:
6184 * SLF_POPUP popup window
6185 * SLF_RIGHTLEFT rightleft window:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6187 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6188 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006189 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006190screen_line(
6191 int row,
6192 int coloff,
6193 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006194 int clear_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006195 int flags UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196{
6197 unsigned off_from;
6198 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006199 unsigned max_off_from;
6200 unsigned max_off_to;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 int force = FALSE; /* force update rest of the line */
6204 int redraw_this /* bool: does character need redraw? */
6205#ifdef FEAT_GUI
6206 = TRUE /* For GUI when while-loop empty */
6207#endif
6208 ;
6209 int redraw_next; /* redraw_this for next character */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 int clear_next = FALSE;
6211 int char_cells; /* 1: normal char */
6212 /* 2: occupies two display cells */
6213# define CHAR_CELLS char_cells
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006215 /* Check for illegal row and col, just in case. */
6216 if (row >= Rows)
6217 row = Rows - 1;
6218 if (endcol > Columns)
6219 endcol = Columns;
6220
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221# ifdef FEAT_CLIPBOARD
6222 clip_may_clear_selection(row, row);
6223# endif
6224
6225 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6226 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006227 max_off_from = off_from + screen_Columns;
6228 max_off_to = LineOffset[row] + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229
6230#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006231 if (flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232 {
6233 /* Clear rest first, because it's left of the text. */
6234 if (clear_width > 0)
6235 {
6236 while (col <= endcol && ScreenLines[off_to] == ' '
6237 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006238 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 {
6240 ++off_to;
6241 ++col;
6242 }
6243 if (col <= endcol)
6244 screen_fill(row, row + 1, col + coloff,
6245 endcol + coloff + 1, ' ', ' ', 0);
6246 }
6247 col = endcol + 1;
6248 off_to = LineOffset[row] + col + coloff;
6249 off_from += col;
6250 endcol = (clear_width > 0 ? clear_width : -clear_width);
6251 }
6252#endif /* FEAT_RIGHTLEFT */
6253
6254 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6255
6256 while (col < endcol)
6257 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006259 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 else
6261 char_cells = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262
6263 redraw_this = redraw_next;
6264 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6265 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6266
6267#ifdef FEAT_GUI
6268 /* If the next character was bold, then redraw the current character to
6269 * remove any pixels that might have spilt over into us. This only
6270 * happens in the GUI.
6271 */
6272 if (redraw_next && gui.in_use)
6273 {
6274 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006275 if (hl > HL_ALL)
6276 hl = syn_attr2attr(hl);
6277 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 redraw_this = TRUE;
6279 }
6280#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02006281#ifdef FEAT_TEXT_PROP
Bram Moolenaarc662ec92019-06-23 00:15:57 +02006282 if (blocked_by_popup(row, col + coloff))
Bram Moolenaar33796b32019-06-08 16:01:13 +02006283 redraw_this = FALSE;
6284#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 if (redraw_this)
6286 {
6287 /*
6288 * Special handling when 'xs' termcap flag set (hpterm):
6289 * Attributes for characters are stored at the position where the
6290 * cursor is when writing the highlighting code. The
6291 * start-highlighting code must be written with the cursor on the
6292 * first highlighted character. The stop-highlighting code must
6293 * be written with the cursor just after the last highlighted
6294 * character.
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01006295 * Overwriting a character doesn't remove its highlighting. Need
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296 * to clear the rest of the line, and force redrawing it
6297 * completely.
6298 */
6299 if ( p_wiv
6300 && !force
6301#ifdef FEAT_GUI
6302 && !gui.in_use
6303#endif
6304 && ScreenAttrs[off_to] != 0
6305 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6306 {
6307 /*
6308 * Need to remove highlighting attributes here.
6309 */
6310 windgoto(row, col + coloff);
6311 out_str(T_CE); /* clear rest of this screen line */
6312 screen_start(); /* don't know where cursor is now */
6313 force = TRUE; /* force redraw of rest of the line */
6314 redraw_next = TRUE; /* or else next char would miss out */
6315
6316 /*
6317 * If the previous character was highlighted, need to stop
6318 * highlighting at this character.
6319 */
6320 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6321 {
6322 screen_attr = ScreenAttrs[off_to - 1];
6323 term_windgoto(row, col + coloff);
6324 screen_stop_highlight();
6325 }
6326 else
6327 screen_attr = 0; /* highlighting has stopped */
6328 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329 if (enc_dbcs != 0)
6330 {
6331 /* Check if overwriting a double-byte with a single-byte or
6332 * the other way around requires another character to be
6333 * redrawn. For UTF-8 this isn't needed, because comparing
6334 * ScreenLinesUC[] is sufficient. */
6335 if (char_cells == 1
6336 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006337 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338 {
6339 /* Writing a single-cell character over a double-cell
6340 * character: need to redraw the next cell. */
6341 ScreenLines[off_to + 1] = 0;
6342 redraw_next = TRUE;
6343 }
6344 else if (char_cells == 2
6345 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006346 && (*mb_off2cells)(off_to, max_off_to) == 1
6347 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348 {
6349 /* Writing the second half of a double-cell character over
6350 * a double-cell character: need to redraw the second
6351 * cell. */
6352 ScreenLines[off_to + 2] = 0;
6353 redraw_next = TRUE;
6354 }
6355
6356 if (enc_dbcs == DBCS_JPNU)
6357 ScreenLines2[off_to] = ScreenLines2[off_from];
6358 }
6359 /* When writing a single-width character over a double-width
6360 * character and at the end of the redrawn text, need to clear out
6361 * the right halve of the old character.
6362 * Also required when writing the right halve of a double-width
6363 * char over the left halve of an existing one. */
6364 if (has_mbyte && col + char_cells == endcol
6365 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006366 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006368 && (*mb_off2cells)(off_to, max_off_to) == 1
6369 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370 clear_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371
6372 ScreenLines[off_to] = ScreenLines[off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373 if (enc_utf8)
6374 {
6375 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6376 if (ScreenLinesUC[off_from] != 0)
6377 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006378 int i;
6379
6380 for (i = 0; i < Screen_mco; ++i)
6381 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382 }
6383 }
6384 if (char_cells == 2)
6385 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386
6387#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006388 /* The bold trick makes a single column of pixels appear in the
6389 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 * character should be redrawn too. This happens for our own GUI
6391 * and for some xterms. */
6392 if (
6393# ifdef FEAT_GUI
6394 gui.in_use
6395# endif
6396# if defined(FEAT_GUI) && defined(UNIX)
6397 ||
6398# endif
6399# ifdef UNIX
6400 term_is_xterm
6401# endif
6402 )
6403 {
6404 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006405 if (hl > HL_ALL)
6406 hl = syn_attr2attr(hl);
6407 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408 redraw_next = TRUE;
6409 }
6410#endif
6411 ScreenAttrs[off_to] = ScreenAttrs[off_from];
Bram Moolenaara12a1612019-01-24 16:39:02 +01006412
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006413 /* For simplicity set the attributes of second half of a
6414 * double-wide character equal to the first half. */
6415 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006417
6418 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421 screen_char(off_to, row, col + coloff);
6422 }
6423 else if ( p_wiv
6424#ifdef FEAT_GUI
6425 && !gui.in_use
6426#endif
6427 && col + coloff > 0)
6428 {
6429 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6430 {
6431 /*
6432 * Don't output stop-highlight when moving the cursor, it will
6433 * stop the highlighting when it should continue.
6434 */
6435 screen_attr = 0;
6436 }
6437 else if (screen_attr != 0)
6438 screen_stop_highlight();
6439 }
6440
6441 off_to += CHAR_CELLS;
6442 off_from += CHAR_CELLS;
6443 col += CHAR_CELLS;
6444 }
6445
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 if (clear_next)
6447 {
6448 /* Clear the second half of a double-wide character of which the left
6449 * half was overwritten with a single-wide character. */
6450 ScreenLines[off_to] = ' ';
6451 if (enc_utf8)
6452 ScreenLinesUC[off_to] = 0;
6453 screen_char(off_to, row, col + coloff);
6454 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455
6456 if (clear_width > 0
6457#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006458 && !(flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459#endif
6460 )
6461 {
6462#ifdef FEAT_GUI
6463 int startCol = col;
6464#endif
6465
6466 /* blank out the rest of the line */
6467 while (col < clear_width && ScreenLines[off_to] == ' '
6468 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006469 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470 {
6471 ++off_to;
6472 ++col;
6473 }
6474 if (col < clear_width)
6475 {
6476#ifdef FEAT_GUI
6477 /*
6478 * In the GUI, clearing the rest of the line may leave pixels
6479 * behind if the first character cleared was bold. Some bold
6480 * fonts spill over the left. In this case we redraw the previous
6481 * character too. If we didn't skip any blanks above, then we
6482 * only redraw if the character wasn't already redrawn anyway.
6483 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006484 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485 {
6486 hl = ScreenAttrs[off_to];
6487 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006488 {
6489 int prev_cells = 1;
Bram Moolenaara12a1612019-01-24 16:39:02 +01006490
Bram Moolenaar9c697322006-10-09 20:11:17 +00006491 if (enc_utf8)
6492 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6493 * that its width is 2. */
6494 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6495 else if (enc_dbcs != 0)
6496 {
6497 /* find previous character by counting from first
6498 * column and get its width. */
6499 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006500 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006501
6502 while (off < off_to)
6503 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006504 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006505 off += prev_cells;
6506 }
6507 }
6508
6509 if (enc_dbcs != 0 && prev_cells > 1)
6510 screen_char_2(off_to - prev_cells, row,
6511 col + coloff - prev_cells);
6512 else
Bram Moolenaar9c697322006-10-09 20:11:17 +00006513 screen_char(off_to - prev_cells, row,
6514 col + coloff - prev_cells);
6515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516 }
6517#endif
6518 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6519 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520 off_to += clear_width - col;
6521 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522 }
6523 }
6524
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006525 if (clear_width > 0
6526#ifdef FEAT_TEXT_PROP
6527 && !(flags & SLF_POPUP) // no separator for popup window
6528#endif
6529 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006531 // For a window that has a right neighbor, draw the separator char
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006532 // right of the window contents. But not on top of a popup window.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006533 if (coloff + col < Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006535#ifdef FEAT_TEXT_PROP
Bram Moolenaarc662ec92019-06-23 00:15:57 +02006536 if (!blocked_by_popup(row, col + coloff))
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006537#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006539 int c;
6540
6541 c = fillchar_vsep(&hl);
6542 if (ScreenLines[off_to] != (schar_T)c
6543 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6544 != (c >= 0x80 ? c : 0))
6545 || ScreenAttrs[off_to] != hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006547 ScreenLines[off_to] = c;
6548 ScreenAttrs[off_to] = hl;
6549 if (enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006551 if (c >= 0x80)
6552 {
6553 ScreenLinesUC[off_to] = c;
6554 ScreenLinesC[0][off_to] = 0;
6555 }
6556 else
6557 ScreenLinesUC[off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558 }
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006559 screen_char(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561 }
6562 }
6563 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564 LineWraps[row] = FALSE;
6565 }
6566}
6567
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006568#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006570 * Mirror text "str" for right-left displaying.
6571 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006573 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006574rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575{
6576 char_u *p1, *p2;
6577 int t;
6578
6579 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6580 {
6581 t = *p1;
6582 *p1 = *p2;
6583 *p2 = t;
6584 }
6585}
6586#endif
6587
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588/*
6589 * mark all status lines for redraw; used after first :cd
6590 */
6591 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006592status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593{
6594 win_T *wp;
6595
Bram Moolenaar29323592016-07-24 22:04:11 +02006596 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597 if (wp->w_status_height)
6598 {
6599 wp->w_redr_status = TRUE;
6600 redraw_later(VALID);
6601 }
6602}
6603
6604/*
6605 * mark all status lines of the current buffer for redraw
6606 */
6607 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006608status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609{
6610 win_T *wp;
6611
Bram Moolenaar29323592016-07-24 22:04:11 +02006612 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6614 {
6615 wp->w_redr_status = TRUE;
6616 redraw_later(VALID);
6617 }
6618}
6619
6620/*
6621 * Redraw all status lines that need to be redrawn.
6622 */
6623 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006624redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625{
6626 win_T *wp;
6627
Bram Moolenaar29323592016-07-24 22:04:11 +02006628 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006630 win_redr_status(wp, FALSE);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006631 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006632 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634
Bram Moolenaar4033c552017-09-16 20:54:51 +02006635#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636/*
6637 * Redraw all status lines at the bottom of frame "frp".
6638 */
6639 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006640win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641{
6642 if (frp->fr_layout == FR_LEAF)
6643 frp->fr_win->w_redr_status = TRUE;
6644 else if (frp->fr_layout == FR_ROW)
6645 {
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006646 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 win_redraw_last_status(frp);
6648 }
6649 else /* frp->fr_layout == FR_COL */
6650 {
6651 frp = frp->fr_child;
6652 while (frp->fr_next != NULL)
6653 frp = frp->fr_next;
6654 win_redraw_last_status(frp);
6655 }
6656}
6657#endif
6658
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659/*
6660 * Draw the verticap separator right of window "wp" starting with line "row".
6661 */
6662 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006663draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664{
6665 int hl;
6666 int c;
6667
6668 if (wp->w_vsep_width)
6669 {
6670 /* draw the vertical separator right of this window */
6671 c = fillchar_vsep(&hl);
6672 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6673 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6674 c, ' ', hl);
6675 }
6676}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677
6678#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006679static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680
6681/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006682 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683 */
6684 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006685status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686{
6687 int len = 0;
6688
6689#ifdef FEAT_MENU
6690 int emenu = (xp->xp_context == EXPAND_MENUS
6691 || xp->xp_context == EXPAND_MENUNAMES);
6692
6693 /* Check for menu separators - replace with '|'. */
6694 if (emenu && menu_is_separator(s))
6695 return 1;
6696#endif
6697
6698 while (*s != NUL)
6699 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006700 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006701 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006702 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703 }
6704
6705 return len;
6706}
6707
6708/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006709 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006710 * These are backslashes used for escaping. Do show backslashes in help tags.
6711 */
6712 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006713skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006714{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006715 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006716#ifdef FEAT_MENU
6717 || ((xp->xp_context == EXPAND_MENUS
6718 || xp->xp_context == EXPAND_MENUNAMES)
6719 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6720#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006721 )
6722 {
6723#ifndef BACKSLASH_IN_FILENAME
6724 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6725 return 2;
6726#endif
6727 return 1;
6728 }
6729 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006730}
6731
6732/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733 * Show wildchar matches in the status line.
6734 * Show at least the "match" item.
6735 * We start at item 'first_match' in the list and show all matches that fit.
6736 *
6737 * If inversion is possible we use it. Else '=' characters are used.
6738 */
6739 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006740win_redr_status_matches(
6741 expand_T *xp,
6742 int num_matches,
6743 char_u **matches, /* list of matches */
6744 int match,
6745 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746{
6747#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6748 int row;
6749 char_u *buf;
6750 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006751 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752 int fillchar;
6753 int attr;
6754 int i;
6755 int highlight = TRUE;
6756 char_u *selstart = NULL;
6757 int selstart_col = 0;
6758 char_u *selend = NULL;
6759 static int first_match = 0;
6760 int add_left = FALSE;
6761 char_u *s;
6762#ifdef FEAT_MENU
6763 int emenu;
6764#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766
6767 if (matches == NULL) /* interrupted completion? */
6768 return;
6769
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006770 if (has_mbyte)
Bram Moolenaar964b3742019-05-24 18:54:09 +02006771 buf = alloc(Columns * MB_MAXBYTES + 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006772 else
Bram Moolenaar964b3742019-05-24 18:54:09 +02006773 buf = alloc(Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774 if (buf == NULL)
6775 return;
6776
6777 if (match == -1) /* don't show match but original text */
6778 {
6779 match = 0;
6780 highlight = FALSE;
6781 }
6782 /* count 1 for the ending ">" */
6783 clen = status_match_len(xp, L_MATCH(match)) + 3;
6784 if (match == 0)
6785 first_match = 0;
6786 else if (match < first_match)
6787 {
6788 /* jumping left, as far as we can go */
6789 first_match = match;
6790 add_left = TRUE;
6791 }
6792 else
6793 {
6794 /* check if match fits on the screen */
6795 for (i = first_match; i < match; ++i)
6796 clen += status_match_len(xp, L_MATCH(i)) + 2;
6797 if (first_match > 0)
6798 clen += 2;
6799 /* jumping right, put match at the left */
6800 if ((long)clen > Columns)
6801 {
6802 first_match = match;
6803 /* if showing the last match, we can add some on the left */
6804 clen = 2;
6805 for (i = match; i < num_matches; ++i)
6806 {
6807 clen += status_match_len(xp, L_MATCH(i)) + 2;
6808 if ((long)clen >= Columns)
6809 break;
6810 }
6811 if (i == num_matches)
6812 add_left = TRUE;
6813 }
6814 }
6815 if (add_left)
6816 while (first_match > 0)
6817 {
6818 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6819 if ((long)clen >= Columns)
6820 break;
6821 --first_match;
6822 }
6823
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006824 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825
6826 if (first_match == 0)
6827 {
6828 *buf = NUL;
6829 len = 0;
6830 }
6831 else
6832 {
6833 STRCPY(buf, "< ");
6834 len = 2;
6835 }
6836 clen = len;
6837
6838 i = first_match;
6839 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6840 {
6841 if (i == match)
6842 {
6843 selstart = buf + len;
6844 selstart_col = clen;
6845 }
6846
6847 s = L_MATCH(i);
6848 /* Check for menu separators - replace with '|' */
6849#ifdef FEAT_MENU
6850 emenu = (xp->xp_context == EXPAND_MENUS
6851 || xp->xp_context == EXPAND_MENUNAMES);
6852 if (emenu && menu_is_separator(s))
6853 {
6854 STRCPY(buf + len, transchar('|'));
6855 l = (int)STRLEN(buf + len);
6856 len += l;
6857 clen += l;
6858 }
6859 else
6860#endif
6861 for ( ; *s != NUL; ++s)
6862 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006863 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864 clen += ptr2cells(s);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006865 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866 {
6867 STRNCPY(buf + len, s, l);
6868 s += l - 1;
6869 len += l;
6870 }
6871 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872 {
6873 STRCPY(buf + len, transchar_byte(*s));
6874 len += (int)STRLEN(buf + len);
6875 }
6876 }
6877 if (i == match)
6878 selend = buf + len;
6879
6880 *(buf + len++) = ' ';
6881 *(buf + len++) = ' ';
6882 clen += 2;
6883 if (++i == num_matches)
6884 break;
6885 }
6886
6887 if (i != num_matches)
6888 {
6889 *(buf + len++) = '>';
6890 ++clen;
6891 }
6892
6893 buf[len] = NUL;
6894
6895 row = cmdline_row - 1;
6896 if (row >= 0)
6897 {
6898 if (wild_menu_showing == 0)
6899 {
6900 if (msg_scrolled > 0)
6901 {
6902 /* Put the wildmenu just above the command line. If there is
6903 * no room, scroll the screen one line up. */
6904 if (cmdline_row == Rows - 1)
6905 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02006906 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907 ++msg_scrolled;
6908 }
6909 else
6910 {
6911 ++cmdline_row;
6912 ++row;
6913 }
6914 wild_menu_showing = WM_SCROLLED;
6915 }
6916 else
6917 {
6918 /* Create status line if needed by setting 'laststatus' to 2.
6919 * Set 'winminheight' to zero to avoid that the window is
6920 * resized. */
6921 if (lastwin->w_status_height == 0)
6922 {
6923 save_p_ls = p_ls;
6924 save_p_wmh = p_wmh;
6925 p_ls = 2;
6926 p_wmh = 0;
6927 last_status(FALSE);
6928 }
6929 wild_menu_showing = WM_SHOWN;
6930 }
6931 }
6932
6933 screen_puts(buf, row, 0, attr);
6934 if (selstart != NULL && highlight)
6935 {
6936 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006937 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938 }
6939
6940 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6941 }
6942
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944 vim_free(buf);
6945}
6946#endif
6947
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948/*
6949 * Redraw the status line of window wp.
6950 *
6951 * If inversion is possible we use it. Else '=' characters are used.
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006952 * If "ignore_pum" is TRUE, also redraw statusline when the popup menu is
6953 * displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 */
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006955 static void
Bram Moolenaar829adb72018-06-24 19:24:03 +02006956win_redr_status(win_T *wp, int ignore_pum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957{
6958 int row;
6959 char_u *p;
6960 int len;
6961 int fillchar;
6962 int attr;
6963 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006964 static int busy = FALSE;
6965
6966 /* It's possible to get here recursively when 'statusline' (indirectly)
6967 * invokes ":redrawstatus". Simply ignore the call then. */
6968 if (busy)
6969 return;
6970 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971
6972 wp->w_redr_status = FALSE;
6973 if (wp->w_status_height == 0)
6974 {
6975 /* no status line, can only be last window */
6976 redraw_cmdline = TRUE;
6977 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006978 else if (!redrawing()
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006979 // don't update status line when popup menu is visible and may be
6980 // drawn over it, unless it will be redrawn later
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006981 || (!ignore_pum && pum_visible()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 {
6983 /* Don't redraw right now, do it later. */
6984 wp->w_redr_status = TRUE;
6985 }
6986#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006987 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 {
6989 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006990 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 }
6992#endif
6993 else
6994 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006995 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006997 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998 p = NameBuff;
6999 len = (int)STRLEN(p);
7000
Bram Moolenaard85f2712017-07-28 21:51:57 +02007001 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002#ifdef FEAT_QUICKFIX
7003 || wp->w_p_pvw
7004#endif
7005 || bufIsChanged(wp->w_buffer)
7006 || wp->w_buffer->b_p_ro)
7007 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02007008 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00007010 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011 len += (int)STRLEN(p + len);
7012 }
7013#ifdef FEAT_QUICKFIX
7014 if (wp->w_p_pvw)
7015 {
7016 STRCPY(p + len, _("[Preview]"));
7017 len += (int)STRLEN(p + len);
7018 }
7019#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02007020 if (bufIsChanged(wp->w_buffer)
7021#ifdef FEAT_TERMINAL
7022 && !bt_terminal(wp->w_buffer)
7023#endif
7024 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025 {
7026 STRCPY(p + len, "[+]");
7027 len += 3;
7028 }
7029 if (wp->w_buffer->b_p_ro)
7030 {
Bram Moolenaar23584032013-06-07 20:17:11 +02007031 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01007032 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 }
7034
Bram Moolenaar02631462017-09-22 15:20:32 +02007035 this_ru_col = ru_col - (Columns - wp->w_width);
7036 if (this_ru_col < (wp->w_width + 1) / 2)
7037 this_ru_col = (wp->w_width + 1) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038 if (this_ru_col <= 1)
7039 {
7040 p = (char_u *)"<"; /* No room for file name! */
7041 len = 1;
7042 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01007043 else if (has_mbyte)
7044 {
7045 int clen = 0, i;
7046
7047 /* Count total number of display cells. */
7048 clen = mb_string2cells(p, -1);
7049
7050 /* Find first character that will fit.
7051 * Going from start to end is much faster for DBCS. */
7052 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
7053 i += (*mb_ptr2len)(p + i))
7054 clen -= (*mb_ptr2cells)(p + i);
7055 len = clen;
7056 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 {
Bram Moolenaara12a1612019-01-24 16:39:02 +01007058 p = p + i - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 *p = '<';
Bram Moolenaara12a1612019-01-24 16:39:02 +01007060 ++len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061 }
7062
Bram Moolenaara12a1612019-01-24 16:39:02 +01007063 }
7064 else if (len > this_ru_col - 1)
7065 {
7066 p += len - (this_ru_col - 1);
7067 *p = '<';
7068 len = this_ru_col - 1;
7069 }
7070
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar53f81742017-09-22 14:35:51 +02007072 screen_puts(p, row, wp->w_wincol, attr);
7073 screen_fill(row, row + 1, len + wp->w_wincol,
7074 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007076 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
7078 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007079 - 1 + wp->w_wincol), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080
7081#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +02007082 win_redr_ruler(wp, TRUE, ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083#endif
7084 }
7085
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086 /*
7087 * May need to draw the character below the vertical separator.
7088 */
7089 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
7090 {
7091 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007092 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093 else
7094 fillchar = fillchar_vsep(&attr);
7095 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
7096 attr);
7097 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007098 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099}
7100
Bram Moolenaar238a5642006-02-21 22:12:05 +00007101#ifdef FEAT_STL_OPT
7102/*
7103 * Redraw the status line according to 'statusline' and take care of any
7104 * errors encountered.
7105 */
7106 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007107redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00007108{
Bram Moolenaar362f3562009-11-03 16:20:34 +00007109 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02007110 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007111
7112 /* When called recursively return. This can happen when the statusline
7113 * contains an expression that triggers a redraw. */
7114 if (entered)
7115 return;
7116 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007117
Bram Moolenaara742e082016-04-05 21:10:38 +02007118 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007119 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02007120 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007121 {
7122 /* When there is an error disable the statusline, otherwise the
7123 * display is messed up with errors and a redraw triggers the problem
7124 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007125 set_string_option_direct((char_u *)"statusline", -1,
7126 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007127 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007128 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007129 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007130 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007131}
7132#endif
7133
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134/*
7135 * Return TRUE if the status line of window "wp" is connected to the status
7136 * line of the window right of it. If not, then it's a vertical separator.
7137 * Only call if (wp->w_vsep_width != 0).
7138 */
7139 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007140stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141{
7142 frame_T *fr;
7143
7144 fr = wp->w_frame;
7145 while (fr->fr_parent != NULL)
7146 {
7147 if (fr->fr_parent->fr_layout == FR_COL)
7148 {
7149 if (fr->fr_next != NULL)
7150 break;
7151 }
7152 else
7153 {
7154 if (fr->fr_next != NULL)
7155 return TRUE;
7156 }
7157 fr = fr->fr_parent;
7158 }
7159 return FALSE;
7160}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163/*
7164 * Get the value to show for the language mappings, active 'keymap'.
7165 */
7166 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007167get_keymap_str(
7168 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007169 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007170 char_u *buf, /* buffer for the result */
7171 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172{
7173 char_u *p;
7174
7175 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7176 return FALSE;
7177
7178 {
7179#ifdef FEAT_EVAL
7180 buf_T *old_curbuf = curbuf;
7181 win_T *old_curwin = curwin;
7182 char_u *s;
7183
7184 curbuf = wp->w_buffer;
7185 curwin = wp;
7186 STRCPY(buf, "b:keymap_name"); /* must be writable */
7187 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007188 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189 --emsg_skip;
7190 curbuf = old_curbuf;
7191 curwin = old_curwin;
7192 if (p == NULL || *p == NUL)
7193#endif
7194 {
7195#ifdef FEAT_KEYMAP
7196 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7197 p = wp->w_buffer->b_p_keymap;
7198 else
7199#endif
7200 p = (char_u *)"lang";
7201 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007202 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203 buf[0] = NUL;
7204#ifdef FEAT_EVAL
7205 vim_free(s);
7206#endif
7207 }
7208 return buf[0] != NUL;
7209}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210
7211#if defined(FEAT_STL_OPT) || defined(PROTO)
7212/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007213 * Redraw the status line or ruler of window "wp".
7214 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215 */
7216 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007217win_redr_custom(
7218 win_T *wp,
7219 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007221 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222 int attr;
7223 int curattr;
7224 int row;
7225 int col = 0;
7226 int maxwidth;
7227 int width;
7228 int n;
7229 int len;
7230 int fillchar;
7231 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007232 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007234 struct stl_hlrec hltab[STL_MAX_ITEM];
7235 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007236 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007237 win_T *ewp;
7238 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239
Bram Moolenaar1d633412013-12-11 15:52:01 +01007240 /* There is a tiny chance that this gets called recursively: When
7241 * redrawing a status line triggers redrawing the ruler or tabline.
7242 * Avoid trouble by not allowing recursion. */
7243 if (entered)
7244 return;
7245 entered = TRUE;
7246
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007248 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007250 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007251 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007252 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007253 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007254 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007255 maxwidth = Columns;
7256# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007257 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007258# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007260 else
7261 {
7262 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007263 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02007264 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007265
7266 if (draw_ruler)
7267 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007268 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007269 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007270 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007271 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007272 if (*++stl == '-')
7273 stl++;
7274 if (atoi((char *)stl))
7275 while (VIM_ISDIGIT(*stl))
7276 stl++;
7277 if (*stl++ != '(')
7278 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007279 }
Bram Moolenaar02631462017-09-22 15:20:32 +02007280 col = ru_col - (Columns - wp->w_width);
7281 if (col < (wp->w_width + 1) / 2)
7282 col = (wp->w_width + 1) / 2;
7283 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007284 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007285 {
7286 row = Rows - 1;
7287 --maxwidth; /* writing in last column may cause scrolling */
7288 fillchar = ' ';
7289 attr = 0;
7290 }
7291
7292# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007293 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007294# endif
7295 }
7296 else
7297 {
7298 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007299 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007300 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007301 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007302# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007303 use_sandbox = was_set_insecurely((char_u *)"statusline",
7304 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007305# endif
7306 }
7307
Bram Moolenaar53f81742017-09-22 14:35:51 +02007308 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007309 }
7310
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007312 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313
Bram Moolenaar61452852011-02-01 18:01:11 +01007314 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7315 * the cursor away and back. */
7316 ewp = wp == NULL ? curwin : wp;
7317 p_crb_save = ewp->w_p_crb;
7318 ewp->w_p_crb = FALSE;
7319
Bram Moolenaar362f3562009-11-03 16:20:34 +00007320 /* Make a copy, because the statusline may include a function call that
7321 * might change the option value and free the memory. */
7322 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007323 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007324 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007325 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007326 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007327 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007329 /* Make all characters printable. */
7330 p = transstr(buf);
7331 if (p != NULL)
7332 {
7333 vim_strncpy(buf, p, sizeof(buf) - 1);
7334 vim_free(p);
7335 }
7336
7337 /* fill up with "fillchar" */
7338 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007339 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341 len += (*mb_char2bytes)(fillchar, buf + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342 ++width;
7343 }
7344 buf[len] = NUL;
7345
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007346 /*
7347 * Draw each snippet with the specified highlighting.
7348 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349 curattr = attr;
7350 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007351 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007353 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354 screen_puts_len(p, len, row, col, curattr);
7355 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007356 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007358 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007360 else if (hltab[n].userhl < 0)
7361 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007362#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007363 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7364 && wp->w_status_height != 0)
7365 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007366 else if (wp != NULL && bt_terminal(wp->w_buffer)
7367 && wp->w_status_height != 0)
7368 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02007369#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007370 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007371 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007373 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374 }
7375 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007376
7377 if (wp == NULL)
7378 {
7379 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7380 col = 0;
7381 len = 0;
7382 p = buf;
7383 fillchar = 0;
7384 for (n = 0; tabtab[n].start != NULL; n++)
7385 {
7386 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7387 while (col < len)
7388 TabPageIdxs[col++] = fillchar;
7389 p = tabtab[n].start;
7390 fillchar = tabtab[n].userhl;
7391 }
7392 while (col < Columns)
7393 TabPageIdxs[col++] = fillchar;
7394 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007395
7396theend:
7397 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398}
7399
7400#endif /* FEAT_STL_OPT */
7401
7402/*
7403 * Output a single character directly to the screen and update ScreenLines.
7404 */
7405 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007406screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408 char_u buf[MB_MAXBYTES + 1];
7409
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007410 if (has_mbyte)
7411 buf[(*mb_char2bytes)(c, buf)] = NUL;
7412 else
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007413 {
7414 buf[0] = c;
7415 buf[1] = NUL;
7416 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 screen_puts(buf, row, col, attr);
7418}
7419
7420/*
7421 * Get a single character directly from ScreenLines into "bytes[]".
7422 * Also return its attribute in *attrp;
7423 */
7424 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007425screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426{
7427 unsigned off;
7428
7429 /* safety check */
7430 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7431 {
7432 off = LineOffset[row] + col;
7433 *attrp = ScreenAttrs[off];
7434 bytes[0] = ScreenLines[off];
7435 bytes[1] = NUL;
7436
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437 if (enc_utf8 && ScreenLinesUC[off] != 0)
7438 bytes[utfc_char2bytes(off, bytes)] = NUL;
7439 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7440 {
7441 bytes[0] = ScreenLines[off];
7442 bytes[1] = ScreenLines2[off];
7443 bytes[2] = NUL;
7444 }
7445 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7446 {
7447 bytes[1] = ScreenLines[off + 1];
7448 bytes[2] = NUL;
7449 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450 }
7451}
7452
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007453/*
7454 * Return TRUE if composing characters for screen posn "off" differs from
7455 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007456 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007457 */
7458 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007459screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007460{
7461 int i;
7462
7463 for (i = 0; i < Screen_mco; ++i)
7464 {
7465 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7466 return TRUE;
7467 if (u8cc[i] == 0)
7468 break;
7469 }
7470 return FALSE;
7471}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007472
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473/*
7474 * Put string '*text' on the screen at position 'row' and 'col', with
7475 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7476 * Note: only outputs within one row, message is truncated at screen boundary!
7477 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7478 */
7479 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007480screen_puts(
7481 char_u *text,
7482 int row,
7483 int col,
7484 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485{
7486 screen_puts_len(text, -1, row, col, attr);
7487}
7488
7489/*
7490 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7491 * a NUL.
7492 */
7493 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007494screen_puts_len(
7495 char_u *text,
7496 int textlen,
7497 int row,
7498 int col,
7499 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500{
7501 unsigned off;
7502 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007503 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504 int c;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007505 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506 int mbyte_blen = 1;
7507 int mbyte_cells = 1;
7508 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007509 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510 int clear_next_cell = FALSE;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007511#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007513 int pc, nc, nc1;
7514 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007516 int force_redraw_this;
7517 int force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007518 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519
Bram Moolenaar0b4c9ed2019-06-03 22:04:23 +02007520 // Safety check. The check for negative row and column is to fix issue
7521 // #4102. TODO: find out why row/col could be negative.
7522 if (ScreenLines == NULL
7523 || row >= screen_Rows || row < 0
7524 || col >= screen_Columns || col < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007526 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527
Bram Moolenaarc236c162008-07-13 17:41:49 +00007528 /* When drawing over the right halve of a double-wide char clear out the
7529 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007530 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaara12a1612019-01-24 16:39:02 +01007531#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00007532 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01007533#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007534 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007535 {
7536 ScreenLines[off - 1] = ' ';
7537 ScreenAttrs[off - 1] = 0;
7538 if (enc_utf8)
7539 {
7540 ScreenLinesUC[off - 1] = 0;
7541 ScreenLinesC[0][off - 1] = 0;
7542 }
7543 /* redraw the previous cell, make it empty */
7544 screen_char(off - 1, row, col - 1);
7545 /* force the cell at "col" to be redrawn */
7546 force_redraw_next = TRUE;
7547 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007548
Bram Moolenaar367329b2007-08-30 11:53:22 +00007549 max_off = LineOffset[row] + screen_Columns;
Bram Moolenaara064ac82007-08-05 18:10:54 +00007550 while (col < screen_Columns
7551 && (len < 0 || (int)(ptr - text) < len)
7552 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553 {
7554 c = *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007555 /* check if this is the first byte of a multibyte */
7556 if (has_mbyte)
7557 {
7558 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007559 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007561 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7563 mbyte_cells = 1;
7564 else if (enc_dbcs != 0)
7565 mbyte_cells = mbyte_blen;
7566 else /* enc_utf8 */
7567 {
7568 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007569 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570 (int)((text + len) - ptr));
7571 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007572 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007574#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7576 {
7577 /* Do Arabic shaping. */
7578 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7579 {
7580 /* Past end of string to be displayed. */
7581 nc = NUL;
7582 nc1 = NUL;
7583 }
7584 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007585 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007586 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7587 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007588 nc1 = pcc[0];
7589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590 pc = prev_c;
7591 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007592 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593 }
7594 else
7595 prev_c = u8c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007596#endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007597 if (col + mbyte_cells > screen_Columns)
7598 {
7599 /* Only 1 cell left, but character requires 2 cells:
7600 * display a '>' in the last column to avoid wrapping. */
7601 c = '>';
7602 mbyte_cells = 1;
7603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604 }
7605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007607 force_redraw_this = force_redraw_next;
7608 force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007609
7610 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611 || (mbyte_cells == 2
7612 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7613 || (enc_dbcs == DBCS_JPNU
7614 && c == 0x8e
7615 && ScreenLines2[off] != ptr[1])
7616 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007617 && (ScreenLinesUC[off] !=
7618 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7619 || (ScreenLinesUC[off] != 0
7620 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007622 || exmode_active;
7623
Bram Moolenaar24a5ac52019-06-08 19:01:18 +02007624 if ((need_redraw || force_redraw_this)
7625#ifdef FEAT_TEXT_PROP
Bram Moolenaarc662ec92019-06-23 00:15:57 +02007626 && !blocked_by_popup(row, col)
Bram Moolenaar24a5ac52019-06-08 19:01:18 +02007627#endif
7628 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629 {
7630#if defined(FEAT_GUI) || defined(UNIX)
7631 /* The bold trick makes a single row of pixels appear in the next
7632 * character. When a bold character is removed, the next
7633 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007634 * and for some xterms. */
7635 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636# ifdef FEAT_GUI
7637 gui.in_use
7638# endif
7639# if defined(FEAT_GUI) && defined(UNIX)
7640 ||
7641# endif
7642# ifdef UNIX
7643 term_is_xterm
7644# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007645 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007647 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007649 if (n > HL_ALL)
7650 n = syn_attr2attr(n);
7651 if (n & HL_BOLD)
7652 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653 }
7654#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655 /* When at the end of the text and overwriting a two-cell
7656 * character with a one-cell character, need to clear the next
7657 * cell. Also when overwriting the left halve of a two-cell char
7658 * with the right halve of a two-cell char. Do this only once
7659 * (mb_off2cells() may return 2 on the right halve). */
7660 if (clear_next_cell)
7661 clear_next_cell = FALSE;
7662 else if (has_mbyte
7663 && (len < 0 ? ptr[mbyte_blen] == NUL
7664 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007665 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007667 && (*mb_off2cells)(off, max_off) == 1
7668 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669 clear_next_cell = TRUE;
7670
7671 /* Make sure we never leave a second byte of a double-byte behind,
7672 * it confuses mb_off2cells(). */
7673 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007674 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007676 && (*mb_off2cells)(off, max_off) == 1
7677 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678 ScreenLines[off + mbyte_blen] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679 ScreenLines[off] = c;
7680 ScreenAttrs[off] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681 if (enc_utf8)
7682 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007683 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 ScreenLinesUC[off] = 0;
7685 else
7686 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007687 int i;
7688
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007690 for (i = 0; i < Screen_mco; ++i)
7691 {
7692 ScreenLinesC[i][off] = u8cc[i];
7693 if (u8cc[i] == 0)
7694 break;
7695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696 }
7697 if (mbyte_cells == 2)
7698 {
7699 ScreenLines[off + 1] = 0;
7700 ScreenAttrs[off + 1] = attr;
7701 }
7702 screen_char(off, row, col);
7703 }
7704 else if (mbyte_cells == 2)
7705 {
7706 ScreenLines[off + 1] = ptr[1];
7707 ScreenAttrs[off + 1] = attr;
7708 screen_char_2(off, row, col);
7709 }
7710 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7711 {
7712 ScreenLines2[off] = ptr[1];
7713 screen_char(off, row, col);
7714 }
7715 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716 screen_char(off, row, col);
7717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718 if (has_mbyte)
7719 {
7720 off += mbyte_cells;
7721 col += mbyte_cells;
7722 ptr += mbyte_blen;
7723 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007724 {
7725 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007727 len = -1;
7728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729 }
7730 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731 {
7732 ++off;
7733 ++col;
7734 ++ptr;
7735 }
7736 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007737
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007738 /* If we detected the next character needs to be redrawn, but the text
7739 * doesn't extend up to there, update the character here. */
7740 if (force_redraw_next && col < screen_Columns)
7741 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007742 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7743 screen_char_2(off, row, col);
7744 else
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007745 screen_char(off, row, col);
7746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747}
7748
7749#ifdef FEAT_SEARCH_EXTRA
7750/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007751 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752 */
7753 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007754start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755{
7756 if (p_hls && !no_hlsearch)
7757 {
7758 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007759 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007760# ifdef FEAT_RELTIME
7761 /* Set the time limit to 'redrawtime'. */
7762 profile_setlimit(p_rdt, &search_hl.tm);
7763# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007764 }
7765}
7766
7767/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007768 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769 */
7770 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007771end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772{
7773 if (search_hl.rm.regprog != NULL)
7774 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007775 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776 search_hl.rm.regprog = NULL;
7777 }
7778}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02007779#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02007780
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007782screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783{
7784 attrentry_T *aep = NULL;
7785
7786 screen_attr = attr;
7787 if (full_screen
Bram Moolenaar4f974752019-02-17 17:44:42 +01007788#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789 && termcap_active
7790#endif
7791 )
7792 {
7793#ifdef FEAT_GUI
7794 if (gui.in_use)
7795 {
7796 char buf[20];
7797
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007798 /* The GUI handles this internally. */
7799 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800 OUT_STR(buf);
7801 }
7802 else
7803#endif
7804 {
7805 if (attr > HL_ALL) /* special HL attr. */
7806 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007807 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808 aep = syn_cterm_attr2entry(attr);
7809 else
7810 aep = syn_term_attr2entry(attr);
7811 if (aep == NULL) /* did ":syntax clear" */
7812 attr = 0;
7813 else
7814 attr = aep->ae_attr;
7815 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01007816 if ((attr & HL_BOLD) && *T_MD != NUL) /* bold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007818 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007819#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007820 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
7821 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
7822 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007823#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007824 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007825 /* If the Normal FG color has BOLD attribute and the new HL
7826 * has a FG color defined, clear BOLD. */
7827 out_str(T_ME);
Bram Moolenaar45a00002017-12-22 21:12:34 +01007828 if ((attr & HL_STANDOUT) && *T_SO != NUL) /* standout */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829 out_str(T_SO);
Bram Moolenaar45a00002017-12-22 21:12:34 +01007830 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) /* undercurl */
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01007831 out_str(T_UCS);
7832 if (((attr & HL_UNDERLINE) /* underline or undercurl */
Bram Moolenaar45a00002017-12-22 21:12:34 +01007833 || ((attr & HL_UNDERCURL) && *T_UCS == NUL))
7834 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835 out_str(T_US);
Bram Moolenaar45a00002017-12-22 21:12:34 +01007836 if ((attr & HL_ITALIC) && *T_CZH != NUL) /* italic */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837 out_str(T_CZH);
Bram Moolenaar45a00002017-12-22 21:12:34 +01007838 if ((attr & HL_INVERSE) && *T_MR != NUL) /* inverse (reverse) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839 out_str(T_MR);
Bram Moolenaar45a00002017-12-22 21:12:34 +01007840 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) /* strike */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02007841 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842
7843 /*
7844 * Output the color or start string after bold etc., in case the
7845 * bold etc. override the color setting.
7846 */
7847 if (aep != NULL)
7848 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007849#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007850 /* When 'termguicolors' is set but fg or bg is unset,
7851 * fall back to the cterm colors. This helps for SpellBad,
7852 * where the GUI uses a red undercurl. */
7853 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007855 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007856 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007857 }
7858 else
7859#endif
7860 if (t_colors > 1)
7861 {
7862 if (aep->ae_u.cterm.fg_color)
7863 term_fg_color(aep->ae_u.cterm.fg_color - 1);
7864 }
7865#ifdef FEAT_TERMGUICOLORS
7866 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
7867 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007868 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007869 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 }
7871 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007872#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007873 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007875 if (aep->ae_u.cterm.bg_color)
7876 term_bg_color(aep->ae_u.cterm.bg_color - 1);
7877 }
7878
Bram Moolenaarf708ac52018-03-12 21:48:32 +01007879 if (!IS_CTERM)
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007880 {
7881 if (aep->ae_u.term.start != NULL)
7882 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883 }
7884 }
7885 }
7886 }
7887}
7888
7889 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007890screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891{
7892 int do_ME = FALSE; /* output T_ME code */
7893
7894 if (screen_attr != 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01007895#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896 && termcap_active
7897#endif
7898 )
7899 {
7900#ifdef FEAT_GUI
7901 if (gui.in_use)
7902 {
7903 char buf[20];
7904
7905 /* use internal GUI code */
7906 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
7907 OUT_STR(buf);
7908 }
7909 else
7910#endif
7911 {
7912 if (screen_attr > HL_ALL) /* special HL attr. */
7913 {
7914 attrentry_T *aep;
7915
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007916 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007917 {
7918 /*
7919 * Assume that t_me restores the original colors!
7920 */
7921 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007922 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007923#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007924 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
7925 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
7926 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007927#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007928 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007929#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007930 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
7931 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
7932 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007933#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007934 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935 do_ME = TRUE;
7936 }
7937 else
7938 {
7939 aep = syn_term_attr2entry(screen_attr);
7940 if (aep != NULL && aep->ae_u.term.stop != NULL)
7941 {
7942 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
7943 do_ME = TRUE;
7944 else
7945 out_str(aep->ae_u.term.stop);
7946 }
7947 }
7948 if (aep == NULL) /* did ":syntax clear" */
7949 screen_attr = 0;
7950 else
7951 screen_attr = aep->ae_attr;
7952 }
7953
7954 /*
7955 * Often all ending-codes are equal to T_ME. Avoid outputting the
7956 * same sequence several times.
7957 */
7958 if (screen_attr & HL_STANDOUT)
7959 {
7960 if (STRCMP(T_SE, T_ME) == 0)
7961 do_ME = TRUE;
7962 else
7963 out_str(T_SE);
7964 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01007965 if ((screen_attr & HL_UNDERCURL) && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01007966 {
7967 if (STRCMP(T_UCE, T_ME) == 0)
7968 do_ME = TRUE;
7969 else
7970 out_str(T_UCE);
7971 }
7972 if ((screen_attr & HL_UNDERLINE)
Bram Moolenaar45a00002017-12-22 21:12:34 +01007973 || ((screen_attr & HL_UNDERCURL) && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974 {
7975 if (STRCMP(T_UE, T_ME) == 0)
7976 do_ME = TRUE;
7977 else
7978 out_str(T_UE);
7979 }
7980 if (screen_attr & HL_ITALIC)
7981 {
7982 if (STRCMP(T_CZR, T_ME) == 0)
7983 do_ME = TRUE;
7984 else
7985 out_str(T_CZR);
7986 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02007987 if (screen_attr & HL_STRIKETHROUGH)
7988 {
7989 if (STRCMP(T_STE, T_ME) == 0)
7990 do_ME = TRUE;
7991 else
7992 out_str(T_STE);
7993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
7995 out_str(T_ME);
7996
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007997#ifdef FEAT_TERMGUICOLORS
7998 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008000 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008001 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008002 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008003 term_bg_rgb_color(cterm_normal_bg_gui_color);
8004 }
8005 else
8006#endif
8007 {
8008 if (t_colors > 1)
8009 {
8010 /* set Normal cterm colors */
8011 if (cterm_normal_fg_color != 0)
8012 term_fg_color(cterm_normal_fg_color - 1);
8013 if (cterm_normal_bg_color != 0)
8014 term_bg_color(cterm_normal_bg_color - 1);
8015 if (cterm_normal_fg_bold)
8016 out_str(T_MD);
8017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018 }
8019 }
8020 }
8021 screen_attr = 0;
8022}
8023
8024/*
8025 * Reset the colors for a cterm. Used when leaving Vim.
8026 * The machine specific code may override this again.
8027 */
8028 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008029reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008031 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032 {
8033 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008034#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008035 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8036 || cterm_normal_bg_gui_color != INVALCOLOR)
8037 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008038#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008040#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041 {
8042 out_str(T_OP);
8043 screen_attr = -1;
8044 }
8045 if (cterm_normal_fg_bold)
8046 {
8047 out_str(T_ME);
8048 screen_attr = -1;
8049 }
8050 }
8051}
8052
8053/*
8054 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8055 * using the attributes from ScreenAttrs["off"].
8056 */
8057 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008058screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059{
8060 int attr;
8061
8062 /* Check for illegal values, just in case (could happen just after
8063 * resizing). */
8064 if (row >= screen_Rows || col >= screen_Columns)
8065 return;
8066
Bram Moolenaar33796b32019-06-08 16:01:13 +02008067 // Skip if under the popup menu.
8068 // Popup windows with zindex higher than POPUPMENU_ZINDEX go on top.
8069 if (pum_under_menu(row, col)
Bram Moolenaare2c453d2019-08-21 14:37:09 +02008070#ifdef FEAT_TEXT_PROP
Bram Moolenaar33796b32019-06-08 16:01:13 +02008071 && screen_zindex <= POPUPMENU_ZINDEX
Bram Moolenaare2c453d2019-08-21 14:37:09 +02008072#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02008073 )
Bram Moolenaarae654382019-01-17 21:09:05 +01008074 return;
Bram Moolenaar33796b32019-06-08 16:01:13 +02008075#ifdef FEAT_TEXT_PROP
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008076 if (blocked_by_popup(row, col))
Bram Moolenaar33796b32019-06-08 16:01:13 +02008077 return;
8078#endif
8079
Bram Moolenaar494838a2015-02-10 19:20:37 +01008080 /* Outputting a character in the last cell on the screen may scroll the
8081 * screen up. Only do it when the "xn" termcap property is set, otherwise
8082 * mark the character invalid (update it when scrolled up). */
8083 if (*T_XN == NUL
8084 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085#ifdef FEAT_RIGHTLEFT
8086 /* account for first command-line character in rightleft mode */
8087 && !cmdmsg_rl
8088#endif
8089 )
8090 {
8091 ScreenAttrs[off] = (sattr_T)-1;
8092 return;
8093 }
8094
8095 /*
8096 * Stop highlighting first, so it's easier to move the cursor.
8097 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 if (screen_char_attr != 0)
8099 attr = screen_char_attr;
8100 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 attr = ScreenAttrs[off];
8102 if (screen_attr != attr)
8103 screen_stop_highlight();
8104
8105 windgoto(row, col);
8106
8107 if (screen_attr != attr)
8108 screen_start_highlight(attr);
8109
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110 if (enc_utf8 && ScreenLinesUC[off] != 0)
8111 {
8112 char_u buf[MB_MAXBYTES + 1];
8113
Bram Moolenaarcb070082016-04-02 22:14:51 +02008114 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008115 {
8116 if (*p_ambw == 'd'
Bram Moolenaara12a1612019-01-24 16:39:02 +01008117#ifdef FEAT_GUI
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008118 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008119#endif
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008120 )
8121 {
8122 /* Clear the two screen cells. If the character is actually
8123 * single width it won't change the second cell. */
8124 out_str((char_u *)" ");
8125 term_windgoto(row, col);
8126 }
8127 /* not sure where the cursor is after drawing the ambiguous width
8128 * character */
Bram Moolenaarcb070082016-04-02 22:14:51 +02008129 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008130 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02008131 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008133
8134 /* Convert the UTF-8 character to bytes and write it. */
8135 buf[utfc_char2bytes(off, buf)] = NUL;
8136 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 }
8138 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142 /* double-byte character in single-width cell */
8143 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8144 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145 }
8146
8147 screen_cur_col++;
8148}
8149
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150/*
8151 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8152 * on the screen at position 'row' and 'col'.
8153 * The attributes of the first byte is used for all. This is required to
8154 * output the two bytes of a double-byte character with nothing in between.
8155 */
8156 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008157screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158{
8159 /* Check for illegal values (could be wrong when screen was resized). */
8160 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8161 return;
8162
8163 /* Outputting the last character on the screen may scrollup the screen.
8164 * Don't to it! Mark the character invalid (update it when scrolled up) */
8165 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8166 {
8167 ScreenAttrs[off] = (sattr_T)-1;
8168 return;
8169 }
8170
8171 /* Output the first byte normally (positions the cursor), then write the
8172 * second byte directly. */
8173 screen_char(off, row, col);
8174 out_char(ScreenLines[off + 1]);
8175 ++screen_cur_col;
8176}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178/*
8179 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8180 * This uses the contents of ScreenLines[] and doesn't change it.
8181 */
8182 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008183screen_draw_rectangle(
8184 int row,
8185 int col,
8186 int height,
8187 int width,
8188 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189{
8190 int r, c;
8191 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008192 int max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008194 /* Can't use ScreenLines unless initialized */
8195 if (ScreenLines == NULL)
8196 return;
8197
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 if (invert)
8199 screen_char_attr = HL_INVERSE;
8200 for (r = row; r < row + height; ++r)
8201 {
8202 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008203 max_off = off + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204 for (c = col; c < col + width; ++c)
8205 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00008206 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008207 {
8208 screen_char_2(off + c, r, c);
8209 ++c;
8210 }
8211 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212 {
8213 screen_char(off + c, r, c);
Bram Moolenaar367329b2007-08-30 11:53:22 +00008214 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215 ++c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 }
8217 }
8218 }
8219 screen_char_attr = 0;
8220}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222/*
8223 * Redraw the characters for a vertically split window.
8224 */
8225 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008226redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227{
8228 int col;
8229 int width;
8230
8231# ifdef FEAT_CLIPBOARD
8232 clip_may_clear_selection(row, end - 1);
8233# endif
8234
8235 if (wp == NULL)
8236 {
8237 col = 0;
8238 width = Columns;
8239 }
8240 else
8241 {
8242 col = wp->w_wincol;
8243 width = wp->w_width;
8244 }
8245 screen_draw_rectangle(row, col, end - row, width, FALSE);
8246}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008248 static void
8249space_to_screenline(int off, int attr)
8250{
8251 ScreenLines[off] = ' ';
8252 ScreenAttrs[off] = attr;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008253 if (enc_utf8)
8254 ScreenLinesUC[off] = 0;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008255}
8256
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257/*
8258 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8259 * with character 'c1' in first column followed by 'c2' in the other columns.
8260 * Use attributes 'attr'.
8261 */
8262 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008263screen_fill(
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008264 int start_row,
8265 int end_row,
8266 int start_col,
8267 int end_col,
8268 int c1,
8269 int c2,
8270 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008272 int row;
8273 int col;
8274 int off;
8275 int end_off;
8276 int did_delete;
8277 int c;
8278 int norm_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008280 int force_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281#endif
8282
8283 if (end_row > screen_Rows) /* safety check */
8284 end_row = screen_Rows;
8285 if (end_col > screen_Columns) /* safety check */
8286 end_col = screen_Columns;
8287 if (ScreenLines == NULL
8288 || start_row >= end_row
8289 || start_col >= end_col) /* nothing to do */
8290 return;
8291
8292 /* it's a "normal" terminal when not in a GUI or cterm */
8293 norm_term = (
8294#ifdef FEAT_GUI
8295 !gui.in_use &&
8296#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008297 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298 for (row = start_row; row < end_row; ++row)
8299 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008300 if (has_mbyte
Bram Moolenaara12a1612019-01-24 16:39:02 +01008301#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00008302 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008303#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00008304 )
8305 {
8306 /* When drawing over the right halve of a double-wide char clear
8307 * out the left halve. When drawing over the left halve of a
8308 * double wide-char clear out the right halve. Only needed in a
8309 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008310 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008311 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008312 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008313 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315 /*
8316 * Try to use delete-line termcap code, when no attributes or in a
8317 * "normal" terminal, where a bold/italic space is just a
8318 * space.
8319 */
8320 did_delete = FALSE;
8321 if (c2 == ' '
8322 && end_col == Columns
8323 && can_clear(T_CE)
8324 && (attr == 0
8325 || (norm_term
8326 && attr <= HL_ALL
8327 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8328 {
8329 /*
8330 * check if we really need to clear something
8331 */
8332 col = start_col;
8333 if (c1 != ' ') /* don't clear first char */
8334 ++col;
8335
8336 off = LineOffset[row] + col;
8337 end_off = LineOffset[row] + end_col;
8338
8339 /* skip blanks (used often, keep it fast!) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 if (enc_utf8)
8341 while (off < end_off && ScreenLines[off] == ' '
8342 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8343 ++off;
8344 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345 while (off < end_off && ScreenLines[off] == ' '
8346 && ScreenAttrs[off] == 0)
8347 ++off;
8348 if (off < end_off) /* something to be cleared */
8349 {
8350 col = off - LineOffset[row];
8351 screen_stop_highlight();
8352 term_windgoto(row, col);/* clear rest of this screen line */
8353 out_str(T_CE);
8354 screen_start(); /* don't know where cursor is now */
8355 col = end_col - col;
8356 while (col--) /* clear chars in ScreenLines */
8357 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008358 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359 ++off;
8360 }
8361 }
8362 did_delete = TRUE; /* the chars are cleared now */
8363 }
8364
8365 off = LineOffset[row] + start_col;
8366 c = c1;
8367 for (col = start_col; col < end_col; ++col)
8368 {
Bram Moolenaar33796b32019-06-08 16:01:13 +02008369 if ((ScreenLines[off] != c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008370 || (enc_utf8 && (int)ScreenLinesUC[off]
8371 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372 || ScreenAttrs[off] != attr
8373#if defined(FEAT_GUI) || defined(UNIX)
8374 || force_next
8375#endif
8376 )
Bram Moolenaar33796b32019-06-08 16:01:13 +02008377#ifdef FEAT_TEXT_PROP
8378 // Skip if under a(nother) popup.
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008379 && !blocked_by_popup(row, col)
Bram Moolenaar33796b32019-06-08 16:01:13 +02008380#endif
8381 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382 {
8383#if defined(FEAT_GUI) || defined(UNIX)
8384 /* The bold trick may make a single row of pixels appear in
8385 * the next character. When a bold character is removed, the
8386 * next character should be redrawn too. This happens for our
8387 * own GUI and for some xterms. */
8388 if (
8389# ifdef FEAT_GUI
8390 gui.in_use
8391# endif
8392# if defined(FEAT_GUI) && defined(UNIX)
8393 ||
8394# endif
8395# ifdef UNIX
8396 term_is_xterm
8397# endif
8398 )
8399 {
8400 if (ScreenLines[off] != ' '
8401 && (ScreenAttrs[off] > HL_ALL
8402 || ScreenAttrs[off] & HL_BOLD))
8403 force_next = TRUE;
8404 else
8405 force_next = FALSE;
8406 }
8407#endif
8408 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409 if (enc_utf8)
8410 {
8411 if (c >= 0x80)
8412 {
8413 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008414 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008415 }
8416 else
8417 ScreenLinesUC[off] = 0;
8418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419 ScreenAttrs[off] = attr;
8420 if (!did_delete || c != ' ')
8421 screen_char(off, row, col);
8422 }
8423 ++off;
8424 if (col == start_col)
8425 {
8426 if (did_delete)
8427 break;
8428 c = c2;
8429 }
8430 }
8431 if (end_col == Columns)
8432 LineWraps[row] = FALSE;
8433 if (row == Rows - 1) /* overwritten the command line */
8434 {
8435 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02008436 if (start_col == 0 && end_col == Columns
8437 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008439 if (start_col == 0)
8440 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008441 }
8442 }
8443}
8444
8445/*
8446 * Check if there should be a delay. Used before clearing or redrawing the
8447 * screen or the command line.
8448 */
8449 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008450check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451{
8452 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8453 && !did_wait_return
8454 && emsg_silent == 0)
8455 {
8456 out_flush();
8457 ui_delay(1000L, TRUE);
8458 emsg_on_display = FALSE;
8459 if (check_msg_scroll)
8460 msg_scroll = FALSE;
8461 }
8462}
8463
8464/*
Bram Moolenaarca57ab52019-04-13 14:53:16 +02008465 * Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect.
8466 */
8467 static void
8468clear_TabPageIdxs(void)
8469{
8470 int scol;
8471
8472 for (scol = 0; scol < Columns; ++scol)
8473 TabPageIdxs[scol] = 0;
8474}
8475
8476/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008478 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479 * Returns TRUE if there is a valid screen to write to.
8480 * Returns FALSE when starting up and screen not initialized yet.
8481 */
8482 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008483screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008485 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486 return (ScreenLines != NULL);
8487}
8488
8489/*
8490 * Resize the shell to Rows and Columns.
8491 * Allocate ScreenLines[] and associated items.
8492 *
8493 * There may be some time between setting Rows and Columns and (re)allocating
8494 * ScreenLines[]. This happens when starting up and when (manually) changing
8495 * the shell size. Always use screen_Rows and screen_Columns to access items
8496 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8497 * final size of the shell is needed.
8498 */
8499 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008500screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501{
8502 int new_row, old_row;
8503#ifdef FEAT_GUI
8504 int old_Rows;
8505#endif
8506 win_T *wp;
8507 int outofmem = FALSE;
8508 int len;
8509 schar_T *new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008511 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008513 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514 sattr_T *new_ScreenAttrs;
8515 unsigned *new_LineOffset;
8516 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008517 short *new_TabPageIdxs;
Bram Moolenaar33796b32019-06-08 16:01:13 +02008518#ifdef FEAT_TEXT_PROP
8519 short *new_popup_mask;
Bram Moolenaar4c063a02019-06-10 21:24:12 +02008520 short *new_popup_mask_next;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008521 char *new_popup_transparent;
Bram Moolenaar33796b32019-06-08 16:01:13 +02008522#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00008523 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008525 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008526 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008528retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529 /*
8530 * Allocation of the screen buffers is done only when the size changes and
8531 * when Rows and Columns have been set and we have started doing full
8532 * screen stuff.
8533 */
8534 if ((ScreenLines != NULL
8535 && Rows == screen_Rows
8536 && Columns == screen_Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537 && enc_utf8 == (ScreenLinesUC != NULL)
8538 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaara12a1612019-01-24 16:39:02 +01008539 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540 || Rows == 0
8541 || Columns == 0
8542 || (!full_screen && ScreenLines == NULL))
8543 return;
8544
8545 /*
8546 * It's possible that we produce an out-of-memory message below, which
8547 * will cause this function to be called again. To break the loop, just
8548 * return here.
8549 */
8550 if (entered)
8551 return;
8552 entered = TRUE;
8553
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008554 /*
8555 * Note that the window sizes are updated before reallocating the arrays,
8556 * thus we must not redraw here!
8557 */
8558 ++RedrawingDisabled;
8559
Bram Moolenaar071d4272004-06-13 20:20:40 +00008560 win_new_shellsize(); /* fit the windows in the new sized shell */
8561
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562 comp_col(); /* recompute columns for shown command and ruler */
8563
8564 /*
8565 * We're changing the size of the screen.
8566 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8567 * - Move lines from the old arrays into the new arrays, clear extra
8568 * lines (unless the screen is going to be cleared).
8569 * - Free the old arrays.
8570 *
8571 * If anything fails, make ScreenLines NULL, so we don't do anything!
8572 * Continuing with the old ScreenLines may result in a crash, because the
8573 * size is wrong.
8574 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008575 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008577 if (aucmd_win != NULL)
8578 win_free_lsize(aucmd_win);
Bram Moolenaar8caaf822019-06-01 18:11:22 +02008579#ifdef FEAT_TEXT_PROP
8580 // global popup windows
8581 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
8582 win_free_lsize(wp);
8583 // tab-local popup windows
8584 FOR_ALL_TABPAGES(tp)
8585 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
8586 win_free_lsize(wp);
8587#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008589 new_ScreenLines = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
Bram Moolenaar216b7102010-03-23 13:56:59 +01008590 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591 if (enc_utf8)
8592 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008593 new_ScreenLinesUC = LALLOC_MULT(u8char_T, (Rows + 1) * Columns);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008594 for (i = 0; i < p_mco; ++i)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008595 new_ScreenLinesC[i] = LALLOC_CLEAR_MULT(u8char_T,
8596 (Rows + 1) * Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597 }
8598 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008599 new_ScreenLines2 = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
8600 new_ScreenAttrs = LALLOC_MULT(sattr_T, (Rows + 1) * Columns);
8601 new_LineOffset = LALLOC_MULT(unsigned, Rows);
8602 new_LineWraps = LALLOC_MULT(char_u, Rows);
8603 new_TabPageIdxs = LALLOC_MULT(short, Columns);
Bram Moolenaar33796b32019-06-08 16:01:13 +02008604#ifdef FEAT_TEXT_PROP
8605 new_popup_mask = LALLOC_MULT(short, Rows * Columns);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02008606 new_popup_mask_next = LALLOC_MULT(short, Rows * Columns);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008607 new_popup_transparent = LALLOC_MULT(char, Rows * Columns);
Bram Moolenaar33796b32019-06-08 16:01:13 +02008608#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008610 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611 {
8612 if (win_alloc_lines(wp) == FAIL)
8613 {
8614 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008615 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616 }
8617 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008618 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8619 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008620 outofmem = TRUE;
Bram Moolenaar8caaf822019-06-01 18:11:22 +02008621#ifdef FEAT_TEXT_PROP
8622 // global popup windows
8623 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
8624 if (win_alloc_lines(wp) == FAIL)
8625 {
8626 outofmem = TRUE;
8627 goto give_up;
8628 }
8629 // tab-local popup windows
8630 FOR_ALL_TABPAGES(tp)
8631 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
8632 if (win_alloc_lines(wp) == FAIL)
8633 {
8634 outofmem = TRUE;
8635 goto give_up;
8636 }
8637#endif
8638
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008639give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008641 for (i = 0; i < p_mco; ++i)
8642 if (new_ScreenLinesC[i] == NULL)
8643 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644 if (new_ScreenLines == NULL
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008645 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647 || new_ScreenAttrs == NULL
8648 || new_LineOffset == NULL
8649 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008650 || new_TabPageIdxs == NULL
Bram Moolenaar33796b32019-06-08 16:01:13 +02008651#ifdef FEAT_TEXT_PROP
8652 || new_popup_mask == NULL
Bram Moolenaar4c063a02019-06-10 21:24:12 +02008653 || new_popup_mask_next == NULL
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008654 || new_popup_transparent == NULL
Bram Moolenaar33796b32019-06-08 16:01:13 +02008655#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656 || outofmem)
8657 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008658 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008659 {
8660 /* guess the size */
8661 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8662
8663 /* Remember we did this to avoid getting outofmem messages over
8664 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008665 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008666 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01008667 VIM_CLEAR(new_ScreenLines);
Bram Moolenaard23a8232018-02-10 18:45:26 +01008668 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008669 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01008670 VIM_CLEAR(new_ScreenLinesC[i]);
8671 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaard23a8232018-02-10 18:45:26 +01008672 VIM_CLEAR(new_ScreenAttrs);
8673 VIM_CLEAR(new_LineOffset);
8674 VIM_CLEAR(new_LineWraps);
8675 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar33796b32019-06-08 16:01:13 +02008676#ifdef FEAT_TEXT_PROP
8677 VIM_CLEAR(new_popup_mask);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02008678 VIM_CLEAR(new_popup_mask_next);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008679 VIM_CLEAR(new_popup_transparent);
Bram Moolenaar33796b32019-06-08 16:01:13 +02008680#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681 }
8682 else
8683 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008684 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008685
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686 for (new_row = 0; new_row < Rows; ++new_row)
8687 {
8688 new_LineOffset[new_row] = new_row * Columns;
8689 new_LineWraps[new_row] = FALSE;
8690
8691 /*
8692 * If the screen is not going to be cleared, copy as much as
8693 * possible from the old screen to the new one and clear the rest
8694 * (used when resizing the window at the "--more--" prompt or when
8695 * executing an external command, for the GUI).
8696 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008697 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698 {
8699 (void)vim_memset(new_ScreenLines + new_row * Columns,
8700 ' ', (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701 if (enc_utf8)
8702 {
8703 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8704 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008705 for (i = 0; i < p_mco; ++i)
8706 (void)vim_memset(new_ScreenLinesC[i]
8707 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708 0, (size_t)Columns * sizeof(u8char_T));
8709 }
8710 if (enc_dbcs == DBCS_JPNU)
8711 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8712 0, (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008713 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8714 0, (size_t)Columns * sizeof(sattr_T));
8715 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008716 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717 {
8718 if (screen_Columns < Columns)
8719 len = screen_Columns;
8720 else
8721 len = Columns;
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008722 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008723 * may be invalid now. Also when p_mco changes. */
8724 if (!(enc_utf8 && ScreenLinesUC == NULL)
8725 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008726 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8727 ScreenLines + LineOffset[old_row],
8728 (size_t)len * sizeof(schar_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008729 if (enc_utf8 && ScreenLinesUC != NULL
8730 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731 {
8732 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8733 ScreenLinesUC + LineOffset[old_row],
8734 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008735 for (i = 0; i < p_mco; ++i)
8736 mch_memmove(new_ScreenLinesC[i]
8737 + new_LineOffset[new_row],
8738 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739 (size_t)len * sizeof(u8char_T));
8740 }
8741 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8742 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8743 ScreenLines2 + LineOffset[old_row],
8744 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8746 ScreenAttrs + LineOffset[old_row],
8747 (size_t)len * sizeof(sattr_T));
8748 }
8749 }
8750 }
8751 /* Use the last line of the screen for the current line. */
8752 current_ScreenLine = new_ScreenLines + Rows * Columns;
Bram Moolenaar6ace95e2019-08-13 23:09:49 +02008753
8754#ifdef FEAT_TEXT_PROP
8755 vim_memset(new_popup_mask, 0, Rows * Columns * sizeof(short));
8756 vim_memset(new_popup_transparent, 0, Rows * Columns * sizeof(char));
8757#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758 }
8759
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008760 free_screenlines();
8761
Bram Moolenaar6ace95e2019-08-13 23:09:49 +02008762 // NOTE: this may result in all pointers to become NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008763 ScreenLines = new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008765 for (i = 0; i < p_mco; ++i)
8766 ScreenLinesC[i] = new_ScreenLinesC[i];
8767 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768 ScreenLines2 = new_ScreenLines2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769 ScreenAttrs = new_ScreenAttrs;
8770 LineOffset = new_LineOffset;
8771 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008772 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar33796b32019-06-08 16:01:13 +02008773#ifdef FEAT_TEXT_PROP
8774 popup_mask = new_popup_mask;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008775 popup_mask_next = new_popup_mask_next;
8776 popup_transparent = new_popup_transparent;
Bram Moolenaar33796b32019-06-08 16:01:13 +02008777 popup_mask_refresh = TRUE;
8778#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779
8780 /* It's important that screen_Rows and screen_Columns reflect the actual
8781 * size of ScreenLines[]. Set them before calling anything. */
8782#ifdef FEAT_GUI
8783 old_Rows = screen_Rows;
8784#endif
8785 screen_Rows = Rows;
8786 screen_Columns = Columns;
8787
8788 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008789 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008790 screenclear2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008791#ifdef FEAT_GUI
8792 else if (gui.in_use
8793 && !gui.starting
8794 && ScreenLines != NULL
8795 && old_Rows != Rows)
8796 {
8797 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8798 /*
8799 * Adjust the position of the cursor, for when executing an external
8800 * command.
8801 */
8802 if (msg_row >= Rows) /* Rows got smaller */
8803 msg_row = Rows - 1; /* put cursor at last row */
8804 else if (Rows > old_Rows) /* Rows got bigger */
8805 msg_row += Rows - old_Rows; /* put cursor in same place */
8806 if (msg_col >= Columns) /* Columns got smaller */
8807 msg_col = Columns - 1; /* put cursor at last column */
8808 }
8809#endif
Bram Moolenaarca57ab52019-04-13 14:53:16 +02008810 clear_TabPageIdxs();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811
Bram Moolenaar071d4272004-06-13 20:20:40 +00008812 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008813 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008814
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008815 /*
8816 * Do not apply autocommands more than 3 times to avoid an endless loop
8817 * in case applying autocommands always changes Rows or Columns.
8818 */
8819 if (starting == 0 && ++retry_count <= 3)
8820 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008821 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008822 /* In rare cases, autocommands may have altered Rows or Columns,
8823 * jump back to check if we need to allocate the screen again. */
8824 goto retry;
8825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826}
8827
8828 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008829free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008830{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008831 int i;
8832
Bram Moolenaar33796b32019-06-08 16:01:13 +02008833 VIM_CLEAR(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008834 for (i = 0; i < Screen_mco; ++i)
Bram Moolenaar33796b32019-06-08 16:01:13 +02008835 VIM_CLEAR(ScreenLinesC[i]);
8836 VIM_CLEAR(ScreenLines2);
8837 VIM_CLEAR(ScreenLines);
8838 VIM_CLEAR(ScreenAttrs);
8839 VIM_CLEAR(LineOffset);
8840 VIM_CLEAR(LineWraps);
8841 VIM_CLEAR(TabPageIdxs);
8842#ifdef FEAT_TEXT_PROP
8843 VIM_CLEAR(popup_mask);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02008844 VIM_CLEAR(popup_mask_next);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008845 VIM_CLEAR(popup_transparent);
Bram Moolenaar33796b32019-06-08 16:01:13 +02008846#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008847}
8848
8849 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008850screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008851{
8852 check_for_delay(FALSE);
8853 screenalloc(FALSE); /* allocate screen buffers if size changed */
8854 screenclear2(); /* clear the screen */
8855}
8856
8857 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008858screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859{
8860 int i;
8861
8862 if (starting == NO_SCREEN || ScreenLines == NULL
8863#ifdef FEAT_GUI
8864 || (gui.in_use && gui.starting)
8865#endif
8866 )
8867 return;
8868
8869#ifdef FEAT_GUI
8870 if (!gui.in_use)
8871#endif
8872 screen_attr = -1; /* force setting the Normal colors */
8873 screen_stop_highlight(); /* don't want highlighting here */
8874
8875#ifdef FEAT_CLIPBOARD
8876 /* disable selection without redrawing it */
8877 clip_scroll_selection(9999);
8878#endif
8879
8880 /* blank out ScreenLines */
8881 for (i = 0; i < Rows; ++i)
8882 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02008883 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884 LineWraps[i] = FALSE;
8885 }
8886
8887 if (can_clear(T_CL))
8888 {
8889 out_str(T_CL); /* clear the display */
8890 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008891 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892 }
8893 else
8894 {
8895 /* can't clear the screen, mark all chars with invalid attributes */
8896 for (i = 0; i < Rows; ++i)
8897 lineinvalid(LineOffset[i], (int)Columns);
8898 clear_cmdline = TRUE;
8899 }
8900
8901 screen_cleared = TRUE; /* can use contents of ScreenLines now */
8902
8903 win_rest_invalid(firstwin);
8904 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008905 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906 if (must_redraw == CLEAR) /* no need to clear again */
8907 must_redraw = NOT_VALID;
8908 compute_cmdrow();
8909 msg_row = cmdline_row; /* put cursor on last line for messages */
8910 msg_col = 0;
8911 screen_start(); /* don't know where cursor is now */
8912 msg_scrolled = 0; /* can't scroll back */
8913 msg_didany = FALSE;
8914 msg_didout = FALSE;
8915}
8916
8917/*
8918 * Clear one line in ScreenLines.
8919 */
8920 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02008921lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922{
8923 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924 if (enc_utf8)
8925 (void)vim_memset(ScreenLinesUC + off, 0,
8926 (size_t)width * sizeof(u8char_T));
Bram Moolenaarcfce7172017-08-17 20:31:48 +02008927 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928}
8929
8930/*
8931 * Mark one line in ScreenLines invalid by setting the attributes to an
8932 * invalid value.
8933 */
8934 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008935lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936{
8937 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
8938}
8939
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940/*
8941 * Copy part of a Screenline for vertically split window "wp".
8942 */
8943 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008944linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945{
8946 unsigned off_to = LineOffset[to] + wp->w_wincol;
8947 unsigned off_from = LineOffset[from] + wp->w_wincol;
8948
8949 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
8950 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951 if (enc_utf8)
8952 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008953 int i;
8954
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
8956 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008957 for (i = 0; i < p_mco; ++i)
8958 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
8959 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960 }
8961 if (enc_dbcs == DBCS_JPNU)
8962 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
8963 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008964 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
8965 wp->w_width * sizeof(sattr_T));
8966}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967
8968/*
8969 * Return TRUE if clearing with term string "p" would work.
8970 * It can't work when the string is empty or it won't set the right background.
Bram Moolenaar33796b32019-06-08 16:01:13 +02008971 * Don't clear to end-of-line when there are popups, it may cause flicker.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972 */
8973 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008974can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008975{
8976 return (*p != NUL && (t_colors <= 1
8977#ifdef FEAT_GUI
8978 || gui.in_use
8979#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008980#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008981 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02008982 || (!p_tgc && cterm_normal_bg_color == 0)
8983#else
8984 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008985#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02008986 || *T_UT != NUL)
8987#ifdef FEAT_TEXT_PROP
8988 && !(p == T_CE && popup_visible)
8989#endif
8990 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00008991}
8992
8993/*
8994 * Reset cursor position. Use whenever cursor was moved because of outputting
8995 * something directly to the screen (shell commands) or a terminal control
8996 * code.
8997 */
8998 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008999screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009000{
9001 screen_cur_row = screen_cur_col = 9999;
9002}
9003
9004/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009005 * Move the cursor to position "row","col" in the screen.
9006 * This tries to find the most efficient way to move, minimizing the number of
9007 * characters sent to the terminal.
9008 */
9009 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009010windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009011{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009012 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009013 int i;
9014 int plan;
9015 int cost;
9016 int wouldbe_col;
9017 int noinvcurs;
9018 char_u *bs;
9019 int goto_cost;
9020 int attr;
9021
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009022#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009023#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9024
9025#define PLAN_LE 1
9026#define PLAN_CR 2
9027#define PLAN_NL 3
9028#define PLAN_WRITE 4
9029 /* Can't use ScreenLines unless initialized */
9030 if (ScreenLines == NULL)
9031 return;
9032
9033 if (col != screen_cur_col || row != screen_cur_row)
9034 {
9035 /* Check for valid position. */
9036 if (row < 0) /* window without text lines? */
9037 row = 0;
9038 if (row >= screen_Rows)
9039 row = screen_Rows - 1;
9040 if (col >= screen_Columns)
9041 col = screen_Columns - 1;
9042
9043 /* check if no cursor movement is allowed in highlight mode */
9044 if (screen_attr && *T_MS == NUL)
9045 noinvcurs = HIGHL_COST;
9046 else
9047 noinvcurs = 0;
9048 goto_cost = GOTO_COST + noinvcurs;
9049
9050 /*
9051 * Plan how to do the positioning:
9052 * 1. Use CR to move it to column 0, same row.
9053 * 2. Use T_LE to move it a few columns to the left.
9054 * 3. Use NL to move a few lines down, column 0.
9055 * 4. Move a few columns to the right with T_ND or by writing chars.
9056 *
9057 * Don't do this if the cursor went beyond the last column, the cursor
9058 * position is unknown then (some terminals wrap, some don't )
9059 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009060 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009061 * characters to move the cursor to the right.
9062 */
9063 if (row >= screen_cur_row && screen_cur_col < Columns)
9064 {
9065 /*
9066 * If the cursor is in the same row, bigger col, we can use CR
9067 * or T_LE.
9068 */
9069 bs = NULL; /* init for GCC */
9070 attr = screen_attr;
9071 if (row == screen_cur_row && col < screen_cur_col)
9072 {
9073 /* "le" is preferred over "bc", because "bc" is obsolete */
9074 if (*T_LE)
9075 bs = T_LE; /* "cursor left" */
9076 else
9077 bs = T_BC; /* "backspace character (old) */
9078 if (*bs)
9079 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9080 else
9081 cost = 999;
9082 if (col + 1 < cost) /* using CR is less characters */
9083 {
9084 plan = PLAN_CR;
9085 wouldbe_col = 0;
9086 cost = 1; /* CR is just one character */
9087 }
9088 else
9089 {
9090 plan = PLAN_LE;
9091 wouldbe_col = col;
9092 }
9093 if (noinvcurs) /* will stop highlighting */
9094 {
9095 cost += noinvcurs;
9096 attr = 0;
9097 }
9098 }
9099
9100 /*
9101 * If the cursor is above where we want to be, we can use CR LF.
9102 */
9103 else if (row > screen_cur_row)
9104 {
9105 plan = PLAN_NL;
9106 wouldbe_col = 0;
9107 cost = (row - screen_cur_row) * 2; /* CR LF */
9108 if (noinvcurs) /* will stop highlighting */
9109 {
9110 cost += noinvcurs;
9111 attr = 0;
9112 }
9113 }
9114
9115 /*
9116 * If the cursor is in the same row, smaller col, just use write.
9117 */
9118 else
9119 {
9120 plan = PLAN_WRITE;
9121 wouldbe_col = screen_cur_col;
9122 cost = 0;
9123 }
9124
9125 /*
9126 * Check if any characters that need to be written have the
9127 * correct attributes. Also avoid UTF-8 characters.
9128 */
9129 i = col - wouldbe_col;
9130 if (i > 0)
9131 cost += i;
9132 if (cost < goto_cost && i > 0)
9133 {
9134 /*
9135 * Check if the attributes are correct without additionally
9136 * stopping highlighting.
9137 */
9138 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9139 while (i && *p++ == attr)
9140 --i;
9141 if (i != 0)
9142 {
9143 /*
9144 * Try if it works when highlighting is stopped here.
9145 */
9146 if (*--p == 0)
9147 {
9148 cost += noinvcurs;
9149 while (i && *p++ == 0)
9150 --i;
9151 }
9152 if (i != 0)
9153 cost = 999; /* different attributes, don't do it */
9154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009155 if (enc_utf8)
9156 {
9157 /* Don't use an UTF-8 char for positioning, it's slow. */
9158 for (i = wouldbe_col; i < col; ++i)
9159 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9160 {
9161 cost = 999;
9162 break;
9163 }
9164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009165 }
9166
9167 /*
9168 * We can do it without term_windgoto()!
9169 */
9170 if (cost < goto_cost)
9171 {
9172 if (plan == PLAN_LE)
9173 {
9174 if (noinvcurs)
9175 screen_stop_highlight();
9176 while (screen_cur_col > col)
9177 {
9178 out_str(bs);
9179 --screen_cur_col;
9180 }
9181 }
9182 else if (plan == PLAN_CR)
9183 {
9184 if (noinvcurs)
9185 screen_stop_highlight();
9186 out_char('\r');
9187 screen_cur_col = 0;
9188 }
9189 else if (plan == PLAN_NL)
9190 {
9191 if (noinvcurs)
9192 screen_stop_highlight();
9193 while (screen_cur_row < row)
9194 {
9195 out_char('\n');
9196 ++screen_cur_row;
9197 }
9198 screen_cur_col = 0;
9199 }
9200
9201 i = col - screen_cur_col;
9202 if (i > 0)
9203 {
9204 /*
9205 * Use cursor-right if it's one character only. Avoids
9206 * removing a line of pixels from the last bold char, when
9207 * using the bold trick in the GUI.
9208 */
9209 if (T_ND[0] != NUL && T_ND[1] == NUL)
9210 {
9211 while (i-- > 0)
9212 out_char(*T_ND);
9213 }
9214 else
9215 {
9216 int off;
9217
9218 off = LineOffset[row] + screen_cur_col;
9219 while (i-- > 0)
9220 {
9221 if (ScreenAttrs[off] != screen_attr)
9222 screen_stop_highlight();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009223 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009224 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009225 if (enc_dbcs == DBCS_JPNU
9226 && ScreenLines[off] == 0x8e)
9227 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009228 ++off;
9229 }
9230 }
9231 }
9232 }
9233 }
9234 else
9235 cost = 999;
9236
9237 if (cost >= goto_cost)
9238 {
9239 if (noinvcurs)
9240 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009241 if (row == screen_cur_row && (col > screen_cur_col)
9242 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009243 term_cursor_right(col - screen_cur_col);
9244 else
9245 term_windgoto(row, col);
9246 }
9247 screen_cur_row = row;
9248 screen_cur_col = col;
9249 }
9250}
9251
9252/*
9253 * Set cursor to its position in the current window.
9254 */
9255 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009256setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009257{
Bram Moolenaar987723e2018-03-06 11:43:04 +01009258 setcursor_mayforce(FALSE);
9259}
9260
9261/*
9262 * Set cursor to its position in the current window.
9263 * When "force" is TRUE also when not redrawing.
9264 */
9265 void
9266setcursor_mayforce(int force)
9267{
9268 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009269 {
9270 validate_cursor();
9271 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009272 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009273#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009274 /* With 'rightleft' set and the cursor on a double-wide
9275 * character, position it on the leftmost column. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01009276 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol
9277 - ((has_mbyte
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009278 && (*mb_ptr2cells)(ml_get_cursor()) == 2
Bram Moolenaara12a1612019-01-24 16:39:02 +01009279 && vim_isprintc(gchar_cursor())) ? 2 : 1)) :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009280#endif
9281 curwin->w_wcol));
9282 }
9283}
9284
9285
9286/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009287 * Insert 'line_count' lines at 'row' in window 'wp'.
9288 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9289 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009290 * scrolling.
9291 * Returns FAIL if the lines are not inserted, OK for success.
9292 */
9293 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009294win_ins_lines(
9295 win_T *wp,
9296 int row,
9297 int line_count,
9298 int invalid,
9299 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009300{
9301 int did_delete;
9302 int nextrow;
9303 int lastrow;
9304 int retval;
9305
9306 if (invalid)
9307 wp->w_lines_valid = 0;
9308
9309 if (wp->w_height < 5)
9310 return FAIL;
9311
9312 if (line_count > wp->w_height - row)
9313 line_count = wp->w_height - row;
9314
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009315 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009316 if (retval != MAYBE)
9317 return retval;
9318
9319 /*
9320 * If there is a next window or a status line, we first try to delete the
9321 * lines at the bottom to avoid messing what is after the window.
Bram Moolenaarc363fe12019-08-04 18:13:46 +02009322 * If this fails and there are following windows, don't do anything to
9323 * avoid messing up those windows, better just redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009324 */
9325 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009326 if (wp->w_next != NULL || wp->w_status_height)
9327 {
9328 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009329 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009330 did_delete = TRUE;
9331 else if (wp->w_next)
9332 return FAIL;
9333 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009334 /*
9335 * if no lines deleted, blank the lines that will end up below the window
9336 */
9337 if (!did_delete)
9338 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009339 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009340 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02009341 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009342 lastrow = nextrow + line_count;
9343 if (lastrow > Rows)
9344 lastrow = Rows;
9345 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009346 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009347 ' ', ' ', 0);
9348 }
9349
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009350 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009351 == FAIL)
9352 {
Bram Moolenaarc363fe12019-08-04 18:13:46 +02009353 // deletion will have messed up other windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00009354 if (did_delete)
9355 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009356 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009357 win_rest_invalid(W_NEXT(wp));
9358 }
9359 return FAIL;
9360 }
9361
9362 return OK;
9363}
9364
9365/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009366 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009367 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9368 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9369 * scrolling
9370 * Return OK for success, FAIL if the lines are not deleted.
9371 */
9372 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009373win_del_lines(
9374 win_T *wp,
9375 int row,
9376 int line_count,
9377 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009378 int mayclear,
9379 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009380{
9381 int retval;
9382
9383 if (invalid)
9384 wp->w_lines_valid = 0;
9385
9386 if (line_count > wp->w_height - row)
9387 line_count = wp->w_height - row;
9388
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009389 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009390 if (retval != MAYBE)
9391 return retval;
9392
9393 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009394 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009395 return FAIL;
9396
Bram Moolenaar071d4272004-06-13 20:20:40 +00009397 /*
9398 * If there are windows or status lines below, try to put them at the
9399 * correct place. If we can't do that, they have to be redrawn.
9400 */
9401 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9402 {
9403 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009404 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009405 {
9406 wp->w_redr_status = TRUE;
9407 win_rest_invalid(wp->w_next);
9408 }
9409 }
9410 /*
9411 * If this is the last window and there is no status line, redraw the
9412 * command line later.
9413 */
9414 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009415 redraw_cmdline = TRUE;
9416 return OK;
9417}
9418
9419/*
9420 * Common code for win_ins_lines() and win_del_lines().
9421 * Returns OK or FAIL when the work has been done.
9422 * Returns MAYBE when not finished yet.
9423 */
9424 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009425win_do_lines(
9426 win_T *wp,
9427 int row,
9428 int line_count,
9429 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009430 int del,
9431 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009432{
9433 int retval;
9434
9435 if (!redrawing() || line_count <= 0)
9436 return FAIL;
9437
Bram Moolenaar33796b32019-06-08 16:01:13 +02009438 // When inserting lines would result in loss of command output, just redraw
9439 // the lines.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009440 if (no_win_do_lines_ins && !del)
9441 return FAIL;
9442
Bram Moolenaar33796b32019-06-08 16:01:13 +02009443 // only a few lines left: redraw is faster
Bram Moolenaar4033c552017-09-16 20:54:51 +02009444 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009445 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009446 if (!no_win_do_lines_ins)
Bram Moolenaar33796b32019-06-08 16:01:13 +02009447 screenclear(); // will set wp->w_lines_valid to 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00009448 return FAIL;
9449 }
9450
Bram Moolenaar33796b32019-06-08 16:01:13 +02009451#ifdef FEAT_TEXT_PROP
Bram Moolenaar4c063a02019-06-10 21:24:12 +02009452 // this doesn't work when there are popups visible
Bram Moolenaar33796b32019-06-08 16:01:13 +02009453 if (popup_visible)
9454 return FAIL;
9455#endif
9456
9457 // Delete all remaining lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00009458 if (row + line_count >= wp->w_height)
9459 {
9460 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009461 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009462 ' ', ' ', 0);
9463 return OK;
9464 }
9465
9466 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009467 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009468 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009469 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009470 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009471 if (!no_win_do_lines_ins)
9472 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009473
9474 /*
9475 * If the terminal can set a scroll region, use that.
9476 * Always do this in a vertically split window. This will redraw from
9477 * ScreenLines[] when t_CV isn't defined. That's faster than using
9478 * win_line().
9479 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009480 * a character in the lower right corner of the scroll region may cause a
9481 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009482 */
Bram Moolenaar02631462017-09-22 15:20:32 +02009483 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009484 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009486 scroll_region_set(wp, row);
9487 if (del)
9488 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009489 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009490 else
9491 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009492 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009493 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494 scroll_region_reset();
9495 return retval;
9496 }
9497
Bram Moolenaar071d4272004-06-13 20:20:40 +00009498 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9499 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009500
9501 return MAYBE;
9502}
9503
9504/*
9505 * window 'wp' and everything after it is messed up, mark it for redraw
9506 */
9507 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009508win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009509{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009510 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511 {
9512 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009513 wp->w_redr_status = TRUE;
9514 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009515 }
9516 redraw_cmdline = TRUE;
9517}
9518
9519/*
9520 * The rest of the routines in this file perform screen manipulations. The
9521 * given operation is performed physically on the screen. The corresponding
9522 * change is also made to the internal screen image. In this way, the editor
9523 * anticipates the effect of editing changes on the appearance of the screen.
9524 * That way, when we call screenupdate a complete redraw isn't usually
9525 * necessary. Another advantage is that we can keep adding code to anticipate
9526 * screen changes, and in the meantime, everything still works.
9527 */
9528
9529/*
9530 * types for inserting or deleting lines
9531 */
9532#define USE_T_CAL 1
9533#define USE_T_CDL 2
9534#define USE_T_AL 3
9535#define USE_T_CE 4
9536#define USE_T_DL 5
9537#define USE_T_SR 6
9538#define USE_NL 7
9539#define USE_T_CD 8
9540#define USE_REDRAW 9
9541
9542/*
9543 * insert lines on the screen and update ScreenLines[]
9544 * 'end' is the line after the scrolled part. Normally it is Rows.
9545 * When scrolling region used 'off' is the offset from the top for the region.
9546 * 'row' and 'end' are relative to the start of the region.
9547 *
9548 * return FAIL for failure, OK for success.
9549 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009550 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009551screen_ins_lines(
9552 int off,
9553 int row,
9554 int line_count,
9555 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009556 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01009557 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558{
9559 int i;
9560 int j;
9561 unsigned temp;
9562 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009563 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009564 int type;
9565 int result_empty;
9566 int can_ce = can_clear(T_CE);
9567
9568 /*
9569 * FAIL if
9570 * - there is no valid screen
9571 * - the screen has to be redrawn completely
9572 * - the line count is less than one
9573 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009574 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar33796b32019-06-08 16:01:13 +02009575 * - there is a popup window
Bram Moolenaar071d4272004-06-13 20:20:40 +00009576 */
Bram Moolenaar33796b32019-06-08 16:01:13 +02009577 if (!screen_valid(TRUE)
9578 || line_count <= 0 || line_count > p_ttyscroll
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009579#ifdef FEAT_CLIPBOARD
9580 || (clip_star.state != SELECT_CLEARED
9581 && redrawing_for_callback > 0)
9582#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02009583#ifdef FEAT_TEXT_PROP
9584 || popup_visible
9585#endif
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009586 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009587 return FAIL;
9588
9589 /*
9590 * There are seven ways to insert lines:
9591 * 0. When in a vertically split window and t_CV isn't set, redraw the
9592 * characters from ScreenLines[].
9593 * 1. Use T_CD (clear to end of display) if it exists and the result of
9594 * the insert is just empty lines
9595 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9596 * present or line_count > 1. It looks better if we do all the inserts
9597 * at once.
9598 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9599 * insert is just empty lines and T_CE is not present or line_count >
9600 * 1.
9601 * 4. Use T_AL (insert line) if it exists.
9602 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9603 * just empty lines.
9604 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9605 * just empty lines.
9606 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9607 * the 'da' flag is not set or we have clear line capability.
9608 * 8. redraw the characters from ScreenLines[].
9609 *
9610 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9611 * the scrollbar for the window. It does have insert line, use that if it
9612 * exists.
9613 */
9614 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009615 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9616 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009617 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009618 type = USE_T_CD;
9619 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9620 type = USE_T_CAL;
9621 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9622 type = USE_T_CDL;
9623 else if (*T_AL != NUL)
9624 type = USE_T_AL;
9625 else if (can_ce && result_empty)
9626 type = USE_T_CE;
9627 else if (*T_DL != NUL && result_empty)
9628 type = USE_T_DL;
9629 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9630 type = USE_T_SR;
9631 else
9632 return FAIL;
9633
9634 /*
9635 * For clearing the lines screen_del_lines() is used. This will also take
9636 * care of t_db if necessary.
9637 */
9638 if (type == USE_T_CD || type == USE_T_CDL ||
9639 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009640 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009641
9642 /*
9643 * If text is retained below the screen, first clear or delete as many
9644 * lines at the bottom of the window as are about to be inserted so that
9645 * the deleted lines won't later surface during a screen_del_lines.
9646 */
9647 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009648 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009649
9650#ifdef FEAT_CLIPBOARD
9651 /* Remove a modeless selection when inserting lines halfway the screen
9652 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009653 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009654 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009655 else
9656 clip_scroll_selection(-line_count);
9657#endif
9658
Bram Moolenaar071d4272004-06-13 20:20:40 +00009659#ifdef FEAT_GUI
9660 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9661 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009662 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009663#endif
9664
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009665 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
9666 cursor_col = wp->w_wincol;
9667
Bram Moolenaar071d4272004-06-13 20:20:40 +00009668 if (*T_CCS != NUL) /* cursor relative to region */
9669 cursor_row = row;
9670 else
9671 cursor_row = row + off;
9672
9673 /*
9674 * Shift LineOffset[] line_count down to reflect the inserted lines.
9675 * Clear the inserted lines in ScreenLines[].
9676 */
9677 row += off;
9678 end += off;
9679 for (i = 0; i < line_count; ++i)
9680 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009681 if (wp != NULL && wp->w_width != Columns)
9682 {
9683 /* need to copy part of a line */
9684 j = end - 1 - i;
9685 while ((j -= line_count) >= row)
9686 linecopy(j + line_count, j, wp);
9687 j += line_count;
9688 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009689 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
9690 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009691 else
9692 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9693 LineWraps[j] = FALSE;
9694 }
9695 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009696 {
9697 j = end - 1 - i;
9698 temp = LineOffset[j];
9699 while ((j -= line_count) >= row)
9700 {
9701 LineOffset[j + line_count] = LineOffset[j];
9702 LineWraps[j + line_count] = LineWraps[j];
9703 }
9704 LineOffset[j + line_count] = temp;
9705 LineWraps[j + line_count] = FALSE;
9706 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009707 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009708 else
9709 lineinvalid(temp, (int)Columns);
9710 }
9711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009712
9713 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009714 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009715 if (clear_attr != 0)
9716 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009717
Bram Moolenaar071d4272004-06-13 20:20:40 +00009718 /* redraw the characters */
9719 if (type == USE_REDRAW)
9720 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02009721 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009722 {
9723 term_append_lines(line_count);
9724 screen_start(); /* don't know where cursor is now */
9725 }
9726 else
9727 {
9728 for (i = 0; i < line_count; i++)
9729 {
9730 if (type == USE_T_AL)
9731 {
9732 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009733 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009734 out_str(T_AL);
9735 }
9736 else /* type == USE_T_SR */
9737 out_str(T_SR);
9738 screen_start(); /* don't know where cursor is now */
9739 }
9740 }
9741
9742 /*
9743 * With scroll-reverse and 'da' flag set we need to clear the lines that
9744 * have been scrolled down into the region.
9745 */
9746 if (type == USE_T_SR && *T_DA)
9747 {
9748 for (i = 0; i < line_count; ++i)
9749 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009750 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009751 out_str(T_CE);
9752 screen_start(); /* don't know where cursor is now */
9753 }
9754 }
9755
9756#ifdef FEAT_GUI
9757 gui_can_update_cursor();
9758 if (gui.in_use)
9759 out_flush(); /* always flush after a scroll */
9760#endif
9761 return OK;
9762}
9763
9764/*
Bram Moolenaar107abd22016-08-12 14:08:25 +02009765 * Delete lines on the screen and update ScreenLines[].
9766 * "end" is the line after the scrolled part. Normally it is Rows.
9767 * When scrolling region used "off" is the offset from the top for the region.
9768 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009769 *
9770 * Return OK for success, FAIL if the lines are not deleted.
9771 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009772 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009773screen_del_lines(
9774 int off,
9775 int row,
9776 int line_count,
9777 int end,
9778 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009779 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +01009780 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009781{
9782 int j;
9783 int i;
9784 unsigned temp;
9785 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009786 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009787 int cursor_end;
9788 int result_empty; /* result is empty until end of region */
9789 int can_delete; /* deleting line codes can be used */
9790 int type;
9791
9792 /*
9793 * FAIL if
9794 * - there is no valid screen
9795 * - the screen has to be redrawn completely
9796 * - the line count is less than one
9797 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009798 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009799 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009800 if (!screen_valid(TRUE) || line_count <= 0
9801 || (!force && line_count > p_ttyscroll)
9802#ifdef FEAT_CLIPBOARD
9803 || (clip_star.state != SELECT_CLEARED
9804 && redrawing_for_callback > 0)
9805#endif
9806 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009807 return FAIL;
9808
9809 /*
9810 * Check if the rest of the current region will become empty.
9811 */
9812 result_empty = row + line_count >= end;
9813
9814 /*
9815 * We can delete lines only when 'db' flag not set or when 'ce' option
9816 * available.
9817 */
9818 can_delete = (*T_DB == NUL || can_clear(T_CE));
9819
9820 /*
9821 * There are six ways to delete lines:
9822 * 0. When in a vertically split window and t_CV isn't set, redraw the
9823 * characters from ScreenLines[].
9824 * 1. Use T_CD if it exists and the result is empty.
9825 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9826 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9827 * none of the other ways work.
9828 * 4. Use T_CE (erase line) if the result is empty.
9829 * 5. Use T_DL (delete line) if it exists.
9830 * 6. redraw the characters from ScreenLines[].
9831 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009832 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9833 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009834 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009835 type = USE_T_CD;
9836#if defined(__BEOS__) && defined(BEOS_DR8)
9837 /*
9838 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9839 * its internal termcap... this works okay for tests which test *T_DB !=
9840 * NUL. It has the disadvantage that the user cannot use any :set t_*
9841 * command to get T_DB (back) to empty_option, only :set term=... will do
9842 * the trick...
9843 * Anyway, this hack will hopefully go away with the next OS release.
9844 * (Olaf Seibert)
9845 */
9846 else if (row == 0 && T_DB == empty_option
9847 && (line_count == 1 || *T_CDL == NUL))
9848#else
9849 else if (row == 0 && (
9850#ifndef AMIGA
9851 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9852 * up, so use delete-line command */
9853 line_count == 1 ||
9854#endif
9855 *T_CDL == NUL))
9856#endif
9857 type = USE_NL;
9858 else if (*T_CDL != NUL && line_count > 1 && can_delete)
9859 type = USE_T_CDL;
9860 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +02009861 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009862 type = USE_T_CE;
9863 else if (*T_DL != NUL && can_delete)
9864 type = USE_T_DL;
9865 else if (*T_CDL != NUL && can_delete)
9866 type = USE_T_CDL;
9867 else
9868 return FAIL;
9869
9870#ifdef FEAT_CLIPBOARD
9871 /* Remove a modeless selection when deleting lines halfway the screen or
9872 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009873 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009874 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009875 else
9876 clip_scroll_selection(line_count);
9877#endif
9878
Bram Moolenaar071d4272004-06-13 20:20:40 +00009879#ifdef FEAT_GUI
9880 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9881 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009882 gui_dont_update_cursor(gui.cursor_row >= row + off
9883 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009884#endif
9885
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009886 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
9887 cursor_col = wp->w_wincol;
9888
Bram Moolenaar071d4272004-06-13 20:20:40 +00009889 if (*T_CCS != NUL) /* cursor relative to region */
9890 {
9891 cursor_row = row;
9892 cursor_end = end;
9893 }
9894 else
9895 {
9896 cursor_row = row + off;
9897 cursor_end = end + off;
9898 }
9899
9900 /*
9901 * Now shift LineOffset[] line_count up to reflect the deleted lines.
9902 * Clear the inserted lines in ScreenLines[].
9903 */
9904 row += off;
9905 end += off;
9906 for (i = 0; i < line_count; ++i)
9907 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009908 if (wp != NULL && wp->w_width != Columns)
9909 {
9910 /* need to copy part of a line */
9911 j = row + i;
9912 while ((j += line_count) <= end - 1)
9913 linecopy(j - line_count, j, wp);
9914 j -= line_count;
9915 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009916 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
9917 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009918 else
9919 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9920 LineWraps[j] = FALSE;
9921 }
9922 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009923 {
9924 /* whole width, moving the line pointers is faster */
9925 j = row + i;
9926 temp = LineOffset[j];
9927 while ((j += line_count) <= end - 1)
9928 {
9929 LineOffset[j - line_count] = LineOffset[j];
9930 LineWraps[j - line_count] = LineWraps[j];
9931 }
9932 LineOffset[j - line_count] = temp;
9933 LineWraps[j - line_count] = FALSE;
9934 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009935 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009936 else
9937 lineinvalid(temp, (int)Columns);
9938 }
9939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009940
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009941 if (screen_attr != clear_attr)
9942 screen_stop_highlight();
9943 if (clear_attr != 0)
9944 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009945
Bram Moolenaar071d4272004-06-13 20:20:40 +00009946 /* redraw the characters */
9947 if (type == USE_REDRAW)
9948 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02009949 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009950 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009951 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009952 out_str(T_CD);
9953 screen_start(); /* don't know where cursor is now */
9954 }
9955 else if (type == USE_T_CDL)
9956 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009957 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009958 term_delete_lines(line_count);
9959 screen_start(); /* don't know where cursor is now */
9960 }
9961 /*
9962 * Deleting lines at top of the screen or scroll region: Just scroll
9963 * the whole screen (scroll region) up by outputting newlines on the
9964 * last line.
9965 */
9966 else if (type == USE_NL)
9967 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009968 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009969 for (i = line_count; --i >= 0; )
9970 out_char('\n'); /* cursor will remain on same line */
9971 }
9972 else
9973 {
9974 for (i = line_count; --i >= 0; )
9975 {
9976 if (type == USE_T_DL)
9977 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009978 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009979 out_str(T_DL); /* delete a line */
9980 }
9981 else /* type == USE_T_CE */
9982 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009983 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009984 out_str(T_CE); /* erase a line */
9985 }
9986 screen_start(); /* don't know where cursor is now */
9987 }
9988 }
9989
9990 /*
9991 * If the 'db' flag is set, we need to clear the lines that have been
9992 * scrolled up at the bottom of the region.
9993 */
9994 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
9995 {
9996 for (i = line_count; i > 0; --i)
9997 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009998 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009999 out_str(T_CE); /* erase a line */
10000 screen_start(); /* don't know where cursor is now */
10001 }
10002 }
10003
10004#ifdef FEAT_GUI
10005 gui_can_update_cursor();
10006 if (gui.in_use)
10007 out_flush(); /* always flush after a scroll */
10008#endif
10009
10010 return OK;
10011}
10012
10013/*
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010014 * Return TRUE when postponing displaying the mode message: when not redrawing
10015 * or inside a mapping.
10016 */
10017 int
10018skip_showmode()
10019{
10020 // Call char_avail() only when we are going to show something, because it
10021 // takes a bit of time. redrawing() may also call char_avail_avail().
10022 if (global_busy
10023 || msg_silent != 0
10024 || !redrawing()
10025 || (char_avail() && !KeyTyped))
10026 {
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010027 redraw_mode = TRUE; // show mode later
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010028 return TRUE;
10029 }
10030 return FALSE;
10031}
10032
10033/*
Bram Moolenaar81226e02018-02-20 21:44:45 +010010034 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010035 *
10036 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10037 * If clear_cmdline is FALSE there may be a message there that needs to be
10038 * cleared only if a mode is shown.
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010039 * If redraw_mode is TRUE show or clear the mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010040 * Return the length of the message (0 if no message).
10041 */
10042 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010043showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010044{
10045 int need_clear;
10046 int length = 0;
10047 int do_mode;
10048 int attr;
10049 int nwr_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010050 int sub_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010051
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010052 do_mode = ((p_smd && msg_silent == 0)
10053 && ((State & INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +020010054 || restart_edit != NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010055 || VIsual_active));
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010056 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057 {
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010058 if (skip_showmode())
10059 return 0; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +000010060
10061 nwr_save = need_wait_return;
10062
10063 /* wait a bit before overwriting an important message */
10064 check_for_delay(FALSE);
10065
10066 /* if the cmdline is more than one line high, erase top lines */
10067 need_clear = clear_cmdline;
10068 if (clear_cmdline && cmdline_row < Rows - 1)
10069 msg_clr_cmdline(); /* will reset clear_cmdline */
10070
10071 /* Position on the last line in the window, column 0 */
10072 msg_pos_mode();
10073 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010074 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010075 if (do_mode)
10076 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010077 msg_puts_attr("--", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010078#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010079 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010080# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010081 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010082# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010083 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010084# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010085 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010086# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar32526b32019-01-19 17:43:09 +010010087 msg_puts_attr(" IM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010088# else
Bram Moolenaar32526b32019-01-19 17:43:09 +010010089 msg_puts_attr(" XIM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010090# endif
10091#endif
10092#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10093 if (gui.in_use)
10094 {
10095 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010096 {
10097 /* HANGUL */
10098 if (enc_utf8)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010099 msg_puts_attr(" \355\225\234\352\270\200", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010100 else
Bram Moolenaar32526b32019-01-19 17:43:09 +010010101 msg_puts_attr(" \307\321\261\333", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010102 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010103 }
10104#endif
Bram Moolenaarea389e92014-05-28 21:40:52 +020010105 /* CTRL-X in Insert mode */
10106 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010107 {
10108 /* These messages can get long, avoid a wrap in a narrow
10109 * window. Prefer showing edit_submode_extra. */
10110 length = (Rows - msg_row) * Columns - 3;
10111 if (edit_submode_extra != NULL)
10112 length -= vim_strsize(edit_submode_extra);
10113 if (length > 0)
10114 {
10115 if (edit_submode_pre != NULL)
10116 length -= vim_strsize(edit_submode_pre);
10117 if (length - vim_strsize(edit_submode) > 0)
10118 {
10119 if (edit_submode_pre != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010120 msg_puts_attr((char *)edit_submode_pre, attr);
10121 msg_puts_attr((char *)edit_submode, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010122 }
10123 if (edit_submode_extra != NULL)
10124 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010125 msg_puts_attr(" ", attr); /* add a space in between */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010126 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010127 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010128 else
10129 sub_attr = attr;
Bram Moolenaar32526b32019-01-19 17:43:09 +010010130 msg_puts_attr((char *)edit_submode_extra, sub_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010131 }
10132 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010133 }
10134 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010135 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010136 if (State & VREPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010137 msg_puts_attr(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010138 else if (State & REPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010139 msg_puts_attr(_(" REPLACE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010140 else if (State & INSERT)
10141 {
10142#ifdef FEAT_RIGHTLEFT
10143 if (p_ri)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010144 msg_puts_attr(_(" REVERSE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010145#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +010010146 msg_puts_attr(_(" INSERT"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010147 }
Bram Moolenaar942b4542018-06-17 16:23:34 +020010148 else if (restart_edit == 'I' || restart_edit == 'A')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010149 msg_puts_attr(_(" (insert)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010150 else if (restart_edit == 'R')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010151 msg_puts_attr(_(" (replace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010152 else if (restart_edit == 'V')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010153 msg_puts_attr(_(" (vreplace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010154#ifdef FEAT_RIGHTLEFT
10155 if (p_hkmap)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010156 msg_puts_attr(_(" Hebrew"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010157#endif
10158#ifdef FEAT_KEYMAP
10159 if (State & LANGMAP)
10160 {
10161# ifdef FEAT_ARABIC
10162 if (curwin->w_p_arab)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010163 msg_puts_attr(_(" Arabic"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010164 else
10165# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010166 if (get_keymap_str(curwin, (char_u *)" (%s)",
10167 NameBuff, MAXPATHL))
Bram Moolenaar32526b32019-01-19 17:43:09 +010010168 msg_puts_attr((char *)NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010169 }
10170#endif
10171 if ((State & INSERT) && p_paste)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010172 msg_puts_attr(_(" (paste)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010173
Bram Moolenaar071d4272004-06-13 20:20:40 +000010174 if (VIsual_active)
10175 {
10176 char *p;
10177
10178 /* Don't concatenate separate words to avoid translation
10179 * problems. */
10180 switch ((VIsual_select ? 4 : 0)
10181 + (VIsual_mode == Ctrl_V) * 2
10182 + (VIsual_mode == 'V'))
10183 {
10184 case 0: p = N_(" VISUAL"); break;
10185 case 1: p = N_(" VISUAL LINE"); break;
10186 case 2: p = N_(" VISUAL BLOCK"); break;
10187 case 4: p = N_(" SELECT"); break;
10188 case 5: p = N_(" SELECT LINE"); break;
10189 default: p = N_(" SELECT BLOCK"); break;
10190 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010191 msg_puts_attr(_(p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010192 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010193 msg_puts_attr(" --", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010194 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010195
Bram Moolenaar071d4272004-06-13 20:20:40 +000010196 need_clear = TRUE;
10197 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010198 if (reg_recording != 0
Bram Moolenaare2c453d2019-08-21 14:37:09 +020010199 && edit_submode == NULL) // otherwise it gets too long
Bram Moolenaar071d4272004-06-13 20:20:40 +000010200 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010201 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010202 need_clear = TRUE;
10203 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010204
10205 mode_displayed = TRUE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010206 if (need_clear || clear_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010207 msg_clr_eos();
10208 msg_didout = FALSE; /* overwrite this message */
10209 length = msg_col;
10210 msg_col = 0;
10211 need_wait_return = nwr_save; /* never ask for hit-return for this */
10212 }
10213 else if (clear_cmdline && msg_silent == 0)
10214 /* Clear the whole command line. Will reset "clear_cmdline". */
10215 msg_clr_cmdline();
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010216 else if (redraw_mode)
10217 {
10218 msg_pos_mode();
10219 msg_clr_eos();
10220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010221
10222#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010223 /* In Visual mode the size of the selected area must be redrawn. */
10224 if (VIsual_active)
10225 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010226
10227 /* If the last window has no status line, the ruler is after the mode
10228 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010229 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +020010230 win_redr_ruler(lastwin, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010231#endif
10232 redraw_cmdline = FALSE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010233 redraw_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010234 clear_cmdline = FALSE;
10235
10236 return length;
10237}
10238
10239/*
10240 * Position for a mode message.
10241 */
10242 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010243msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010244{
10245 msg_col = 0;
10246 msg_row = Rows - 1;
10247}
10248
10249/*
10250 * Delete mode message. Used when ESC is typed which is expected to end
10251 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010252 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010253 */
10254 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010255unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010256{
10257 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010258 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010259 */
10260 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10261 redraw_cmdline = TRUE; /* delete mode later */
10262 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010263 clearmode();
10264}
10265
10266/*
10267 * Clear the mode message.
10268 */
10269 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010270clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010271{
Bram Moolenaar2abad542018-05-19 14:43:45 +020010272 int save_msg_row = msg_row;
10273 int save_msg_col = msg_col;
10274
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010275 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010276 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010277 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010278 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +020010279
10280 msg_col = save_msg_col;
10281 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010282}
10283
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010284 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010285recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010286{
Bram Moolenaar32526b32019-01-19 17:43:09 +010010287 msg_puts_attr(_("recording"), attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010288 if (!shortmess(SHM_RECORDING))
10289 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010290 char s[4];
10291
10292 sprintf(s, " @%c", reg_recording);
10293 msg_puts_attr(s, attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010294 }
10295}
10296
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010297/*
10298 * Draw the tab pages line at the top of the Vim window.
10299 */
Bram Moolenaare12bab32019-01-08 22:02:56 +010010300 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010301draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010302{
10303 int tabcount = 0;
10304 tabpage_T *tp;
10305 int tabwidth;
10306 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010307 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010308 int attr;
10309 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010310 win_T *cwp;
10311 int wincount;
10312 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010313 int c;
10314 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010315 int attr_sel = HL_ATTR(HLF_TPS);
10316 int attr_nosel = HL_ATTR(HLF_TP);
10317 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010318 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010319 int room;
10320 int use_sep_chars = (t_colors < 8
10321#ifdef FEAT_GUI
10322 && !gui.in_use
10323#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010324#ifdef FEAT_TERMGUICOLORS
10325 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010326#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010327 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010328
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010329 if (ScreenLines == NULL)
10330 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010331 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010332
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010333#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010334 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010335 if (gui_use_tabline())
10336 {
10337 gui_update_tabline();
10338 return;
10339 }
10340#endif
10341
10342 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010343 return;
10344
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010345#if defined(FEAT_STL_OPT)
Bram Moolenaarca57ab52019-04-13 14:53:16 +020010346 clear_TabPageIdxs();
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010347
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010348 /* Use the 'tabline' option if it's set. */
10349 if (*p_tal != NUL)
10350 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010351 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010352
10353 /* Check for an error. If there is one we would loop in redrawing the
10354 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010355 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010356 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010357 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010358 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010359 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010360 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010361 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010362 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010363#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010364 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010365 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010366 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010367
Bram Moolenaar238a5642006-02-21 22:12:05 +000010368 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10369 if (tabwidth < 6)
10370 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010371
Bram Moolenaar238a5642006-02-21 22:12:05 +000010372 attr = attr_nosel;
10373 tabcount = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010374 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10375 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010376 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010377 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010378
Bram Moolenaar238a5642006-02-21 22:12:05 +000010379 if (tp->tp_topframe == topframe)
10380 attr = attr_sel;
10381 if (use_sep_chars && col > 0)
10382 screen_putchar('|', 0, col++, attr);
10383
10384 if (tp->tp_topframe != topframe)
10385 attr = attr_nosel;
10386
10387 screen_putchar(' ', 0, col++, attr);
10388
10389 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010390 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010391 cwp = curwin;
10392 wp = firstwin;
10393 }
10394 else
10395 {
10396 cwp = tp->tp_curwin;
10397 wp = tp->tp_firstwin;
10398 }
10399
10400 modified = FALSE;
10401 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10402 if (bufIsChanged(wp->w_buffer))
10403 modified = TRUE;
10404 if (modified || wincount > 1)
10405 {
10406 if (wincount > 1)
10407 {
10408 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010409 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010410 if (col + len >= Columns - 3)
10411 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010412 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010413#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010414 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010415#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010416 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010417#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010418 );
10419 col += len;
10420 }
10421 if (modified)
10422 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10423 screen_putchar(' ', 0, col++, attr);
10424 }
10425
10426 room = scol - col + tabwidth - 1;
10427 if (room > 0)
10428 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010429 /* Get buffer name in NameBuff[] */
10430 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010431 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010432 len = vim_strsize(NameBuff);
10433 p = NameBuff;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010434 if (has_mbyte)
10435 while (len > room)
10436 {
10437 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010438 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010439 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010010440 else if (len > room)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010441 {
10442 p += len - room;
10443 len = room;
10444 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010445 if (len > Columns - col - 1)
10446 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010447
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010448 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010449 col += len;
10450 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010451 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010452
10453 /* Store the tab page number in TabPageIdxs[], so that
10454 * jump_to_mouse() knows where each one is. */
10455 ++tabcount;
10456 while (scol < col)
10457 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010458 }
10459
Bram Moolenaar238a5642006-02-21 22:12:05 +000010460 if (use_sep_chars)
10461 c = '_';
10462 else
10463 c = ' ';
10464 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010465
10466 /* Put an "X" for closing the current tab if there are several. */
10467 if (first_tabpage->tp_next != NULL)
10468 {
10469 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10470 TabPageIdxs[Columns - 1] = -999;
10471 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010472 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010473
10474 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10475 * set. */
10476 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010477}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010478
10479/*
10480 * Get buffer name for "buf" into NameBuff[].
10481 * Takes care of special buffer names and translates special characters.
10482 */
10483 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010484get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010485{
10486 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010487 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010488 else
10489 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10490 trans_characters(NameBuff, MAXPATHL);
10491}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010492
Bram Moolenaar071d4272004-06-13 20:20:40 +000010493/*
10494 * Get the character to use in a status line. Get its attributes in "*attr".
10495 */
10496 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010497fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010498{
10499 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010500
10501#ifdef FEAT_TERMINAL
10502 if (bt_terminal(wp->w_buffer))
10503 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010504 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010505 {
10506 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010507 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010508 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010509 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010510 {
10511 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010512 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010513 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010514 }
10515 else
10516#endif
10517 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010518 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010519 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010520 fill = fill_stl;
10521 }
10522 else
10523 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010524 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010525 fill = fill_stlnc;
10526 }
10527 /* Use fill when there is highlighting, and highlighting of current
10528 * window differs, or the fillchars differ, or this is not the
10529 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010530 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010531 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010532 || (fill_stl != fill_stlnc)))
10533 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010534 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010535 return '^';
10536 return '=';
10537}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010538
Bram Moolenaar071d4272004-06-13 20:20:40 +000010539/*
10540 * Get the character to use in a separator between vertically split windows.
10541 * Get its attributes in "*attr".
10542 */
10543 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010544fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010545{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010546 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010547 if (*attr == 0 && fill_vert == ' ')
10548 return '|';
10549 else
10550 return fill_vert;
10551}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010552
10553/*
10554 * Return TRUE if redrawing should currently be done.
10555 */
10556 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010557redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010558{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010559#ifdef FEAT_EVAL
10560 if (disable_redraw_for_testing)
10561 return 0;
10562 else
10563#endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +020010564 return ((!RedrawingDisabled
10565#ifdef FEAT_EVAL
10566 || ignore_redraw_flag_for_testing
10567#endif
10568 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010569}
10570
10571/*
10572 * Return TRUE if printing messages should currently be done.
10573 */
10574 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010575messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010576{
10577 return (!(p_lz && char_avail() && !KeyTyped));
10578}
10579
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010580#ifdef FEAT_MENU
10581/*
10582 * Draw the window toolbar.
10583 */
10584 static void
10585redraw_win_toolbar(win_T *wp)
10586{
10587 vimmenu_T *menu;
10588 int item_idx = 0;
10589 int item_count = 0;
10590 int col = 0;
10591 int next_col;
10592 int off = (int)(current_ScreenLine - ScreenLines);
10593 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
10594 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
10595
10596 vim_free(wp->w_winbar_items);
10597 for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
10598 ++item_count;
Bram Moolenaarc799fe22019-05-28 23:08:19 +020010599 wp->w_winbar_items = ALLOC_CLEAR_MULT(winbar_item_T, item_count + 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010600
10601 /* TODO: use fewer spaces if there is not enough room */
10602 for (menu = wp->w_winbar->children;
Bram Moolenaar02631462017-09-22 15:20:32 +020010603 menu != NULL && col < wp->w_width; menu = menu->next)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010604 {
10605 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010606 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010607 break;
10608 if (col > 1)
10609 {
10610 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010611 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010612 break;
10613 }
10614
10615 wp->w_winbar_items[item_idx].wb_startcol = col;
10616 space_to_screenline(off + col, button_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010617 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010618 break;
10619
10620 next_col = text_to_screenline(wp, menu->name, col);
10621 while (col < next_col)
10622 {
10623 ScreenAttrs[off + col] = button_attr;
10624 ++col;
10625 }
10626 wp->w_winbar_items[item_idx].wb_endcol = col;
10627 wp->w_winbar_items[item_idx].wb_menu = menu;
10628 ++item_idx;
10629
Bram Moolenaar02631462017-09-22 15:20:32 +020010630 if (col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010631 break;
10632 space_to_screenline(off + col, button_attr);
10633 ++col;
10634 }
Bram Moolenaar02631462017-09-22 15:20:32 +020010635 while (col < wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010636 {
10637 space_to_screenline(off + col, fill_attr);
10638 ++col;
10639 }
10640 wp->w_winbar_items[item_idx].wb_menu = NULL; /* end marker */
10641
Bram Moolenaar02631462017-09-22 15:20:32 +020010642 screen_line(wp->w_winrow, wp->w_wincol, (int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +020010643 (int)wp->w_width, 0);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010644}
10645#endif
Bram Moolenaar491ac282018-06-17 14:47:55 +020010646
Bram Moolenaar071d4272004-06-13 20:20:40 +000010647/*
10648 * Show current status info in ruler and various other places
10649 * If always is FALSE, only show ruler if position has changed.
10650 */
10651 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010652showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010653{
10654 if (!always && !redrawing())
10655 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010656 if (pum_visible())
10657 {
10658 /* Don't redraw right now, do it later. */
10659 curwin->w_redr_status = TRUE;
10660 return;
10661 }
Bram Moolenaar4033c552017-09-16 20:54:51 +020010662#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010663 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar362f3562009-11-03 16:20:34 +000010664 redraw_custom_statusline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010665 else
10666#endif
10667#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +020010668 win_redr_ruler(curwin, always, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010669#endif
10670
10671#ifdef FEAT_TITLE
10672 if (need_maketitle
10673# ifdef FEAT_STL_OPT
10674 || (p_icon && (stl_syntax & STL_IN_ICON))
10675 || (p_title && (stl_syntax & STL_IN_TITLE))
10676# endif
10677 )
10678 maketitle();
10679#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010680 /* Redraw the tab pages line if needed. */
10681 if (redraw_tabline)
10682 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010683}
10684
10685#ifdef FEAT_CMDL_INFO
10686 static void
Bram Moolenaar491ac282018-06-17 14:47:55 +020010687win_redr_ruler(win_T *wp, int always, int ignore_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010688{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010689#define RULER_BUF_LEN 70
10690 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010691 int row;
10692 int fillchar;
10693 int attr;
10694 int empty_line = FALSE;
10695 colnr_T virtcol;
10696 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010697 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010698 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010699 int this_ru_col;
10700 int off = 0;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +010010701 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010702
10703 /* If 'ruler' off or redrawing disabled, don't do anything */
10704 if (!p_ru)
10705 return;
10706
10707 /*
10708 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10709 * after deleting lines, before cursor.lnum is corrected.
10710 */
10711 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10712 return;
10713
Bram Moolenaare2c453d2019-08-21 14:37:09 +020010714 // Don't draw the ruler while doing insert-completion, it might overwrite
10715 // the (long) mode message.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010716 if (wp == lastwin && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010717 if (edit_submode != NULL)
10718 return;
Bram Moolenaar491ac282018-06-17 14:47:55 +020010719 // Don't draw the ruler when the popup menu is visible, it may overlap.
10720 // Except when the popup menu will be redrawn anyway.
10721 if (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010722 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010723
10724#ifdef FEAT_STL_OPT
10725 if (*p_ruf)
10726 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010727 int save_called_emsg = called_emsg;
10728
10729 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010730 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010731 if (called_emsg)
10732 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010733 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010734 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010735 return;
10736 }
10737#endif
10738
10739 /*
10740 * Check if not in Insert mode and the line is empty (will show "0-1").
10741 */
10742 if (!(State & INSERT)
10743 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10744 empty_line = TRUE;
10745
10746 /*
10747 * Only draw the ruler when something changed.
10748 */
10749 validate_virtcol_win(wp);
10750 if ( redraw_cmdline
10751 || always
10752 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10753 || wp->w_cursor.col != wp->w_ru_cursor.col
10754 || wp->w_virtcol != wp->w_ru_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +000010755 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
Bram Moolenaar071d4272004-06-13 20:20:40 +000010756 || wp->w_topline != wp->w_ru_topline
10757 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10758#ifdef FEAT_DIFF
10759 || wp->w_topfill != wp->w_ru_topfill
10760#endif
10761 || empty_line != wp->w_ru_empty)
10762 {
10763 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010764 if (wp->w_status_height)
10765 {
10766 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010767 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar53f81742017-09-22 14:35:51 +020010768 off = wp->w_wincol;
Bram Moolenaar02631462017-09-22 15:20:32 +020010769 width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010770 }
10771 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010772 {
10773 row = Rows - 1;
10774 fillchar = ' ';
10775 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010776 width = Columns;
10777 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010778 }
10779
10780 /* In list mode virtcol needs to be recomputed */
10781 virtcol = wp->w_virtcol;
10782 if (wp->w_p_list && lcs_tab1 == NUL)
10783 {
10784 wp->w_p_list = FALSE;
10785 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10786 wp->w_p_list = TRUE;
10787 }
10788
10789 /*
10790 * Some sprintfs return the length, some return a pointer.
10791 * To avoid portability problems we use strlen() here.
10792 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010793 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010794 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10795 ? 0L
10796 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010797 len = STRLEN(buffer);
10798 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010799 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10800 (int)virtcol + 1);
10801
10802 /*
10803 * Add a "50%" if there is room for it.
10804 * On the last line, don't print in the last column (scrolls the
10805 * screen up on some terminals).
10806 */
10807 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010808 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010809 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010810 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010811 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010812 this_ru_col = ru_col - (Columns - width);
10813 if (this_ru_col < 0)
10814 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010815 /* Never use more than half the window/screen width, leave the other
10816 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010817 if (this_ru_col < (width + 1) / 2)
10818 this_ru_col = (width + 1) / 2;
10819 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010820 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010010821 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010822 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010823 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010824 if (has_mbyte)
10825 i += (*mb_char2bytes)(fillchar, buffer + i);
10826 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010827 buffer[i++] = fillchar;
10828 ++o;
10829 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010830 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010831 }
10832 /* Truncate at window boundary. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010833 if (has_mbyte)
10834 {
10835 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010836 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010837 {
10838 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010839 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010840 {
10841 buffer[i] = NUL;
10842 break;
10843 }
10844 }
10845 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010010846 else if (this_ru_col + (int)STRLEN(buffer) > width)
Bram Moolenaar4033c552017-09-16 20:54:51 +020010847 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010848
Bram Moolenaar4033c552017-09-16 20:54:51 +020010849 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010850 i = redraw_cmdline;
10851 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020010852 this_ru_col + off + (int)STRLEN(buffer),
10853 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010854 fillchar, fillchar, attr);
10855 /* don't redraw the cmdline because of showing the ruler */
10856 redraw_cmdline = i;
10857 wp->w_ru_cursor = wp->w_cursor;
10858 wp->w_ru_virtcol = wp->w_virtcol;
10859 wp->w_ru_empty = empty_line;
10860 wp->w_ru_topline = wp->w_topline;
10861 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10862#ifdef FEAT_DIFF
10863 wp->w_ru_topfill = wp->w_topfill;
10864#endif
10865 }
10866}
10867#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010868
Bram Moolenaare677df82019-09-02 22:31:11 +020010869/*
10870 * Compute columns for ruler and shown command. 'sc_col' is also used to
10871 * decide what the maximum length of a message on the status line can be.
10872 * If there is a status line for the last window, 'sc_col' is independent
10873 * of 'ru_col'.
10874 */
10875
10876#define COL_RULER 17 // columns needed by standard ruler
10877
10878 void
10879comp_col(void)
10880{
10881#if defined(FEAT_CMDL_INFO)
10882 int last_has_status = (p_ls == 2 || (p_ls == 1 && !ONE_WINDOW));
10883
10884 sc_col = 0;
10885 ru_col = 0;
10886 if (p_ru)
10887 {
10888# ifdef FEAT_STL_OPT
10889 ru_col = (ru_wid ? ru_wid : COL_RULER) + 1;
10890# else
10891 ru_col = COL_RULER + 1;
10892# endif
10893 // no last status line, adjust sc_col
10894 if (!last_has_status)
10895 sc_col = ru_col;
10896 }
10897 if (p_sc)
10898 {
10899 sc_col += SHOWCMD_COLS;
10900 if (!p_ru || last_has_status) // no need for separating space
10901 ++sc_col;
10902 }
10903 sc_col = Columns - sc_col;
10904 ru_col = Columns - ru_col;
10905 if (sc_col <= 0) // screen too narrow, will become a mess
10906 sc_col = 1;
10907 if (ru_col <= 0)
10908 ru_col = 1;
10909#else
10910 sc_col = Columns;
10911 ru_col = Columns;
10912#endif
10913#ifdef FEAT_EVAL
10914 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
10915#endif
10916}
10917
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010918#if defined(FEAT_LINEBREAK) || defined(PROTO)
10919/*
Bram Moolenaar64486672010-05-16 15:46:46 +020010920 * Return the width of the 'number' and 'relativenumber' column.
10921 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010922 * Otherwise it depends on 'numberwidth' and the line count.
10923 */
10924 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010925number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010926{
10927 int n;
10928 linenr_T lnum;
10929
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020010930 if (wp->w_p_rnu && !wp->w_p_nu)
10931 /* cursor line shows "0" */
10932 lnum = wp->w_height;
10933 else
10934 /* cursor line shows absolute line number */
10935 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020010936
Bram Moolenaar6b314672015-03-20 15:42:10 +010010937 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010938 return wp->w_nrwidth_width;
10939 wp->w_nrwidth_line_count = lnum;
10940
10941 n = 0;
10942 do
10943 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010944 lnum /= 10;
10945 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010946 } while (lnum > 0);
10947
10948 /* 'numberwidth' gives the minimal width plus one */
10949 if (n < wp->w_p_nuw - 1)
10950 n = wp->w_p_nuw - 1;
10951
Bram Moolenaare4b407f2019-07-04 11:59:28 +020010952# ifdef FEAT_SIGNS
10953 // If 'signcolumn' is set to 'number' and there is a sign to display, then
10954 // the minimal width for the number column is 2.
10955 if (n < 2 && (wp->w_buffer->b_signlist != NULL)
10956 && (*wp->w_p_scl == 'n' && *(wp->w_p_scl + 1) == 'u'))
10957 n = 2;
10958# endif
10959
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010960 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010010961 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010962 return n;
10963}
10964#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010965
Bram Moolenaar113e1072019-01-20 15:30:40 +010010966#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010967/*
10968 * Return the current cursor column. This is the actual position on the
10969 * screen. First column is 0.
10970 */
10971 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010972screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010973{
10974 return screen_cur_col;
10975}
10976
10977/*
10978 * Return the current cursor row. This is the actual position on the screen.
10979 * First row is 0.
10980 */
10981 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010982screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010983{
10984 return screen_cur_row;
10985}
Bram Moolenaar113e1072019-01-20 15:30:40 +010010986#endif
Bram Moolenaare677df82019-09-02 22:31:11 +020010987
10988/*
10989 * Handle setting 'listchars' or 'fillchars'.
10990 * Returns error message, NULL if it's OK.
10991 */
10992 char *
10993set_chars_option(char_u **varp)
10994{
10995 int round, i, len, entries;
10996 char_u *p, *s;
10997 int c1 = 0, c2 = 0, c3 = 0;
10998 struct charstab
10999 {
11000 int *cp;
11001 char *name;
11002 };
11003 static struct charstab filltab[] =
11004 {
11005 {&fill_stl, "stl"},
11006 {&fill_stlnc, "stlnc"},
11007 {&fill_vert, "vert"},
11008 {&fill_fold, "fold"},
11009 {&fill_diff, "diff"},
11010 };
11011 static struct charstab lcstab[] =
11012 {
11013 {&lcs_eol, "eol"},
11014 {&lcs_ext, "extends"},
11015 {&lcs_nbsp, "nbsp"},
11016 {&lcs_prec, "precedes"},
11017 {&lcs_space, "space"},
11018 {&lcs_tab2, "tab"},
11019 {&lcs_trail, "trail"},
11020#ifdef FEAT_CONCEAL
11021 {&lcs_conceal, "conceal"},
11022#else
11023 {NULL, "conceal"},
11024#endif
11025 };
11026 struct charstab *tab;
11027
11028 if (varp == &p_lcs)
11029 {
11030 tab = lcstab;
11031 entries = sizeof(lcstab) / sizeof(struct charstab);
11032 }
11033 else
11034 {
11035 tab = filltab;
11036 entries = sizeof(filltab) / sizeof(struct charstab);
11037 }
11038
11039 // first round: check for valid value, second round: assign values
11040 for (round = 0; round <= 1; ++round)
11041 {
11042 if (round > 0)
11043 {
11044 // After checking that the value is valid: set defaults: space for
11045 // 'fillchars', NUL for 'listchars'
11046 for (i = 0; i < entries; ++i)
11047 if (tab[i].cp != NULL)
11048 *(tab[i].cp) = (varp == &p_lcs ? NUL : ' ');
11049
11050 if (varp == &p_lcs)
11051 {
11052 lcs_tab1 = NUL;
11053 lcs_tab3 = NUL;
11054 }
11055 else
11056 fill_diff = '-';
11057 }
11058 p = *varp;
11059 while (*p)
11060 {
11061 for (i = 0; i < entries; ++i)
11062 {
11063 len = (int)STRLEN(tab[i].name);
11064 if (STRNCMP(p, tab[i].name, len) == 0
11065 && p[len] == ':'
11066 && p[len + 1] != NUL)
11067 {
11068 c2 = c3 = 0;
11069 s = p + len + 1;
11070 c1 = mb_ptr2char_adv(&s);
11071 if (mb_char2cells(c1) > 1)
11072 continue;
11073 if (tab[i].cp == &lcs_tab2)
11074 {
11075 if (*s == NUL)
11076 continue;
11077 c2 = mb_ptr2char_adv(&s);
11078 if (mb_char2cells(c2) > 1)
11079 continue;
11080 if (!(*s == ',' || *s == NUL))
11081 {
11082 c3 = mb_ptr2char_adv(&s);
11083 if (mb_char2cells(c3) > 1)
11084 continue;
11085 }
11086 }
11087
11088 if (*s == ',' || *s == NUL)
11089 {
11090 if (round)
11091 {
11092 if (tab[i].cp == &lcs_tab2)
11093 {
11094 lcs_tab1 = c1;
11095 lcs_tab2 = c2;
11096 lcs_tab3 = c3;
11097 }
11098 else if (tab[i].cp != NULL)
11099 *(tab[i].cp) = c1;
11100
11101 }
11102 p = s;
11103 break;
11104 }
11105 }
11106 }
11107
11108 if (i == entries)
11109 return e_invarg;
11110 if (*p == ',')
11111 ++p;
11112 }
11113 }
11114
11115 return NULL; // no error
11116}
Bram Moolenaar017ba072019-09-14 21:01:23 +020011117
11118#ifdef FEAT_SYN_HL
11119/*
11120 * Used when 'cursorlineopt' contains "screenline": compute the margins between
11121 * which the highlighting is used.
11122 */
11123 static void
11124margin_columns_win(win_T *wp, int *left_col, int *right_col)
11125{
11126 // cache previous calculations depending on w_virtcol
11127 static int saved_w_virtcol;
11128 static win_T *prev_wp;
11129 static int prev_left_col;
11130 static int prev_right_col;
11131 static int prev_col_off;
11132
11133 int cur_col_off = win_col_off(wp);
11134 int width1;
11135 int width2;
11136
11137 if (saved_w_virtcol == wp->w_virtcol
11138 && prev_wp == wp && prev_col_off == cur_col_off)
11139 {
11140 *right_col = prev_right_col;
11141 *left_col = prev_left_col;
11142 return;
11143 }
11144
11145 width1 = wp->w_width - cur_col_off;
11146 width2 = width1 + win_col_off2(wp);
11147
11148 *left_col = 0;
11149 *right_col = width1;
11150
11151 if (wp->w_virtcol >= (colnr_T)width1)
11152 *right_col = width1 + ((wp->w_virtcol - width1) / width2 + 1) * width2;
11153 if (wp->w_virtcol >= (colnr_T)width1 && width2 > 0)
11154 *left_col = (wp->w_virtcol - width1) / width2 * width2 + width1;
11155
11156 // cache values
11157 prev_left_col = *left_col;
11158 prev_right_col = *right_col;
11159 prev_wp = wp;
11160 saved_w_virtcol = wp->w_virtcol;
11161 prev_col_off = cur_col_off;
11162}
11163#endif