blob: 22b26b472ee67425fcdb1cd1ee030b1c07c13f0c [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 Moolenaar8ae54372019-09-15 18:11:16 +02004156 // combine 'showbreak' with 'wincolor'
4157 if (win_attr != 0)
4158 char_attr = hl_combine_attr(win_attr, char_attr);
Bram Moolenaar017ba072019-09-14 21:01:23 +02004159# ifdef FEAT_SYN_HL
4160 // combine 'showbreak' with 'cursorline'
4161 if (cul_attr != 0)
4162 char_attr = hl_combine_attr(char_attr, cul_attr);
4163# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 }
4165# endif
4166 }
4167#endif
4168
4169 if (draw_state == WL_LINE - 1 && n_extra == 0)
4170 {
4171 draw_state = WL_LINE;
4172 if (saved_n_extra)
4173 {
4174 /* Continue item from end of wrapped line. */
4175 n_extra = saved_n_extra;
4176 c_extra = saved_c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004177 c_final = saved_c_final;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 p_extra = saved_p_extra;
4179 char_attr = saved_char_attr;
4180 }
4181 else
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004182 char_attr = win_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 }
4184 }
Bram Moolenaar017ba072019-09-14 21:01:23 +02004185#ifdef FEAT_SYN_HL
4186 if (cul_screenline)
4187 {
4188 if (draw_state == WL_LINE
4189 && vcol >= left_curline_col
4190 && vcol < right_curline_col)
4191 {
4192 cul_attr = HL_ATTR(HLF_CUL);
4193 line_attr = cul_attr;
4194 }
4195 else
4196 {
4197 cul_attr = 0;
4198 line_attr = line_attr_save;
4199 }
4200 }
4201#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004203 // When still displaying '$' of change command, stop at cursor.
4204 // When only displaying the (relative) line number and that's done,
4205 // stop here.
4206 if ((dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004207 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208#ifdef FEAT_DIFF
4209 && filler_todo <= 0
4210#endif
4211 )
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004212 || (number_only && draw_state > WL_NR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 {
Bram Moolenaar02631462017-09-22 15:20:32 +02004214 screen_line(screen_row, wp->w_wincol, col, -(int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004215 screen_line_flags);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004216 /* Pretend we have finished updating the window. Except when
4217 * 'cursorcolumn' is set. */
4218#ifdef FEAT_SYN_HL
4219 if (wp->w_p_cuc)
4220 row = wp->w_cline_row + wp->w_cline_height;
4221 else
4222#endif
4223 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 break;
4225 }
4226
Bram Moolenaar637532b2019-01-03 21:44:40 +01004227 if (draw_state == WL_LINE && (area_highlighting
4228#ifdef FEAT_SPELL
4229 || has_spell
4230#endif
4231 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 {
4233 /* handle Visual or match highlighting in this line */
4234 if (vcol == fromcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4236 && (*mb_ptr2cells)(ptr) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004238 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 && vcol < tocol))
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004240 area_attr = vi_attr; /* start highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 else if (area_attr != 0
4242 && (vcol == tocol
4243 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245
4246#ifdef FEAT_SEARCH_EXTRA
4247 if (!n_extra)
4248 {
4249 /*
Bram Moolenaarbbca7732019-07-24 18:13:16 +02004250 * Check for start/end of 'hlsearch' and other matches.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 * After end, check for start/end of next match.
4252 * When another match, have to check for start again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004254 v = (long)(ptr - line);
Bram Moolenaarbbca7732019-07-24 18:13:16 +02004255 search_attr = update_search_hl(wp, lnum, (colnr_T)v, &line,
4256 &search_hl, &has_match_conc, &match_conc,
4257 did_line_attr, lcs_eol_one);
4258 ptr = line + v; // "line" may have been changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 }
4260#endif
4261
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004263 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004265 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4266 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004268 if (diff_hlf == HLF_TXD && ptr - line > change_end
4269 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004271 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaar410e98a2019-09-09 22:05:49 +02004272 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaar017ba072019-09-14 21:01:23 +02004273 && wp->w_p_culopt_flags != CULOPT_NBR
4274 && (!cul_screenline || (vcol >= left_curline_col
4275 && vcol <= right_curline_col)))
4276 line_attr = hl_combine_attr(
4277 line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 }
4279#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004280
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004281#ifdef FEAT_TEXT_PROP
4282 if (text_props != NULL)
4283 {
4284 int pi;
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004285 int bcol = (int)(ptr - line);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004286
Bram Moolenaara956bf62019-06-19 17:34:24 +02004287 if (n_extra > 0)
4288 --bcol; // still working on the previous char, e.g. Tab
4289
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004290 // Check if any active property ends.
4291 for (pi = 0; pi < text_props_active; ++pi)
4292 {
4293 int tpi = text_prop_idxs[pi];
4294
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004295 if (bcol >= text_props[tpi].tp_col - 1
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004296 + text_props[tpi].tp_len)
4297 {
4298 if (pi + 1 < text_props_active)
4299 mch_memmove(text_prop_idxs + pi,
4300 text_prop_idxs + pi + 1,
4301 sizeof(int)
4302 * (text_props_active - (pi + 1)));
4303 --text_props_active;
4304 --pi;
4305 }
4306 }
4307
4308 // Add any text property that starts in this column.
4309 while (text_prop_next < text_prop_count
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004310 && bcol >= text_props[text_prop_next].tp_col - 1)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004311 text_prop_idxs[text_props_active++] = text_prop_next++;
4312
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004313 text_prop_attr = 0;
Bram Moolenaarde24a872019-05-05 15:48:00 +02004314 text_prop_combine = FALSE;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004315 if (text_props_active > 0)
4316 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004317 // Sort the properties on priority and/or starting last.
4318 // Then combine the attributes, highest priority last.
4319 current_text_props = text_props;
4320 current_buf = wp->w_buffer;
4321 qsort((void *)text_prop_idxs, (size_t)text_props_active,
4322 sizeof(int), text_prop_compare);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004323
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004324 for (pi = 0; pi < text_props_active; ++pi)
4325 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004326 int tpi = text_prop_idxs[pi];
Bram Moolenaarde24a872019-05-05 15:48:00 +02004327 proptype_T *pt = text_prop_type_by_id(
4328 wp->w_buffer, text_props[tpi].tp_type);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004329
Bram Moolenaard74af422019-06-28 21:38:00 +02004330 if (pt != NULL && pt->pt_hl_id > 0)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004331 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004332 int pt_attr = syn_id2attr(pt->pt_hl_id);
4333
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004334 text_prop_type = pt;
Bram Moolenaarde24a872019-05-05 15:48:00 +02004335 text_prop_attr =
4336 hl_combine_attr(text_prop_attr, pt_attr);
4337 text_prop_combine = pt->pt_flags & PT_FLAG_COMBINE;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004338 }
4339 }
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004340 }
4341 }
4342#endif
4343
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004344 /* Decide which of the highlight attributes to use. */
4345 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004346#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004347 if (area_attr != 0)
4348 char_attr = hl_combine_attr(line_attr, area_attr);
4349 else if (search_attr != 0)
4350 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004351# ifdef FEAT_TEXT_PROP
4352 else if (text_prop_type != NULL)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02004353 {
4354 char_attr = hl_combine_attr(
4355 line_attr != 0 ? line_attr : win_attr, text_prop_attr);
4356 }
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004357# endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004358 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004359 || vcol < fromcol || vcol_prev < fromcol_prev
4360 || vcol >= tocol))
Bram Moolenaar017ba072019-09-14 21:01:23 +02004361 {
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004362 // Use line_attr when not in the Visual or 'incsearch' area
4363 // (area_attr may be 0 when "noinvcur" is set).
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004364 char_attr = line_attr;
Bram Moolenaar017ba072019-09-14 21:01:23 +02004365 attr_pri = FALSE;
4366 }
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004367#else
4368 if (area_attr != 0)
4369 char_attr = area_attr;
4370 else if (search_attr != 0)
4371 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004372#endif
4373 else
4374 {
4375 attr_pri = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004376#ifdef FEAT_TEXT_PROP
4377 if (text_prop_type != NULL)
Bram Moolenaarde24a872019-05-05 15:48:00 +02004378 {
4379 if (text_prop_combine)
4380 char_attr = hl_combine_attr(
4381 syntax_attr, text_prop_attr);
4382 else
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02004383 char_attr = hl_combine_attr(
4384 win_attr, text_prop_attr);
Bram Moolenaarde24a872019-05-05 15:48:00 +02004385 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004386 else
4387#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004388#ifdef FEAT_SYN_HL
4389 if (has_syntax)
4390 char_attr = syntax_attr;
4391 else
4392#endif
4393 char_attr = 0;
4394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004396 if (char_attr == 0)
4397 char_attr = win_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398
4399 /*
4400 * Get the next character to put on the screen.
4401 */
4402 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004403 * The "p_extra" points to the extra stuff that is inserted to
4404 * represent special characters (non-printable stuff) and other
4405 * things. When all characters are the same, c_extra is used.
Bram Moolenaar83a52172019-01-16 22:41:54 +01004406 * If c_final is set, it will compulsorily be used at the end.
Bram Moolenaara064ac82007-08-05 18:10:54 +00004407 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4408 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4410 */
4411 if (n_extra > 0)
4412 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004413 if (c_extra != NUL || (n_extra == 1 && c_final != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004415 c = (n_extra == 1 && c_final != NUL) ? c_final : c_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004417 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 {
4419 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004420 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004421 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 }
4423 else
4424 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 }
4426 else
4427 {
4428 c = *p_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 if (has_mbyte)
4430 {
4431 mb_c = c;
4432 if (enc_utf8)
4433 {
4434 /* If the UTF-8 character is more than one byte:
4435 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004436 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 mb_utf8 = FALSE;
4438 if (mb_l > n_extra)
4439 mb_l = 1;
4440 else if (mb_l > 1)
4441 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004442 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004444 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 }
4446 }
4447 else
4448 {
4449 /* if this is a DBCS character, put it in "mb_c" */
4450 mb_l = MB_BYTE2LEN(c);
4451 if (mb_l >= n_extra)
4452 mb_l = 1;
4453 else if (mb_l > 1)
4454 mb_c = (c << 8) + p_extra[1];
4455 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004456 if (mb_l == 0) /* at the NUL at end-of-line */
4457 mb_l = 1;
4458
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 /* If a double-width char doesn't fit display a '>' in the
4460 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004461 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462# ifdef FEAT_RIGHTLEFT
4463 wp->w_p_rl ? (col <= 0) :
4464# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004465 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 && (*mb_char2cells)(mb_c) == 2)
4467 {
4468 c = '>';
4469 mb_c = c;
4470 mb_l = 1;
4471 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004472 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar017ba072019-09-14 21:01:23 +02004473#ifdef FEAT_SYN_HL
4474 if (cul_attr)
4475 multi_attr = hl_combine_attr(multi_attr, cul_attr);
4476#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 /* put the pointer back to output the double-width
4478 * character at the start of the next line. */
4479 ++n_extra;
4480 --p_extra;
4481 }
4482 else
4483 {
4484 n_extra -= mb_l - 1;
4485 p_extra += mb_l - 1;
4486 }
4487 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 ++p_extra;
4489 }
4490 --n_extra;
4491 }
4492 else
4493 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004494#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004495 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004496#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004497
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004498 if (p_extra_free != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01004499 VIM_CLEAR(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 /*
4501 * Get a character from the line itself.
4502 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004503 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004504#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004505 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004506#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 if (has_mbyte)
4508 {
4509 mb_c = c;
4510 if (enc_utf8)
4511 {
4512 /* If the UTF-8 character is more than one byte: Decode it
4513 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004514 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 mb_utf8 = FALSE;
4516 if (mb_l > 1)
4517 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004518 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 /* Overlong encoded ASCII or ASCII with composing char
4520 * is displayed normally, except a NUL. */
4521 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004522 {
4523 c = mb_c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004524#ifdef FEAT_LINEBREAK
Bram Moolenaar88e76882017-02-27 20:33:46 +01004525 c0 = mb_c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004526#endif
Bram Moolenaar88e76882017-02-27 20:33:46 +01004527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004529
4530 /* At start of the line we can have a composing char.
4531 * Draw it as a space with a composing char. */
4532 if (utf_iscomposing(mb_c))
4533 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004534 int i;
4535
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004536 for (i = Screen_mco - 1; i > 0; --i)
4537 u8cc[i] = u8cc[i - 1];
4538 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004539 mb_c = ' ';
4540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 }
4542
4543 if ((mb_l == 1 && c >= 0x80)
4544 || (mb_l >= 1 && mb_c == 0)
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004545 || (mb_l > 1 && (!vim_isprintc(mb_c))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 {
4547 /*
4548 * Illegal UTF-8 byte: display as <xx>.
4549 * Non-BMP character : display as ? or fullwidth ?.
4550 */
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004551 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004552# ifdef FEAT_RIGHTLEFT
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004553 if (wp->w_p_rl) /* reverse */
4554 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004555# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 p_extra = extra;
4557 c = *p_extra;
4558 mb_c = mb_ptr2char_adv(&p_extra);
4559 mb_utf8 = (c >= 0x80);
4560 n_extra = (int)STRLEN(p_extra);
4561 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004562 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 if (area_attr == 0 && search_attr == 0)
4564 {
4565 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004566 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 saved_attr2 = char_attr; /* save current attr */
4568 }
4569 }
4570 else if (mb_l == 0) /* at the NUL at end-of-line */
4571 mb_l = 1;
4572#ifdef FEAT_ARABIC
4573 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4574 {
4575 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004576 int pc, pc1, nc;
4577 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578
4579 /* The idea of what is the previous and next
4580 * character depends on 'rightleft'. */
4581 if (wp->w_p_rl)
4582 {
4583 pc = prev_c;
4584 pc1 = prev_c1;
4585 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004586 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 }
4588 else
4589 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004590 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004592 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 }
4594 prev_c = mb_c;
4595
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004596 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 }
4598 else
4599 prev_c = mb_c;
4600#endif
4601 }
4602 else /* enc_dbcs */
4603 {
4604 mb_l = MB_BYTE2LEN(c);
4605 if (mb_l == 0) /* at the NUL at end-of-line */
4606 mb_l = 1;
4607 else if (mb_l > 1)
4608 {
4609 /* We assume a second byte below 32 is illegal.
4610 * Hopefully this is OK for all double-byte encodings!
4611 */
4612 if (ptr[1] >= 32)
4613 mb_c = (c << 8) + ptr[1];
4614 else
4615 {
4616 if (ptr[1] == NUL)
4617 {
4618 /* head byte at end of line */
4619 mb_l = 1;
4620 transchar_nonprint(extra, c);
4621 }
4622 else
4623 {
4624 /* illegal tail byte */
4625 mb_l = 2;
4626 STRCPY(extra, "XX");
4627 }
4628 p_extra = extra;
4629 n_extra = (int)STRLEN(extra) - 1;
4630 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004631 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 c = *p_extra++;
4633 if (area_attr == 0 && search_attr == 0)
4634 {
4635 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004636 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 saved_attr2 = char_attr; /* save current attr */
4638 }
4639 mb_c = c;
4640 }
4641 }
4642 }
4643 /* If a double-width char doesn't fit display a '>' in the
4644 * last column; the character is displayed at the start of the
4645 * next line. */
4646 if ((
4647# ifdef FEAT_RIGHTLEFT
4648 wp->w_p_rl ? (col <= 0) :
4649# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004650 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 && (*mb_char2cells)(mb_c) == 2)
4652 {
4653 c = '>';
4654 mb_c = c;
4655 mb_utf8 = FALSE;
4656 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004657 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02004658 // Put pointer back so that the character will be
4659 // displayed at the start of the next line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 --ptr;
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02004661#ifdef FEAT_CONCEAL
4662 did_decrement_ptr = TRUE;
4663#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 }
4665 else if (*ptr != NUL)
4666 ptr += mb_l - 1;
4667
4668 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004669 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004670 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004671 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004674 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004675 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 c = ' ';
4677 if (area_attr == 0 && search_attr == 0)
4678 {
4679 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004680 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 saved_attr2 = char_attr; /* save current attr */
4682 }
4683 mb_c = c;
4684 mb_utf8 = FALSE;
4685 mb_l = 1;
4686 }
4687
4688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 ++ptr;
4690
4691 if (extra_check)
4692 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004693#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004694 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004695#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004696
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004697#ifdef FEAT_TERMINAL
4698 if (get_term_attr)
4699 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02004700 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004701
4702 if (!attr_pri)
4703 char_attr = syntax_attr;
4704 else
4705 char_attr = hl_combine_attr(syntax_attr, char_attr);
4706 }
4707#endif
4708
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004709#ifdef FEAT_SYN_HL
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004710 // Get syntax attribute, unless still at the start of the line
4711 // (double-wide char that doesn't fit).
Bram Moolenaar217ad922005-03-20 22:37:15 +00004712 v = (long)(ptr - line);
4713 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 {
4715 /* Get the syntax attribute for the character. If there
4716 * is an error, disable syntax highlighting. */
4717 save_did_emsg = did_emsg;
4718 did_emsg = FALSE;
4719
Bram Moolenaar217ad922005-03-20 22:37:15 +00004720 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004721# ifdef FEAT_SPELL
4722 has_spell ? &can_spell :
4723# endif
4724 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725
4726 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004727 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004728 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004729 has_syntax = FALSE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004730 syntax_attr = 0;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 else
4733 did_emsg = save_did_emsg;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004734
4735 // combine syntax attribute with 'wincolor'
4736 if (win_attr != 0)
4737 syntax_attr = hl_combine_attr(win_attr, syntax_attr);
4738
Bram Moolenaar017ba072019-09-14 21:01:23 +02004739# ifdef SYN_TIME_LIMIT
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004740 if (wp->w_s->b_syn_slow)
4741 has_syntax = FALSE;
Bram Moolenaar017ba072019-09-14 21:01:23 +02004742# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743
4744 /* Need to get the line again, a multi-line regexp may
4745 * have made it invalid. */
4746 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4747 ptr = line + v;
4748
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004749# ifdef FEAT_TEXT_PROP
Bram Moolenaarde24a872019-05-05 15:48:00 +02004750 // Text properties overrule syntax highlighting or combine.
4751 if (text_prop_attr == 0 || text_prop_combine)
4752# endif
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004753 {
Bram Moolenaarde24a872019-05-05 15:48:00 +02004754 int comb_attr = syntax_attr;
4755# ifdef FEAT_TEXT_PROP
4756 comb_attr = hl_combine_attr(text_prop_attr, comb_attr);
4757# endif
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004758 if (!attr_pri)
Bram Moolenaar017ba072019-09-14 21:01:23 +02004759 {
4760#ifdef FEAT_SYN_HL
4761 if (cul_attr)
4762 char_attr = hl_combine_attr(
4763 comb_attr, cul_attr);
4764 else
4765#endif
4766 char_attr = comb_attr;
4767 }
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004768 else
Bram Moolenaarde24a872019-05-05 15:48:00 +02004769 char_attr = hl_combine_attr(comb_attr, char_attr);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004770 }
Bram Moolenaarc095b282010-07-20 22:33:34 +02004771# ifdef FEAT_CONCEAL
4772 /* no concealing past the end of the line, it interferes
4773 * with line highlighting */
4774 if (c == NUL)
4775 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004776 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004777 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004778# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004780#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004781
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004782#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004783 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004784 * Only do this when there is no syntax highlighting, the
4785 * @Spell cluster is not used or the current syntax item
4786 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004787 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004788 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004789 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004790 if (c != 0 && (
4791# ifdef FEAT_SYN_HL
4792 !has_syntax ||
4793# endif
4794 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004795 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004796 char_u *prev_ptr, *p;
4797 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004798 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaare7566042005-06-17 22:00:15 +00004799 if (has_mbyte)
4800 {
4801 prev_ptr = ptr - mb_l;
4802 v -= mb_l - 1;
4803 }
4804 else
Bram Moolenaare7566042005-06-17 22:00:15 +00004805 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004806
4807 /* Use nextline[] if possible, it has the start of the
4808 * next line concatenated. */
4809 if ((prev_ptr - line) - nextlinecol >= 0)
4810 p = nextline + (prev_ptr - line) - nextlinecol;
4811 else
4812 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004813 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004814 len = spell_check(wp, p, &spell_hlf, &cap_col,
4815 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004816 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004817
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004818 /* In Insert mode only highlight a word that
4819 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004820 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004821 && (State & INSERT) != 0
4822 && wp->w_cursor.lnum == lnum
4823 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004824 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004825 && wp->w_cursor.col < (colnr_T)word_end)
4826 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004827 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004828 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004829 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004830
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004831 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004832 && (p - nextline) + len > nextline_idx)
4833 {
4834 /* Remember that the good word continues at the
4835 * start of the next line. */
4836 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004837 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004838 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004839
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004840 /* Turn index into actual attributes. */
4841 if (spell_hlf != HLF_COUNT)
4842 spell_attr = highlight_attr[spell_hlf];
4843
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004844 if (cap_col > 0)
4845 {
4846 if (p != prev_ptr
4847 && (p - nextline) + cap_col >= nextline_idx)
4848 {
4849 /* Remember that the word in the next line
4850 * must start with a capital. */
4851 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004852 cap_col = (int)((p - nextline) + cap_col
4853 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004854 }
4855 else
4856 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004857 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004858 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004859 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004860 }
4861 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004862 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004863 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004864 char_attr = hl_combine_attr(char_attr, spell_attr);
4865 else
4866 char_attr = hl_combine_attr(spell_attr, char_attr);
4867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868#endif
4869#ifdef FEAT_LINEBREAK
4870 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004871 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004873 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004874 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 {
Bram Moolenaar4df70292015-03-21 14:20:16 +01004876 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004877 char_u *p = ptr - (mb_off + 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004878
Bram Moolenaar597a4222014-06-25 14:39:50 +02004879 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004880 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004881 NULL) - 1;
Bram Moolenaar02631462017-09-22 15:20:32 +02004882 if (c == TAB && n_extra + col > wp->w_width)
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004883# ifdef FEAT_VARTABS
Bram Moolenaara87b72c2018-06-25 21:24:51 +02004884 n_extra = tabstop_padding(vcol, wp->w_buffer->b_p_ts,
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004885 wp->w_buffer->b_p_vts_array) - 1;
4886# else
Bram Moolenaara3650912014-11-19 13:21:57 +01004887 n_extra = (int)wp->w_buffer->b_p_ts
4888 - vcol % (int)wp->w_buffer->b_p_ts - 1;
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004889# endif
Bram Moolenaara3650912014-11-19 13:21:57 +01004890
Bram Moolenaar4df70292015-03-21 14:20:16 +01004891 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004892 c_final = NUL;
Bram Moolenaar1c465442017-03-12 20:10:05 +01004893 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004894 {
4895#ifdef FEAT_CONCEAL
4896 if (c == TAB)
4897 /* See "Tab alignment" below. */
4898 FIX_FOR_BOGUSCOLS;
4899#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004900 if (!wp->w_p_list)
4901 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 }
4904#endif
4905
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004906 // 'list': Change char 160 to lcs_nbsp and space to lcs_space.
4907 // But not when the character is followed by a composing
4908 // character (use mb_l to check that).
4909 if (wp->w_p_list
4910 && ((((c == 160 && mb_l == 1)
4911 || (mb_utf8
4912 && ((mb_c == 160 && mb_l == 2)
4913 || (mb_c == 0x202f && mb_l == 3))))
4914 && lcs_nbsp)
4915 || (c == ' '
4916 && mb_l == 1
4917 && lcs_space
4918 && ptr - line <= trailcol)))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004919 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004920 c = (c == ' ') ? lcs_space : lcs_nbsp;
4921 if (area_attr == 0 && search_attr == 0)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004922 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004923 n_attr = 1;
4924 extra_attr = HL_ATTR(HLF_8);
4925 saved_attr2 = char_attr; /* save current attr */
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004926 }
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004927 mb_c = c;
4928 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004929 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004930 mb_utf8 = TRUE;
4931 u8cc[0] = 0;
4932 c = 0xc0;
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004933 }
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004934 else
4935 mb_utf8 = FALSE;
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004936 }
4937
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4939 {
4940 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004941 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 {
4943 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004944 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 saved_attr2 = char_attr; /* save current attr */
4946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004948 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 {
4950 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004951 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004952 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 }
4954 else
4955 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 }
4957 }
4958
4959 /*
4960 * Handling of non-printable characters.
4961 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004962 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 {
4964 /*
4965 * when getting a character from the file, we may have to
4966 * turn it into something else on the way to putting it
4967 * into "ScreenLines".
4968 */
4969 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4970 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004971 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004972 long vcol_adjusted = vcol; /* removed showbreak length */
4973#ifdef FEAT_LINEBREAK
4974 /* only adjust the tab_len, when at the first column
4975 * after the showbreak value was drawn */
4976 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4977 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4978#endif
Bram Moolenaar69cbbec2019-08-17 14:10:56 +02004979 // tab amount depends on current column
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004980#ifdef FEAT_VARTABS
4981 tab_len = tabstop_padding(vcol_adjusted,
4982 wp->w_buffer->b_p_ts,
4983 wp->w_buffer->b_p_vts_array) - 1;
4984#else
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004985 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004986 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4987#endif
Bram Moolenaard574ea22015-01-14 19:35:14 +01004988
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004989#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004990 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004991#endif
Bram Moolenaar69cbbec2019-08-17 14:10:56 +02004992 // tab amount depends on current column
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004993 n_extra = tab_len;
4994#ifdef FEAT_LINEBREAK
4995 else
4996 {
Bram Moolenaar69cbbec2019-08-17 14:10:56 +02004997 char_u *p;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01004998 int len;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004999 int i;
5000 int saved_nextra = n_extra;
5001
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005002#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005003 if (vcol_off > 0)
Bram Moolenaar69cbbec2019-08-17 14:10:56 +02005004 // there are characters to conceal
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005005 tab_len += vcol_off;
Bram Moolenaar69cbbec2019-08-17 14:10:56 +02005006 // boguscols before FIX_FOR_BOGUSCOLS macro from above
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005007 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
5008 && n_extra > tab_len)
5009 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005010#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005011
Bram Moolenaar69cbbec2019-08-17 14:10:56 +02005012 // if n_extra > 0, it gives the number of chars, to
5013 // use for a tab, else we need to calculate the width
5014 // for a tab
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005015 len = (tab_len * mb_char2len(lcs_tab2));
5016 if (n_extra > 0)
5017 len += n_extra - tab_len;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005018 c = lcs_tab1;
Bram Moolenaar964b3742019-05-24 18:54:09 +02005019 p = alloc(len + 1);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005020 vim_memset(p, ' ', len);
5021 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005022 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005023 p_extra_free = p;
5024 for (i = 0; i < tab_len; i++)
5025 {
Bram Moolenaar69cbbec2019-08-17 14:10:56 +02005026 int lcs = lcs_tab2;
5027
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02005028 if (*p == NUL)
5029 {
5030 tab_len = i;
5031 break;
5032 }
Bram Moolenaar69cbbec2019-08-17 14:10:56 +02005033
5034 // if lcs_tab3 is given, need to change the char
5035 // for tab
5036 if (lcs_tab3 && i == tab_len - 1)
5037 lcs = lcs_tab3;
5038 mb_char2bytes(lcs, p);
5039 p += mb_char2len(lcs);
5040 n_extra += mb_char2len(lcs)
5041 - (saved_nextra > 0 ? 1 : 0);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005042 }
5043 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005044#ifdef FEAT_CONCEAL
Bram Moolenaar69cbbec2019-08-17 14:10:56 +02005045 // n_extra will be increased by FIX_FOX_BOGUSCOLS
5046 // macro below, so need to adjust for that here
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005047 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005048 n_extra -= vcol_off;
5049#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005050 }
5051#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005052#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005053 {
5054 int vc_saved = vcol_off;
5055
5056 /* Tab alignment should be identical regardless of
5057 * 'conceallevel' value. So tab compensates of all
5058 * previous concealed characters, and thus resets
5059 * vcol_off and boguscols accumulated so far in the
5060 * line. Note that the tab can be longer than
5061 * 'tabstop' when there are concealed characters. */
5062 FIX_FOR_BOGUSCOLS;
5063
5064 /* Make sure, the highlighting for the tab char will be
5065 * correctly set further below (effectively reverts the
5066 * FIX_FOR_BOGSUCOLS macro */
5067 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005068 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005069 tab_len += vc_saved;
5070 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005071#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 if (wp->w_p_list)
5074 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01005075 c = (n_extra == 0 && lcs_tab3) ? lcs_tab3 : lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005076#ifdef FEAT_LINEBREAK
5077 if (wp->w_p_lbr)
5078 c_extra = NUL; /* using p_extra from above */
5079 else
5080#endif
5081 c_extra = lcs_tab2;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005082 c_final = lcs_tab3;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005083 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005084 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 saved_attr2 = char_attr; /* save current attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005087 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 {
5089 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005090 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005091 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 }
5094 else
5095 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01005096 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 c_extra = ' ';
5098 c = ' ';
5099 }
5100 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005101 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02005102 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005103 || ((fromcol >= 0 || fromcol_prev >= 0)
5104 && tocol > vcol
5105 && VIsual_mode != Ctrl_V
5106 && (
5107# ifdef FEAT_RIGHTLEFT
5108 wp->w_p_rl ? (col >= 0) :
5109# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005110 (col < wp->w_width))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005111 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005112 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005113 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02005114 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005116 /* Display a '$' after the line or highlight an extra
5117 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118#if defined(FEAT_DIFF) || defined(LINE_ATTR)
5119 /* For a diff line the highlighting continues after the
5120 * "$". */
5121 if (
5122# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005123 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124# ifdef LINE_ATTR
5125 &&
5126# endif
5127# endif
5128# ifdef LINE_ATTR
5129 line_attr == 0
5130# endif
5131 )
5132#endif
5133 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 /* In virtualedit, visual selections may extend
5135 * beyond end of line. */
5136 if (area_highlighting && virtual_active()
5137 && tocol != MAXCOL && vcol < tocol)
5138 n_extra = 0;
5139 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 {
5141 p_extra = at_end_str;
5142 n_extra = 1;
5143 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005144 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 }
5146 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02005147 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005148 c = lcs_eol;
5149 else
5150 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 lcs_eol_one = -1;
5152 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005153 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005155 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 n_attr = 1;
5157 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005159 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 {
5161 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005162 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005163 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 }
5165 else
5166 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 }
5168 else if (c != NUL)
5169 {
5170 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02005171 if (n_extra == 0)
5172 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173#ifdef FEAT_RIGHTLEFT
5174 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
5175 rl_mirror(p_extra); /* reverse "<12>" */
5176#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005178 c_final = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005179#ifdef FEAT_LINEBREAK
5180 if (wp->w_p_lbr)
5181 {
5182 char_u *p;
5183
5184 c = *p_extra;
Bram Moolenaar964b3742019-05-24 18:54:09 +02005185 p = alloc(n_extra + 1);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005186 vim_memset(p, ' ', n_extra);
5187 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5188 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005189 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005190 p_extra_free = p_extra = p;
5191 }
5192 else
5193#endif
5194 {
5195 n_extra = byte2cells(c) - 1;
5196 c = *p_extra++;
5197 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005198 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 {
5200 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005201 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 saved_attr2 = char_attr; /* save current attr */
5203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 else if (VIsual_active
5207 && (VIsual_mode == Ctrl_V
5208 || VIsual_mode == 'v')
5209 && virtual_active()
5210 && tocol != MAXCOL
5211 && vcol < tocol
5212 && (
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005213#ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 wp->w_p_rl ? (col >= 0) :
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005215#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005216 (col < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 {
5218 c = ' ';
5219 --ptr; /* put it back at the NUL */
5220 }
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005221#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 else if ((
5223# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005224 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005226# ifdef FEAT_TERMINAL
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005227 win_attr != 0 ||
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005228# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 ) && (
5231# ifdef FEAT_RIGHTLEFT
5232 wp->w_p_rl ? (col >= 0) :
5233# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005234 (col
5235# ifdef FEAT_CONCEAL
5236 - boguscols
5237# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005238 < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 {
5240 /* Highlight until the right side of the window */
5241 c = ' ';
5242 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005243
5244 /* Remember we do the char for line highlighting. */
5245 ++did_line_attr;
5246
5247 /* don't do search HL for the rest of the line */
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005248 if (line_attr != 0 && char_attr == search_attr
5249 && (did_line_attr > 1
5250 || (wp->w_p_list && lcs_eol > 0)))
Bram Moolenaar91170f82006-05-05 21:15:17 +00005251 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252# ifdef FEAT_DIFF
5253 if (diff_hlf == HLF_TXD)
5254 {
5255 diff_hlf = HLF_CHD;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005256 if (vi_attr == 0 || char_attr != vi_attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005257 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005258 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005259 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaar017ba072019-09-14 21:01:23 +02005260 && wp->w_p_culopt_flags != CULOPT_NBR
5261 && (!cul_screenline
5262 || (vcol >= left_curline_col
5263 && vcol <= right_curline_col)))
5264 char_attr = hl_combine_attr(
5265 char_attr, HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 }
5268# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005269# ifdef FEAT_TERMINAL
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005270 if (win_attr != 0)
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005271 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005272 char_attr = win_attr;
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005273 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar017ba072019-09-14 21:01:23 +02005274 {
5275 if (!cul_screenline || (vcol >= left_curline_col
5276 && vcol <= right_curline_col))
5277 char_attr = hl_combine_attr(
5278 char_attr, HL_ATTR(HLF_CUL));
5279 }
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02005280 else if (line_attr)
5281 char_attr = hl_combine_attr(char_attr, line_attr);
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005282 }
5283# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 }
5285#endif
5286 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005287
5288#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005289 if ( wp->w_p_cole > 0
5290 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaarab62c192019-03-30 16:39:05 +01005291 conceal_cursor_line(wp))
5292 && ((syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005293 && !(lnum_in_visual_area
5294 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005295 {
5296 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005297 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005298 && (syn_get_sub_char() != NUL || match_conc
5299 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005300 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005301 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005302 /* First time at this concealed item: display one
5303 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005304 if (match_conc)
5305 c = match_conc;
5306 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005307 c = syn_get_sub_char();
5308 else if (lcs_conceal != NUL)
5309 c = lcs_conceal;
5310 else
5311 c = ' ';
5312
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005313 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005314
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005315 if (n_extra > 0)
5316 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005317 vcol += n_extra;
5318 if (wp->w_p_wrap && n_extra > 0)
5319 {
5320# ifdef FEAT_RIGHTLEFT
5321 if (wp->w_p_rl)
5322 {
5323 col -= n_extra;
5324 boguscols -= n_extra;
5325 }
5326 else
5327# endif
5328 {
5329 boguscols += n_extra;
5330 col += n_extra;
5331 }
5332 }
5333 n_extra = 0;
5334 n_attr = 0;
5335 }
5336 else if (n_skip == 0)
5337 {
5338 is_concealing = TRUE;
5339 n_skip = 1;
5340 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005341 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005342 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005343 {
5344 mb_utf8 = TRUE;
5345 u8cc[0] = 0;
5346 c = 0xc0;
5347 }
5348 else
5349 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005350 }
5351 else
5352 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005353 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005354 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005355 }
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02005356
5357 if (n_skip > 0 && did_decrement_ptr)
5358 // not showing the '>', put pointer back to avoid getting stuck
5359 ++ptr;
5360
5361#endif // FEAT_CONCEAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 }
5363
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005364#ifdef FEAT_CONCEAL
5365 /* In the cursor line and we may be concealing characters: correct
5366 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005367 if (!did_wcol && draw_state == WL_LINE
5368 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005369 && conceal_cursor_line(wp)
5370 && (int)wp->w_virtcol <= vcol + n_skip)
5371 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005372# ifdef FEAT_RIGHTLEFT
5373 if (wp->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02005374 wp->w_wcol = wp->w_width - col + boguscols - 1;
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005375 else
5376# endif
5377 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005378 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005379 did_wcol = TRUE;
Bram Moolenaar5babc6e2019-09-14 21:55:51 +02005380 curwin->w_valid |= VALID_WCOL|VALID_WROW|VALID_VIRTCOL;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005381 }
5382#endif
5383
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 /* Don't override visual selection highlighting. */
5385 if (n_attr > 0
5386 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005387 && !attr_pri)
Bram Moolenaar017ba072019-09-14 21:01:23 +02005388 {
Bram Moolenaar0571f3d2019-09-14 22:33:47 +02005389#ifdef LINE_ATTR
Bram Moolenaar017ba072019-09-14 21:01:23 +02005390 if (line_attr)
5391 char_attr = hl_combine_attr(extra_attr, line_attr);
5392 else
Bram Moolenaar0571f3d2019-09-14 22:33:47 +02005393#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02005394 char_attr = extra_attr;
5395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396
Bram Moolenaar81695252004-12-29 20:58:21 +00005397#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 /* XIM don't send preedit_start and preedit_end, but they send
5399 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5400 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005401 if (p_imst == IM_ON_THE_SPOT
5402 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005403 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 && (State & INSERT)
5405 && !p_imdisable
5406 && im_is_preediting()
5407 && draw_state == WL_LINE)
5408 {
5409 colnr_T tcol;
5410
5411 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005412 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 else
5414 tcol = preedit_end_col;
5415 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5416 {
5417 if (feedback_old_attr < 0)
5418 {
5419 feedback_col = 0;
5420 feedback_old_attr = char_attr;
5421 }
5422 char_attr = im_get_feedback_attr(feedback_col);
5423 if (char_attr < 0)
5424 char_attr = feedback_old_attr;
5425 feedback_col++;
5426 }
5427 else if (feedback_old_attr >= 0)
5428 {
5429 char_attr = feedback_old_attr;
5430 feedback_old_attr = -1;
5431 feedback_col = 0;
5432 }
5433 }
5434#endif
5435 /*
5436 * Handle the case where we are in column 0 but not on the first
5437 * character of the line and the user wants us to show us a
5438 * special character (via 'listchars' option "precedes:<char>".
5439 */
5440 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005441 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5443#ifdef FEAT_DIFF
5444 && filler_todo <= 0
5445#endif
5446 && draw_state > WL_NR
5447 && c != NUL)
5448 {
5449 c = lcs_prec;
5450 lcs_prec_todo = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005451 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5452 {
5453 /* Double-width character being overwritten by the "precedes"
5454 * character, need to fill up half the character. */
5455 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005456 c_final = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005457 n_extra = 1;
5458 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005459 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005462 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 {
5464 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005465 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005466 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 }
5468 else
5469 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005470 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 {
5472 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005473 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 n_attr3 = 1;
5475 }
5476 }
5477
5478 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005479 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 */
Bram Moolenaarf914a332019-07-20 15:09:56 +02005481 if ((c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005482#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005483 || did_line_attr == 1
5484#endif
Bram Moolenaarf914a332019-07-20 15:09:56 +02005485 ) && eol_hl_off == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005487#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarbbca7732019-07-24 18:13:16 +02005488 // flag to indicate whether prevcol equals startcol of search_hl or
5489 // one of the matches
5490 int prevcol_hl_flag = get_prevcol_hl_flag(wp, &search_hl,
5491 (long)(ptr - line) - (c == NUL));
Bram Moolenaar91170f82006-05-05 21:15:17 +00005492#endif
Bram Moolenaarbbca7732019-07-24 18:13:16 +02005493 // Invert at least one char, used for Visual and empty line or
5494 // highlight match at end of line. If it's beyond the last
5495 // char on the screen, just overwrite that one (tricky!) Not
5496 // needed when a '$' was displayed for 'list'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005498 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005499 && (VIsual_mode != Ctrl_V
5500 || lnum == VIsual.lnum
5501 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005502 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarbbca7732019-07-24 18:13:16 +02005504 // highlight 'hlsearch' match at end of line
5505 || (prevcol_hl_flag
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005506# ifdef FEAT_SYN_HL
5507 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
5508 && !(wp == curwin && VIsual_active))
5509# endif
5510# ifdef FEAT_DIFF
5511 && diff_hlf == (hlf_T)0
5512# endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005513# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005514 && did_line_attr <= 1
5515# endif
5516 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517#endif
5518 ))
5519 {
5520 int n = 0;
5521
5522#ifdef FEAT_RIGHTLEFT
5523 if (wp->w_p_rl)
5524 {
5525 if (col < 0)
5526 n = 1;
5527 }
5528 else
5529#endif
5530 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005531 if (col >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532 n = -1;
5533 }
5534 if (n != 0)
5535 {
5536 /* At the window boundary, highlight the last character
5537 * instead (better than nothing). */
5538 off += n;
5539 col += n;
5540 }
5541 else
5542 {
5543 /* Add a blank character to highlight. */
5544 ScreenLines[off] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 if (enc_utf8)
5546 ScreenLinesUC[off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547 }
5548#ifdef FEAT_SEARCH_EXTRA
5549 if (area_attr == 0)
5550 {
Bram Moolenaarbbca7732019-07-24 18:13:16 +02005551 // Use attributes from match with highest priority among
5552 // 'search_hl' and the match list.
5553 get_search_match_hl(wp, &search_hl,
5554 (long)(ptr - line), &char_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 }
5556#endif
5557 ScreenAttrs[off] = char_attr;
5558#ifdef FEAT_RIGHTLEFT
5559 if (wp->w_p_rl)
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 Moolenaar071d4272004-06-13 20:20:40 +00005564 else
5565#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005566 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005568 ++off;
5569 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005570 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005571 eol_hl_off = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574
Bram Moolenaar91170f82006-05-05 21:15:17 +00005575 /*
5576 * At end of the text line.
5577 */
5578 if (c == NUL)
5579 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005580#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005581 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005582 if (wp->w_p_wrap)
5583 v = wp->w_skipcol;
5584 else
5585 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005586
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005587 /* check if line ends before left margin */
5588 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005589 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005590#ifdef FEAT_CONCEAL
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01005591 // Get rid of the boguscols now, we want to draw until the right
5592 // edge for 'cursorcolumn'.
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005593 col -= boguscols;
5594 boguscols = 0;
5595#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005596
5597 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005598 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005599
5600 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005601 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5602 && (int)wp->w_virtcol <
Bram Moolenaar02631462017-09-22 15:20:32 +02005603 wp->w_width * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005604 && lnum != wp->w_cursor.lnum)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005605 || draw_color_col
5606 || win_attr != 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005607# ifdef FEAT_RIGHTLEFT
5608 && !wp->w_p_rl
5609# endif
5610 )
5611 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005612 int rightmost_vcol = 0;
5613 int i;
5614
5615 if (wp->w_p_cuc)
5616 rightmost_vcol = wp->w_virtcol;
5617 if (draw_color_col)
5618 /* determine rightmost colorcolumn to possibly draw */
5619 for (i = 0; color_cols[i] >= 0; ++i)
5620 if (rightmost_vcol < color_cols[i])
5621 rightmost_vcol = color_cols[i];
5622
Bram Moolenaar02631462017-09-22 15:20:32 +02005623 while (col < wp->w_width)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005624 {
5625 ScreenLines[off] = ' ';
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005626 if (enc_utf8)
5627 ScreenLinesUC[off] = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005628 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005629 if (draw_color_col)
5630 draw_color_col = advance_color_col(VCOL_HLC,
5631 &color_cols);
5632
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005633 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005634 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005635 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005636 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005637 else
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005638 ScreenAttrs[off++] = win_attr;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005639
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005640 if (VCOL_HLC >= rightmost_vcol && win_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005641 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005642
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005643 ++vcol;
5644 }
5645 }
5646#endif
5647
Bram Moolenaar53f81742017-09-22 14:35:51 +02005648 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005649 (int)wp->w_width, screen_line_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650 row++;
5651
5652 /*
5653 * Update w_cline_height and w_cline_folded if the cursor line was
5654 * updated (saves a call to plines() later).
5655 */
5656 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5657 {
5658 curwin->w_cline_row = startrow;
5659 curwin->w_cline_height = row - startrow;
5660#ifdef FEAT_FOLDING
5661 curwin->w_cline_folded = FALSE;
5662#endif
5663 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5664 }
5665
5666 break;
5667 }
5668
Bram Moolenaara5c6a0b2019-05-08 20:20:46 +02005669 // Show "extends" character from 'listchars' if beyond the line end and
5670 // 'list' is set.
5671 if (lcs_ext != NUL
5672 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 && !wp->w_p_wrap
5674#ifdef FEAT_DIFF
5675 && filler_todo <= 0
5676#endif
5677 && (
5678#ifdef FEAT_RIGHTLEFT
5679 wp->w_p_rl ? col == 0 :
5680#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005681 col == wp->w_width - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005683 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5685 {
5686 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005687 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005689 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 {
5691 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005692 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005693 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 }
5695 else
5696 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 }
5698
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005699#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005700 /* advance to the next 'colorcolumn' */
5701 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005702 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005703
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005704 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005705 * highlight the cursor position itself.
5706 * Also highlight the 'colorcolumn' if it is different than
5707 * 'cursorcolumn' */
5708 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005709 if (draw_state == WL_LINE && !lnum_in_visual_area
5710 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005711 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005712 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005713 && lnum != wp->w_cursor.lnum)
5714 {
5715 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005716 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005717 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005718 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005719 {
5720 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005721 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005722 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005723 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005724#endif
5725
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 /*
5727 * Store character to be displayed.
5728 * Skip characters that are left of the screen for 'nowrap'.
5729 */
5730 vcol_prev = vcol;
5731 if (draw_state < WL_LINE || n_skip <= 0)
5732 {
5733 /*
5734 * Store the character.
5735 */
Bram Moolenaara12a1612019-01-24 16:39:02 +01005736#if defined(FEAT_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5738 {
5739 /* A double-wide character is: put first halve in left cell. */
5740 --off;
5741 --col;
5742 }
5743#endif
5744 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005746 {
5747 if ((mb_c & 0xff00) == 0x8e00)
5748 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 else if (enc_utf8)
5752 {
5753 if (mb_utf8)
5754 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005755 int i;
5756
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005758 if ((c & 0xff) == 0)
5759 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005760 for (i = 0; i < Screen_mco; ++i)
5761 {
5762 ScreenLinesC[i][off] = u8cc[i];
5763 if (u8cc[i] == 0)
5764 break;
5765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 }
5767 else
5768 ScreenLinesUC[off] = 0;
5769 }
5770 if (multi_attr)
5771 {
5772 ScreenAttrs[off] = multi_attr;
5773 multi_attr = 0;
5774 }
5775 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 ScreenAttrs[off] = char_attr;
5777
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5779 {
5780 /* Need to fill two screen columns. */
5781 ++off;
5782 ++col;
5783 if (enc_utf8)
5784 /* UTF-8: Put a 0 in the second screen char. */
5785 ScreenLines[off] = 0;
5786 else
5787 /* DBCS: Put second byte in the second screen char. */
5788 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005789 if (draw_state > WL_NR
5790#ifdef FEAT_DIFF
5791 && filler_todo <= 0
5792#endif
5793 )
5794 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795 /* When "tocol" is halfway a character, set it to the end of
5796 * the character, otherwise highlighting won't stop. */
5797 if (tocol == vcol)
5798 ++tocol;
5799#ifdef FEAT_RIGHTLEFT
5800 if (wp->w_p_rl)
5801 {
5802 /* now it's time to backup one cell */
5803 --off;
5804 --col;
5805 }
5806#endif
5807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808#ifdef FEAT_RIGHTLEFT
5809 if (wp->w_p_rl)
5810 {
5811 --off;
5812 --col;
5813 }
5814 else
5815#endif
5816 {
5817 ++off;
5818 ++col;
5819 }
5820 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005821#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005822 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005823 {
5824 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005825 ++vcol_off;
5826 if (n_extra > 0)
5827 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005828 if (wp->w_p_wrap)
5829 {
5830 /*
5831 * Special voodoo required if 'wrap' is on.
5832 *
5833 * Advance the column indicator to force the line
5834 * drawing to wrap early. This will make the line
5835 * take up the same screen space when parts are concealed,
5836 * so that cursor line computations aren't messed up.
5837 *
5838 * To avoid the fictitious advance of 'col' causing
5839 * trailing junk to be written out of the screen line
5840 * we are building, 'boguscols' keeps track of the number
5841 * of bad columns we have advanced.
5842 */
5843 if (n_extra > 0)
5844 {
5845 vcol += n_extra;
5846# ifdef FEAT_RIGHTLEFT
5847 if (wp->w_p_rl)
5848 {
5849 col -= n_extra;
5850 boguscols -= n_extra;
5851 }
5852 else
5853# endif
5854 {
5855 col += n_extra;
5856 boguscols += n_extra;
5857 }
5858 n_extra = 0;
5859 n_attr = 0;
5860 }
5861
5862
Bram Moolenaar860cae12010-06-05 23:22:07 +02005863 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5864 {
5865 /* Need to fill two screen columns. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01005866# ifdef FEAT_RIGHTLEFT
Bram Moolenaar860cae12010-06-05 23:22:07 +02005867 if (wp->w_p_rl)
5868 {
5869 --boguscols;
5870 --col;
5871 }
5872 else
Bram Moolenaara12a1612019-01-24 16:39:02 +01005873# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02005874 {
5875 ++boguscols;
5876 ++col;
5877 }
5878 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005879
5880# ifdef FEAT_RIGHTLEFT
5881 if (wp->w_p_rl)
5882 {
5883 --boguscols;
5884 --col;
5885 }
5886 else
5887# endif
5888 {
5889 ++boguscols;
5890 ++col;
5891 }
5892 }
5893 else
5894 {
5895 if (n_extra > 0)
5896 {
5897 vcol += n_extra;
5898 n_extra = 0;
5899 n_attr = 0;
5900 }
5901 }
5902
5903 }
5904#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905 else
5906 --n_skip;
5907
Bram Moolenaar64486672010-05-16 15:46:46 +02005908 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5909 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005910 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911#ifdef FEAT_DIFF
5912 && filler_todo <= 0
5913#endif
5914 )
5915 ++vcol;
5916
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005917#ifdef FEAT_SYN_HL
5918 if (vcol_save_attr >= 0)
5919 char_attr = vcol_save_attr;
5920#endif
5921
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 /* restore attributes after "predeces" in 'listchars' */
5923 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5924 char_attr = saved_attr3;
5925
5926 /* restore attributes after last 'listchars' or 'number' char */
5927 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5928 char_attr = saved_attr2;
5929
5930 /*
5931 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005932 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 */
5934 if ((
5935#ifdef FEAT_RIGHTLEFT
5936 wp->w_p_rl ? (col < 0) :
5937#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005938 (col >= wp->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 && (*ptr != NUL
5940#ifdef FEAT_DIFF
5941 || filler_todo > 0
5942#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005943 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5945 )
5946 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005947#ifdef FEAT_CONCEAL
Bram Moolenaar53f81742017-09-22 14:35:51 +02005948 screen_line(screen_row, wp->w_wincol, col - boguscols,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005949 (int)wp->w_width, screen_line_flags);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005950 boguscols = 0;
5951#else
Bram Moolenaar53f81742017-09-22 14:35:51 +02005952 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005953 (int)wp->w_width, screen_line_flags);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005954#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 ++row;
5956 ++screen_row;
5957
5958 /* When not wrapping and finished diff lines, or when displayed
5959 * '$' and highlighting until last column, break here. */
5960 if ((!wp->w_p_wrap
5961#ifdef FEAT_DIFF
5962 && filler_todo <= 0
5963#endif
5964 ) || lcs_eol_one == -1)
5965 break;
5966
5967 /* When the window is too narrow draw all "@" lines. */
5968 if (draw_state != WL_LINE
5969#ifdef FEAT_DIFF
5970 && filler_todo <= 0
5971#endif
5972 )
5973 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01005974 win_draw_end(wp, '@', ' ', TRUE, row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976 row = endrow;
5977 }
5978
5979 /* When line got too long for screen break here. */
5980 if (row == endrow)
5981 {
5982 ++row;
5983 break;
5984 }
5985
5986 if (screen_cur_row == screen_row - 1
5987#ifdef FEAT_DIFF
5988 && filler_todo <= 0
5989#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005990 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991 {
5992 /* Remember that the line wraps, used for modeless copy. */
5993 LineWraps[screen_row - 1] = TRUE;
5994
5995 /*
5996 * Special trick to make copy/paste of wrapped lines work with
5997 * xterm/screen: write an extra character beyond the end of
5998 * the line. This will work with all terminal types
5999 * (regardless of the xn,am settings).
6000 * Only do this on a fast tty.
6001 * Only do this if the cursor is on the current line
6002 * (something has been written in it).
6003 * Don't do this for the GUI.
6004 * Don't do this for double-width characters.
6005 * Don't do this for a window not at the right screen border.
6006 */
6007 if (p_tf
6008#ifdef FEAT_GUI
6009 && !gui.in_use
6010#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00006012 && ((*mb_off2cells)(LineOffset[screen_row],
6013 LineOffset[screen_row] + screen_Columns)
6014 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00006016 + (int)Columns - 2,
6017 LineOffset[screen_row] + screen_Columns)
Bram Moolenaara12a1612019-01-24 16:39:02 +01006018 == 2)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019 {
6020 /* First make sure we are at the end of the screen line,
6021 * then output the same character again to let the
6022 * terminal know about the wrap. If the terminal doesn't
6023 * auto-wrap, we overwrite the character. */
Bram Moolenaar02631462017-09-22 15:20:32 +02006024 if (screen_cur_col != wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025 screen_char(LineOffset[screen_row - 1]
6026 + (unsigned)Columns - 1,
6027 screen_row - 1, (int)(Columns - 1));
6028
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029 /* When there is a multi-byte character, just output a
6030 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00006031 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
6032 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033 out_char(' ');
6034 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035 out_char(ScreenLines[LineOffset[screen_row - 1]
6036 + (Columns - 1)]);
6037 /* force a redraw of the first char on the next line */
6038 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
6039 screen_start(); /* don't know where cursor is now */
6040 }
6041 }
6042
6043 col = 0;
6044 off = (unsigned)(current_ScreenLine - ScreenLines);
6045#ifdef FEAT_RIGHTLEFT
6046 if (wp->w_p_rl)
6047 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006048 col = wp->w_width - 1; /* col is not used if breaking! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049 off += col;
6050 }
6051#endif
6052
6053 /* reset the drawing state for the start of a wrapped line */
6054 draw_state = WL_START;
6055 saved_n_extra = n_extra;
6056 saved_p_extra = p_extra;
6057 saved_c_extra = c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01006058 saved_c_final = c_final;
Bram Moolenaar017ba072019-09-14 21:01:23 +02006059#ifdef FEAT_SYN_HL
6060 if (!(cul_screenline
6061# ifdef FEAT_DIFF
6062 && diff_hlf == (hlf_T)0)
6063# endif
6064 )
6065 saved_char_attr = char_attr;
6066 else
6067#endif
6068 saved_char_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069 n_extra = 0;
6070 lcs_prec_todo = lcs_prec;
6071#ifdef FEAT_LINEBREAK
6072# ifdef FEAT_DIFF
6073 if (filler_todo <= 0)
6074# endif
6075 need_showbreak = TRUE;
6076#endif
6077#ifdef FEAT_DIFF
6078 --filler_todo;
6079 /* When the filler lines are actually below the last line of the
6080 * file, don't draw the line itself, break here. */
6081 if (filler_todo == 0 && wp->w_botfill)
6082 break;
6083#endif
6084 }
6085
6086 } /* for every character in the line */
6087
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006088#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006089 /* After an empty line check first word for capital. */
6090 if (*skipwhite(line) == NUL)
6091 {
6092 capcol_lnum = lnum + 1;
6093 cap_col = 0;
6094 }
6095#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006096#ifdef FEAT_TEXT_PROP
6097 vim_free(text_props);
6098 vim_free(text_prop_idxs);
6099#endif
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006100
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01006101 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102 return row;
6103}
6104
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006105/*
6106 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006107 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006108 */
6109 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006110comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006111{
6112 int i;
6113
6114 for (i = 0; i < Screen_mco; ++i)
6115 {
6116 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
6117 return TRUE;
6118 if (ScreenLinesC[i][off_from] == 0)
6119 break;
6120 }
6121 return FALSE;
6122}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006123
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124/*
6125 * Check whether the given character needs redrawing:
6126 * - the (first byte of the) character is different
6127 * - the attributes are different
6128 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006129 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130 */
6131 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006132char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133{
6134 if (cols > 0
6135 && ((ScreenLines[off_from] != ScreenLines[off_to]
6136 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137 || (enc_dbcs != 0
6138 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
6139 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
6140 ? ScreenLines2[off_from] != ScreenLines2[off_to]
6141 : (cols > 1 && ScreenLines[off_from + 1]
6142 != ScreenLines[off_to + 1])))
6143 || (enc_utf8
6144 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6145 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006146 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006147 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6148 && ScreenLines[off_from + 1]
Bram Moolenaara12a1612019-01-24 16:39:02 +01006149 != ScreenLines[off_to + 1])))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150 return TRUE;
6151 return FALSE;
6152}
6153
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006154#if defined(FEAT_TERMINAL) || defined(PROTO)
6155/*
6156 * Return the index in ScreenLines[] for the current screen line.
6157 */
6158 int
6159screen_get_current_line_off()
6160{
6161 return (int)(current_ScreenLine - ScreenLines);
6162}
6163#endif
6164
Bram Moolenaarc662ec92019-06-23 00:15:57 +02006165#ifdef FEAT_TEXT_PROP
6166/*
6167 * Return TRUE if this position has a higher level popup or this cell is
6168 * transparent in the current popup.
6169 */
6170 static int
6171blocked_by_popup(int row, int col)
6172{
6173 int off;
6174
6175 if (!popup_visible)
6176 return FALSE;
6177 off = row * screen_Columns + col;
6178 return popup_mask[off] > screen_zindex || popup_transparent[off];
6179}
6180#endif
6181
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182/*
6183 * Move one "cooked" screen line to the screen, but only the characters that
6184 * have actually changed. Handle insert/delete character.
6185 * "coloff" gives the first column on the screen for this line.
6186 * "endcol" gives the columns where valid characters are.
6187 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6188 * needs to be cleared, negative otherwise.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006189 * "flags" can have bits:
6190 * SLF_POPUP popup window
6191 * SLF_RIGHTLEFT rightleft window:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6193 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6194 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006195 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006196screen_line(
6197 int row,
6198 int coloff,
6199 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006200 int clear_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006201 int flags UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202{
6203 unsigned off_from;
6204 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006205 unsigned max_off_from;
6206 unsigned max_off_to;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 int force = FALSE; /* force update rest of the line */
6210 int redraw_this /* bool: does character need redraw? */
6211#ifdef FEAT_GUI
6212 = TRUE /* For GUI when while-loop empty */
6213#endif
6214 ;
6215 int redraw_next; /* redraw_this for next character */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 int clear_next = FALSE;
6217 int char_cells; /* 1: normal char */
6218 /* 2: occupies two display cells */
6219# define CHAR_CELLS char_cells
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006221 /* Check for illegal row and col, just in case. */
6222 if (row >= Rows)
6223 row = Rows - 1;
6224 if (endcol > Columns)
6225 endcol = Columns;
6226
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227# ifdef FEAT_CLIPBOARD
6228 clip_may_clear_selection(row, row);
6229# endif
6230
6231 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6232 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006233 max_off_from = off_from + screen_Columns;
6234 max_off_to = LineOffset[row] + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235
6236#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006237 if (flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 {
6239 /* Clear rest first, because it's left of the text. */
6240 if (clear_width > 0)
6241 {
6242 while (col <= endcol && ScreenLines[off_to] == ' '
6243 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006244 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245 {
6246 ++off_to;
6247 ++col;
6248 }
6249 if (col <= endcol)
6250 screen_fill(row, row + 1, col + coloff,
6251 endcol + coloff + 1, ' ', ' ', 0);
6252 }
6253 col = endcol + 1;
6254 off_to = LineOffset[row] + col + coloff;
6255 off_from += col;
6256 endcol = (clear_width > 0 ? clear_width : -clear_width);
6257 }
6258#endif /* FEAT_RIGHTLEFT */
6259
6260 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6261
6262 while (col < endcol)
6263 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006265 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266 else
6267 char_cells = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268
6269 redraw_this = redraw_next;
6270 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6271 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6272
6273#ifdef FEAT_GUI
6274 /* If the next character was bold, then redraw the current character to
6275 * remove any pixels that might have spilt over into us. This only
6276 * happens in the GUI.
6277 */
6278 if (redraw_next && gui.in_use)
6279 {
6280 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006281 if (hl > HL_ALL)
6282 hl = syn_attr2attr(hl);
6283 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284 redraw_this = TRUE;
6285 }
6286#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02006287#ifdef FEAT_TEXT_PROP
Bram Moolenaarc662ec92019-06-23 00:15:57 +02006288 if (blocked_by_popup(row, col + coloff))
Bram Moolenaar33796b32019-06-08 16:01:13 +02006289 redraw_this = FALSE;
6290#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291 if (redraw_this)
6292 {
6293 /*
6294 * Special handling when 'xs' termcap flag set (hpterm):
6295 * Attributes for characters are stored at the position where the
6296 * cursor is when writing the highlighting code. The
6297 * start-highlighting code must be written with the cursor on the
6298 * first highlighted character. The stop-highlighting code must
6299 * be written with the cursor just after the last highlighted
6300 * character.
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01006301 * Overwriting a character doesn't remove its highlighting. Need
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302 * to clear the rest of the line, and force redrawing it
6303 * completely.
6304 */
6305 if ( p_wiv
6306 && !force
6307#ifdef FEAT_GUI
6308 && !gui.in_use
6309#endif
6310 && ScreenAttrs[off_to] != 0
6311 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6312 {
6313 /*
6314 * Need to remove highlighting attributes here.
6315 */
6316 windgoto(row, col + coloff);
6317 out_str(T_CE); /* clear rest of this screen line */
6318 screen_start(); /* don't know where cursor is now */
6319 force = TRUE; /* force redraw of rest of the line */
6320 redraw_next = TRUE; /* or else next char would miss out */
6321
6322 /*
6323 * If the previous character was highlighted, need to stop
6324 * highlighting at this character.
6325 */
6326 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6327 {
6328 screen_attr = ScreenAttrs[off_to - 1];
6329 term_windgoto(row, col + coloff);
6330 screen_stop_highlight();
6331 }
6332 else
6333 screen_attr = 0; /* highlighting has stopped */
6334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335 if (enc_dbcs != 0)
6336 {
6337 /* Check if overwriting a double-byte with a single-byte or
6338 * the other way around requires another character to be
6339 * redrawn. For UTF-8 this isn't needed, because comparing
6340 * ScreenLinesUC[] is sufficient. */
6341 if (char_cells == 1
6342 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006343 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344 {
6345 /* Writing a single-cell character over a double-cell
6346 * character: need to redraw the next cell. */
6347 ScreenLines[off_to + 1] = 0;
6348 redraw_next = TRUE;
6349 }
6350 else if (char_cells == 2
6351 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006352 && (*mb_off2cells)(off_to, max_off_to) == 1
6353 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354 {
6355 /* Writing the second half of a double-cell character over
6356 * a double-cell character: need to redraw the second
6357 * cell. */
6358 ScreenLines[off_to + 2] = 0;
6359 redraw_next = TRUE;
6360 }
6361
6362 if (enc_dbcs == DBCS_JPNU)
6363 ScreenLines2[off_to] = ScreenLines2[off_from];
6364 }
6365 /* When writing a single-width character over a double-width
6366 * character and at the end of the redrawn text, need to clear out
6367 * the right halve of the old character.
6368 * Also required when writing the right halve of a double-width
6369 * char over the left halve of an existing one. */
6370 if (has_mbyte && col + char_cells == endcol
6371 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006372 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006374 && (*mb_off2cells)(off_to, max_off_to) == 1
6375 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376 clear_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377
6378 ScreenLines[off_to] = ScreenLines[off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379 if (enc_utf8)
6380 {
6381 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6382 if (ScreenLinesUC[off_from] != 0)
6383 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006384 int i;
6385
6386 for (i = 0; i < Screen_mco; ++i)
6387 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388 }
6389 }
6390 if (char_cells == 2)
6391 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392
6393#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006394 /* The bold trick makes a single column of pixels appear in the
6395 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396 * character should be redrawn too. This happens for our own GUI
6397 * and for some xterms. */
6398 if (
6399# ifdef FEAT_GUI
6400 gui.in_use
6401# endif
6402# if defined(FEAT_GUI) && defined(UNIX)
6403 ||
6404# endif
6405# ifdef UNIX
6406 term_is_xterm
6407# endif
6408 )
6409 {
6410 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006411 if (hl > HL_ALL)
6412 hl = syn_attr2attr(hl);
6413 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414 redraw_next = TRUE;
6415 }
6416#endif
6417 ScreenAttrs[off_to] = ScreenAttrs[off_from];
Bram Moolenaara12a1612019-01-24 16:39:02 +01006418
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006419 /* For simplicity set the attributes of second half of a
6420 * double-wide character equal to the first half. */
6421 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006423
6424 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427 screen_char(off_to, row, col + coloff);
6428 }
6429 else if ( p_wiv
6430#ifdef FEAT_GUI
6431 && !gui.in_use
6432#endif
6433 && col + coloff > 0)
6434 {
6435 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6436 {
6437 /*
6438 * Don't output stop-highlight when moving the cursor, it will
6439 * stop the highlighting when it should continue.
6440 */
6441 screen_attr = 0;
6442 }
6443 else if (screen_attr != 0)
6444 screen_stop_highlight();
6445 }
6446
6447 off_to += CHAR_CELLS;
6448 off_from += CHAR_CELLS;
6449 col += CHAR_CELLS;
6450 }
6451
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452 if (clear_next)
6453 {
6454 /* Clear the second half of a double-wide character of which the left
6455 * half was overwritten with a single-wide character. */
6456 ScreenLines[off_to] = ' ';
6457 if (enc_utf8)
6458 ScreenLinesUC[off_to] = 0;
6459 screen_char(off_to, row, col + coloff);
6460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461
6462 if (clear_width > 0
6463#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006464 && !(flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465#endif
6466 )
6467 {
6468#ifdef FEAT_GUI
6469 int startCol = col;
6470#endif
6471
6472 /* blank out the rest of the line */
6473 while (col < clear_width && ScreenLines[off_to] == ' '
6474 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006475 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 {
6477 ++off_to;
6478 ++col;
6479 }
6480 if (col < clear_width)
6481 {
6482#ifdef FEAT_GUI
6483 /*
6484 * In the GUI, clearing the rest of the line may leave pixels
6485 * behind if the first character cleared was bold. Some bold
6486 * fonts spill over the left. In this case we redraw the previous
6487 * character too. If we didn't skip any blanks above, then we
6488 * only redraw if the character wasn't already redrawn anyway.
6489 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006490 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491 {
6492 hl = ScreenAttrs[off_to];
6493 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006494 {
6495 int prev_cells = 1;
Bram Moolenaara12a1612019-01-24 16:39:02 +01006496
Bram Moolenaar9c697322006-10-09 20:11:17 +00006497 if (enc_utf8)
6498 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6499 * that its width is 2. */
6500 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6501 else if (enc_dbcs != 0)
6502 {
6503 /* find previous character by counting from first
6504 * column and get its width. */
6505 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006506 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006507
6508 while (off < off_to)
6509 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006510 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006511 off += prev_cells;
6512 }
6513 }
6514
6515 if (enc_dbcs != 0 && prev_cells > 1)
6516 screen_char_2(off_to - prev_cells, row,
6517 col + coloff - prev_cells);
6518 else
Bram Moolenaar9c697322006-10-09 20:11:17 +00006519 screen_char(off_to - prev_cells, row,
6520 col + coloff - prev_cells);
6521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522 }
6523#endif
6524 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6525 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526 off_to += clear_width - col;
6527 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528 }
6529 }
6530
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006531 if (clear_width > 0
6532#ifdef FEAT_TEXT_PROP
6533 && !(flags & SLF_POPUP) // no separator for popup window
6534#endif
6535 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006537 // For a window that has a right neighbor, draw the separator char
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006538 // right of the window contents. But not on top of a popup window.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006539 if (coloff + col < Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006541#ifdef FEAT_TEXT_PROP
Bram Moolenaarc662ec92019-06-23 00:15:57 +02006542 if (!blocked_by_popup(row, col + coloff))
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006543#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006545 int c;
6546
6547 c = fillchar_vsep(&hl);
6548 if (ScreenLines[off_to] != (schar_T)c
6549 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6550 != (c >= 0x80 ? c : 0))
6551 || ScreenAttrs[off_to] != hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006553 ScreenLines[off_to] = c;
6554 ScreenAttrs[off_to] = hl;
6555 if (enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006557 if (c >= 0x80)
6558 {
6559 ScreenLinesUC[off_to] = c;
6560 ScreenLinesC[0][off_to] = 0;
6561 }
6562 else
6563 ScreenLinesUC[off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564 }
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006565 screen_char(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567 }
6568 }
6569 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570 LineWraps[row] = FALSE;
6571 }
6572}
6573
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006574#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006576 * Mirror text "str" for right-left displaying.
6577 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006579 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006580rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581{
6582 char_u *p1, *p2;
6583 int t;
6584
6585 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6586 {
6587 t = *p1;
6588 *p1 = *p2;
6589 *p2 = t;
6590 }
6591}
6592#endif
6593
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594/*
6595 * mark all status lines for redraw; used after first :cd
6596 */
6597 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006598status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599{
6600 win_T *wp;
6601
Bram Moolenaar29323592016-07-24 22:04:11 +02006602 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603 if (wp->w_status_height)
6604 {
6605 wp->w_redr_status = TRUE;
6606 redraw_later(VALID);
6607 }
6608}
6609
6610/*
6611 * mark all status lines of the current buffer for redraw
6612 */
6613 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006614status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615{
6616 win_T *wp;
6617
Bram Moolenaar29323592016-07-24 22:04:11 +02006618 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6620 {
6621 wp->w_redr_status = TRUE;
6622 redraw_later(VALID);
6623 }
6624}
6625
6626/*
6627 * Redraw all status lines that need to be redrawn.
6628 */
6629 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006630redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631{
6632 win_T *wp;
6633
Bram Moolenaar29323592016-07-24 22:04:11 +02006634 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006636 win_redr_status(wp, FALSE);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006637 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006638 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640
Bram Moolenaar4033c552017-09-16 20:54:51 +02006641#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642/*
6643 * Redraw all status lines at the bottom of frame "frp".
6644 */
6645 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006646win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647{
6648 if (frp->fr_layout == FR_LEAF)
6649 frp->fr_win->w_redr_status = TRUE;
6650 else if (frp->fr_layout == FR_ROW)
6651 {
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006652 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 win_redraw_last_status(frp);
6654 }
6655 else /* frp->fr_layout == FR_COL */
6656 {
6657 frp = frp->fr_child;
6658 while (frp->fr_next != NULL)
6659 frp = frp->fr_next;
6660 win_redraw_last_status(frp);
6661 }
6662}
6663#endif
6664
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665/*
6666 * Draw the verticap separator right of window "wp" starting with line "row".
6667 */
6668 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006669draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670{
6671 int hl;
6672 int c;
6673
6674 if (wp->w_vsep_width)
6675 {
6676 /* draw the vertical separator right of this window */
6677 c = fillchar_vsep(&hl);
6678 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6679 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6680 c, ' ', hl);
6681 }
6682}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683
6684#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006685static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686
6687/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006688 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689 */
6690 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006691status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692{
6693 int len = 0;
6694
6695#ifdef FEAT_MENU
6696 int emenu = (xp->xp_context == EXPAND_MENUS
6697 || xp->xp_context == EXPAND_MENUNAMES);
6698
6699 /* Check for menu separators - replace with '|'. */
6700 if (emenu && menu_is_separator(s))
6701 return 1;
6702#endif
6703
6704 while (*s != NUL)
6705 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006706 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006707 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006708 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709 }
6710
6711 return len;
6712}
6713
6714/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006715 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006716 * These are backslashes used for escaping. Do show backslashes in help tags.
6717 */
6718 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006719skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006720{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006721 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006722#ifdef FEAT_MENU
6723 || ((xp->xp_context == EXPAND_MENUS
6724 || xp->xp_context == EXPAND_MENUNAMES)
6725 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6726#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006727 )
6728 {
6729#ifndef BACKSLASH_IN_FILENAME
6730 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6731 return 2;
6732#endif
6733 return 1;
6734 }
6735 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006736}
6737
6738/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 * Show wildchar matches in the status line.
6740 * Show at least the "match" item.
6741 * We start at item 'first_match' in the list and show all matches that fit.
6742 *
6743 * If inversion is possible we use it. Else '=' characters are used.
6744 */
6745 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006746win_redr_status_matches(
6747 expand_T *xp,
6748 int num_matches,
6749 char_u **matches, /* list of matches */
6750 int match,
6751 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752{
6753#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6754 int row;
6755 char_u *buf;
6756 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006757 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758 int fillchar;
6759 int attr;
6760 int i;
6761 int highlight = TRUE;
6762 char_u *selstart = NULL;
6763 int selstart_col = 0;
6764 char_u *selend = NULL;
6765 static int first_match = 0;
6766 int add_left = FALSE;
6767 char_u *s;
6768#ifdef FEAT_MENU
6769 int emenu;
6770#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772
6773 if (matches == NULL) /* interrupted completion? */
6774 return;
6775
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006776 if (has_mbyte)
Bram Moolenaar964b3742019-05-24 18:54:09 +02006777 buf = alloc(Columns * MB_MAXBYTES + 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006778 else
Bram Moolenaar964b3742019-05-24 18:54:09 +02006779 buf = alloc(Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780 if (buf == NULL)
6781 return;
6782
6783 if (match == -1) /* don't show match but original text */
6784 {
6785 match = 0;
6786 highlight = FALSE;
6787 }
6788 /* count 1 for the ending ">" */
6789 clen = status_match_len(xp, L_MATCH(match)) + 3;
6790 if (match == 0)
6791 first_match = 0;
6792 else if (match < first_match)
6793 {
6794 /* jumping left, as far as we can go */
6795 first_match = match;
6796 add_left = TRUE;
6797 }
6798 else
6799 {
6800 /* check if match fits on the screen */
6801 for (i = first_match; i < match; ++i)
6802 clen += status_match_len(xp, L_MATCH(i)) + 2;
6803 if (first_match > 0)
6804 clen += 2;
6805 /* jumping right, put match at the left */
6806 if ((long)clen > Columns)
6807 {
6808 first_match = match;
6809 /* if showing the last match, we can add some on the left */
6810 clen = 2;
6811 for (i = match; i < num_matches; ++i)
6812 {
6813 clen += status_match_len(xp, L_MATCH(i)) + 2;
6814 if ((long)clen >= Columns)
6815 break;
6816 }
6817 if (i == num_matches)
6818 add_left = TRUE;
6819 }
6820 }
6821 if (add_left)
6822 while (first_match > 0)
6823 {
6824 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6825 if ((long)clen >= Columns)
6826 break;
6827 --first_match;
6828 }
6829
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006830 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831
6832 if (first_match == 0)
6833 {
6834 *buf = NUL;
6835 len = 0;
6836 }
6837 else
6838 {
6839 STRCPY(buf, "< ");
6840 len = 2;
6841 }
6842 clen = len;
6843
6844 i = first_match;
6845 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6846 {
6847 if (i == match)
6848 {
6849 selstart = buf + len;
6850 selstart_col = clen;
6851 }
6852
6853 s = L_MATCH(i);
6854 /* Check for menu separators - replace with '|' */
6855#ifdef FEAT_MENU
6856 emenu = (xp->xp_context == EXPAND_MENUS
6857 || xp->xp_context == EXPAND_MENUNAMES);
6858 if (emenu && menu_is_separator(s))
6859 {
6860 STRCPY(buf + len, transchar('|'));
6861 l = (int)STRLEN(buf + len);
6862 len += l;
6863 clen += l;
6864 }
6865 else
6866#endif
6867 for ( ; *s != NUL; ++s)
6868 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006869 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 clen += ptr2cells(s);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006871 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872 {
6873 STRNCPY(buf + len, s, l);
6874 s += l - 1;
6875 len += l;
6876 }
6877 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878 {
6879 STRCPY(buf + len, transchar_byte(*s));
6880 len += (int)STRLEN(buf + len);
6881 }
6882 }
6883 if (i == match)
6884 selend = buf + len;
6885
6886 *(buf + len++) = ' ';
6887 *(buf + len++) = ' ';
6888 clen += 2;
6889 if (++i == num_matches)
6890 break;
6891 }
6892
6893 if (i != num_matches)
6894 {
6895 *(buf + len++) = '>';
6896 ++clen;
6897 }
6898
6899 buf[len] = NUL;
6900
6901 row = cmdline_row - 1;
6902 if (row >= 0)
6903 {
6904 if (wild_menu_showing == 0)
6905 {
6906 if (msg_scrolled > 0)
6907 {
6908 /* Put the wildmenu just above the command line. If there is
6909 * no room, scroll the screen one line up. */
6910 if (cmdline_row == Rows - 1)
6911 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02006912 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913 ++msg_scrolled;
6914 }
6915 else
6916 {
6917 ++cmdline_row;
6918 ++row;
6919 }
6920 wild_menu_showing = WM_SCROLLED;
6921 }
6922 else
6923 {
6924 /* Create status line if needed by setting 'laststatus' to 2.
6925 * Set 'winminheight' to zero to avoid that the window is
6926 * resized. */
6927 if (lastwin->w_status_height == 0)
6928 {
6929 save_p_ls = p_ls;
6930 save_p_wmh = p_wmh;
6931 p_ls = 2;
6932 p_wmh = 0;
6933 last_status(FALSE);
6934 }
6935 wild_menu_showing = WM_SHOWN;
6936 }
6937 }
6938
6939 screen_puts(buf, row, 0, attr);
6940 if (selstart != NULL && highlight)
6941 {
6942 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006943 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944 }
6945
6946 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6947 }
6948
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950 vim_free(buf);
6951}
6952#endif
6953
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954/*
6955 * Redraw the status line of window wp.
6956 *
6957 * If inversion is possible we use it. Else '=' characters are used.
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006958 * If "ignore_pum" is TRUE, also redraw statusline when the popup menu is
6959 * displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 */
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006961 static void
Bram Moolenaar829adb72018-06-24 19:24:03 +02006962win_redr_status(win_T *wp, int ignore_pum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963{
6964 int row;
6965 char_u *p;
6966 int len;
6967 int fillchar;
6968 int attr;
6969 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006970 static int busy = FALSE;
6971
6972 /* It's possible to get here recursively when 'statusline' (indirectly)
6973 * invokes ":redrawstatus". Simply ignore the call then. */
6974 if (busy)
6975 return;
6976 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977
6978 wp->w_redr_status = FALSE;
6979 if (wp->w_status_height == 0)
6980 {
6981 /* no status line, can only be last window */
6982 redraw_cmdline = TRUE;
6983 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006984 else if (!redrawing()
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006985 // don't update status line when popup menu is visible and may be
6986 // drawn over it, unless it will be redrawn later
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006987 || (!ignore_pum && pum_visible()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 {
6989 /* Don't redraw right now, do it later. */
6990 wp->w_redr_status = TRUE;
6991 }
6992#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006993 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 {
6995 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006996 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997 }
6998#endif
6999 else
7000 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007001 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007003 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 p = NameBuff;
7005 len = (int)STRLEN(p);
7006
Bram Moolenaard85f2712017-07-28 21:51:57 +02007007 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008#ifdef FEAT_QUICKFIX
7009 || wp->w_p_pvw
7010#endif
7011 || bufIsChanged(wp->w_buffer)
7012 || wp->w_buffer->b_p_ro)
7013 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02007014 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00007016 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017 len += (int)STRLEN(p + len);
7018 }
7019#ifdef FEAT_QUICKFIX
7020 if (wp->w_p_pvw)
7021 {
7022 STRCPY(p + len, _("[Preview]"));
7023 len += (int)STRLEN(p + len);
7024 }
7025#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02007026 if (bufIsChanged(wp->w_buffer)
7027#ifdef FEAT_TERMINAL
7028 && !bt_terminal(wp->w_buffer)
7029#endif
7030 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 {
7032 STRCPY(p + len, "[+]");
7033 len += 3;
7034 }
7035 if (wp->w_buffer->b_p_ro)
7036 {
Bram Moolenaar23584032013-06-07 20:17:11 +02007037 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01007038 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039 }
7040
Bram Moolenaar02631462017-09-22 15:20:32 +02007041 this_ru_col = ru_col - (Columns - wp->w_width);
7042 if (this_ru_col < (wp->w_width + 1) / 2)
7043 this_ru_col = (wp->w_width + 1) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 if (this_ru_col <= 1)
7045 {
7046 p = (char_u *)"<"; /* No room for file name! */
7047 len = 1;
7048 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01007049 else if (has_mbyte)
7050 {
7051 int clen = 0, i;
7052
7053 /* Count total number of display cells. */
7054 clen = mb_string2cells(p, -1);
7055
7056 /* Find first character that will fit.
7057 * Going from start to end is much faster for DBCS. */
7058 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
7059 i += (*mb_ptr2len)(p + i))
7060 clen -= (*mb_ptr2cells)(p + i);
7061 len = clen;
7062 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063 {
Bram Moolenaara12a1612019-01-24 16:39:02 +01007064 p = p + i - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 *p = '<';
Bram Moolenaara12a1612019-01-24 16:39:02 +01007066 ++len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 }
7068
Bram Moolenaara12a1612019-01-24 16:39:02 +01007069 }
7070 else if (len > this_ru_col - 1)
7071 {
7072 p += len - (this_ru_col - 1);
7073 *p = '<';
7074 len = this_ru_col - 1;
7075 }
7076
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar53f81742017-09-22 14:35:51 +02007078 screen_puts(p, row, wp->w_wincol, attr);
7079 screen_fill(row, row + 1, len + wp->w_wincol,
7080 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007082 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
7084 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007085 - 1 + wp->w_wincol), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086
7087#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +02007088 win_redr_ruler(wp, TRUE, ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089#endif
7090 }
7091
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092 /*
7093 * May need to draw the character below the vertical separator.
7094 */
7095 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
7096 {
7097 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007098 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 else
7100 fillchar = fillchar_vsep(&attr);
7101 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
7102 attr);
7103 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007104 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105}
7106
Bram Moolenaar238a5642006-02-21 22:12:05 +00007107#ifdef FEAT_STL_OPT
7108/*
7109 * Redraw the status line according to 'statusline' and take care of any
7110 * errors encountered.
7111 */
7112 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007113redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00007114{
Bram Moolenaar362f3562009-11-03 16:20:34 +00007115 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02007116 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007117
7118 /* When called recursively return. This can happen when the statusline
7119 * contains an expression that triggers a redraw. */
7120 if (entered)
7121 return;
7122 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007123
Bram Moolenaara742e082016-04-05 21:10:38 +02007124 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007125 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02007126 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007127 {
7128 /* When there is an error disable the statusline, otherwise the
7129 * display is messed up with errors and a redraw triggers the problem
7130 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007131 set_string_option_direct((char_u *)"statusline", -1,
7132 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007133 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007134 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007135 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007136 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007137}
7138#endif
7139
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140/*
7141 * Return TRUE if the status line of window "wp" is connected to the status
7142 * line of the window right of it. If not, then it's a vertical separator.
7143 * Only call if (wp->w_vsep_width != 0).
7144 */
7145 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007146stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147{
7148 frame_T *fr;
7149
7150 fr = wp->w_frame;
7151 while (fr->fr_parent != NULL)
7152 {
7153 if (fr->fr_parent->fr_layout == FR_COL)
7154 {
7155 if (fr->fr_next != NULL)
7156 break;
7157 }
7158 else
7159 {
7160 if (fr->fr_next != NULL)
7161 return TRUE;
7162 }
7163 fr = fr->fr_parent;
7164 }
7165 return FALSE;
7166}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169/*
7170 * Get the value to show for the language mappings, active 'keymap'.
7171 */
7172 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007173get_keymap_str(
7174 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007175 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007176 char_u *buf, /* buffer for the result */
7177 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178{
7179 char_u *p;
7180
7181 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7182 return FALSE;
7183
7184 {
7185#ifdef FEAT_EVAL
7186 buf_T *old_curbuf = curbuf;
7187 win_T *old_curwin = curwin;
7188 char_u *s;
7189
7190 curbuf = wp->w_buffer;
7191 curwin = wp;
7192 STRCPY(buf, "b:keymap_name"); /* must be writable */
7193 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007194 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195 --emsg_skip;
7196 curbuf = old_curbuf;
7197 curwin = old_curwin;
7198 if (p == NULL || *p == NUL)
7199#endif
7200 {
7201#ifdef FEAT_KEYMAP
7202 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7203 p = wp->w_buffer->b_p_keymap;
7204 else
7205#endif
7206 p = (char_u *)"lang";
7207 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007208 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 buf[0] = NUL;
7210#ifdef FEAT_EVAL
7211 vim_free(s);
7212#endif
7213 }
7214 return buf[0] != NUL;
7215}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216
7217#if defined(FEAT_STL_OPT) || defined(PROTO)
7218/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007219 * Redraw the status line or ruler of window "wp".
7220 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 */
7222 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007223win_redr_custom(
7224 win_T *wp,
7225 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007227 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228 int attr;
7229 int curattr;
7230 int row;
7231 int col = 0;
7232 int maxwidth;
7233 int width;
7234 int n;
7235 int len;
7236 int fillchar;
7237 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007238 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007240 struct stl_hlrec hltab[STL_MAX_ITEM];
7241 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007242 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007243 win_T *ewp;
7244 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245
Bram Moolenaar1d633412013-12-11 15:52:01 +01007246 /* There is a tiny chance that this gets called recursively: When
7247 * redrawing a status line triggers redrawing the ruler or tabline.
7248 * Avoid trouble by not allowing recursion. */
7249 if (entered)
7250 return;
7251 entered = TRUE;
7252
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007254 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007256 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007257 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007258 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007259 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007260 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007261 maxwidth = Columns;
7262# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007263 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007264# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007266 else
7267 {
7268 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007269 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02007270 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007271
7272 if (draw_ruler)
7273 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007274 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007275 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007276 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007277 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007278 if (*++stl == '-')
7279 stl++;
7280 if (atoi((char *)stl))
7281 while (VIM_ISDIGIT(*stl))
7282 stl++;
7283 if (*stl++ != '(')
7284 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007285 }
Bram Moolenaar02631462017-09-22 15:20:32 +02007286 col = ru_col - (Columns - wp->w_width);
7287 if (col < (wp->w_width + 1) / 2)
7288 col = (wp->w_width + 1) / 2;
7289 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007290 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007291 {
7292 row = Rows - 1;
7293 --maxwidth; /* writing in last column may cause scrolling */
7294 fillchar = ' ';
7295 attr = 0;
7296 }
7297
7298# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007299 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007300# endif
7301 }
7302 else
7303 {
7304 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007305 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007306 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007307 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007308# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007309 use_sandbox = was_set_insecurely((char_u *)"statusline",
7310 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007311# endif
7312 }
7313
Bram Moolenaar53f81742017-09-22 14:35:51 +02007314 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007315 }
7316
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007318 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319
Bram Moolenaar61452852011-02-01 18:01:11 +01007320 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7321 * the cursor away and back. */
7322 ewp = wp == NULL ? curwin : wp;
7323 p_crb_save = ewp->w_p_crb;
7324 ewp->w_p_crb = FALSE;
7325
Bram Moolenaar362f3562009-11-03 16:20:34 +00007326 /* Make a copy, because the statusline may include a function call that
7327 * might change the option value and free the memory. */
7328 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007329 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007330 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007331 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007332 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007333 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007335 /* Make all characters printable. */
7336 p = transstr(buf);
7337 if (p != NULL)
7338 {
7339 vim_strncpy(buf, p, sizeof(buf) - 1);
7340 vim_free(p);
7341 }
7342
7343 /* fill up with "fillchar" */
7344 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007345 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347 len += (*mb_char2bytes)(fillchar, buf + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348 ++width;
7349 }
7350 buf[len] = NUL;
7351
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007352 /*
7353 * Draw each snippet with the specified highlighting.
7354 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 curattr = attr;
7356 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007357 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007359 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360 screen_puts_len(p, len, row, col, curattr);
7361 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007362 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007364 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007366 else if (hltab[n].userhl < 0)
7367 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007368#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007369 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7370 && wp->w_status_height != 0)
7371 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007372 else if (wp != NULL && bt_terminal(wp->w_buffer)
7373 && wp->w_status_height != 0)
7374 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02007375#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007376 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007377 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007379 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 }
7381 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007382
7383 if (wp == NULL)
7384 {
7385 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7386 col = 0;
7387 len = 0;
7388 p = buf;
7389 fillchar = 0;
7390 for (n = 0; tabtab[n].start != NULL; n++)
7391 {
7392 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7393 while (col < len)
7394 TabPageIdxs[col++] = fillchar;
7395 p = tabtab[n].start;
7396 fillchar = tabtab[n].userhl;
7397 }
7398 while (col < Columns)
7399 TabPageIdxs[col++] = fillchar;
7400 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007401
7402theend:
7403 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404}
7405
7406#endif /* FEAT_STL_OPT */
7407
7408/*
7409 * Output a single character directly to the screen and update ScreenLines.
7410 */
7411 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007412screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414 char_u buf[MB_MAXBYTES + 1];
7415
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007416 if (has_mbyte)
7417 buf[(*mb_char2bytes)(c, buf)] = NUL;
7418 else
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007419 {
7420 buf[0] = c;
7421 buf[1] = NUL;
7422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423 screen_puts(buf, row, col, attr);
7424}
7425
7426/*
7427 * Get a single character directly from ScreenLines into "bytes[]".
7428 * Also return its attribute in *attrp;
7429 */
7430 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007431screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432{
7433 unsigned off;
7434
7435 /* safety check */
7436 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7437 {
7438 off = LineOffset[row] + col;
7439 *attrp = ScreenAttrs[off];
7440 bytes[0] = ScreenLines[off];
7441 bytes[1] = NUL;
7442
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443 if (enc_utf8 && ScreenLinesUC[off] != 0)
7444 bytes[utfc_char2bytes(off, bytes)] = NUL;
7445 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7446 {
7447 bytes[0] = ScreenLines[off];
7448 bytes[1] = ScreenLines2[off];
7449 bytes[2] = NUL;
7450 }
7451 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7452 {
7453 bytes[1] = ScreenLines[off + 1];
7454 bytes[2] = NUL;
7455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 }
7457}
7458
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007459/*
7460 * Return TRUE if composing characters for screen posn "off" differs from
7461 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007462 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007463 */
7464 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007465screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007466{
7467 int i;
7468
7469 for (i = 0; i < Screen_mco; ++i)
7470 {
7471 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7472 return TRUE;
7473 if (u8cc[i] == 0)
7474 break;
7475 }
7476 return FALSE;
7477}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007478
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479/*
7480 * Put string '*text' on the screen at position 'row' and 'col', with
7481 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7482 * Note: only outputs within one row, message is truncated at screen boundary!
7483 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7484 */
7485 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007486screen_puts(
7487 char_u *text,
7488 int row,
7489 int col,
7490 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491{
7492 screen_puts_len(text, -1, row, col, attr);
7493}
7494
7495/*
7496 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7497 * a NUL.
7498 */
7499 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007500screen_puts_len(
7501 char_u *text,
7502 int textlen,
7503 int row,
7504 int col,
7505 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506{
7507 unsigned off;
7508 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007509 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510 int c;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007511 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512 int mbyte_blen = 1;
7513 int mbyte_cells = 1;
7514 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007515 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516 int clear_next_cell = FALSE;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007517#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007519 int pc, nc, nc1;
7520 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007522 int force_redraw_this;
7523 int force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007524 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525
Bram Moolenaar0b4c9ed2019-06-03 22:04:23 +02007526 // Safety check. The check for negative row and column is to fix issue
7527 // #4102. TODO: find out why row/col could be negative.
7528 if (ScreenLines == NULL
7529 || row >= screen_Rows || row < 0
7530 || col >= screen_Columns || col < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007532 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533
Bram Moolenaarc236c162008-07-13 17:41:49 +00007534 /* When drawing over the right halve of a double-wide char clear out the
7535 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007536 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaara12a1612019-01-24 16:39:02 +01007537#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00007538 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01007539#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007540 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007541 {
7542 ScreenLines[off - 1] = ' ';
7543 ScreenAttrs[off - 1] = 0;
7544 if (enc_utf8)
7545 {
7546 ScreenLinesUC[off - 1] = 0;
7547 ScreenLinesC[0][off - 1] = 0;
7548 }
7549 /* redraw the previous cell, make it empty */
7550 screen_char(off - 1, row, col - 1);
7551 /* force the cell at "col" to be redrawn */
7552 force_redraw_next = TRUE;
7553 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007554
Bram Moolenaar367329b2007-08-30 11:53:22 +00007555 max_off = LineOffset[row] + screen_Columns;
Bram Moolenaara064ac82007-08-05 18:10:54 +00007556 while (col < screen_Columns
7557 && (len < 0 || (int)(ptr - text) < len)
7558 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559 {
7560 c = *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561 /* check if this is the first byte of a multibyte */
7562 if (has_mbyte)
7563 {
7564 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007565 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007566 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007567 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7569 mbyte_cells = 1;
7570 else if (enc_dbcs != 0)
7571 mbyte_cells = mbyte_blen;
7572 else /* enc_utf8 */
7573 {
7574 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007575 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576 (int)((text + len) - ptr));
7577 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007578 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007580#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7582 {
7583 /* Do Arabic shaping. */
7584 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7585 {
7586 /* Past end of string to be displayed. */
7587 nc = NUL;
7588 nc1 = NUL;
7589 }
7590 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007591 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007592 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7593 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007594 nc1 = pcc[0];
7595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596 pc = prev_c;
7597 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007598 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599 }
7600 else
7601 prev_c = u8c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007602#endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007603 if (col + mbyte_cells > screen_Columns)
7604 {
7605 /* Only 1 cell left, but character requires 2 cells:
7606 * display a '>' in the last column to avoid wrapping. */
7607 c = '>';
7608 mbyte_cells = 1;
7609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610 }
7611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007613 force_redraw_this = force_redraw_next;
7614 force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007615
7616 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617 || (mbyte_cells == 2
7618 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7619 || (enc_dbcs == DBCS_JPNU
7620 && c == 0x8e
7621 && ScreenLines2[off] != ptr[1])
7622 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007623 && (ScreenLinesUC[off] !=
7624 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7625 || (ScreenLinesUC[off] != 0
7626 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007628 || exmode_active;
7629
Bram Moolenaar24a5ac52019-06-08 19:01:18 +02007630 if ((need_redraw || force_redraw_this)
7631#ifdef FEAT_TEXT_PROP
Bram Moolenaarc662ec92019-06-23 00:15:57 +02007632 && !blocked_by_popup(row, col)
Bram Moolenaar24a5ac52019-06-08 19:01:18 +02007633#endif
7634 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635 {
7636#if defined(FEAT_GUI) || defined(UNIX)
7637 /* The bold trick makes a single row of pixels appear in the next
7638 * character. When a bold character is removed, the next
7639 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007640 * and for some xterms. */
7641 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642# ifdef FEAT_GUI
7643 gui.in_use
7644# endif
7645# if defined(FEAT_GUI) && defined(UNIX)
7646 ||
7647# endif
7648# ifdef UNIX
7649 term_is_xterm
7650# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007651 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007653 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007655 if (n > HL_ALL)
7656 n = syn_attr2attr(n);
7657 if (n & HL_BOLD)
7658 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659 }
7660#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661 /* When at the end of the text and overwriting a two-cell
7662 * character with a one-cell character, need to clear the next
7663 * cell. Also when overwriting the left halve of a two-cell char
7664 * with the right halve of a two-cell char. Do this only once
7665 * (mb_off2cells() may return 2 on the right halve). */
7666 if (clear_next_cell)
7667 clear_next_cell = FALSE;
7668 else if (has_mbyte
7669 && (len < 0 ? ptr[mbyte_blen] == NUL
7670 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007671 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007673 && (*mb_off2cells)(off, max_off) == 1
7674 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675 clear_next_cell = TRUE;
7676
7677 /* Make sure we never leave a second byte of a double-byte behind,
7678 * it confuses mb_off2cells(). */
7679 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007680 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007682 && (*mb_off2cells)(off, max_off) == 1
7683 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 ScreenLines[off + mbyte_blen] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685 ScreenLines[off] = c;
7686 ScreenAttrs[off] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687 if (enc_utf8)
7688 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007689 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 ScreenLinesUC[off] = 0;
7691 else
7692 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007693 int i;
7694
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007696 for (i = 0; i < Screen_mco; ++i)
7697 {
7698 ScreenLinesC[i][off] = u8cc[i];
7699 if (u8cc[i] == 0)
7700 break;
7701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702 }
7703 if (mbyte_cells == 2)
7704 {
7705 ScreenLines[off + 1] = 0;
7706 ScreenAttrs[off + 1] = attr;
7707 }
7708 screen_char(off, row, col);
7709 }
7710 else if (mbyte_cells == 2)
7711 {
7712 ScreenLines[off + 1] = ptr[1];
7713 ScreenAttrs[off + 1] = attr;
7714 screen_char_2(off, row, col);
7715 }
7716 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7717 {
7718 ScreenLines2[off] = ptr[1];
7719 screen_char(off, row, col);
7720 }
7721 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722 screen_char(off, row, col);
7723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724 if (has_mbyte)
7725 {
7726 off += mbyte_cells;
7727 col += mbyte_cells;
7728 ptr += mbyte_blen;
7729 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007730 {
7731 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007733 len = -1;
7734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735 }
7736 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 {
7738 ++off;
7739 ++col;
7740 ++ptr;
7741 }
7742 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007743
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007744 /* If we detected the next character needs to be redrawn, but the text
7745 * doesn't extend up to there, update the character here. */
7746 if (force_redraw_next && col < screen_Columns)
7747 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007748 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7749 screen_char_2(off, row, col);
7750 else
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007751 screen_char(off, row, col);
7752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753}
7754
7755#ifdef FEAT_SEARCH_EXTRA
7756/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007757 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758 */
7759 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007760start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761{
7762 if (p_hls && !no_hlsearch)
7763 {
7764 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007765 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007766# ifdef FEAT_RELTIME
7767 /* Set the time limit to 'redrawtime'. */
7768 profile_setlimit(p_rdt, &search_hl.tm);
7769# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770 }
7771}
7772
7773/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007774 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775 */
7776 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007777end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778{
7779 if (search_hl.rm.regprog != NULL)
7780 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007781 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782 search_hl.rm.regprog = NULL;
7783 }
7784}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02007785#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02007786
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007788screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789{
7790 attrentry_T *aep = NULL;
7791
7792 screen_attr = attr;
7793 if (full_screen
Bram Moolenaar4f974752019-02-17 17:44:42 +01007794#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795 && termcap_active
7796#endif
7797 )
7798 {
7799#ifdef FEAT_GUI
7800 if (gui.in_use)
7801 {
7802 char buf[20];
7803
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007804 /* The GUI handles this internally. */
7805 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806 OUT_STR(buf);
7807 }
7808 else
7809#endif
7810 {
7811 if (attr > HL_ALL) /* special HL attr. */
7812 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007813 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814 aep = syn_cterm_attr2entry(attr);
7815 else
7816 aep = syn_term_attr2entry(attr);
7817 if (aep == NULL) /* did ":syntax clear" */
7818 attr = 0;
7819 else
7820 attr = aep->ae_attr;
7821 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01007822 if ((attr & HL_BOLD) && *T_MD != NUL) /* bold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007824 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007825#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007826 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
7827 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
7828 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007829#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007830 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007831 /* If the Normal FG color has BOLD attribute and the new HL
7832 * has a FG color defined, clear BOLD. */
7833 out_str(T_ME);
Bram Moolenaar45a00002017-12-22 21:12:34 +01007834 if ((attr & HL_STANDOUT) && *T_SO != NUL) /* standout */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835 out_str(T_SO);
Bram Moolenaar45a00002017-12-22 21:12:34 +01007836 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) /* undercurl */
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01007837 out_str(T_UCS);
7838 if (((attr & HL_UNDERLINE) /* underline or undercurl */
Bram Moolenaar45a00002017-12-22 21:12:34 +01007839 || ((attr & HL_UNDERCURL) && *T_UCS == NUL))
7840 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841 out_str(T_US);
Bram Moolenaar45a00002017-12-22 21:12:34 +01007842 if ((attr & HL_ITALIC) && *T_CZH != NUL) /* italic */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843 out_str(T_CZH);
Bram Moolenaar45a00002017-12-22 21:12:34 +01007844 if ((attr & HL_INVERSE) && *T_MR != NUL) /* inverse (reverse) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845 out_str(T_MR);
Bram Moolenaar45a00002017-12-22 21:12:34 +01007846 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) /* strike */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02007847 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848
7849 /*
7850 * Output the color or start string after bold etc., in case the
7851 * bold etc. override the color setting.
7852 */
7853 if (aep != NULL)
7854 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007855#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007856 /* When 'termguicolors' is set but fg or bg is unset,
7857 * fall back to the cterm colors. This helps for SpellBad,
7858 * where the GUI uses a red undercurl. */
7859 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007861 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007862 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007863 }
7864 else
7865#endif
7866 if (t_colors > 1)
7867 {
7868 if (aep->ae_u.cterm.fg_color)
7869 term_fg_color(aep->ae_u.cterm.fg_color - 1);
7870 }
7871#ifdef FEAT_TERMGUICOLORS
7872 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
7873 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007874 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007875 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876 }
7877 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007878#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007879 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007881 if (aep->ae_u.cterm.bg_color)
7882 term_bg_color(aep->ae_u.cterm.bg_color - 1);
7883 }
7884
Bram Moolenaarf708ac52018-03-12 21:48:32 +01007885 if (!IS_CTERM)
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007886 {
7887 if (aep->ae_u.term.start != NULL)
7888 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889 }
7890 }
7891 }
7892 }
7893}
7894
7895 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007896screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897{
7898 int do_ME = FALSE; /* output T_ME code */
7899
7900 if (screen_attr != 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01007901#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902 && termcap_active
7903#endif
7904 )
7905 {
7906#ifdef FEAT_GUI
7907 if (gui.in_use)
7908 {
7909 char buf[20];
7910
7911 /* use internal GUI code */
7912 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
7913 OUT_STR(buf);
7914 }
7915 else
7916#endif
7917 {
7918 if (screen_attr > HL_ALL) /* special HL attr. */
7919 {
7920 attrentry_T *aep;
7921
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007922 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923 {
7924 /*
7925 * Assume that t_me restores the original colors!
7926 */
7927 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007928 if (aep != NULL && ((
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.fg_rgb != CTERMCOLOR
7931 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
7932 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007933#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007934 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007935#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007936 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
7937 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
7938 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007939#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007940 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941 do_ME = TRUE;
7942 }
7943 else
7944 {
7945 aep = syn_term_attr2entry(screen_attr);
7946 if (aep != NULL && aep->ae_u.term.stop != NULL)
7947 {
7948 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
7949 do_ME = TRUE;
7950 else
7951 out_str(aep->ae_u.term.stop);
7952 }
7953 }
7954 if (aep == NULL) /* did ":syntax clear" */
7955 screen_attr = 0;
7956 else
7957 screen_attr = aep->ae_attr;
7958 }
7959
7960 /*
7961 * Often all ending-codes are equal to T_ME. Avoid outputting the
7962 * same sequence several times.
7963 */
7964 if (screen_attr & HL_STANDOUT)
7965 {
7966 if (STRCMP(T_SE, T_ME) == 0)
7967 do_ME = TRUE;
7968 else
7969 out_str(T_SE);
7970 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01007971 if ((screen_attr & HL_UNDERCURL) && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01007972 {
7973 if (STRCMP(T_UCE, T_ME) == 0)
7974 do_ME = TRUE;
7975 else
7976 out_str(T_UCE);
7977 }
7978 if ((screen_attr & HL_UNDERLINE)
Bram Moolenaar45a00002017-12-22 21:12:34 +01007979 || ((screen_attr & HL_UNDERCURL) && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980 {
7981 if (STRCMP(T_UE, T_ME) == 0)
7982 do_ME = TRUE;
7983 else
7984 out_str(T_UE);
7985 }
7986 if (screen_attr & HL_ITALIC)
7987 {
7988 if (STRCMP(T_CZR, T_ME) == 0)
7989 do_ME = TRUE;
7990 else
7991 out_str(T_CZR);
7992 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02007993 if (screen_attr & HL_STRIKETHROUGH)
7994 {
7995 if (STRCMP(T_STE, T_ME) == 0)
7996 do_ME = TRUE;
7997 else
7998 out_str(T_STE);
7999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8001 out_str(T_ME);
8002
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008003#ifdef FEAT_TERMGUICOLORS
8004 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008006 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008007 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008008 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008009 term_bg_rgb_color(cterm_normal_bg_gui_color);
8010 }
8011 else
8012#endif
8013 {
8014 if (t_colors > 1)
8015 {
8016 /* set Normal cterm colors */
8017 if (cterm_normal_fg_color != 0)
8018 term_fg_color(cterm_normal_fg_color - 1);
8019 if (cterm_normal_bg_color != 0)
8020 term_bg_color(cterm_normal_bg_color - 1);
8021 if (cterm_normal_fg_bold)
8022 out_str(T_MD);
8023 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024 }
8025 }
8026 }
8027 screen_attr = 0;
8028}
8029
8030/*
8031 * Reset the colors for a cterm. Used when leaving Vim.
8032 * The machine specific code may override this again.
8033 */
8034 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008035reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008037 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008038 {
8039 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008040#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008041 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8042 || cterm_normal_bg_gui_color != INVALCOLOR)
8043 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008044#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008046#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047 {
8048 out_str(T_OP);
8049 screen_attr = -1;
8050 }
8051 if (cterm_normal_fg_bold)
8052 {
8053 out_str(T_ME);
8054 screen_attr = -1;
8055 }
8056 }
8057}
8058
8059/*
8060 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8061 * using the attributes from ScreenAttrs["off"].
8062 */
8063 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008064screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065{
8066 int attr;
8067
8068 /* Check for illegal values, just in case (could happen just after
8069 * resizing). */
8070 if (row >= screen_Rows || col >= screen_Columns)
8071 return;
8072
Bram Moolenaar33796b32019-06-08 16:01:13 +02008073 // Skip if under the popup menu.
8074 // Popup windows with zindex higher than POPUPMENU_ZINDEX go on top.
8075 if (pum_under_menu(row, col)
Bram Moolenaare2c453d2019-08-21 14:37:09 +02008076#ifdef FEAT_TEXT_PROP
Bram Moolenaar33796b32019-06-08 16:01:13 +02008077 && screen_zindex <= POPUPMENU_ZINDEX
Bram Moolenaare2c453d2019-08-21 14:37:09 +02008078#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02008079 )
Bram Moolenaarae654382019-01-17 21:09:05 +01008080 return;
Bram Moolenaar33796b32019-06-08 16:01:13 +02008081#ifdef FEAT_TEXT_PROP
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008082 if (blocked_by_popup(row, col))
Bram Moolenaar33796b32019-06-08 16:01:13 +02008083 return;
8084#endif
8085
Bram Moolenaar494838a2015-02-10 19:20:37 +01008086 /* Outputting a character in the last cell on the screen may scroll the
8087 * screen up. Only do it when the "xn" termcap property is set, otherwise
8088 * mark the character invalid (update it when scrolled up). */
8089 if (*T_XN == NUL
8090 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091#ifdef FEAT_RIGHTLEFT
8092 /* account for first command-line character in rightleft mode */
8093 && !cmdmsg_rl
8094#endif
8095 )
8096 {
8097 ScreenAttrs[off] = (sattr_T)-1;
8098 return;
8099 }
8100
8101 /*
8102 * Stop highlighting first, so it's easier to move the cursor.
8103 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104 if (screen_char_attr != 0)
8105 attr = screen_char_attr;
8106 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107 attr = ScreenAttrs[off];
8108 if (screen_attr != attr)
8109 screen_stop_highlight();
8110
8111 windgoto(row, col);
8112
8113 if (screen_attr != attr)
8114 screen_start_highlight(attr);
8115
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116 if (enc_utf8 && ScreenLinesUC[off] != 0)
8117 {
8118 char_u buf[MB_MAXBYTES + 1];
8119
Bram Moolenaarcb070082016-04-02 22:14:51 +02008120 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008121 {
8122 if (*p_ambw == 'd'
Bram Moolenaara12a1612019-01-24 16:39:02 +01008123#ifdef FEAT_GUI
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008124 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008125#endif
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008126 )
8127 {
8128 /* Clear the two screen cells. If the character is actually
8129 * single width it won't change the second cell. */
8130 out_str((char_u *)" ");
8131 term_windgoto(row, col);
8132 }
8133 /* not sure where the cursor is after drawing the ambiguous width
8134 * character */
Bram Moolenaarcb070082016-04-02 22:14:51 +02008135 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008136 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02008137 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008139
8140 /* Convert the UTF-8 character to bytes and write it. */
8141 buf[utfc_char2bytes(off, buf)] = NUL;
8142 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 }
8144 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148 /* double-byte character in single-width cell */
8149 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8150 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151 }
8152
8153 screen_cur_col++;
8154}
8155
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156/*
8157 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8158 * on the screen at position 'row' and 'col'.
8159 * The attributes of the first byte is used for all. This is required to
8160 * output the two bytes of a double-byte character with nothing in between.
8161 */
8162 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008163screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164{
8165 /* Check for illegal values (could be wrong when screen was resized). */
8166 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8167 return;
8168
8169 /* Outputting the last character on the screen may scrollup the screen.
8170 * Don't to it! Mark the character invalid (update it when scrolled up) */
8171 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8172 {
8173 ScreenAttrs[off] = (sattr_T)-1;
8174 return;
8175 }
8176
8177 /* Output the first byte normally (positions the cursor), then write the
8178 * second byte directly. */
8179 screen_char(off, row, col);
8180 out_char(ScreenLines[off + 1]);
8181 ++screen_cur_col;
8182}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184/*
8185 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8186 * This uses the contents of ScreenLines[] and doesn't change it.
8187 */
8188 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008189screen_draw_rectangle(
8190 int row,
8191 int col,
8192 int height,
8193 int width,
8194 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195{
8196 int r, c;
8197 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008198 int max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008200 /* Can't use ScreenLines unless initialized */
8201 if (ScreenLines == NULL)
8202 return;
8203
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204 if (invert)
8205 screen_char_attr = HL_INVERSE;
8206 for (r = row; r < row + height; ++r)
8207 {
8208 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008209 max_off = off + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210 for (c = col; c < col + width; ++c)
8211 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00008212 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213 {
8214 screen_char_2(off + c, r, c);
8215 ++c;
8216 }
8217 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 {
8219 screen_char(off + c, r, c);
Bram Moolenaar367329b2007-08-30 11:53:22 +00008220 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 ++c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222 }
8223 }
8224 }
8225 screen_char_attr = 0;
8226}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228/*
8229 * Redraw the characters for a vertically split window.
8230 */
8231 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008232redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233{
8234 int col;
8235 int width;
8236
8237# ifdef FEAT_CLIPBOARD
8238 clip_may_clear_selection(row, end - 1);
8239# endif
8240
8241 if (wp == NULL)
8242 {
8243 col = 0;
8244 width = Columns;
8245 }
8246 else
8247 {
8248 col = wp->w_wincol;
8249 width = wp->w_width;
8250 }
8251 screen_draw_rectangle(row, col, end - row, width, FALSE);
8252}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008254 static void
8255space_to_screenline(int off, int attr)
8256{
8257 ScreenLines[off] = ' ';
8258 ScreenAttrs[off] = attr;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008259 if (enc_utf8)
8260 ScreenLinesUC[off] = 0;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008261}
8262
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263/*
8264 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8265 * with character 'c1' in first column followed by 'c2' in the other columns.
8266 * Use attributes 'attr'.
8267 */
8268 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008269screen_fill(
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008270 int start_row,
8271 int end_row,
8272 int start_col,
8273 int end_col,
8274 int c1,
8275 int c2,
8276 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008278 int row;
8279 int col;
8280 int off;
8281 int end_off;
8282 int did_delete;
8283 int c;
8284 int norm_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008286 int force_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287#endif
8288
8289 if (end_row > screen_Rows) /* safety check */
8290 end_row = screen_Rows;
8291 if (end_col > screen_Columns) /* safety check */
8292 end_col = screen_Columns;
8293 if (ScreenLines == NULL
8294 || start_row >= end_row
8295 || start_col >= end_col) /* nothing to do */
8296 return;
8297
8298 /* it's a "normal" terminal when not in a GUI or cterm */
8299 norm_term = (
8300#ifdef FEAT_GUI
8301 !gui.in_use &&
8302#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008303 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 for (row = start_row; row < end_row; ++row)
8305 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008306 if (has_mbyte
Bram Moolenaara12a1612019-01-24 16:39:02 +01008307#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00008308 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008309#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00008310 )
8311 {
8312 /* When drawing over the right halve of a double-wide char clear
8313 * out the left halve. When drawing over the left halve of a
8314 * double wide-char clear out the right halve. Only needed in a
8315 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008316 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008317 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008318 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008319 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008320 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 /*
8322 * Try to use delete-line termcap code, when no attributes or in a
8323 * "normal" terminal, where a bold/italic space is just a
8324 * space.
8325 */
8326 did_delete = FALSE;
8327 if (c2 == ' '
8328 && end_col == Columns
8329 && can_clear(T_CE)
8330 && (attr == 0
8331 || (norm_term
8332 && attr <= HL_ALL
8333 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8334 {
8335 /*
8336 * check if we really need to clear something
8337 */
8338 col = start_col;
8339 if (c1 != ' ') /* don't clear first char */
8340 ++col;
8341
8342 off = LineOffset[row] + col;
8343 end_off = LineOffset[row] + end_col;
8344
8345 /* skip blanks (used often, keep it fast!) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 if (enc_utf8)
8347 while (off < end_off && ScreenLines[off] == ' '
8348 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8349 ++off;
8350 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351 while (off < end_off && ScreenLines[off] == ' '
8352 && ScreenAttrs[off] == 0)
8353 ++off;
8354 if (off < end_off) /* something to be cleared */
8355 {
8356 col = off - LineOffset[row];
8357 screen_stop_highlight();
8358 term_windgoto(row, col);/* clear rest of this screen line */
8359 out_str(T_CE);
8360 screen_start(); /* don't know where cursor is now */
8361 col = end_col - col;
8362 while (col--) /* clear chars in ScreenLines */
8363 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008364 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365 ++off;
8366 }
8367 }
8368 did_delete = TRUE; /* the chars are cleared now */
8369 }
8370
8371 off = LineOffset[row] + start_col;
8372 c = c1;
8373 for (col = start_col; col < end_col; ++col)
8374 {
Bram Moolenaar33796b32019-06-08 16:01:13 +02008375 if ((ScreenLines[off] != c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008376 || (enc_utf8 && (int)ScreenLinesUC[off]
8377 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378 || ScreenAttrs[off] != attr
8379#if defined(FEAT_GUI) || defined(UNIX)
8380 || force_next
8381#endif
8382 )
Bram Moolenaar33796b32019-06-08 16:01:13 +02008383#ifdef FEAT_TEXT_PROP
8384 // Skip if under a(nother) popup.
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008385 && !blocked_by_popup(row, col)
Bram Moolenaar33796b32019-06-08 16:01:13 +02008386#endif
8387 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388 {
8389#if defined(FEAT_GUI) || defined(UNIX)
8390 /* The bold trick may make a single row of pixels appear in
8391 * the next character. When a bold character is removed, the
8392 * next character should be redrawn too. This happens for our
8393 * own GUI and for some xterms. */
8394 if (
8395# ifdef FEAT_GUI
8396 gui.in_use
8397# endif
8398# if defined(FEAT_GUI) && defined(UNIX)
8399 ||
8400# endif
8401# ifdef UNIX
8402 term_is_xterm
8403# endif
8404 )
8405 {
8406 if (ScreenLines[off] != ' '
8407 && (ScreenAttrs[off] > HL_ALL
8408 || ScreenAttrs[off] & HL_BOLD))
8409 force_next = TRUE;
8410 else
8411 force_next = FALSE;
8412 }
8413#endif
8414 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008415 if (enc_utf8)
8416 {
8417 if (c >= 0x80)
8418 {
8419 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008420 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421 }
8422 else
8423 ScreenLinesUC[off] = 0;
8424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008425 ScreenAttrs[off] = attr;
8426 if (!did_delete || c != ' ')
8427 screen_char(off, row, col);
8428 }
8429 ++off;
8430 if (col == start_col)
8431 {
8432 if (did_delete)
8433 break;
8434 c = c2;
8435 }
8436 }
8437 if (end_col == Columns)
8438 LineWraps[row] = FALSE;
8439 if (row == Rows - 1) /* overwritten the command line */
8440 {
8441 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02008442 if (start_col == 0 && end_col == Columns
8443 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008445 if (start_col == 0)
8446 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447 }
8448 }
8449}
8450
8451/*
8452 * Check if there should be a delay. Used before clearing or redrawing the
8453 * screen or the command line.
8454 */
8455 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008456check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008457{
8458 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8459 && !did_wait_return
8460 && emsg_silent == 0)
8461 {
8462 out_flush();
8463 ui_delay(1000L, TRUE);
8464 emsg_on_display = FALSE;
8465 if (check_msg_scroll)
8466 msg_scroll = FALSE;
8467 }
8468}
8469
8470/*
Bram Moolenaarca57ab52019-04-13 14:53:16 +02008471 * Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect.
8472 */
8473 static void
8474clear_TabPageIdxs(void)
8475{
8476 int scol;
8477
8478 for (scol = 0; scol < Columns; ++scol)
8479 TabPageIdxs[scol] = 0;
8480}
8481
8482/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008484 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485 * Returns TRUE if there is a valid screen to write to.
8486 * Returns FALSE when starting up and screen not initialized yet.
8487 */
8488 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008489screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008490{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008491 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492 return (ScreenLines != NULL);
8493}
8494
8495/*
8496 * Resize the shell to Rows and Columns.
8497 * Allocate ScreenLines[] and associated items.
8498 *
8499 * There may be some time between setting Rows and Columns and (re)allocating
8500 * ScreenLines[]. This happens when starting up and when (manually) changing
8501 * the shell size. Always use screen_Rows and screen_Columns to access items
8502 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8503 * final size of the shell is needed.
8504 */
8505 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008506screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008507{
8508 int new_row, old_row;
8509#ifdef FEAT_GUI
8510 int old_Rows;
8511#endif
8512 win_T *wp;
8513 int outofmem = FALSE;
8514 int len;
8515 schar_T *new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008517 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008519 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520 sattr_T *new_ScreenAttrs;
8521 unsigned *new_LineOffset;
8522 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008523 short *new_TabPageIdxs;
Bram Moolenaar33796b32019-06-08 16:01:13 +02008524#ifdef FEAT_TEXT_PROP
8525 short *new_popup_mask;
Bram Moolenaar4c063a02019-06-10 21:24:12 +02008526 short *new_popup_mask_next;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008527 char *new_popup_transparent;
Bram Moolenaar33796b32019-06-08 16:01:13 +02008528#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00008529 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008531 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008532 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008534retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535 /*
8536 * Allocation of the screen buffers is done only when the size changes and
8537 * when Rows and Columns have been set and we have started doing full
8538 * screen stuff.
8539 */
8540 if ((ScreenLines != NULL
8541 && Rows == screen_Rows
8542 && Columns == screen_Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543 && enc_utf8 == (ScreenLinesUC != NULL)
8544 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaara12a1612019-01-24 16:39:02 +01008545 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546 || Rows == 0
8547 || Columns == 0
8548 || (!full_screen && ScreenLines == NULL))
8549 return;
8550
8551 /*
8552 * It's possible that we produce an out-of-memory message below, which
8553 * will cause this function to be called again. To break the loop, just
8554 * return here.
8555 */
8556 if (entered)
8557 return;
8558 entered = TRUE;
8559
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008560 /*
8561 * Note that the window sizes are updated before reallocating the arrays,
8562 * thus we must not redraw here!
8563 */
8564 ++RedrawingDisabled;
8565
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566 win_new_shellsize(); /* fit the windows in the new sized shell */
8567
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568 comp_col(); /* recompute columns for shown command and ruler */
8569
8570 /*
8571 * We're changing the size of the screen.
8572 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8573 * - Move lines from the old arrays into the new arrays, clear extra
8574 * lines (unless the screen is going to be cleared).
8575 * - Free the old arrays.
8576 *
8577 * If anything fails, make ScreenLines NULL, so we don't do anything!
8578 * Continuing with the old ScreenLines may result in a crash, because the
8579 * size is wrong.
8580 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008581 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008583 if (aucmd_win != NULL)
8584 win_free_lsize(aucmd_win);
Bram Moolenaar8caaf822019-06-01 18:11:22 +02008585#ifdef FEAT_TEXT_PROP
8586 // global popup windows
8587 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
8588 win_free_lsize(wp);
8589 // tab-local popup windows
8590 FOR_ALL_TABPAGES(tp)
8591 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
8592 win_free_lsize(wp);
8593#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008595 new_ScreenLines = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
Bram Moolenaar216b7102010-03-23 13:56:59 +01008596 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597 if (enc_utf8)
8598 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008599 new_ScreenLinesUC = LALLOC_MULT(u8char_T, (Rows + 1) * Columns);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008600 for (i = 0; i < p_mco; ++i)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008601 new_ScreenLinesC[i] = LALLOC_CLEAR_MULT(u8char_T,
8602 (Rows + 1) * Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008603 }
8604 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008605 new_ScreenLines2 = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
8606 new_ScreenAttrs = LALLOC_MULT(sattr_T, (Rows + 1) * Columns);
8607 new_LineOffset = LALLOC_MULT(unsigned, Rows);
8608 new_LineWraps = LALLOC_MULT(char_u, Rows);
8609 new_TabPageIdxs = LALLOC_MULT(short, Columns);
Bram Moolenaar33796b32019-06-08 16:01:13 +02008610#ifdef FEAT_TEXT_PROP
8611 new_popup_mask = LALLOC_MULT(short, Rows * Columns);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02008612 new_popup_mask_next = LALLOC_MULT(short, Rows * Columns);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008613 new_popup_transparent = LALLOC_MULT(char, Rows * Columns);
Bram Moolenaar33796b32019-06-08 16:01:13 +02008614#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008616 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617 {
8618 if (win_alloc_lines(wp) == FAIL)
8619 {
8620 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008621 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622 }
8623 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008624 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8625 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008626 outofmem = TRUE;
Bram Moolenaar8caaf822019-06-01 18:11:22 +02008627#ifdef FEAT_TEXT_PROP
8628 // global popup windows
8629 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
8630 if (win_alloc_lines(wp) == FAIL)
8631 {
8632 outofmem = TRUE;
8633 goto give_up;
8634 }
8635 // tab-local popup windows
8636 FOR_ALL_TABPAGES(tp)
8637 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
8638 if (win_alloc_lines(wp) == FAIL)
8639 {
8640 outofmem = TRUE;
8641 goto give_up;
8642 }
8643#endif
8644
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008645give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008647 for (i = 0; i < p_mco; ++i)
8648 if (new_ScreenLinesC[i] == NULL)
8649 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650 if (new_ScreenLines == NULL
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008651 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653 || new_ScreenAttrs == NULL
8654 || new_LineOffset == NULL
8655 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008656 || new_TabPageIdxs == NULL
Bram Moolenaar33796b32019-06-08 16:01:13 +02008657#ifdef FEAT_TEXT_PROP
8658 || new_popup_mask == NULL
Bram Moolenaar4c063a02019-06-10 21:24:12 +02008659 || new_popup_mask_next == NULL
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008660 || new_popup_transparent == NULL
Bram Moolenaar33796b32019-06-08 16:01:13 +02008661#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008662 || outofmem)
8663 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008664 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008665 {
8666 /* guess the size */
8667 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8668
8669 /* Remember we did this to avoid getting outofmem messages over
8670 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008671 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008672 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01008673 VIM_CLEAR(new_ScreenLines);
Bram Moolenaard23a8232018-02-10 18:45:26 +01008674 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008675 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01008676 VIM_CLEAR(new_ScreenLinesC[i]);
8677 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaard23a8232018-02-10 18:45:26 +01008678 VIM_CLEAR(new_ScreenAttrs);
8679 VIM_CLEAR(new_LineOffset);
8680 VIM_CLEAR(new_LineWraps);
8681 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar33796b32019-06-08 16:01:13 +02008682#ifdef FEAT_TEXT_PROP
8683 VIM_CLEAR(new_popup_mask);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02008684 VIM_CLEAR(new_popup_mask_next);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008685 VIM_CLEAR(new_popup_transparent);
Bram Moolenaar33796b32019-06-08 16:01:13 +02008686#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008687 }
8688 else
8689 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008690 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008691
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692 for (new_row = 0; new_row < Rows; ++new_row)
8693 {
8694 new_LineOffset[new_row] = new_row * Columns;
8695 new_LineWraps[new_row] = FALSE;
8696
8697 /*
8698 * If the screen is not going to be cleared, copy as much as
8699 * possible from the old screen to the new one and clear the rest
8700 * (used when resizing the window at the "--more--" prompt or when
8701 * executing an external command, for the GUI).
8702 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008703 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704 {
8705 (void)vim_memset(new_ScreenLines + new_row * Columns,
8706 ' ', (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008707 if (enc_utf8)
8708 {
8709 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8710 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008711 for (i = 0; i < p_mco; ++i)
8712 (void)vim_memset(new_ScreenLinesC[i]
8713 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714 0, (size_t)Columns * sizeof(u8char_T));
8715 }
8716 if (enc_dbcs == DBCS_JPNU)
8717 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8718 0, (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008719 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8720 0, (size_t)Columns * sizeof(sattr_T));
8721 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008722 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723 {
8724 if (screen_Columns < Columns)
8725 len = screen_Columns;
8726 else
8727 len = Columns;
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008728 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008729 * may be invalid now. Also when p_mco changes. */
8730 if (!(enc_utf8 && ScreenLinesUC == NULL)
8731 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008732 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8733 ScreenLines + LineOffset[old_row],
8734 (size_t)len * sizeof(schar_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008735 if (enc_utf8 && ScreenLinesUC != NULL
8736 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008737 {
8738 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8739 ScreenLinesUC + LineOffset[old_row],
8740 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008741 for (i = 0; i < p_mco; ++i)
8742 mch_memmove(new_ScreenLinesC[i]
8743 + new_LineOffset[new_row],
8744 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745 (size_t)len * sizeof(u8char_T));
8746 }
8747 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8748 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8749 ScreenLines2 + LineOffset[old_row],
8750 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8752 ScreenAttrs + LineOffset[old_row],
8753 (size_t)len * sizeof(sattr_T));
8754 }
8755 }
8756 }
8757 /* Use the last line of the screen for the current line. */
8758 current_ScreenLine = new_ScreenLines + Rows * Columns;
Bram Moolenaar6ace95e2019-08-13 23:09:49 +02008759
8760#ifdef FEAT_TEXT_PROP
8761 vim_memset(new_popup_mask, 0, Rows * Columns * sizeof(short));
8762 vim_memset(new_popup_transparent, 0, Rows * Columns * sizeof(char));
8763#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764 }
8765
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008766 free_screenlines();
8767
Bram Moolenaar6ace95e2019-08-13 23:09:49 +02008768 // NOTE: this may result in all pointers to become NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769 ScreenLines = new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008770 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008771 for (i = 0; i < p_mco; ++i)
8772 ScreenLinesC[i] = new_ScreenLinesC[i];
8773 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774 ScreenLines2 = new_ScreenLines2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775 ScreenAttrs = new_ScreenAttrs;
8776 LineOffset = new_LineOffset;
8777 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008778 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar33796b32019-06-08 16:01:13 +02008779#ifdef FEAT_TEXT_PROP
8780 popup_mask = new_popup_mask;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008781 popup_mask_next = new_popup_mask_next;
8782 popup_transparent = new_popup_transparent;
Bram Moolenaar33796b32019-06-08 16:01:13 +02008783 popup_mask_refresh = TRUE;
8784#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785
8786 /* It's important that screen_Rows and screen_Columns reflect the actual
8787 * size of ScreenLines[]. Set them before calling anything. */
8788#ifdef FEAT_GUI
8789 old_Rows = screen_Rows;
8790#endif
8791 screen_Rows = Rows;
8792 screen_Columns = Columns;
8793
8794 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008795 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008796 screenclear2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008797#ifdef FEAT_GUI
8798 else if (gui.in_use
8799 && !gui.starting
8800 && ScreenLines != NULL
8801 && old_Rows != Rows)
8802 {
8803 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8804 /*
8805 * Adjust the position of the cursor, for when executing an external
8806 * command.
8807 */
8808 if (msg_row >= Rows) /* Rows got smaller */
8809 msg_row = Rows - 1; /* put cursor at last row */
8810 else if (Rows > old_Rows) /* Rows got bigger */
8811 msg_row += Rows - old_Rows; /* put cursor in same place */
8812 if (msg_col >= Columns) /* Columns got smaller */
8813 msg_col = Columns - 1; /* put cursor at last column */
8814 }
8815#endif
Bram Moolenaarca57ab52019-04-13 14:53:16 +02008816 clear_TabPageIdxs();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008817
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008819 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008820
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008821 /*
8822 * Do not apply autocommands more than 3 times to avoid an endless loop
8823 * in case applying autocommands always changes Rows or Columns.
8824 */
8825 if (starting == 0 && ++retry_count <= 3)
8826 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008827 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008828 /* In rare cases, autocommands may have altered Rows or Columns,
8829 * jump back to check if we need to allocate the screen again. */
8830 goto retry;
8831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832}
8833
8834 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008835free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008836{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008837 int i;
8838
Bram Moolenaar33796b32019-06-08 16:01:13 +02008839 VIM_CLEAR(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008840 for (i = 0; i < Screen_mco; ++i)
Bram Moolenaar33796b32019-06-08 16:01:13 +02008841 VIM_CLEAR(ScreenLinesC[i]);
8842 VIM_CLEAR(ScreenLines2);
8843 VIM_CLEAR(ScreenLines);
8844 VIM_CLEAR(ScreenAttrs);
8845 VIM_CLEAR(LineOffset);
8846 VIM_CLEAR(LineWraps);
8847 VIM_CLEAR(TabPageIdxs);
8848#ifdef FEAT_TEXT_PROP
8849 VIM_CLEAR(popup_mask);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02008850 VIM_CLEAR(popup_mask_next);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008851 VIM_CLEAR(popup_transparent);
Bram Moolenaar33796b32019-06-08 16:01:13 +02008852#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008853}
8854
8855 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008856screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857{
8858 check_for_delay(FALSE);
8859 screenalloc(FALSE); /* allocate screen buffers if size changed */
8860 screenclear2(); /* clear the screen */
8861}
8862
8863 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008864screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865{
8866 int i;
8867
8868 if (starting == NO_SCREEN || ScreenLines == NULL
8869#ifdef FEAT_GUI
8870 || (gui.in_use && gui.starting)
8871#endif
8872 )
8873 return;
8874
8875#ifdef FEAT_GUI
8876 if (!gui.in_use)
8877#endif
8878 screen_attr = -1; /* force setting the Normal colors */
8879 screen_stop_highlight(); /* don't want highlighting here */
8880
8881#ifdef FEAT_CLIPBOARD
8882 /* disable selection without redrawing it */
8883 clip_scroll_selection(9999);
8884#endif
8885
8886 /* blank out ScreenLines */
8887 for (i = 0; i < Rows; ++i)
8888 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02008889 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890 LineWraps[i] = FALSE;
8891 }
8892
8893 if (can_clear(T_CL))
8894 {
8895 out_str(T_CL); /* clear the display */
8896 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008897 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898 }
8899 else
8900 {
8901 /* can't clear the screen, mark all chars with invalid attributes */
8902 for (i = 0; i < Rows; ++i)
8903 lineinvalid(LineOffset[i], (int)Columns);
8904 clear_cmdline = TRUE;
8905 }
8906
8907 screen_cleared = TRUE; /* can use contents of ScreenLines now */
8908
8909 win_rest_invalid(firstwin);
8910 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008911 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912 if (must_redraw == CLEAR) /* no need to clear again */
8913 must_redraw = NOT_VALID;
8914 compute_cmdrow();
8915 msg_row = cmdline_row; /* put cursor on last line for messages */
8916 msg_col = 0;
8917 screen_start(); /* don't know where cursor is now */
8918 msg_scrolled = 0; /* can't scroll back */
8919 msg_didany = FALSE;
8920 msg_didout = FALSE;
8921}
8922
8923/*
8924 * Clear one line in ScreenLines.
8925 */
8926 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02008927lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928{
8929 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008930 if (enc_utf8)
8931 (void)vim_memset(ScreenLinesUC + off, 0,
8932 (size_t)width * sizeof(u8char_T));
Bram Moolenaarcfce7172017-08-17 20:31:48 +02008933 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934}
8935
8936/*
8937 * Mark one line in ScreenLines invalid by setting the attributes to an
8938 * invalid value.
8939 */
8940 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008941lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942{
8943 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
8944}
8945
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946/*
8947 * Copy part of a Screenline for vertically split window "wp".
8948 */
8949 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008950linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951{
8952 unsigned off_to = LineOffset[to] + wp->w_wincol;
8953 unsigned off_from = LineOffset[from] + wp->w_wincol;
8954
8955 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
8956 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008957 if (enc_utf8)
8958 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008959 int i;
8960
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
8962 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008963 for (i = 0; i < p_mco; ++i)
8964 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
8965 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008966 }
8967 if (enc_dbcs == DBCS_JPNU)
8968 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
8969 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
8971 wp->w_width * sizeof(sattr_T));
8972}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008973
8974/*
8975 * Return TRUE if clearing with term string "p" would work.
8976 * It can't work when the string is empty or it won't set the right background.
Bram Moolenaar33796b32019-06-08 16:01:13 +02008977 * Don't clear to end-of-line when there are popups, it may cause flicker.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978 */
8979 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008980can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981{
8982 return (*p != NUL && (t_colors <= 1
8983#ifdef FEAT_GUI
8984 || gui.in_use
8985#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008986#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008987 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02008988 || (!p_tgc && cterm_normal_bg_color == 0)
8989#else
8990 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008991#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02008992 || *T_UT != NUL)
8993#ifdef FEAT_TEXT_PROP
8994 && !(p == T_CE && popup_visible)
8995#endif
8996 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00008997}
8998
8999/*
9000 * Reset cursor position. Use whenever cursor was moved because of outputting
9001 * something directly to the screen (shell commands) or a terminal control
9002 * code.
9003 */
9004 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009005screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009006{
9007 screen_cur_row = screen_cur_col = 9999;
9008}
9009
9010/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009011 * Move the cursor to position "row","col" in the screen.
9012 * This tries to find the most efficient way to move, minimizing the number of
9013 * characters sent to the terminal.
9014 */
9015 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009016windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009017{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009018 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019 int i;
9020 int plan;
9021 int cost;
9022 int wouldbe_col;
9023 int noinvcurs;
9024 char_u *bs;
9025 int goto_cost;
9026 int attr;
9027
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009028#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009029#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9030
9031#define PLAN_LE 1
9032#define PLAN_CR 2
9033#define PLAN_NL 3
9034#define PLAN_WRITE 4
9035 /* Can't use ScreenLines unless initialized */
9036 if (ScreenLines == NULL)
9037 return;
9038
9039 if (col != screen_cur_col || row != screen_cur_row)
9040 {
9041 /* Check for valid position. */
9042 if (row < 0) /* window without text lines? */
9043 row = 0;
9044 if (row >= screen_Rows)
9045 row = screen_Rows - 1;
9046 if (col >= screen_Columns)
9047 col = screen_Columns - 1;
9048
9049 /* check if no cursor movement is allowed in highlight mode */
9050 if (screen_attr && *T_MS == NUL)
9051 noinvcurs = HIGHL_COST;
9052 else
9053 noinvcurs = 0;
9054 goto_cost = GOTO_COST + noinvcurs;
9055
9056 /*
9057 * Plan how to do the positioning:
9058 * 1. Use CR to move it to column 0, same row.
9059 * 2. Use T_LE to move it a few columns to the left.
9060 * 3. Use NL to move a few lines down, column 0.
9061 * 4. Move a few columns to the right with T_ND or by writing chars.
9062 *
9063 * Don't do this if the cursor went beyond the last column, the cursor
9064 * position is unknown then (some terminals wrap, some don't )
9065 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009066 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009067 * characters to move the cursor to the right.
9068 */
9069 if (row >= screen_cur_row && screen_cur_col < Columns)
9070 {
9071 /*
9072 * If the cursor is in the same row, bigger col, we can use CR
9073 * or T_LE.
9074 */
9075 bs = NULL; /* init for GCC */
9076 attr = screen_attr;
9077 if (row == screen_cur_row && col < screen_cur_col)
9078 {
9079 /* "le" is preferred over "bc", because "bc" is obsolete */
9080 if (*T_LE)
9081 bs = T_LE; /* "cursor left" */
9082 else
9083 bs = T_BC; /* "backspace character (old) */
9084 if (*bs)
9085 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9086 else
9087 cost = 999;
9088 if (col + 1 < cost) /* using CR is less characters */
9089 {
9090 plan = PLAN_CR;
9091 wouldbe_col = 0;
9092 cost = 1; /* CR is just one character */
9093 }
9094 else
9095 {
9096 plan = PLAN_LE;
9097 wouldbe_col = col;
9098 }
9099 if (noinvcurs) /* will stop highlighting */
9100 {
9101 cost += noinvcurs;
9102 attr = 0;
9103 }
9104 }
9105
9106 /*
9107 * If the cursor is above where we want to be, we can use CR LF.
9108 */
9109 else if (row > screen_cur_row)
9110 {
9111 plan = PLAN_NL;
9112 wouldbe_col = 0;
9113 cost = (row - screen_cur_row) * 2; /* CR LF */
9114 if (noinvcurs) /* will stop highlighting */
9115 {
9116 cost += noinvcurs;
9117 attr = 0;
9118 }
9119 }
9120
9121 /*
9122 * If the cursor is in the same row, smaller col, just use write.
9123 */
9124 else
9125 {
9126 plan = PLAN_WRITE;
9127 wouldbe_col = screen_cur_col;
9128 cost = 0;
9129 }
9130
9131 /*
9132 * Check if any characters that need to be written have the
9133 * correct attributes. Also avoid UTF-8 characters.
9134 */
9135 i = col - wouldbe_col;
9136 if (i > 0)
9137 cost += i;
9138 if (cost < goto_cost && i > 0)
9139 {
9140 /*
9141 * Check if the attributes are correct without additionally
9142 * stopping highlighting.
9143 */
9144 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9145 while (i && *p++ == attr)
9146 --i;
9147 if (i != 0)
9148 {
9149 /*
9150 * Try if it works when highlighting is stopped here.
9151 */
9152 if (*--p == 0)
9153 {
9154 cost += noinvcurs;
9155 while (i && *p++ == 0)
9156 --i;
9157 }
9158 if (i != 0)
9159 cost = 999; /* different attributes, don't do it */
9160 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009161 if (enc_utf8)
9162 {
9163 /* Don't use an UTF-8 char for positioning, it's slow. */
9164 for (i = wouldbe_col; i < col; ++i)
9165 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9166 {
9167 cost = 999;
9168 break;
9169 }
9170 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009171 }
9172
9173 /*
9174 * We can do it without term_windgoto()!
9175 */
9176 if (cost < goto_cost)
9177 {
9178 if (plan == PLAN_LE)
9179 {
9180 if (noinvcurs)
9181 screen_stop_highlight();
9182 while (screen_cur_col > col)
9183 {
9184 out_str(bs);
9185 --screen_cur_col;
9186 }
9187 }
9188 else if (plan == PLAN_CR)
9189 {
9190 if (noinvcurs)
9191 screen_stop_highlight();
9192 out_char('\r');
9193 screen_cur_col = 0;
9194 }
9195 else if (plan == PLAN_NL)
9196 {
9197 if (noinvcurs)
9198 screen_stop_highlight();
9199 while (screen_cur_row < row)
9200 {
9201 out_char('\n');
9202 ++screen_cur_row;
9203 }
9204 screen_cur_col = 0;
9205 }
9206
9207 i = col - screen_cur_col;
9208 if (i > 0)
9209 {
9210 /*
9211 * Use cursor-right if it's one character only. Avoids
9212 * removing a line of pixels from the last bold char, when
9213 * using the bold trick in the GUI.
9214 */
9215 if (T_ND[0] != NUL && T_ND[1] == NUL)
9216 {
9217 while (i-- > 0)
9218 out_char(*T_ND);
9219 }
9220 else
9221 {
9222 int off;
9223
9224 off = LineOffset[row] + screen_cur_col;
9225 while (i-- > 0)
9226 {
9227 if (ScreenAttrs[off] != screen_attr)
9228 screen_stop_highlight();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009229 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009230 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009231 if (enc_dbcs == DBCS_JPNU
9232 && ScreenLines[off] == 0x8e)
9233 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009234 ++off;
9235 }
9236 }
9237 }
9238 }
9239 }
9240 else
9241 cost = 999;
9242
9243 if (cost >= goto_cost)
9244 {
9245 if (noinvcurs)
9246 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009247 if (row == screen_cur_row && (col > screen_cur_col)
9248 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009249 term_cursor_right(col - screen_cur_col);
9250 else
9251 term_windgoto(row, col);
9252 }
9253 screen_cur_row = row;
9254 screen_cur_col = col;
9255 }
9256}
9257
9258/*
9259 * Set cursor to its position in the current window.
9260 */
9261 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009262setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009263{
Bram Moolenaar987723e2018-03-06 11:43:04 +01009264 setcursor_mayforce(FALSE);
9265}
9266
9267/*
9268 * Set cursor to its position in the current window.
9269 * When "force" is TRUE also when not redrawing.
9270 */
9271 void
9272setcursor_mayforce(int force)
9273{
9274 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009275 {
9276 validate_cursor();
9277 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009278 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009279#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009280 /* With 'rightleft' set and the cursor on a double-wide
9281 * character, position it on the leftmost column. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01009282 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol
9283 - ((has_mbyte
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009284 && (*mb_ptr2cells)(ml_get_cursor()) == 2
Bram Moolenaara12a1612019-01-24 16:39:02 +01009285 && vim_isprintc(gchar_cursor())) ? 2 : 1)) :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009286#endif
9287 curwin->w_wcol));
9288 }
9289}
9290
9291
9292/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009293 * Insert 'line_count' lines at 'row' in window 'wp'.
9294 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9295 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009296 * scrolling.
9297 * Returns FAIL if the lines are not inserted, OK for success.
9298 */
9299 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009300win_ins_lines(
9301 win_T *wp,
9302 int row,
9303 int line_count,
9304 int invalid,
9305 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009306{
9307 int did_delete;
9308 int nextrow;
9309 int lastrow;
9310 int retval;
9311
9312 if (invalid)
9313 wp->w_lines_valid = 0;
9314
9315 if (wp->w_height < 5)
9316 return FAIL;
9317
9318 if (line_count > wp->w_height - row)
9319 line_count = wp->w_height - row;
9320
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009321 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009322 if (retval != MAYBE)
9323 return retval;
9324
9325 /*
9326 * If there is a next window or a status line, we first try to delete the
9327 * lines at the bottom to avoid messing what is after the window.
Bram Moolenaarc363fe12019-08-04 18:13:46 +02009328 * If this fails and there are following windows, don't do anything to
9329 * avoid messing up those windows, better just redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009330 */
9331 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009332 if (wp->w_next != NULL || wp->w_status_height)
9333 {
9334 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009335 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009336 did_delete = TRUE;
9337 else if (wp->w_next)
9338 return FAIL;
9339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009340 /*
9341 * if no lines deleted, blank the lines that will end up below the window
9342 */
9343 if (!did_delete)
9344 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009345 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009346 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02009347 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009348 lastrow = nextrow + line_count;
9349 if (lastrow > Rows)
9350 lastrow = Rows;
9351 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009352 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009353 ' ', ' ', 0);
9354 }
9355
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009356 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009357 == FAIL)
9358 {
Bram Moolenaarc363fe12019-08-04 18:13:46 +02009359 // deletion will have messed up other windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00009360 if (did_delete)
9361 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009362 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009363 win_rest_invalid(W_NEXT(wp));
9364 }
9365 return FAIL;
9366 }
9367
9368 return OK;
9369}
9370
9371/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009372 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009373 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9374 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9375 * scrolling
9376 * Return OK for success, FAIL if the lines are not deleted.
9377 */
9378 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009379win_del_lines(
9380 win_T *wp,
9381 int row,
9382 int line_count,
9383 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009384 int mayclear,
9385 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009386{
9387 int retval;
9388
9389 if (invalid)
9390 wp->w_lines_valid = 0;
9391
9392 if (line_count > wp->w_height - row)
9393 line_count = wp->w_height - row;
9394
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009395 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009396 if (retval != MAYBE)
9397 return retval;
9398
9399 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009400 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009401 return FAIL;
9402
Bram Moolenaar071d4272004-06-13 20:20:40 +00009403 /*
9404 * If there are windows or status lines below, try to put them at the
9405 * correct place. If we can't do that, they have to be redrawn.
9406 */
9407 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9408 {
9409 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009410 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009411 {
9412 wp->w_redr_status = TRUE;
9413 win_rest_invalid(wp->w_next);
9414 }
9415 }
9416 /*
9417 * If this is the last window and there is no status line, redraw the
9418 * command line later.
9419 */
9420 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009421 redraw_cmdline = TRUE;
9422 return OK;
9423}
9424
9425/*
9426 * Common code for win_ins_lines() and win_del_lines().
9427 * Returns OK or FAIL when the work has been done.
9428 * Returns MAYBE when not finished yet.
9429 */
9430 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009431win_do_lines(
9432 win_T *wp,
9433 int row,
9434 int line_count,
9435 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009436 int del,
9437 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009438{
9439 int retval;
9440
9441 if (!redrawing() || line_count <= 0)
9442 return FAIL;
9443
Bram Moolenaar33796b32019-06-08 16:01:13 +02009444 // When inserting lines would result in loss of command output, just redraw
9445 // the lines.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009446 if (no_win_do_lines_ins && !del)
9447 return FAIL;
9448
Bram Moolenaar33796b32019-06-08 16:01:13 +02009449 // only a few lines left: redraw is faster
Bram Moolenaar4033c552017-09-16 20:54:51 +02009450 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009451 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009452 if (!no_win_do_lines_ins)
Bram Moolenaar33796b32019-06-08 16:01:13 +02009453 screenclear(); // will set wp->w_lines_valid to 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00009454 return FAIL;
9455 }
9456
Bram Moolenaar33796b32019-06-08 16:01:13 +02009457#ifdef FEAT_TEXT_PROP
Bram Moolenaar4c063a02019-06-10 21:24:12 +02009458 // this doesn't work when there are popups visible
Bram Moolenaar33796b32019-06-08 16:01:13 +02009459 if (popup_visible)
9460 return FAIL;
9461#endif
9462
9463 // Delete all remaining lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00009464 if (row + line_count >= wp->w_height)
9465 {
9466 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009467 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009468 ' ', ' ', 0);
9469 return OK;
9470 }
9471
9472 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009473 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009474 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009475 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009476 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009477 if (!no_win_do_lines_ins)
9478 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009479
9480 /*
9481 * If the terminal can set a scroll region, use that.
9482 * Always do this in a vertically split window. This will redraw from
9483 * ScreenLines[] when t_CV isn't defined. That's faster than using
9484 * win_line().
9485 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009486 * a character in the lower right corner of the scroll region may cause a
9487 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009488 */
Bram Moolenaar02631462017-09-22 15:20:32 +02009489 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009490 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009491 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492 scroll_region_set(wp, row);
9493 if (del)
9494 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009495 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009496 else
9497 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009498 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009499 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009500 scroll_region_reset();
9501 return retval;
9502 }
9503
Bram Moolenaar071d4272004-06-13 20:20:40 +00009504 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9505 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009506
9507 return MAYBE;
9508}
9509
9510/*
9511 * window 'wp' and everything after it is messed up, mark it for redraw
9512 */
9513 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009514win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009515{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009516 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009517 {
9518 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009519 wp->w_redr_status = TRUE;
9520 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009521 }
9522 redraw_cmdline = TRUE;
9523}
9524
9525/*
9526 * The rest of the routines in this file perform screen manipulations. The
9527 * given operation is performed physically on the screen. The corresponding
9528 * change is also made to the internal screen image. In this way, the editor
9529 * anticipates the effect of editing changes on the appearance of the screen.
9530 * That way, when we call screenupdate a complete redraw isn't usually
9531 * necessary. Another advantage is that we can keep adding code to anticipate
9532 * screen changes, and in the meantime, everything still works.
9533 */
9534
9535/*
9536 * types for inserting or deleting lines
9537 */
9538#define USE_T_CAL 1
9539#define USE_T_CDL 2
9540#define USE_T_AL 3
9541#define USE_T_CE 4
9542#define USE_T_DL 5
9543#define USE_T_SR 6
9544#define USE_NL 7
9545#define USE_T_CD 8
9546#define USE_REDRAW 9
9547
9548/*
9549 * insert lines on the screen and update ScreenLines[]
9550 * 'end' is the line after the scrolled part. Normally it is Rows.
9551 * When scrolling region used 'off' is the offset from the top for the region.
9552 * 'row' and 'end' are relative to the start of the region.
9553 *
9554 * return FAIL for failure, OK for success.
9555 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009556 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009557screen_ins_lines(
9558 int off,
9559 int row,
9560 int line_count,
9561 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009562 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01009563 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009564{
9565 int i;
9566 int j;
9567 unsigned temp;
9568 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009569 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009570 int type;
9571 int result_empty;
9572 int can_ce = can_clear(T_CE);
9573
9574 /*
9575 * FAIL if
9576 * - there is no valid screen
9577 * - the screen has to be redrawn completely
9578 * - the line count is less than one
9579 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009580 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar33796b32019-06-08 16:01:13 +02009581 * - there is a popup window
Bram Moolenaar071d4272004-06-13 20:20:40 +00009582 */
Bram Moolenaar33796b32019-06-08 16:01:13 +02009583 if (!screen_valid(TRUE)
9584 || line_count <= 0 || line_count > p_ttyscroll
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009585#ifdef FEAT_CLIPBOARD
9586 || (clip_star.state != SELECT_CLEARED
9587 && redrawing_for_callback > 0)
9588#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02009589#ifdef FEAT_TEXT_PROP
9590 || popup_visible
9591#endif
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009592 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009593 return FAIL;
9594
9595 /*
9596 * There are seven ways to insert lines:
9597 * 0. When in a vertically split window and t_CV isn't set, redraw the
9598 * characters from ScreenLines[].
9599 * 1. Use T_CD (clear to end of display) if it exists and the result of
9600 * the insert is just empty lines
9601 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9602 * present or line_count > 1. It looks better if we do all the inserts
9603 * at once.
9604 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9605 * insert is just empty lines and T_CE is not present or line_count >
9606 * 1.
9607 * 4. Use T_AL (insert line) if it exists.
9608 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9609 * just empty lines.
9610 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9611 * just empty lines.
9612 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9613 * the 'da' flag is not set or we have clear line capability.
9614 * 8. redraw the characters from ScreenLines[].
9615 *
9616 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9617 * the scrollbar for the window. It does have insert line, use that if it
9618 * exists.
9619 */
9620 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009621 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9622 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009623 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009624 type = USE_T_CD;
9625 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9626 type = USE_T_CAL;
9627 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9628 type = USE_T_CDL;
9629 else if (*T_AL != NUL)
9630 type = USE_T_AL;
9631 else if (can_ce && result_empty)
9632 type = USE_T_CE;
9633 else if (*T_DL != NUL && result_empty)
9634 type = USE_T_DL;
9635 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9636 type = USE_T_SR;
9637 else
9638 return FAIL;
9639
9640 /*
9641 * For clearing the lines screen_del_lines() is used. This will also take
9642 * care of t_db if necessary.
9643 */
9644 if (type == USE_T_CD || type == USE_T_CDL ||
9645 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009646 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009647
9648 /*
9649 * If text is retained below the screen, first clear or delete as many
9650 * lines at the bottom of the window as are about to be inserted so that
9651 * the deleted lines won't later surface during a screen_del_lines.
9652 */
9653 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009654 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009655
9656#ifdef FEAT_CLIPBOARD
9657 /* Remove a modeless selection when inserting lines halfway the screen
9658 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009659 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009660 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009661 else
9662 clip_scroll_selection(-line_count);
9663#endif
9664
Bram Moolenaar071d4272004-06-13 20:20:40 +00009665#ifdef FEAT_GUI
9666 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9667 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009668 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009669#endif
9670
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009671 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
9672 cursor_col = wp->w_wincol;
9673
Bram Moolenaar071d4272004-06-13 20:20:40 +00009674 if (*T_CCS != NUL) /* cursor relative to region */
9675 cursor_row = row;
9676 else
9677 cursor_row = row + off;
9678
9679 /*
9680 * Shift LineOffset[] line_count down to reflect the inserted lines.
9681 * Clear the inserted lines in ScreenLines[].
9682 */
9683 row += off;
9684 end += off;
9685 for (i = 0; i < line_count; ++i)
9686 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009687 if (wp != NULL && wp->w_width != Columns)
9688 {
9689 /* need to copy part of a line */
9690 j = end - 1 - i;
9691 while ((j -= line_count) >= row)
9692 linecopy(j + line_count, j, wp);
9693 j += line_count;
9694 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009695 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
9696 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009697 else
9698 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9699 LineWraps[j] = FALSE;
9700 }
9701 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009702 {
9703 j = end - 1 - i;
9704 temp = LineOffset[j];
9705 while ((j -= line_count) >= row)
9706 {
9707 LineOffset[j + line_count] = LineOffset[j];
9708 LineWraps[j + line_count] = LineWraps[j];
9709 }
9710 LineOffset[j + line_count] = temp;
9711 LineWraps[j + line_count] = FALSE;
9712 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009713 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009714 else
9715 lineinvalid(temp, (int)Columns);
9716 }
9717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009718
9719 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009720 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009721 if (clear_attr != 0)
9722 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009723
Bram Moolenaar071d4272004-06-13 20:20:40 +00009724 /* redraw the characters */
9725 if (type == USE_REDRAW)
9726 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02009727 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009728 {
9729 term_append_lines(line_count);
9730 screen_start(); /* don't know where cursor is now */
9731 }
9732 else
9733 {
9734 for (i = 0; i < line_count; i++)
9735 {
9736 if (type == USE_T_AL)
9737 {
9738 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009739 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740 out_str(T_AL);
9741 }
9742 else /* type == USE_T_SR */
9743 out_str(T_SR);
9744 screen_start(); /* don't know where cursor is now */
9745 }
9746 }
9747
9748 /*
9749 * With scroll-reverse and 'da' flag set we need to clear the lines that
9750 * have been scrolled down into the region.
9751 */
9752 if (type == USE_T_SR && *T_DA)
9753 {
9754 for (i = 0; i < line_count; ++i)
9755 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009756 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757 out_str(T_CE);
9758 screen_start(); /* don't know where cursor is now */
9759 }
9760 }
9761
9762#ifdef FEAT_GUI
9763 gui_can_update_cursor();
9764 if (gui.in_use)
9765 out_flush(); /* always flush after a scroll */
9766#endif
9767 return OK;
9768}
9769
9770/*
Bram Moolenaar107abd22016-08-12 14:08:25 +02009771 * Delete lines on the screen and update ScreenLines[].
9772 * "end" is the line after the scrolled part. Normally it is Rows.
9773 * When scrolling region used "off" is the offset from the top for the region.
9774 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775 *
9776 * Return OK for success, FAIL if the lines are not deleted.
9777 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009778 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009779screen_del_lines(
9780 int off,
9781 int row,
9782 int line_count,
9783 int end,
9784 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009785 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +01009786 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009787{
9788 int j;
9789 int i;
9790 unsigned temp;
9791 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009792 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009793 int cursor_end;
9794 int result_empty; /* result is empty until end of region */
9795 int can_delete; /* deleting line codes can be used */
9796 int type;
9797
9798 /*
9799 * FAIL if
9800 * - there is no valid screen
9801 * - the screen has to be redrawn completely
9802 * - the line count is less than one
9803 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009804 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009805 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009806 if (!screen_valid(TRUE) || line_count <= 0
9807 || (!force && line_count > p_ttyscroll)
9808#ifdef FEAT_CLIPBOARD
9809 || (clip_star.state != SELECT_CLEARED
9810 && redrawing_for_callback > 0)
9811#endif
9812 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009813 return FAIL;
9814
9815 /*
9816 * Check if the rest of the current region will become empty.
9817 */
9818 result_empty = row + line_count >= end;
9819
9820 /*
9821 * We can delete lines only when 'db' flag not set or when 'ce' option
9822 * available.
9823 */
9824 can_delete = (*T_DB == NUL || can_clear(T_CE));
9825
9826 /*
9827 * There are six ways to delete lines:
9828 * 0. When in a vertically split window and t_CV isn't set, redraw the
9829 * characters from ScreenLines[].
9830 * 1. Use T_CD if it exists and the result is empty.
9831 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9832 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9833 * none of the other ways work.
9834 * 4. Use T_CE (erase line) if the result is empty.
9835 * 5. Use T_DL (delete line) if it exists.
9836 * 6. redraw the characters from ScreenLines[].
9837 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009838 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9839 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009840 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009841 type = USE_T_CD;
9842#if defined(__BEOS__) && defined(BEOS_DR8)
9843 /*
9844 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9845 * its internal termcap... this works okay for tests which test *T_DB !=
9846 * NUL. It has the disadvantage that the user cannot use any :set t_*
9847 * command to get T_DB (back) to empty_option, only :set term=... will do
9848 * the trick...
9849 * Anyway, this hack will hopefully go away with the next OS release.
9850 * (Olaf Seibert)
9851 */
9852 else if (row == 0 && T_DB == empty_option
9853 && (line_count == 1 || *T_CDL == NUL))
9854#else
9855 else if (row == 0 && (
9856#ifndef AMIGA
9857 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9858 * up, so use delete-line command */
9859 line_count == 1 ||
9860#endif
9861 *T_CDL == NUL))
9862#endif
9863 type = USE_NL;
9864 else if (*T_CDL != NUL && line_count > 1 && can_delete)
9865 type = USE_T_CDL;
9866 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +02009867 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009868 type = USE_T_CE;
9869 else if (*T_DL != NUL && can_delete)
9870 type = USE_T_DL;
9871 else if (*T_CDL != NUL && can_delete)
9872 type = USE_T_CDL;
9873 else
9874 return FAIL;
9875
9876#ifdef FEAT_CLIPBOARD
9877 /* Remove a modeless selection when deleting lines halfway the screen or
9878 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009879 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009880 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009881 else
9882 clip_scroll_selection(line_count);
9883#endif
9884
Bram Moolenaar071d4272004-06-13 20:20:40 +00009885#ifdef FEAT_GUI
9886 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9887 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009888 gui_dont_update_cursor(gui.cursor_row >= row + off
9889 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009890#endif
9891
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009892 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
9893 cursor_col = wp->w_wincol;
9894
Bram Moolenaar071d4272004-06-13 20:20:40 +00009895 if (*T_CCS != NUL) /* cursor relative to region */
9896 {
9897 cursor_row = row;
9898 cursor_end = end;
9899 }
9900 else
9901 {
9902 cursor_row = row + off;
9903 cursor_end = end + off;
9904 }
9905
9906 /*
9907 * Now shift LineOffset[] line_count up to reflect the deleted lines.
9908 * Clear the inserted lines in ScreenLines[].
9909 */
9910 row += off;
9911 end += off;
9912 for (i = 0; i < line_count; ++i)
9913 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009914 if (wp != NULL && wp->w_width != Columns)
9915 {
9916 /* need to copy part of a line */
9917 j = row + i;
9918 while ((j += line_count) <= end - 1)
9919 linecopy(j - line_count, j, wp);
9920 j -= line_count;
9921 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009922 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
9923 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924 else
9925 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9926 LineWraps[j] = FALSE;
9927 }
9928 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009929 {
9930 /* whole width, moving the line pointers is faster */
9931 j = row + i;
9932 temp = LineOffset[j];
9933 while ((j += line_count) <= end - 1)
9934 {
9935 LineOffset[j - line_count] = LineOffset[j];
9936 LineWraps[j - line_count] = LineWraps[j];
9937 }
9938 LineOffset[j - line_count] = temp;
9939 LineWraps[j - line_count] = FALSE;
9940 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009941 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009942 else
9943 lineinvalid(temp, (int)Columns);
9944 }
9945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009946
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009947 if (screen_attr != clear_attr)
9948 screen_stop_highlight();
9949 if (clear_attr != 0)
9950 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009951
Bram Moolenaar071d4272004-06-13 20:20:40 +00009952 /* redraw the characters */
9953 if (type == USE_REDRAW)
9954 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02009955 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009956 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009957 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009958 out_str(T_CD);
9959 screen_start(); /* don't know where cursor is now */
9960 }
9961 else if (type == USE_T_CDL)
9962 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009963 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009964 term_delete_lines(line_count);
9965 screen_start(); /* don't know where cursor is now */
9966 }
9967 /*
9968 * Deleting lines at top of the screen or scroll region: Just scroll
9969 * the whole screen (scroll region) up by outputting newlines on the
9970 * last line.
9971 */
9972 else if (type == USE_NL)
9973 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009974 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009975 for (i = line_count; --i >= 0; )
9976 out_char('\n'); /* cursor will remain on same line */
9977 }
9978 else
9979 {
9980 for (i = line_count; --i >= 0; )
9981 {
9982 if (type == USE_T_DL)
9983 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009984 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009985 out_str(T_DL); /* delete a line */
9986 }
9987 else /* type == USE_T_CE */
9988 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009989 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009990 out_str(T_CE); /* erase a line */
9991 }
9992 screen_start(); /* don't know where cursor is now */
9993 }
9994 }
9995
9996 /*
9997 * If the 'db' flag is set, we need to clear the lines that have been
9998 * scrolled up at the bottom of the region.
9999 */
10000 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10001 {
10002 for (i = line_count; i > 0; --i)
10003 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010004 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010005 out_str(T_CE); /* erase a line */
10006 screen_start(); /* don't know where cursor is now */
10007 }
10008 }
10009
10010#ifdef FEAT_GUI
10011 gui_can_update_cursor();
10012 if (gui.in_use)
10013 out_flush(); /* always flush after a scroll */
10014#endif
10015
10016 return OK;
10017}
10018
10019/*
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010020 * Return TRUE when postponing displaying the mode message: when not redrawing
10021 * or inside a mapping.
10022 */
10023 int
10024skip_showmode()
10025{
10026 // Call char_avail() only when we are going to show something, because it
10027 // takes a bit of time. redrawing() may also call char_avail_avail().
10028 if (global_busy
10029 || msg_silent != 0
10030 || !redrawing()
10031 || (char_avail() && !KeyTyped))
10032 {
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010033 redraw_mode = TRUE; // show mode later
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010034 return TRUE;
10035 }
10036 return FALSE;
10037}
10038
10039/*
Bram Moolenaar81226e02018-02-20 21:44:45 +010010040 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010041 *
10042 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10043 * If clear_cmdline is FALSE there may be a message there that needs to be
10044 * cleared only if a mode is shown.
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010045 * If redraw_mode is TRUE show or clear the mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010046 * Return the length of the message (0 if no message).
10047 */
10048 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010049showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010050{
10051 int need_clear;
10052 int length = 0;
10053 int do_mode;
10054 int attr;
10055 int nwr_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010056 int sub_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010058 do_mode = ((p_smd && msg_silent == 0)
10059 && ((State & INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +020010060 || restart_edit != NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010061 || VIsual_active));
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010062 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010063 {
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010064 if (skip_showmode())
10065 return 0; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +000010066
10067 nwr_save = need_wait_return;
10068
10069 /* wait a bit before overwriting an important message */
10070 check_for_delay(FALSE);
10071
10072 /* if the cmdline is more than one line high, erase top lines */
10073 need_clear = clear_cmdline;
10074 if (clear_cmdline && cmdline_row < Rows - 1)
10075 msg_clr_cmdline(); /* will reset clear_cmdline */
10076
10077 /* Position on the last line in the window, column 0 */
10078 msg_pos_mode();
10079 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010080 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010081 if (do_mode)
10082 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010083 msg_puts_attr("--", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010084#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010085 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010086# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010087 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010088# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010089 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010090# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010091 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010092# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar32526b32019-01-19 17:43:09 +010010093 msg_puts_attr(" IM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010094# else
Bram Moolenaar32526b32019-01-19 17:43:09 +010010095 msg_puts_attr(" XIM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010096# endif
10097#endif
10098#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10099 if (gui.in_use)
10100 {
10101 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010102 {
10103 /* HANGUL */
10104 if (enc_utf8)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010105 msg_puts_attr(" \355\225\234\352\270\200", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010106 else
Bram Moolenaar32526b32019-01-19 17:43:09 +010010107 msg_puts_attr(" \307\321\261\333", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010108 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010109 }
10110#endif
Bram Moolenaarea389e92014-05-28 21:40:52 +020010111 /* CTRL-X in Insert mode */
10112 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010113 {
10114 /* These messages can get long, avoid a wrap in a narrow
10115 * window. Prefer showing edit_submode_extra. */
10116 length = (Rows - msg_row) * Columns - 3;
10117 if (edit_submode_extra != NULL)
10118 length -= vim_strsize(edit_submode_extra);
10119 if (length > 0)
10120 {
10121 if (edit_submode_pre != NULL)
10122 length -= vim_strsize(edit_submode_pre);
10123 if (length - vim_strsize(edit_submode) > 0)
10124 {
10125 if (edit_submode_pre != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010126 msg_puts_attr((char *)edit_submode_pre, attr);
10127 msg_puts_attr((char *)edit_submode, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010128 }
10129 if (edit_submode_extra != NULL)
10130 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010131 msg_puts_attr(" ", attr); /* add a space in between */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010132 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010133 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010134 else
10135 sub_attr = attr;
Bram Moolenaar32526b32019-01-19 17:43:09 +010010136 msg_puts_attr((char *)edit_submode_extra, sub_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010137 }
10138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010139 }
10140 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010141 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010142 if (State & VREPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010143 msg_puts_attr(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010144 else if (State & REPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010145 msg_puts_attr(_(" REPLACE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010146 else if (State & INSERT)
10147 {
10148#ifdef FEAT_RIGHTLEFT
10149 if (p_ri)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010150 msg_puts_attr(_(" REVERSE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010151#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +010010152 msg_puts_attr(_(" INSERT"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010153 }
Bram Moolenaar942b4542018-06-17 16:23:34 +020010154 else if (restart_edit == 'I' || restart_edit == 'A')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010155 msg_puts_attr(_(" (insert)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010156 else if (restart_edit == 'R')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010157 msg_puts_attr(_(" (replace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010158 else if (restart_edit == 'V')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010159 msg_puts_attr(_(" (vreplace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010160#ifdef FEAT_RIGHTLEFT
10161 if (p_hkmap)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010162 msg_puts_attr(_(" Hebrew"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010163#endif
10164#ifdef FEAT_KEYMAP
10165 if (State & LANGMAP)
10166 {
10167# ifdef FEAT_ARABIC
10168 if (curwin->w_p_arab)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010169 msg_puts_attr(_(" Arabic"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010170 else
10171# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010172 if (get_keymap_str(curwin, (char_u *)" (%s)",
10173 NameBuff, MAXPATHL))
Bram Moolenaar32526b32019-01-19 17:43:09 +010010174 msg_puts_attr((char *)NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010175 }
10176#endif
10177 if ((State & INSERT) && p_paste)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010178 msg_puts_attr(_(" (paste)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010179
Bram Moolenaar071d4272004-06-13 20:20:40 +000010180 if (VIsual_active)
10181 {
10182 char *p;
10183
10184 /* Don't concatenate separate words to avoid translation
10185 * problems. */
10186 switch ((VIsual_select ? 4 : 0)
10187 + (VIsual_mode == Ctrl_V) * 2
10188 + (VIsual_mode == 'V'))
10189 {
10190 case 0: p = N_(" VISUAL"); break;
10191 case 1: p = N_(" VISUAL LINE"); break;
10192 case 2: p = N_(" VISUAL BLOCK"); break;
10193 case 4: p = N_(" SELECT"); break;
10194 case 5: p = N_(" SELECT LINE"); break;
10195 default: p = N_(" SELECT BLOCK"); break;
10196 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010197 msg_puts_attr(_(p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010198 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010199 msg_puts_attr(" --", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010200 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010201
Bram Moolenaar071d4272004-06-13 20:20:40 +000010202 need_clear = TRUE;
10203 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010204 if (reg_recording != 0
Bram Moolenaare2c453d2019-08-21 14:37:09 +020010205 && edit_submode == NULL) // otherwise it gets too long
Bram Moolenaar071d4272004-06-13 20:20:40 +000010206 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010207 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010208 need_clear = TRUE;
10209 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010210
10211 mode_displayed = TRUE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010212 if (need_clear || clear_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010213 msg_clr_eos();
10214 msg_didout = FALSE; /* overwrite this message */
10215 length = msg_col;
10216 msg_col = 0;
10217 need_wait_return = nwr_save; /* never ask for hit-return for this */
10218 }
10219 else if (clear_cmdline && msg_silent == 0)
10220 /* Clear the whole command line. Will reset "clear_cmdline". */
10221 msg_clr_cmdline();
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010222 else if (redraw_mode)
10223 {
10224 msg_pos_mode();
10225 msg_clr_eos();
10226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010227
10228#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010229 /* In Visual mode the size of the selected area must be redrawn. */
10230 if (VIsual_active)
10231 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010232
10233 /* If the last window has no status line, the ruler is after the mode
10234 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010235 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +020010236 win_redr_ruler(lastwin, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010237#endif
10238 redraw_cmdline = FALSE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010239 redraw_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010240 clear_cmdline = FALSE;
10241
10242 return length;
10243}
10244
10245/*
10246 * Position for a mode message.
10247 */
10248 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010249msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010250{
10251 msg_col = 0;
10252 msg_row = Rows - 1;
10253}
10254
10255/*
10256 * Delete mode message. Used when ESC is typed which is expected to end
10257 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010258 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010259 */
10260 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010261unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010262{
10263 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010264 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010265 */
10266 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10267 redraw_cmdline = TRUE; /* delete mode later */
10268 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010269 clearmode();
10270}
10271
10272/*
10273 * Clear the mode message.
10274 */
10275 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010276clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010277{
Bram Moolenaar2abad542018-05-19 14:43:45 +020010278 int save_msg_row = msg_row;
10279 int save_msg_col = msg_col;
10280
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010281 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010282 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010283 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010284 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +020010285
10286 msg_col = save_msg_col;
10287 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010288}
10289
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010290 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010291recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010292{
Bram Moolenaar32526b32019-01-19 17:43:09 +010010293 msg_puts_attr(_("recording"), attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010294 if (!shortmess(SHM_RECORDING))
10295 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010296 char s[4];
10297
10298 sprintf(s, " @%c", reg_recording);
10299 msg_puts_attr(s, attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010300 }
10301}
10302
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010303/*
10304 * Draw the tab pages line at the top of the Vim window.
10305 */
Bram Moolenaare12bab32019-01-08 22:02:56 +010010306 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010307draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010308{
10309 int tabcount = 0;
10310 tabpage_T *tp;
10311 int tabwidth;
10312 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010313 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010314 int attr;
10315 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010316 win_T *cwp;
10317 int wincount;
10318 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010319 int c;
10320 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010321 int attr_sel = HL_ATTR(HLF_TPS);
10322 int attr_nosel = HL_ATTR(HLF_TP);
10323 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010324 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010325 int room;
10326 int use_sep_chars = (t_colors < 8
10327#ifdef FEAT_GUI
10328 && !gui.in_use
10329#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010330#ifdef FEAT_TERMGUICOLORS
10331 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010332#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010333 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010334
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010335 if (ScreenLines == NULL)
10336 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010337 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010338
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010339#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010340 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010341 if (gui_use_tabline())
10342 {
10343 gui_update_tabline();
10344 return;
10345 }
10346#endif
10347
10348 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010349 return;
10350
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010351#if defined(FEAT_STL_OPT)
Bram Moolenaarca57ab52019-04-13 14:53:16 +020010352 clear_TabPageIdxs();
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010353
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010354 /* Use the 'tabline' option if it's set. */
10355 if (*p_tal != NUL)
10356 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010357 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010358
10359 /* Check for an error. If there is one we would loop in redrawing the
10360 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010361 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010362 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010363 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010364 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010365 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010366 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010367 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010368 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010369#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010370 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010371 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010372 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010373
Bram Moolenaar238a5642006-02-21 22:12:05 +000010374 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10375 if (tabwidth < 6)
10376 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010377
Bram Moolenaar238a5642006-02-21 22:12:05 +000010378 attr = attr_nosel;
10379 tabcount = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010380 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10381 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010382 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010383 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010384
Bram Moolenaar238a5642006-02-21 22:12:05 +000010385 if (tp->tp_topframe == topframe)
10386 attr = attr_sel;
10387 if (use_sep_chars && col > 0)
10388 screen_putchar('|', 0, col++, attr);
10389
10390 if (tp->tp_topframe != topframe)
10391 attr = attr_nosel;
10392
10393 screen_putchar(' ', 0, col++, attr);
10394
10395 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010396 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010397 cwp = curwin;
10398 wp = firstwin;
10399 }
10400 else
10401 {
10402 cwp = tp->tp_curwin;
10403 wp = tp->tp_firstwin;
10404 }
10405
10406 modified = FALSE;
10407 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10408 if (bufIsChanged(wp->w_buffer))
10409 modified = TRUE;
10410 if (modified || wincount > 1)
10411 {
10412 if (wincount > 1)
10413 {
10414 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010415 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010416 if (col + len >= Columns - 3)
10417 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010418 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010419#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010420 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010421#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010422 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010423#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010424 );
10425 col += len;
10426 }
10427 if (modified)
10428 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10429 screen_putchar(' ', 0, col++, attr);
10430 }
10431
10432 room = scol - col + tabwidth - 1;
10433 if (room > 0)
10434 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010435 /* Get buffer name in NameBuff[] */
10436 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010437 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010438 len = vim_strsize(NameBuff);
10439 p = NameBuff;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010440 if (has_mbyte)
10441 while (len > room)
10442 {
10443 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010444 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010445 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010010446 else if (len > room)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010447 {
10448 p += len - room;
10449 len = room;
10450 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010451 if (len > Columns - col - 1)
10452 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010453
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010454 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010455 col += len;
10456 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010457 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010458
10459 /* Store the tab page number in TabPageIdxs[], so that
10460 * jump_to_mouse() knows where each one is. */
10461 ++tabcount;
10462 while (scol < col)
10463 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010464 }
10465
Bram Moolenaar238a5642006-02-21 22:12:05 +000010466 if (use_sep_chars)
10467 c = '_';
10468 else
10469 c = ' ';
10470 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010471
10472 /* Put an "X" for closing the current tab if there are several. */
10473 if (first_tabpage->tp_next != NULL)
10474 {
10475 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10476 TabPageIdxs[Columns - 1] = -999;
10477 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010478 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010479
10480 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10481 * set. */
10482 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010483}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010484
10485/*
10486 * Get buffer name for "buf" into NameBuff[].
10487 * Takes care of special buffer names and translates special characters.
10488 */
10489 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010490get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010491{
10492 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010493 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010494 else
10495 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10496 trans_characters(NameBuff, MAXPATHL);
10497}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010498
Bram Moolenaar071d4272004-06-13 20:20:40 +000010499/*
10500 * Get the character to use in a status line. Get its attributes in "*attr".
10501 */
10502 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010503fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010504{
10505 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010506
10507#ifdef FEAT_TERMINAL
10508 if (bt_terminal(wp->w_buffer))
10509 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010510 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010511 {
10512 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010513 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010514 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010515 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010516 {
10517 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010518 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010519 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010520 }
10521 else
10522#endif
10523 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010524 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010525 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010526 fill = fill_stl;
10527 }
10528 else
10529 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010530 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010531 fill = fill_stlnc;
10532 }
10533 /* Use fill when there is highlighting, and highlighting of current
10534 * window differs, or the fillchars differ, or this is not the
10535 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010536 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010537 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010538 || (fill_stl != fill_stlnc)))
10539 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010540 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010541 return '^';
10542 return '=';
10543}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010544
Bram Moolenaar071d4272004-06-13 20:20:40 +000010545/*
10546 * Get the character to use in a separator between vertically split windows.
10547 * Get its attributes in "*attr".
10548 */
10549 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010550fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010551{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010552 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010553 if (*attr == 0 && fill_vert == ' ')
10554 return '|';
10555 else
10556 return fill_vert;
10557}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010558
10559/*
10560 * Return TRUE if redrawing should currently be done.
10561 */
10562 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010563redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010564{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010565#ifdef FEAT_EVAL
10566 if (disable_redraw_for_testing)
10567 return 0;
10568 else
10569#endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +020010570 return ((!RedrawingDisabled
10571#ifdef FEAT_EVAL
10572 || ignore_redraw_flag_for_testing
10573#endif
10574 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010575}
10576
10577/*
10578 * Return TRUE if printing messages should currently be done.
10579 */
10580 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010581messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010582{
10583 return (!(p_lz && char_avail() && !KeyTyped));
10584}
10585
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010586#ifdef FEAT_MENU
10587/*
10588 * Draw the window toolbar.
10589 */
10590 static void
10591redraw_win_toolbar(win_T *wp)
10592{
10593 vimmenu_T *menu;
10594 int item_idx = 0;
10595 int item_count = 0;
10596 int col = 0;
10597 int next_col;
10598 int off = (int)(current_ScreenLine - ScreenLines);
10599 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
10600 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
10601
10602 vim_free(wp->w_winbar_items);
10603 for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
10604 ++item_count;
Bram Moolenaarc799fe22019-05-28 23:08:19 +020010605 wp->w_winbar_items = ALLOC_CLEAR_MULT(winbar_item_T, item_count + 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010606
10607 /* TODO: use fewer spaces if there is not enough room */
10608 for (menu = wp->w_winbar->children;
Bram Moolenaar02631462017-09-22 15:20:32 +020010609 menu != NULL && col < wp->w_width; menu = menu->next)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010610 {
10611 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010612 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010613 break;
10614 if (col > 1)
10615 {
10616 space_to_screenline(off + col, fill_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
10621 wp->w_winbar_items[item_idx].wb_startcol = col;
10622 space_to_screenline(off + col, button_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010623 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010624 break;
10625
10626 next_col = text_to_screenline(wp, menu->name, col);
10627 while (col < next_col)
10628 {
10629 ScreenAttrs[off + col] = button_attr;
10630 ++col;
10631 }
10632 wp->w_winbar_items[item_idx].wb_endcol = col;
10633 wp->w_winbar_items[item_idx].wb_menu = menu;
10634 ++item_idx;
10635
Bram Moolenaar02631462017-09-22 15:20:32 +020010636 if (col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010637 break;
10638 space_to_screenline(off + col, button_attr);
10639 ++col;
10640 }
Bram Moolenaar02631462017-09-22 15:20:32 +020010641 while (col < wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010642 {
10643 space_to_screenline(off + col, fill_attr);
10644 ++col;
10645 }
10646 wp->w_winbar_items[item_idx].wb_menu = NULL; /* end marker */
10647
Bram Moolenaar02631462017-09-22 15:20:32 +020010648 screen_line(wp->w_winrow, wp->w_wincol, (int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +020010649 (int)wp->w_width, 0);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010650}
10651#endif
Bram Moolenaar491ac282018-06-17 14:47:55 +020010652
Bram Moolenaar071d4272004-06-13 20:20:40 +000010653/*
10654 * Show current status info in ruler and various other places
10655 * If always is FALSE, only show ruler if position has changed.
10656 */
10657 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010658showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010659{
10660 if (!always && !redrawing())
10661 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010662 if (pum_visible())
10663 {
10664 /* Don't redraw right now, do it later. */
10665 curwin->w_redr_status = TRUE;
10666 return;
10667 }
Bram Moolenaar4033c552017-09-16 20:54:51 +020010668#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010669 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar362f3562009-11-03 16:20:34 +000010670 redraw_custom_statusline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010671 else
10672#endif
10673#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +020010674 win_redr_ruler(curwin, always, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010675#endif
10676
10677#ifdef FEAT_TITLE
10678 if (need_maketitle
10679# ifdef FEAT_STL_OPT
10680 || (p_icon && (stl_syntax & STL_IN_ICON))
10681 || (p_title && (stl_syntax & STL_IN_TITLE))
10682# endif
10683 )
10684 maketitle();
10685#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010686 /* Redraw the tab pages line if needed. */
10687 if (redraw_tabline)
10688 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010689}
10690
10691#ifdef FEAT_CMDL_INFO
10692 static void
Bram Moolenaar491ac282018-06-17 14:47:55 +020010693win_redr_ruler(win_T *wp, int always, int ignore_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010694{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010695#define RULER_BUF_LEN 70
10696 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010697 int row;
10698 int fillchar;
10699 int attr;
10700 int empty_line = FALSE;
10701 colnr_T virtcol;
10702 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010703 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010704 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010705 int this_ru_col;
10706 int off = 0;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +010010707 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010708
10709 /* If 'ruler' off or redrawing disabled, don't do anything */
10710 if (!p_ru)
10711 return;
10712
10713 /*
10714 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10715 * after deleting lines, before cursor.lnum is corrected.
10716 */
10717 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10718 return;
10719
Bram Moolenaare2c453d2019-08-21 14:37:09 +020010720 // Don't draw the ruler while doing insert-completion, it might overwrite
10721 // the (long) mode message.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010722 if (wp == lastwin && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010723 if (edit_submode != NULL)
10724 return;
Bram Moolenaar491ac282018-06-17 14:47:55 +020010725 // Don't draw the ruler when the popup menu is visible, it may overlap.
10726 // Except when the popup menu will be redrawn anyway.
10727 if (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010728 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010729
10730#ifdef FEAT_STL_OPT
10731 if (*p_ruf)
10732 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010733 int save_called_emsg = called_emsg;
10734
10735 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010736 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010737 if (called_emsg)
10738 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010739 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010740 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010741 return;
10742 }
10743#endif
10744
10745 /*
10746 * Check if not in Insert mode and the line is empty (will show "0-1").
10747 */
10748 if (!(State & INSERT)
10749 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10750 empty_line = TRUE;
10751
10752 /*
10753 * Only draw the ruler when something changed.
10754 */
10755 validate_virtcol_win(wp);
10756 if ( redraw_cmdline
10757 || always
10758 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10759 || wp->w_cursor.col != wp->w_ru_cursor.col
10760 || wp->w_virtcol != wp->w_ru_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +000010761 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
Bram Moolenaar071d4272004-06-13 20:20:40 +000010762 || wp->w_topline != wp->w_ru_topline
10763 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10764#ifdef FEAT_DIFF
10765 || wp->w_topfill != wp->w_ru_topfill
10766#endif
10767 || empty_line != wp->w_ru_empty)
10768 {
10769 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010770 if (wp->w_status_height)
10771 {
10772 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010773 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar53f81742017-09-22 14:35:51 +020010774 off = wp->w_wincol;
Bram Moolenaar02631462017-09-22 15:20:32 +020010775 width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010776 }
10777 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010778 {
10779 row = Rows - 1;
10780 fillchar = ' ';
10781 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010782 width = Columns;
10783 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010784 }
10785
10786 /* In list mode virtcol needs to be recomputed */
10787 virtcol = wp->w_virtcol;
10788 if (wp->w_p_list && lcs_tab1 == NUL)
10789 {
10790 wp->w_p_list = FALSE;
10791 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10792 wp->w_p_list = TRUE;
10793 }
10794
10795 /*
10796 * Some sprintfs return the length, some return a pointer.
10797 * To avoid portability problems we use strlen() here.
10798 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010799 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010800 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10801 ? 0L
10802 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010803 len = STRLEN(buffer);
10804 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010805 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10806 (int)virtcol + 1);
10807
10808 /*
10809 * Add a "50%" if there is room for it.
10810 * On the last line, don't print in the last column (scrolls the
10811 * screen up on some terminals).
10812 */
10813 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010814 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010815 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010816 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010817 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010818 this_ru_col = ru_col - (Columns - width);
10819 if (this_ru_col < 0)
10820 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010821 /* Never use more than half the window/screen width, leave the other
10822 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010823 if (this_ru_col < (width + 1) / 2)
10824 this_ru_col = (width + 1) / 2;
10825 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010826 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010010827 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010828 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010829 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010830 if (has_mbyte)
10831 i += (*mb_char2bytes)(fillchar, buffer + i);
10832 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010833 buffer[i++] = fillchar;
10834 ++o;
10835 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010836 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010837 }
10838 /* Truncate at window boundary. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010839 if (has_mbyte)
10840 {
10841 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010842 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010843 {
10844 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010845 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010846 {
10847 buffer[i] = NUL;
10848 break;
10849 }
10850 }
10851 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010010852 else if (this_ru_col + (int)STRLEN(buffer) > width)
Bram Moolenaar4033c552017-09-16 20:54:51 +020010853 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010854
Bram Moolenaar4033c552017-09-16 20:54:51 +020010855 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010856 i = redraw_cmdline;
10857 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020010858 this_ru_col + off + (int)STRLEN(buffer),
10859 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010860 fillchar, fillchar, attr);
10861 /* don't redraw the cmdline because of showing the ruler */
10862 redraw_cmdline = i;
10863 wp->w_ru_cursor = wp->w_cursor;
10864 wp->w_ru_virtcol = wp->w_virtcol;
10865 wp->w_ru_empty = empty_line;
10866 wp->w_ru_topline = wp->w_topline;
10867 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10868#ifdef FEAT_DIFF
10869 wp->w_ru_topfill = wp->w_topfill;
10870#endif
10871 }
10872}
10873#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010874
Bram Moolenaare677df82019-09-02 22:31:11 +020010875/*
10876 * Compute columns for ruler and shown command. 'sc_col' is also used to
10877 * decide what the maximum length of a message on the status line can be.
10878 * If there is a status line for the last window, 'sc_col' is independent
10879 * of 'ru_col'.
10880 */
10881
10882#define COL_RULER 17 // columns needed by standard ruler
10883
10884 void
10885comp_col(void)
10886{
10887#if defined(FEAT_CMDL_INFO)
10888 int last_has_status = (p_ls == 2 || (p_ls == 1 && !ONE_WINDOW));
10889
10890 sc_col = 0;
10891 ru_col = 0;
10892 if (p_ru)
10893 {
10894# ifdef FEAT_STL_OPT
10895 ru_col = (ru_wid ? ru_wid : COL_RULER) + 1;
10896# else
10897 ru_col = COL_RULER + 1;
10898# endif
10899 // no last status line, adjust sc_col
10900 if (!last_has_status)
10901 sc_col = ru_col;
10902 }
10903 if (p_sc)
10904 {
10905 sc_col += SHOWCMD_COLS;
10906 if (!p_ru || last_has_status) // no need for separating space
10907 ++sc_col;
10908 }
10909 sc_col = Columns - sc_col;
10910 ru_col = Columns - ru_col;
10911 if (sc_col <= 0) // screen too narrow, will become a mess
10912 sc_col = 1;
10913 if (ru_col <= 0)
10914 ru_col = 1;
10915#else
10916 sc_col = Columns;
10917 ru_col = Columns;
10918#endif
10919#ifdef FEAT_EVAL
10920 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
10921#endif
10922}
10923
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010924#if defined(FEAT_LINEBREAK) || defined(PROTO)
10925/*
Bram Moolenaar64486672010-05-16 15:46:46 +020010926 * Return the width of the 'number' and 'relativenumber' column.
10927 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010928 * Otherwise it depends on 'numberwidth' and the line count.
10929 */
10930 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010931number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010932{
10933 int n;
10934 linenr_T lnum;
10935
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020010936 if (wp->w_p_rnu && !wp->w_p_nu)
10937 /* cursor line shows "0" */
10938 lnum = wp->w_height;
10939 else
10940 /* cursor line shows absolute line number */
10941 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020010942
Bram Moolenaar6b314672015-03-20 15:42:10 +010010943 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010944 return wp->w_nrwidth_width;
10945 wp->w_nrwidth_line_count = lnum;
10946
10947 n = 0;
10948 do
10949 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010950 lnum /= 10;
10951 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010952 } while (lnum > 0);
10953
10954 /* 'numberwidth' gives the minimal width plus one */
10955 if (n < wp->w_p_nuw - 1)
10956 n = wp->w_p_nuw - 1;
10957
Bram Moolenaare4b407f2019-07-04 11:59:28 +020010958# ifdef FEAT_SIGNS
10959 // If 'signcolumn' is set to 'number' and there is a sign to display, then
10960 // the minimal width for the number column is 2.
10961 if (n < 2 && (wp->w_buffer->b_signlist != NULL)
10962 && (*wp->w_p_scl == 'n' && *(wp->w_p_scl + 1) == 'u'))
10963 n = 2;
10964# endif
10965
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010966 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010010967 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010968 return n;
10969}
10970#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010971
Bram Moolenaar113e1072019-01-20 15:30:40 +010010972#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010973/*
10974 * Return the current cursor column. This is the actual position on the
10975 * screen. First column is 0.
10976 */
10977 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010978screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010979{
10980 return screen_cur_col;
10981}
10982
10983/*
10984 * Return the current cursor row. This is the actual position on the screen.
10985 * First row is 0.
10986 */
10987 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010988screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010989{
10990 return screen_cur_row;
10991}
Bram Moolenaar113e1072019-01-20 15:30:40 +010010992#endif
Bram Moolenaare677df82019-09-02 22:31:11 +020010993
10994/*
10995 * Handle setting 'listchars' or 'fillchars'.
10996 * Returns error message, NULL if it's OK.
10997 */
10998 char *
10999set_chars_option(char_u **varp)
11000{
11001 int round, i, len, entries;
11002 char_u *p, *s;
11003 int c1 = 0, c2 = 0, c3 = 0;
11004 struct charstab
11005 {
11006 int *cp;
11007 char *name;
11008 };
11009 static struct charstab filltab[] =
11010 {
11011 {&fill_stl, "stl"},
11012 {&fill_stlnc, "stlnc"},
11013 {&fill_vert, "vert"},
11014 {&fill_fold, "fold"},
11015 {&fill_diff, "diff"},
11016 };
11017 static struct charstab lcstab[] =
11018 {
11019 {&lcs_eol, "eol"},
11020 {&lcs_ext, "extends"},
11021 {&lcs_nbsp, "nbsp"},
11022 {&lcs_prec, "precedes"},
11023 {&lcs_space, "space"},
11024 {&lcs_tab2, "tab"},
11025 {&lcs_trail, "trail"},
11026#ifdef FEAT_CONCEAL
11027 {&lcs_conceal, "conceal"},
11028#else
11029 {NULL, "conceal"},
11030#endif
11031 };
11032 struct charstab *tab;
11033
11034 if (varp == &p_lcs)
11035 {
11036 tab = lcstab;
11037 entries = sizeof(lcstab) / sizeof(struct charstab);
11038 }
11039 else
11040 {
11041 tab = filltab;
11042 entries = sizeof(filltab) / sizeof(struct charstab);
11043 }
11044
11045 // first round: check for valid value, second round: assign values
11046 for (round = 0; round <= 1; ++round)
11047 {
11048 if (round > 0)
11049 {
11050 // After checking that the value is valid: set defaults: space for
11051 // 'fillchars', NUL for 'listchars'
11052 for (i = 0; i < entries; ++i)
11053 if (tab[i].cp != NULL)
11054 *(tab[i].cp) = (varp == &p_lcs ? NUL : ' ');
11055
11056 if (varp == &p_lcs)
11057 {
11058 lcs_tab1 = NUL;
11059 lcs_tab3 = NUL;
11060 }
11061 else
11062 fill_diff = '-';
11063 }
11064 p = *varp;
11065 while (*p)
11066 {
11067 for (i = 0; i < entries; ++i)
11068 {
11069 len = (int)STRLEN(tab[i].name);
11070 if (STRNCMP(p, tab[i].name, len) == 0
11071 && p[len] == ':'
11072 && p[len + 1] != NUL)
11073 {
11074 c2 = c3 = 0;
11075 s = p + len + 1;
11076 c1 = mb_ptr2char_adv(&s);
11077 if (mb_char2cells(c1) > 1)
11078 continue;
11079 if (tab[i].cp == &lcs_tab2)
11080 {
11081 if (*s == NUL)
11082 continue;
11083 c2 = mb_ptr2char_adv(&s);
11084 if (mb_char2cells(c2) > 1)
11085 continue;
11086 if (!(*s == ',' || *s == NUL))
11087 {
11088 c3 = mb_ptr2char_adv(&s);
11089 if (mb_char2cells(c3) > 1)
11090 continue;
11091 }
11092 }
11093
11094 if (*s == ',' || *s == NUL)
11095 {
11096 if (round)
11097 {
11098 if (tab[i].cp == &lcs_tab2)
11099 {
11100 lcs_tab1 = c1;
11101 lcs_tab2 = c2;
11102 lcs_tab3 = c3;
11103 }
11104 else if (tab[i].cp != NULL)
11105 *(tab[i].cp) = c1;
11106
11107 }
11108 p = s;
11109 break;
11110 }
11111 }
11112 }
11113
11114 if (i == entries)
11115 return e_invarg;
11116 if (*p == ',')
11117 ++p;
11118 }
11119 }
11120
11121 return NULL; // no error
11122}
Bram Moolenaar017ba072019-09-14 21:01:23 +020011123
11124#ifdef FEAT_SYN_HL
11125/*
11126 * Used when 'cursorlineopt' contains "screenline": compute the margins between
11127 * which the highlighting is used.
11128 */
11129 static void
11130margin_columns_win(win_T *wp, int *left_col, int *right_col)
11131{
11132 // cache previous calculations depending on w_virtcol
11133 static int saved_w_virtcol;
11134 static win_T *prev_wp;
11135 static int prev_left_col;
11136 static int prev_right_col;
11137 static int prev_col_off;
11138
11139 int cur_col_off = win_col_off(wp);
11140 int width1;
11141 int width2;
11142
11143 if (saved_w_virtcol == wp->w_virtcol
11144 && prev_wp == wp && prev_col_off == cur_col_off)
11145 {
11146 *right_col = prev_right_col;
11147 *left_col = prev_left_col;
11148 return;
11149 }
11150
11151 width1 = wp->w_width - cur_col_off;
11152 width2 = width1 + win_col_off2(wp);
11153
11154 *left_col = 0;
11155 *right_col = width1;
11156
11157 if (wp->w_virtcol >= (colnr_T)width1)
11158 *right_col = width1 + ((wp->w_virtcol - width1) / width2 + 1) * width2;
11159 if (wp->w_virtcol >= (colnr_T)width1 && width2 > 0)
11160 *left_col = (wp->w_virtcol - width1) / width2 * width2 + width1;
11161
11162 // cache values
11163 prev_left_col = *left_col;
11164 prev_right_col = *right_col;
11165 prev_wp = wp;
11166 saved_w_virtcol = wp->w_virtcol;
11167 prev_col_off = cur_col_off;
11168}
11169#endif