blob: 4d976fd6863e8babfc8c25ad54f01a4bd3331a0a [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
Bram Moolenaare00fdf32019-09-15 19:09:42 +02004766 if (line_attr)
4767 char_attr = hl_combine_attr(
4768 comb_attr, line_attr);
4769 else
Bram Moolenaar017ba072019-09-14 21:01:23 +02004770 char_attr = comb_attr;
4771 }
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004772 else
Bram Moolenaarde24a872019-05-05 15:48:00 +02004773 char_attr = hl_combine_attr(comb_attr, char_attr);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004774 }
Bram Moolenaarc095b282010-07-20 22:33:34 +02004775# ifdef FEAT_CONCEAL
Bram Moolenaare00fdf32019-09-15 19:09:42 +02004776 // no concealing past the end of the line, it interferes
4777 // with line highlighting
Bram Moolenaarc095b282010-07-20 22:33:34 +02004778 if (c == NUL)
4779 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004780 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004781 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004782# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004784#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004785
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004786#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004787 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004788 * Only do this when there is no syntax highlighting, the
4789 * @Spell cluster is not used or the current syntax item
4790 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004791 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004792 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004793 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004794 if (c != 0 && (
4795# ifdef FEAT_SYN_HL
4796 !has_syntax ||
4797# endif
4798 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004799 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004800 char_u *prev_ptr, *p;
4801 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004802 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaare7566042005-06-17 22:00:15 +00004803 if (has_mbyte)
4804 {
4805 prev_ptr = ptr - mb_l;
4806 v -= mb_l - 1;
4807 }
4808 else
Bram Moolenaare7566042005-06-17 22:00:15 +00004809 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004810
4811 /* Use nextline[] if possible, it has the start of the
4812 * next line concatenated. */
4813 if ((prev_ptr - line) - nextlinecol >= 0)
4814 p = nextline + (prev_ptr - line) - nextlinecol;
4815 else
4816 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004817 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004818 len = spell_check(wp, p, &spell_hlf, &cap_col,
4819 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004820 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004821
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004822 /* In Insert mode only highlight a word that
4823 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004824 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004825 && (State & INSERT) != 0
4826 && wp->w_cursor.lnum == lnum
4827 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004828 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004829 && wp->w_cursor.col < (colnr_T)word_end)
4830 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004831 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004832 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004833 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004834
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004835 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004836 && (p - nextline) + len > nextline_idx)
4837 {
4838 /* Remember that the good word continues at the
4839 * start of the next line. */
4840 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004841 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004842 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004843
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004844 /* Turn index into actual attributes. */
4845 if (spell_hlf != HLF_COUNT)
4846 spell_attr = highlight_attr[spell_hlf];
4847
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004848 if (cap_col > 0)
4849 {
4850 if (p != prev_ptr
4851 && (p - nextline) + cap_col >= nextline_idx)
4852 {
4853 /* Remember that the word in the next line
4854 * must start with a capital. */
4855 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004856 cap_col = (int)((p - nextline) + cap_col
4857 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004858 }
4859 else
4860 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004861 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004862 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004863 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004864 }
4865 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004866 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004867 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004868 char_attr = hl_combine_attr(char_attr, spell_attr);
4869 else
4870 char_attr = hl_combine_attr(spell_attr, char_attr);
4871 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872#endif
4873#ifdef FEAT_LINEBREAK
4874 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004875 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004877 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004878 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 {
Bram Moolenaar4df70292015-03-21 14:20:16 +01004880 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004881 char_u *p = ptr - (mb_off + 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004882
Bram Moolenaar597a4222014-06-25 14:39:50 +02004883 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004884 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004885 NULL) - 1;
Bram Moolenaar02631462017-09-22 15:20:32 +02004886 if (c == TAB && n_extra + col > wp->w_width)
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004887# ifdef FEAT_VARTABS
Bram Moolenaara87b72c2018-06-25 21:24:51 +02004888 n_extra = tabstop_padding(vcol, wp->w_buffer->b_p_ts,
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004889 wp->w_buffer->b_p_vts_array) - 1;
4890# else
Bram Moolenaara3650912014-11-19 13:21:57 +01004891 n_extra = (int)wp->w_buffer->b_p_ts
4892 - vcol % (int)wp->w_buffer->b_p_ts - 1;
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004893# endif
Bram Moolenaara3650912014-11-19 13:21:57 +01004894
Bram Moolenaar4df70292015-03-21 14:20:16 +01004895 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004896 c_final = NUL;
Bram Moolenaar1c465442017-03-12 20:10:05 +01004897 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004898 {
4899#ifdef FEAT_CONCEAL
4900 if (c == TAB)
4901 /* See "Tab alignment" below. */
4902 FIX_FOR_BOGUSCOLS;
4903#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004904 if (!wp->w_p_list)
4905 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 }
4908#endif
4909
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004910 // 'list': Change char 160 to lcs_nbsp and space to lcs_space.
4911 // But not when the character is followed by a composing
4912 // character (use mb_l to check that).
4913 if (wp->w_p_list
4914 && ((((c == 160 && mb_l == 1)
4915 || (mb_utf8
4916 && ((mb_c == 160 && mb_l == 2)
4917 || (mb_c == 0x202f && mb_l == 3))))
4918 && lcs_nbsp)
4919 || (c == ' '
4920 && mb_l == 1
4921 && lcs_space
4922 && ptr - line <= trailcol)))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004923 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004924 c = (c == ' ') ? lcs_space : lcs_nbsp;
4925 if (area_attr == 0 && search_attr == 0)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004926 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004927 n_attr = 1;
4928 extra_attr = HL_ATTR(HLF_8);
4929 saved_attr2 = char_attr; /* save current attr */
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004930 }
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004931 mb_c = c;
4932 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004933 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004934 mb_utf8 = TRUE;
4935 u8cc[0] = 0;
4936 c = 0xc0;
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004937 }
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004938 else
4939 mb_utf8 = FALSE;
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004940 }
4941
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4943 {
4944 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004945 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 {
4947 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004948 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 saved_attr2 = char_attr; /* save current attr */
4950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004952 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 {
4954 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004955 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004956 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 }
4958 else
4959 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 }
4961 }
4962
4963 /*
4964 * Handling of non-printable characters.
4965 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004966 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 {
4968 /*
4969 * when getting a character from the file, we may have to
4970 * turn it into something else on the way to putting it
4971 * into "ScreenLines".
4972 */
4973 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4974 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004975 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004976 long vcol_adjusted = vcol; /* removed showbreak length */
4977#ifdef FEAT_LINEBREAK
4978 /* only adjust the tab_len, when at the first column
4979 * after the showbreak value was drawn */
4980 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4981 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4982#endif
Bram Moolenaar69cbbec2019-08-17 14:10:56 +02004983 // tab amount depends on current column
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004984#ifdef FEAT_VARTABS
4985 tab_len = tabstop_padding(vcol_adjusted,
4986 wp->w_buffer->b_p_ts,
4987 wp->w_buffer->b_p_vts_array) - 1;
4988#else
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004989 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004990 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4991#endif
Bram Moolenaard574ea22015-01-14 19:35:14 +01004992
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004993#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004994 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004995#endif
Bram Moolenaar69cbbec2019-08-17 14:10:56 +02004996 // tab amount depends on current column
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004997 n_extra = tab_len;
4998#ifdef FEAT_LINEBREAK
4999 else
5000 {
Bram Moolenaar69cbbec2019-08-17 14:10:56 +02005001 char_u *p;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01005002 int len;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005003 int i;
5004 int saved_nextra = n_extra;
5005
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005006#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005007 if (vcol_off > 0)
Bram Moolenaar69cbbec2019-08-17 14:10:56 +02005008 // there are characters to conceal
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005009 tab_len += vcol_off;
Bram Moolenaar69cbbec2019-08-17 14:10:56 +02005010 // boguscols before FIX_FOR_BOGUSCOLS macro from above
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005011 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
5012 && n_extra > tab_len)
5013 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005014#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005015
Bram Moolenaar69cbbec2019-08-17 14:10:56 +02005016 // if n_extra > 0, it gives the number of chars, to
5017 // use for a tab, else we need to calculate the width
5018 // for a tab
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005019 len = (tab_len * mb_char2len(lcs_tab2));
5020 if (n_extra > 0)
5021 len += n_extra - tab_len;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005022 c = lcs_tab1;
Bram Moolenaar964b3742019-05-24 18:54:09 +02005023 p = alloc(len + 1);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005024 vim_memset(p, ' ', len);
5025 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005026 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005027 p_extra_free = p;
5028 for (i = 0; i < tab_len; i++)
5029 {
Bram Moolenaar69cbbec2019-08-17 14:10:56 +02005030 int lcs = lcs_tab2;
5031
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02005032 if (*p == NUL)
5033 {
5034 tab_len = i;
5035 break;
5036 }
Bram Moolenaar69cbbec2019-08-17 14:10:56 +02005037
5038 // if lcs_tab3 is given, need to change the char
5039 // for tab
5040 if (lcs_tab3 && i == tab_len - 1)
5041 lcs = lcs_tab3;
5042 mb_char2bytes(lcs, p);
5043 p += mb_char2len(lcs);
5044 n_extra += mb_char2len(lcs)
5045 - (saved_nextra > 0 ? 1 : 0);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005046 }
5047 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005048#ifdef FEAT_CONCEAL
Bram Moolenaar69cbbec2019-08-17 14:10:56 +02005049 // n_extra will be increased by FIX_FOX_BOGUSCOLS
5050 // macro below, so need to adjust for that here
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005051 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005052 n_extra -= vcol_off;
5053#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005054 }
5055#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005056#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005057 {
5058 int vc_saved = vcol_off;
5059
5060 /* Tab alignment should be identical regardless of
5061 * 'conceallevel' value. So tab compensates of all
5062 * previous concealed characters, and thus resets
5063 * vcol_off and boguscols accumulated so far in the
5064 * line. Note that the tab can be longer than
5065 * 'tabstop' when there are concealed characters. */
5066 FIX_FOR_BOGUSCOLS;
5067
5068 /* Make sure, the highlighting for the tab char will be
5069 * correctly set further below (effectively reverts the
5070 * FIX_FOR_BOGSUCOLS macro */
5071 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005072 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005073 tab_len += vc_saved;
5074 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005075#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 if (wp->w_p_list)
5078 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01005079 c = (n_extra == 0 && lcs_tab3) ? lcs_tab3 : lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005080#ifdef FEAT_LINEBREAK
5081 if (wp->w_p_lbr)
5082 c_extra = NUL; /* using p_extra from above */
5083 else
5084#endif
5085 c_extra = lcs_tab2;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005086 c_final = lcs_tab3;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005087 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005088 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 saved_attr2 = char_attr; /* save current attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005091 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 {
5093 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005094 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005095 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 }
5098 else
5099 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01005100 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 c_extra = ' ';
5102 c = ' ';
5103 }
5104 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005105 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02005106 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005107 || ((fromcol >= 0 || fromcol_prev >= 0)
5108 && tocol > vcol
5109 && VIsual_mode != Ctrl_V
5110 && (
5111# ifdef FEAT_RIGHTLEFT
5112 wp->w_p_rl ? (col >= 0) :
5113# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005114 (col < wp->w_width))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005115 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005116 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005117 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02005118 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005120 /* Display a '$' after the line or highlight an extra
5121 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122#if defined(FEAT_DIFF) || defined(LINE_ATTR)
5123 /* For a diff line the highlighting continues after the
5124 * "$". */
5125 if (
5126# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005127 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128# ifdef LINE_ATTR
5129 &&
5130# endif
5131# endif
5132# ifdef LINE_ATTR
5133 line_attr == 0
5134# endif
5135 )
5136#endif
5137 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 /* In virtualedit, visual selections may extend
5139 * beyond end of line. */
5140 if (area_highlighting && virtual_active()
5141 && tocol != MAXCOL && vcol < tocol)
5142 n_extra = 0;
5143 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 {
5145 p_extra = at_end_str;
5146 n_extra = 1;
5147 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005148 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 }
5150 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02005151 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005152 c = lcs_eol;
5153 else
5154 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 lcs_eol_one = -1;
5156 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005157 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005159 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 n_attr = 1;
5161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005163 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 {
5165 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005166 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005167 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 }
5169 else
5170 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 }
5172 else if (c != NUL)
5173 {
5174 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02005175 if (n_extra == 0)
5176 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177#ifdef FEAT_RIGHTLEFT
5178 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
5179 rl_mirror(p_extra); /* reverse "<12>" */
5180#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005182 c_final = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005183#ifdef FEAT_LINEBREAK
5184 if (wp->w_p_lbr)
5185 {
5186 char_u *p;
5187
5188 c = *p_extra;
Bram Moolenaar964b3742019-05-24 18:54:09 +02005189 p = alloc(n_extra + 1);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005190 vim_memset(p, ' ', n_extra);
5191 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5192 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005193 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005194 p_extra_free = p_extra = p;
5195 }
5196 else
5197#endif
5198 {
5199 n_extra = byte2cells(c) - 1;
5200 c = *p_extra++;
5201 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005202 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 {
5204 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005205 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 saved_attr2 = char_attr; /* save current attr */
5207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 else if (VIsual_active
5211 && (VIsual_mode == Ctrl_V
5212 || VIsual_mode == 'v')
5213 && virtual_active()
5214 && tocol != MAXCOL
5215 && vcol < tocol
5216 && (
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005217#ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 wp->w_p_rl ? (col >= 0) :
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005219#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005220 (col < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 {
5222 c = ' ';
5223 --ptr; /* put it back at the NUL */
5224 }
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005225#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 else if ((
5227# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005228 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005230# ifdef FEAT_TERMINAL
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005231 win_attr != 0 ||
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005232# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 ) && (
5235# ifdef FEAT_RIGHTLEFT
5236 wp->w_p_rl ? (col >= 0) :
5237# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005238 (col
5239# ifdef FEAT_CONCEAL
5240 - boguscols
5241# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005242 < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 {
5244 /* Highlight until the right side of the window */
5245 c = ' ';
5246 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005247
5248 /* Remember we do the char for line highlighting. */
5249 ++did_line_attr;
5250
5251 /* don't do search HL for the rest of the line */
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005252 if (line_attr != 0 && char_attr == search_attr
5253 && (did_line_attr > 1
5254 || (wp->w_p_list && lcs_eol > 0)))
Bram Moolenaar91170f82006-05-05 21:15:17 +00005255 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256# ifdef FEAT_DIFF
5257 if (diff_hlf == HLF_TXD)
5258 {
5259 diff_hlf = HLF_CHD;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005260 if (vi_attr == 0 || char_attr != vi_attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005261 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005262 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005263 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaar017ba072019-09-14 21:01:23 +02005264 && wp->w_p_culopt_flags != CULOPT_NBR
5265 && (!cul_screenline
5266 || (vcol >= left_curline_col
5267 && vcol <= right_curline_col)))
5268 char_attr = hl_combine_attr(
5269 char_attr, HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 }
5272# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005273# ifdef FEAT_TERMINAL
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005274 if (win_attr != 0)
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005275 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005276 char_attr = win_attr;
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005277 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar017ba072019-09-14 21:01:23 +02005278 {
5279 if (!cul_screenline || (vcol >= left_curline_col
5280 && vcol <= right_curline_col))
5281 char_attr = hl_combine_attr(
5282 char_attr, HL_ATTR(HLF_CUL));
5283 }
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02005284 else if (line_attr)
5285 char_attr = hl_combine_attr(char_attr, line_attr);
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005286 }
5287# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 }
5289#endif
5290 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005291
5292#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005293 if ( wp->w_p_cole > 0
5294 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaarab62c192019-03-30 16:39:05 +01005295 conceal_cursor_line(wp))
5296 && ((syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005297 && !(lnum_in_visual_area
5298 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005299 {
5300 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005301 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005302 && (syn_get_sub_char() != NUL || match_conc
5303 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005304 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005305 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005306 /* First time at this concealed item: display one
5307 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005308 if (match_conc)
5309 c = match_conc;
5310 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005311 c = syn_get_sub_char();
5312 else if (lcs_conceal != NUL)
5313 c = lcs_conceal;
5314 else
5315 c = ' ';
5316
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005317 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005318
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005319 if (n_extra > 0)
5320 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005321 vcol += n_extra;
5322 if (wp->w_p_wrap && n_extra > 0)
5323 {
5324# ifdef FEAT_RIGHTLEFT
5325 if (wp->w_p_rl)
5326 {
5327 col -= n_extra;
5328 boguscols -= n_extra;
5329 }
5330 else
5331# endif
5332 {
5333 boguscols += n_extra;
5334 col += n_extra;
5335 }
5336 }
5337 n_extra = 0;
5338 n_attr = 0;
5339 }
5340 else if (n_skip == 0)
5341 {
5342 is_concealing = TRUE;
5343 n_skip = 1;
5344 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005345 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005346 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005347 {
5348 mb_utf8 = TRUE;
5349 u8cc[0] = 0;
5350 c = 0xc0;
5351 }
5352 else
5353 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005354 }
5355 else
5356 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005357 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005358 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005359 }
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02005360
5361 if (n_skip > 0 && did_decrement_ptr)
5362 // not showing the '>', put pointer back to avoid getting stuck
5363 ++ptr;
5364
5365#endif // FEAT_CONCEAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 }
5367
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005368#ifdef FEAT_CONCEAL
5369 /* In the cursor line and we may be concealing characters: correct
5370 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005371 if (!did_wcol && draw_state == WL_LINE
5372 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005373 && conceal_cursor_line(wp)
5374 && (int)wp->w_virtcol <= vcol + n_skip)
5375 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005376# ifdef FEAT_RIGHTLEFT
5377 if (wp->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02005378 wp->w_wcol = wp->w_width - col + boguscols - 1;
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005379 else
5380# endif
5381 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005382 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005383 did_wcol = TRUE;
Bram Moolenaar5babc6e2019-09-14 21:55:51 +02005384 curwin->w_valid |= VALID_WCOL|VALID_WROW|VALID_VIRTCOL;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005385 }
5386#endif
5387
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 /* Don't override visual selection highlighting. */
5389 if (n_attr > 0
5390 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005391 && !attr_pri)
Bram Moolenaar017ba072019-09-14 21:01:23 +02005392 {
Bram Moolenaar0571f3d2019-09-14 22:33:47 +02005393#ifdef LINE_ATTR
Bram Moolenaar017ba072019-09-14 21:01:23 +02005394 if (line_attr)
5395 char_attr = hl_combine_attr(extra_attr, line_attr);
5396 else
Bram Moolenaar0571f3d2019-09-14 22:33:47 +02005397#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02005398 char_attr = extra_attr;
5399 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400
Bram Moolenaar81695252004-12-29 20:58:21 +00005401#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402 /* XIM don't send preedit_start and preedit_end, but they send
5403 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5404 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005405 if (p_imst == IM_ON_THE_SPOT
5406 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005407 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 && (State & INSERT)
5409 && !p_imdisable
5410 && im_is_preediting()
5411 && draw_state == WL_LINE)
5412 {
5413 colnr_T tcol;
5414
5415 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005416 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 else
5418 tcol = preedit_end_col;
5419 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5420 {
5421 if (feedback_old_attr < 0)
5422 {
5423 feedback_col = 0;
5424 feedback_old_attr = char_attr;
5425 }
5426 char_attr = im_get_feedback_attr(feedback_col);
5427 if (char_attr < 0)
5428 char_attr = feedback_old_attr;
5429 feedback_col++;
5430 }
5431 else if (feedback_old_attr >= 0)
5432 {
5433 char_attr = feedback_old_attr;
5434 feedback_old_attr = -1;
5435 feedback_col = 0;
5436 }
5437 }
5438#endif
5439 /*
5440 * Handle the case where we are in column 0 but not on the first
5441 * character of the line and the user wants us to show us a
5442 * special character (via 'listchars' option "precedes:<char>".
5443 */
5444 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005445 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5447#ifdef FEAT_DIFF
5448 && filler_todo <= 0
5449#endif
5450 && draw_state > WL_NR
5451 && c != NUL)
5452 {
5453 c = lcs_prec;
5454 lcs_prec_todo = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005455 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5456 {
5457 /* Double-width character being overwritten by the "precedes"
5458 * character, need to fill up half the character. */
5459 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005460 c_final = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005461 n_extra = 1;
5462 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005463 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005466 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 {
5468 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005469 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005470 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 }
5472 else
5473 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005474 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 {
5476 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005477 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 n_attr3 = 1;
5479 }
5480 }
5481
5482 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005483 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 */
Bram Moolenaarf914a332019-07-20 15:09:56 +02005485 if ((c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005486#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005487 || did_line_attr == 1
5488#endif
Bram Moolenaarf914a332019-07-20 15:09:56 +02005489 ) && eol_hl_off == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005491#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarbbca7732019-07-24 18:13:16 +02005492 // flag to indicate whether prevcol equals startcol of search_hl or
5493 // one of the matches
5494 int prevcol_hl_flag = get_prevcol_hl_flag(wp, &search_hl,
5495 (long)(ptr - line) - (c == NUL));
Bram Moolenaar91170f82006-05-05 21:15:17 +00005496#endif
Bram Moolenaarbbca7732019-07-24 18:13:16 +02005497 // Invert at least one char, used for Visual and empty line or
5498 // highlight match at end of line. If it's beyond the last
5499 // char on the screen, just overwrite that one (tricky!) Not
5500 // needed when a '$' was displayed for 'list'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005502 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005503 && (VIsual_mode != Ctrl_V
5504 || lnum == VIsual.lnum
5505 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005506 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarbbca7732019-07-24 18:13:16 +02005508 // highlight 'hlsearch' match at end of line
5509 || (prevcol_hl_flag
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005510# ifdef FEAT_SYN_HL
5511 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
5512 && !(wp == curwin && VIsual_active))
5513# endif
5514# ifdef FEAT_DIFF
5515 && diff_hlf == (hlf_T)0
5516# endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005517# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005518 && did_line_attr <= 1
5519# endif
5520 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521#endif
5522 ))
5523 {
5524 int n = 0;
5525
5526#ifdef FEAT_RIGHTLEFT
5527 if (wp->w_p_rl)
5528 {
5529 if (col < 0)
5530 n = 1;
5531 }
5532 else
5533#endif
5534 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005535 if (col >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 n = -1;
5537 }
5538 if (n != 0)
5539 {
5540 /* At the window boundary, highlight the last character
5541 * instead (better than nothing). */
5542 off += n;
5543 col += n;
5544 }
5545 else
5546 {
5547 /* Add a blank character to highlight. */
5548 ScreenLines[off] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 if (enc_utf8)
5550 ScreenLinesUC[off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 }
5552#ifdef FEAT_SEARCH_EXTRA
5553 if (area_attr == 0)
5554 {
Bram Moolenaarbbca7732019-07-24 18:13:16 +02005555 // Use attributes from match with highest priority among
5556 // 'search_hl' and the match list.
5557 get_search_match_hl(wp, &search_hl,
5558 (long)(ptr - line), &char_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 }
5560#endif
5561 ScreenAttrs[off] = char_attr;
5562#ifdef FEAT_RIGHTLEFT
5563 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005564 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005566 --off;
5567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 else
5569#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005570 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005572 ++off;
5573 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005574 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005575 eol_hl_off = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578
Bram Moolenaar91170f82006-05-05 21:15:17 +00005579 /*
5580 * At end of the text line.
5581 */
5582 if (c == NUL)
5583 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005584#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005585 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005586 if (wp->w_p_wrap)
5587 v = wp->w_skipcol;
5588 else
5589 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005590
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005591 /* check if line ends before left margin */
5592 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005593 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005594#ifdef FEAT_CONCEAL
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01005595 // Get rid of the boguscols now, we want to draw until the right
5596 // edge for 'cursorcolumn'.
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005597 col -= boguscols;
5598 boguscols = 0;
5599#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005600
5601 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005602 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005603
5604 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005605 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5606 && (int)wp->w_virtcol <
Bram Moolenaar02631462017-09-22 15:20:32 +02005607 wp->w_width * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005608 && lnum != wp->w_cursor.lnum)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005609 || draw_color_col
5610 || win_attr != 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005611# ifdef FEAT_RIGHTLEFT
5612 && !wp->w_p_rl
5613# endif
5614 )
5615 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005616 int rightmost_vcol = 0;
5617 int i;
5618
5619 if (wp->w_p_cuc)
5620 rightmost_vcol = wp->w_virtcol;
5621 if (draw_color_col)
5622 /* determine rightmost colorcolumn to possibly draw */
5623 for (i = 0; color_cols[i] >= 0; ++i)
5624 if (rightmost_vcol < color_cols[i])
5625 rightmost_vcol = color_cols[i];
5626
Bram Moolenaar02631462017-09-22 15:20:32 +02005627 while (col < wp->w_width)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005628 {
5629 ScreenLines[off] = ' ';
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005630 if (enc_utf8)
5631 ScreenLinesUC[off] = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005632 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005633 if (draw_color_col)
5634 draw_color_col = advance_color_col(VCOL_HLC,
5635 &color_cols);
5636
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005637 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005638 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005639 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005640 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005641 else
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005642 ScreenAttrs[off++] = win_attr;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005643
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005644 if (VCOL_HLC >= rightmost_vcol && win_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005645 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005646
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005647 ++vcol;
5648 }
5649 }
5650#endif
5651
Bram Moolenaar53f81742017-09-22 14:35:51 +02005652 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005653 (int)wp->w_width, screen_line_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 row++;
5655
5656 /*
5657 * Update w_cline_height and w_cline_folded if the cursor line was
5658 * updated (saves a call to plines() later).
5659 */
5660 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5661 {
5662 curwin->w_cline_row = startrow;
5663 curwin->w_cline_height = row - startrow;
5664#ifdef FEAT_FOLDING
5665 curwin->w_cline_folded = FALSE;
5666#endif
5667 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5668 }
5669
5670 break;
5671 }
5672
Bram Moolenaara5c6a0b2019-05-08 20:20:46 +02005673 // Show "extends" character from 'listchars' if beyond the line end and
5674 // 'list' is set.
5675 if (lcs_ext != NUL
5676 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 && !wp->w_p_wrap
5678#ifdef FEAT_DIFF
5679 && filler_todo <= 0
5680#endif
5681 && (
5682#ifdef FEAT_RIGHTLEFT
5683 wp->w_p_rl ? col == 0 :
5684#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005685 col == wp->w_width - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005687 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5689 {
5690 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005691 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005693 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 {
5695 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005696 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005697 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 }
5699 else
5700 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 }
5702
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005703#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005704 /* advance to the next 'colorcolumn' */
5705 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005706 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005707
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005708 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005709 * highlight the cursor position itself.
5710 * Also highlight the 'colorcolumn' if it is different than
5711 * 'cursorcolumn' */
5712 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005713 if (draw_state == WL_LINE && !lnum_in_visual_area
5714 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005715 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005716 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005717 && lnum != wp->w_cursor.lnum)
5718 {
5719 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005720 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005721 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005722 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005723 {
5724 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005725 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005726 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005727 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005728#endif
5729
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 /*
5731 * Store character to be displayed.
5732 * Skip characters that are left of the screen for 'nowrap'.
5733 */
5734 vcol_prev = vcol;
5735 if (draw_state < WL_LINE || n_skip <= 0)
5736 {
5737 /*
5738 * Store the character.
5739 */
Bram Moolenaara12a1612019-01-24 16:39:02 +01005740#if defined(FEAT_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5742 {
5743 /* A double-wide character is: put first halve in left cell. */
5744 --off;
5745 --col;
5746 }
5747#endif
5748 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005750 {
5751 if ((mb_c & 0xff00) == 0x8e00)
5752 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 else if (enc_utf8)
5756 {
5757 if (mb_utf8)
5758 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005759 int i;
5760
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005762 if ((c & 0xff) == 0)
5763 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005764 for (i = 0; i < Screen_mco; ++i)
5765 {
5766 ScreenLinesC[i][off] = u8cc[i];
5767 if (u8cc[i] == 0)
5768 break;
5769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 }
5771 else
5772 ScreenLinesUC[off] = 0;
5773 }
5774 if (multi_attr)
5775 {
5776 ScreenAttrs[off] = multi_attr;
5777 multi_attr = 0;
5778 }
5779 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 ScreenAttrs[off] = char_attr;
5781
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5783 {
5784 /* Need to fill two screen columns. */
5785 ++off;
5786 ++col;
5787 if (enc_utf8)
5788 /* UTF-8: Put a 0 in the second screen char. */
5789 ScreenLines[off] = 0;
5790 else
5791 /* DBCS: Put second byte in the second screen char. */
5792 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005793 if (draw_state > WL_NR
5794#ifdef FEAT_DIFF
5795 && filler_todo <= 0
5796#endif
5797 )
5798 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 /* When "tocol" is halfway a character, set it to the end of
5800 * the character, otherwise highlighting won't stop. */
5801 if (tocol == vcol)
5802 ++tocol;
5803#ifdef FEAT_RIGHTLEFT
5804 if (wp->w_p_rl)
5805 {
5806 /* now it's time to backup one cell */
5807 --off;
5808 --col;
5809 }
5810#endif
5811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812#ifdef FEAT_RIGHTLEFT
5813 if (wp->w_p_rl)
5814 {
5815 --off;
5816 --col;
5817 }
5818 else
5819#endif
5820 {
5821 ++off;
5822 ++col;
5823 }
5824 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005825#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005826 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005827 {
5828 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005829 ++vcol_off;
5830 if (n_extra > 0)
5831 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005832 if (wp->w_p_wrap)
5833 {
5834 /*
5835 * Special voodoo required if 'wrap' is on.
5836 *
5837 * Advance the column indicator to force the line
5838 * drawing to wrap early. This will make the line
5839 * take up the same screen space when parts are concealed,
5840 * so that cursor line computations aren't messed up.
5841 *
5842 * To avoid the fictitious advance of 'col' causing
5843 * trailing junk to be written out of the screen line
5844 * we are building, 'boguscols' keeps track of the number
5845 * of bad columns we have advanced.
5846 */
5847 if (n_extra > 0)
5848 {
5849 vcol += n_extra;
5850# ifdef FEAT_RIGHTLEFT
5851 if (wp->w_p_rl)
5852 {
5853 col -= n_extra;
5854 boguscols -= n_extra;
5855 }
5856 else
5857# endif
5858 {
5859 col += n_extra;
5860 boguscols += n_extra;
5861 }
5862 n_extra = 0;
5863 n_attr = 0;
5864 }
5865
5866
Bram Moolenaar860cae12010-06-05 23:22:07 +02005867 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5868 {
5869 /* Need to fill two screen columns. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01005870# ifdef FEAT_RIGHTLEFT
Bram Moolenaar860cae12010-06-05 23:22:07 +02005871 if (wp->w_p_rl)
5872 {
5873 --boguscols;
5874 --col;
5875 }
5876 else
Bram Moolenaara12a1612019-01-24 16:39:02 +01005877# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02005878 {
5879 ++boguscols;
5880 ++col;
5881 }
5882 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005883
5884# ifdef FEAT_RIGHTLEFT
5885 if (wp->w_p_rl)
5886 {
5887 --boguscols;
5888 --col;
5889 }
5890 else
5891# endif
5892 {
5893 ++boguscols;
5894 ++col;
5895 }
5896 }
5897 else
5898 {
5899 if (n_extra > 0)
5900 {
5901 vcol += n_extra;
5902 n_extra = 0;
5903 n_attr = 0;
5904 }
5905 }
5906
5907 }
5908#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909 else
5910 --n_skip;
5911
Bram Moolenaar64486672010-05-16 15:46:46 +02005912 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5913 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005914 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915#ifdef FEAT_DIFF
5916 && filler_todo <= 0
5917#endif
5918 )
5919 ++vcol;
5920
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005921#ifdef FEAT_SYN_HL
5922 if (vcol_save_attr >= 0)
5923 char_attr = vcol_save_attr;
5924#endif
5925
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926 /* restore attributes after "predeces" in 'listchars' */
5927 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5928 char_attr = saved_attr3;
5929
5930 /* restore attributes after last 'listchars' or 'number' char */
5931 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5932 char_attr = saved_attr2;
5933
5934 /*
5935 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005936 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 */
5938 if ((
5939#ifdef FEAT_RIGHTLEFT
5940 wp->w_p_rl ? (col < 0) :
5941#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005942 (col >= wp->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943 && (*ptr != NUL
5944#ifdef FEAT_DIFF
5945 || filler_todo > 0
5946#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005947 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5949 )
5950 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005951#ifdef FEAT_CONCEAL
Bram Moolenaar53f81742017-09-22 14:35:51 +02005952 screen_line(screen_row, wp->w_wincol, col - boguscols,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005953 (int)wp->w_width, screen_line_flags);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005954 boguscols = 0;
5955#else
Bram Moolenaar53f81742017-09-22 14:35:51 +02005956 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005957 (int)wp->w_width, screen_line_flags);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005958#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959 ++row;
5960 ++screen_row;
5961
5962 /* When not wrapping and finished diff lines, or when displayed
5963 * '$' and highlighting until last column, break here. */
5964 if ((!wp->w_p_wrap
5965#ifdef FEAT_DIFF
5966 && filler_todo <= 0
5967#endif
5968 ) || lcs_eol_one == -1)
5969 break;
5970
5971 /* When the window is too narrow draw all "@" lines. */
5972 if (draw_state != WL_LINE
5973#ifdef FEAT_DIFF
5974 && filler_todo <= 0
5975#endif
5976 )
5977 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01005978 win_draw_end(wp, '@', ' ', TRUE, row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980 row = endrow;
5981 }
5982
5983 /* When line got too long for screen break here. */
5984 if (row == endrow)
5985 {
5986 ++row;
5987 break;
5988 }
5989
5990 if (screen_cur_row == screen_row - 1
5991#ifdef FEAT_DIFF
5992 && filler_todo <= 0
5993#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005994 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995 {
5996 /* Remember that the line wraps, used for modeless copy. */
5997 LineWraps[screen_row - 1] = TRUE;
5998
5999 /*
6000 * Special trick to make copy/paste of wrapped lines work with
6001 * xterm/screen: write an extra character beyond the end of
6002 * the line. This will work with all terminal types
6003 * (regardless of the xn,am settings).
6004 * Only do this on a fast tty.
6005 * Only do this if the cursor is on the current line
6006 * (something has been written in it).
6007 * Don't do this for the GUI.
6008 * Don't do this for double-width characters.
6009 * Don't do this for a window not at the right screen border.
6010 */
6011 if (p_tf
6012#ifdef FEAT_GUI
6013 && !gui.in_use
6014#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00006016 && ((*mb_off2cells)(LineOffset[screen_row],
6017 LineOffset[screen_row] + screen_Columns)
6018 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00006020 + (int)Columns - 2,
6021 LineOffset[screen_row] + screen_Columns)
Bram Moolenaara12a1612019-01-24 16:39:02 +01006022 == 2)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023 {
6024 /* First make sure we are at the end of the screen line,
6025 * then output the same character again to let the
6026 * terminal know about the wrap. If the terminal doesn't
6027 * auto-wrap, we overwrite the character. */
Bram Moolenaar02631462017-09-22 15:20:32 +02006028 if (screen_cur_col != wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029 screen_char(LineOffset[screen_row - 1]
6030 + (unsigned)Columns - 1,
6031 screen_row - 1, (int)(Columns - 1));
6032
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033 /* When there is a multi-byte character, just output a
6034 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00006035 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
6036 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 out_char(' ');
6038 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039 out_char(ScreenLines[LineOffset[screen_row - 1]
6040 + (Columns - 1)]);
6041 /* force a redraw of the first char on the next line */
6042 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
6043 screen_start(); /* don't know where cursor is now */
6044 }
6045 }
6046
6047 col = 0;
6048 off = (unsigned)(current_ScreenLine - ScreenLines);
6049#ifdef FEAT_RIGHTLEFT
6050 if (wp->w_p_rl)
6051 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006052 col = wp->w_width - 1; /* col is not used if breaking! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053 off += col;
6054 }
6055#endif
6056
6057 /* reset the drawing state for the start of a wrapped line */
6058 draw_state = WL_START;
6059 saved_n_extra = n_extra;
6060 saved_p_extra = p_extra;
6061 saved_c_extra = c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01006062 saved_c_final = c_final;
Bram Moolenaar017ba072019-09-14 21:01:23 +02006063#ifdef FEAT_SYN_HL
6064 if (!(cul_screenline
6065# ifdef FEAT_DIFF
6066 && diff_hlf == (hlf_T)0)
6067# endif
6068 )
6069 saved_char_attr = char_attr;
6070 else
6071#endif
6072 saved_char_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073 n_extra = 0;
6074 lcs_prec_todo = lcs_prec;
6075#ifdef FEAT_LINEBREAK
6076# ifdef FEAT_DIFF
6077 if (filler_todo <= 0)
6078# endif
6079 need_showbreak = TRUE;
6080#endif
6081#ifdef FEAT_DIFF
6082 --filler_todo;
6083 /* When the filler lines are actually below the last line of the
6084 * file, don't draw the line itself, break here. */
6085 if (filler_todo == 0 && wp->w_botfill)
6086 break;
6087#endif
6088 }
6089
6090 } /* for every character in the line */
6091
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006092#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006093 /* After an empty line check first word for capital. */
6094 if (*skipwhite(line) == NUL)
6095 {
6096 capcol_lnum = lnum + 1;
6097 cap_col = 0;
6098 }
6099#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006100#ifdef FEAT_TEXT_PROP
6101 vim_free(text_props);
6102 vim_free(text_prop_idxs);
6103#endif
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006104
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01006105 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106 return row;
6107}
6108
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006109/*
6110 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006111 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006112 */
6113 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006114comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006115{
6116 int i;
6117
6118 for (i = 0; i < Screen_mco; ++i)
6119 {
6120 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
6121 return TRUE;
6122 if (ScreenLinesC[i][off_from] == 0)
6123 break;
6124 }
6125 return FALSE;
6126}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006127
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128/*
6129 * Check whether the given character needs redrawing:
6130 * - the (first byte of the) character is different
6131 * - the attributes are different
6132 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006133 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 */
6135 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006136char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137{
6138 if (cols > 0
6139 && ((ScreenLines[off_from] != ScreenLines[off_to]
6140 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 || (enc_dbcs != 0
6142 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
6143 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
6144 ? ScreenLines2[off_from] != ScreenLines2[off_to]
6145 : (cols > 1 && ScreenLines[off_from + 1]
6146 != ScreenLines[off_to + 1])))
6147 || (enc_utf8
6148 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6149 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006150 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006151 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6152 && ScreenLines[off_from + 1]
Bram Moolenaara12a1612019-01-24 16:39:02 +01006153 != ScreenLines[off_to + 1])))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 return TRUE;
6155 return FALSE;
6156}
6157
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006158#if defined(FEAT_TERMINAL) || defined(PROTO)
6159/*
6160 * Return the index in ScreenLines[] for the current screen line.
6161 */
6162 int
6163screen_get_current_line_off()
6164{
6165 return (int)(current_ScreenLine - ScreenLines);
6166}
6167#endif
6168
Bram Moolenaarc662ec92019-06-23 00:15:57 +02006169#ifdef FEAT_TEXT_PROP
6170/*
6171 * Return TRUE if this position has a higher level popup or this cell is
6172 * transparent in the current popup.
6173 */
6174 static int
6175blocked_by_popup(int row, int col)
6176{
6177 int off;
6178
6179 if (!popup_visible)
6180 return FALSE;
6181 off = row * screen_Columns + col;
6182 return popup_mask[off] > screen_zindex || popup_transparent[off];
6183}
6184#endif
6185
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186/*
6187 * Move one "cooked" screen line to the screen, but only the characters that
6188 * have actually changed. Handle insert/delete character.
6189 * "coloff" gives the first column on the screen for this line.
6190 * "endcol" gives the columns where valid characters are.
6191 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6192 * needs to be cleared, negative otherwise.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006193 * "flags" can have bits:
6194 * SLF_POPUP popup window
6195 * SLF_RIGHTLEFT rightleft window:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6197 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6198 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006199 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006200screen_line(
6201 int row,
6202 int coloff,
6203 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006204 int clear_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006205 int flags UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206{
6207 unsigned off_from;
6208 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006209 unsigned max_off_from;
6210 unsigned max_off_to;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 int force = FALSE; /* force update rest of the line */
6214 int redraw_this /* bool: does character need redraw? */
6215#ifdef FEAT_GUI
6216 = TRUE /* For GUI when while-loop empty */
6217#endif
6218 ;
6219 int redraw_next; /* redraw_this for next character */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220 int clear_next = FALSE;
6221 int char_cells; /* 1: normal char */
6222 /* 2: occupies two display cells */
6223# define CHAR_CELLS char_cells
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006225 /* Check for illegal row and col, just in case. */
6226 if (row >= Rows)
6227 row = Rows - 1;
6228 if (endcol > Columns)
6229 endcol = Columns;
6230
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231# ifdef FEAT_CLIPBOARD
6232 clip_may_clear_selection(row, row);
6233# endif
6234
6235 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6236 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006237 max_off_from = off_from + screen_Columns;
6238 max_off_to = LineOffset[row] + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239
6240#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006241 if (flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242 {
6243 /* Clear rest first, because it's left of the text. */
6244 if (clear_width > 0)
6245 {
6246 while (col <= endcol && ScreenLines[off_to] == ' '
6247 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006248 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 {
6250 ++off_to;
6251 ++col;
6252 }
6253 if (col <= endcol)
6254 screen_fill(row, row + 1, col + coloff,
6255 endcol + coloff + 1, ' ', ' ', 0);
6256 }
6257 col = endcol + 1;
6258 off_to = LineOffset[row] + col + coloff;
6259 off_from += col;
6260 endcol = (clear_width > 0 ? clear_width : -clear_width);
6261 }
6262#endif /* FEAT_RIGHTLEFT */
6263
6264 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6265
6266 while (col < endcol)
6267 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006269 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 else
6271 char_cells = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272
6273 redraw_this = redraw_next;
6274 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6275 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6276
6277#ifdef FEAT_GUI
6278 /* If the next character was bold, then redraw the current character to
6279 * remove any pixels that might have spilt over into us. This only
6280 * happens in the GUI.
6281 */
6282 if (redraw_next && gui.in_use)
6283 {
6284 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006285 if (hl > HL_ALL)
6286 hl = syn_attr2attr(hl);
6287 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288 redraw_this = TRUE;
6289 }
6290#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02006291#ifdef FEAT_TEXT_PROP
Bram Moolenaarc662ec92019-06-23 00:15:57 +02006292 if (blocked_by_popup(row, col + coloff))
Bram Moolenaar33796b32019-06-08 16:01:13 +02006293 redraw_this = FALSE;
6294#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295 if (redraw_this)
6296 {
6297 /*
6298 * Special handling when 'xs' termcap flag set (hpterm):
6299 * Attributes for characters are stored at the position where the
6300 * cursor is when writing the highlighting code. The
6301 * start-highlighting code must be written with the cursor on the
6302 * first highlighted character. The stop-highlighting code must
6303 * be written with the cursor just after the last highlighted
6304 * character.
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01006305 * Overwriting a character doesn't remove its highlighting. Need
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306 * to clear the rest of the line, and force redrawing it
6307 * completely.
6308 */
6309 if ( p_wiv
6310 && !force
6311#ifdef FEAT_GUI
6312 && !gui.in_use
6313#endif
6314 && ScreenAttrs[off_to] != 0
6315 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6316 {
6317 /*
6318 * Need to remove highlighting attributes here.
6319 */
6320 windgoto(row, col + coloff);
6321 out_str(T_CE); /* clear rest of this screen line */
6322 screen_start(); /* don't know where cursor is now */
6323 force = TRUE; /* force redraw of rest of the line */
6324 redraw_next = TRUE; /* or else next char would miss out */
6325
6326 /*
6327 * If the previous character was highlighted, need to stop
6328 * highlighting at this character.
6329 */
6330 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6331 {
6332 screen_attr = ScreenAttrs[off_to - 1];
6333 term_windgoto(row, col + coloff);
6334 screen_stop_highlight();
6335 }
6336 else
6337 screen_attr = 0; /* highlighting has stopped */
6338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339 if (enc_dbcs != 0)
6340 {
6341 /* Check if overwriting a double-byte with a single-byte or
6342 * the other way around requires another character to be
6343 * redrawn. For UTF-8 this isn't needed, because comparing
6344 * ScreenLinesUC[] is sufficient. */
6345 if (char_cells == 1
6346 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006347 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348 {
6349 /* Writing a single-cell character over a double-cell
6350 * character: need to redraw the next cell. */
6351 ScreenLines[off_to + 1] = 0;
6352 redraw_next = TRUE;
6353 }
6354 else if (char_cells == 2
6355 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006356 && (*mb_off2cells)(off_to, max_off_to) == 1
6357 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358 {
6359 /* Writing the second half of a double-cell character over
6360 * a double-cell character: need to redraw the second
6361 * cell. */
6362 ScreenLines[off_to + 2] = 0;
6363 redraw_next = TRUE;
6364 }
6365
6366 if (enc_dbcs == DBCS_JPNU)
6367 ScreenLines2[off_to] = ScreenLines2[off_from];
6368 }
6369 /* When writing a single-width character over a double-width
6370 * character and at the end of the redrawn text, need to clear out
6371 * the right halve of the old character.
6372 * Also required when writing the right halve of a double-width
6373 * char over the left halve of an existing one. */
6374 if (has_mbyte && col + char_cells == endcol
6375 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006376 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006378 && (*mb_off2cells)(off_to, max_off_to) == 1
6379 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380 clear_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381
6382 ScreenLines[off_to] = ScreenLines[off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 if (enc_utf8)
6384 {
6385 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6386 if (ScreenLinesUC[off_from] != 0)
6387 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006388 int i;
6389
6390 for (i = 0; i < Screen_mco; ++i)
6391 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392 }
6393 }
6394 if (char_cells == 2)
6395 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396
6397#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006398 /* The bold trick makes a single column of pixels appear in the
6399 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400 * character should be redrawn too. This happens for our own GUI
6401 * and for some xterms. */
6402 if (
6403# ifdef FEAT_GUI
6404 gui.in_use
6405# endif
6406# if defined(FEAT_GUI) && defined(UNIX)
6407 ||
6408# endif
6409# ifdef UNIX
6410 term_is_xterm
6411# endif
6412 )
6413 {
6414 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006415 if (hl > HL_ALL)
6416 hl = syn_attr2attr(hl);
6417 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418 redraw_next = TRUE;
6419 }
6420#endif
6421 ScreenAttrs[off_to] = ScreenAttrs[off_from];
Bram Moolenaara12a1612019-01-24 16:39:02 +01006422
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006423 /* For simplicity set the attributes of second half of a
6424 * double-wide character equal to the first half. */
6425 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006427
6428 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431 screen_char(off_to, row, col + coloff);
6432 }
6433 else if ( p_wiv
6434#ifdef FEAT_GUI
6435 && !gui.in_use
6436#endif
6437 && col + coloff > 0)
6438 {
6439 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6440 {
6441 /*
6442 * Don't output stop-highlight when moving the cursor, it will
6443 * stop the highlighting when it should continue.
6444 */
6445 screen_attr = 0;
6446 }
6447 else if (screen_attr != 0)
6448 screen_stop_highlight();
6449 }
6450
6451 off_to += CHAR_CELLS;
6452 off_from += CHAR_CELLS;
6453 col += CHAR_CELLS;
6454 }
6455
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456 if (clear_next)
6457 {
6458 /* Clear the second half of a double-wide character of which the left
6459 * half was overwritten with a single-wide character. */
6460 ScreenLines[off_to] = ' ';
6461 if (enc_utf8)
6462 ScreenLinesUC[off_to] = 0;
6463 screen_char(off_to, row, col + coloff);
6464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465
6466 if (clear_width > 0
6467#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006468 && !(flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469#endif
6470 )
6471 {
6472#ifdef FEAT_GUI
6473 int startCol = col;
6474#endif
6475
6476 /* blank out the rest of the line */
6477 while (col < clear_width && ScreenLines[off_to] == ' '
6478 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006479 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 {
6481 ++off_to;
6482 ++col;
6483 }
6484 if (col < clear_width)
6485 {
6486#ifdef FEAT_GUI
6487 /*
6488 * In the GUI, clearing the rest of the line may leave pixels
6489 * behind if the first character cleared was bold. Some bold
6490 * fonts spill over the left. In this case we redraw the previous
6491 * character too. If we didn't skip any blanks above, then we
6492 * only redraw if the character wasn't already redrawn anyway.
6493 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006494 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495 {
6496 hl = ScreenAttrs[off_to];
6497 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006498 {
6499 int prev_cells = 1;
Bram Moolenaara12a1612019-01-24 16:39:02 +01006500
Bram Moolenaar9c697322006-10-09 20:11:17 +00006501 if (enc_utf8)
6502 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6503 * that its width is 2. */
6504 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6505 else if (enc_dbcs != 0)
6506 {
6507 /* find previous character by counting from first
6508 * column and get its width. */
6509 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006510 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006511
6512 while (off < off_to)
6513 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006514 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006515 off += prev_cells;
6516 }
6517 }
6518
6519 if (enc_dbcs != 0 && prev_cells > 1)
6520 screen_char_2(off_to - prev_cells, row,
6521 col + coloff - prev_cells);
6522 else
Bram Moolenaar9c697322006-10-09 20:11:17 +00006523 screen_char(off_to - prev_cells, row,
6524 col + coloff - prev_cells);
6525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526 }
6527#endif
6528 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6529 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530 off_to += clear_width - col;
6531 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532 }
6533 }
6534
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006535 if (clear_width > 0
6536#ifdef FEAT_TEXT_PROP
6537 && !(flags & SLF_POPUP) // no separator for popup window
6538#endif
6539 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006541 // For a window that has a right neighbor, draw the separator char
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006542 // right of the window contents. But not on top of a popup window.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006543 if (coloff + col < Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006545#ifdef FEAT_TEXT_PROP
Bram Moolenaarc662ec92019-06-23 00:15:57 +02006546 if (!blocked_by_popup(row, col + coloff))
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006547#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006549 int c;
6550
6551 c = fillchar_vsep(&hl);
6552 if (ScreenLines[off_to] != (schar_T)c
6553 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6554 != (c >= 0x80 ? c : 0))
6555 || ScreenAttrs[off_to] != hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006557 ScreenLines[off_to] = c;
6558 ScreenAttrs[off_to] = hl;
6559 if (enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006561 if (c >= 0x80)
6562 {
6563 ScreenLinesUC[off_to] = c;
6564 ScreenLinesC[0][off_to] = 0;
6565 }
6566 else
6567 ScreenLinesUC[off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 }
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006569 screen_char(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571 }
6572 }
6573 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574 LineWraps[row] = FALSE;
6575 }
6576}
6577
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006578#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006580 * Mirror text "str" for right-left displaying.
6581 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006582 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006583 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006584rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585{
6586 char_u *p1, *p2;
6587 int t;
6588
6589 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6590 {
6591 t = *p1;
6592 *p1 = *p2;
6593 *p2 = t;
6594 }
6595}
6596#endif
6597
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598/*
6599 * mark all status lines for redraw; used after first :cd
6600 */
6601 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006602status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603{
6604 win_T *wp;
6605
Bram Moolenaar29323592016-07-24 22:04:11 +02006606 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607 if (wp->w_status_height)
6608 {
6609 wp->w_redr_status = TRUE;
6610 redraw_later(VALID);
6611 }
6612}
6613
6614/*
6615 * mark all status lines of the current buffer for redraw
6616 */
6617 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006618status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619{
6620 win_T *wp;
6621
Bram Moolenaar29323592016-07-24 22:04:11 +02006622 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6624 {
6625 wp->w_redr_status = TRUE;
6626 redraw_later(VALID);
6627 }
6628}
6629
6630/*
6631 * Redraw all status lines that need to be redrawn.
6632 */
6633 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006634redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635{
6636 win_T *wp;
6637
Bram Moolenaar29323592016-07-24 22:04:11 +02006638 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006640 win_redr_status(wp, FALSE);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006641 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006642 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644
Bram Moolenaar4033c552017-09-16 20:54:51 +02006645#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646/*
6647 * Redraw all status lines at the bottom of frame "frp".
6648 */
6649 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006650win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651{
6652 if (frp->fr_layout == FR_LEAF)
6653 frp->fr_win->w_redr_status = TRUE;
6654 else if (frp->fr_layout == FR_ROW)
6655 {
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006656 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657 win_redraw_last_status(frp);
6658 }
6659 else /* frp->fr_layout == FR_COL */
6660 {
6661 frp = frp->fr_child;
6662 while (frp->fr_next != NULL)
6663 frp = frp->fr_next;
6664 win_redraw_last_status(frp);
6665 }
6666}
6667#endif
6668
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669/*
6670 * Draw the verticap separator right of window "wp" starting with line "row".
6671 */
6672 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006673draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674{
6675 int hl;
6676 int c;
6677
6678 if (wp->w_vsep_width)
6679 {
6680 /* draw the vertical separator right of this window */
6681 c = fillchar_vsep(&hl);
6682 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6683 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6684 c, ' ', hl);
6685 }
6686}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687
6688#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006689static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690
6691/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006692 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693 */
6694 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006695status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696{
6697 int len = 0;
6698
6699#ifdef FEAT_MENU
6700 int emenu = (xp->xp_context == EXPAND_MENUS
6701 || xp->xp_context == EXPAND_MENUNAMES);
6702
6703 /* Check for menu separators - replace with '|'. */
6704 if (emenu && menu_is_separator(s))
6705 return 1;
6706#endif
6707
6708 while (*s != NUL)
6709 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006710 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006711 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006712 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713 }
6714
6715 return len;
6716}
6717
6718/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006719 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006720 * These are backslashes used for escaping. Do show backslashes in help tags.
6721 */
6722 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006723skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006724{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006725 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006726#ifdef FEAT_MENU
6727 || ((xp->xp_context == EXPAND_MENUS
6728 || xp->xp_context == EXPAND_MENUNAMES)
6729 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6730#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006731 )
6732 {
6733#ifndef BACKSLASH_IN_FILENAME
6734 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6735 return 2;
6736#endif
6737 return 1;
6738 }
6739 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006740}
6741
6742/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743 * Show wildchar matches in the status line.
6744 * Show at least the "match" item.
6745 * We start at item 'first_match' in the list and show all matches that fit.
6746 *
6747 * If inversion is possible we use it. Else '=' characters are used.
6748 */
6749 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006750win_redr_status_matches(
6751 expand_T *xp,
6752 int num_matches,
6753 char_u **matches, /* list of matches */
6754 int match,
6755 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756{
6757#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6758 int row;
6759 char_u *buf;
6760 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006761 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 int fillchar;
6763 int attr;
6764 int i;
6765 int highlight = TRUE;
6766 char_u *selstart = NULL;
6767 int selstart_col = 0;
6768 char_u *selend = NULL;
6769 static int first_match = 0;
6770 int add_left = FALSE;
6771 char_u *s;
6772#ifdef FEAT_MENU
6773 int emenu;
6774#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776
6777 if (matches == NULL) /* interrupted completion? */
6778 return;
6779
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006780 if (has_mbyte)
Bram Moolenaar964b3742019-05-24 18:54:09 +02006781 buf = alloc(Columns * MB_MAXBYTES + 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006782 else
Bram Moolenaar964b3742019-05-24 18:54:09 +02006783 buf = alloc(Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784 if (buf == NULL)
6785 return;
6786
6787 if (match == -1) /* don't show match but original text */
6788 {
6789 match = 0;
6790 highlight = FALSE;
6791 }
6792 /* count 1 for the ending ">" */
6793 clen = status_match_len(xp, L_MATCH(match)) + 3;
6794 if (match == 0)
6795 first_match = 0;
6796 else if (match < first_match)
6797 {
6798 /* jumping left, as far as we can go */
6799 first_match = match;
6800 add_left = TRUE;
6801 }
6802 else
6803 {
6804 /* check if match fits on the screen */
6805 for (i = first_match; i < match; ++i)
6806 clen += status_match_len(xp, L_MATCH(i)) + 2;
6807 if (first_match > 0)
6808 clen += 2;
6809 /* jumping right, put match at the left */
6810 if ((long)clen > Columns)
6811 {
6812 first_match = match;
6813 /* if showing the last match, we can add some on the left */
6814 clen = 2;
6815 for (i = match; i < num_matches; ++i)
6816 {
6817 clen += status_match_len(xp, L_MATCH(i)) + 2;
6818 if ((long)clen >= Columns)
6819 break;
6820 }
6821 if (i == num_matches)
6822 add_left = TRUE;
6823 }
6824 }
6825 if (add_left)
6826 while (first_match > 0)
6827 {
6828 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6829 if ((long)clen >= Columns)
6830 break;
6831 --first_match;
6832 }
6833
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006834 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835
6836 if (first_match == 0)
6837 {
6838 *buf = NUL;
6839 len = 0;
6840 }
6841 else
6842 {
6843 STRCPY(buf, "< ");
6844 len = 2;
6845 }
6846 clen = len;
6847
6848 i = first_match;
6849 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6850 {
6851 if (i == match)
6852 {
6853 selstart = buf + len;
6854 selstart_col = clen;
6855 }
6856
6857 s = L_MATCH(i);
6858 /* Check for menu separators - replace with '|' */
6859#ifdef FEAT_MENU
6860 emenu = (xp->xp_context == EXPAND_MENUS
6861 || xp->xp_context == EXPAND_MENUNAMES);
6862 if (emenu && menu_is_separator(s))
6863 {
6864 STRCPY(buf + len, transchar('|'));
6865 l = (int)STRLEN(buf + len);
6866 len += l;
6867 clen += l;
6868 }
6869 else
6870#endif
6871 for ( ; *s != NUL; ++s)
6872 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006873 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874 clen += ptr2cells(s);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006875 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876 {
6877 STRNCPY(buf + len, s, l);
6878 s += l - 1;
6879 len += l;
6880 }
6881 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882 {
6883 STRCPY(buf + len, transchar_byte(*s));
6884 len += (int)STRLEN(buf + len);
6885 }
6886 }
6887 if (i == match)
6888 selend = buf + len;
6889
6890 *(buf + len++) = ' ';
6891 *(buf + len++) = ' ';
6892 clen += 2;
6893 if (++i == num_matches)
6894 break;
6895 }
6896
6897 if (i != num_matches)
6898 {
6899 *(buf + len++) = '>';
6900 ++clen;
6901 }
6902
6903 buf[len] = NUL;
6904
6905 row = cmdline_row - 1;
6906 if (row >= 0)
6907 {
6908 if (wild_menu_showing == 0)
6909 {
6910 if (msg_scrolled > 0)
6911 {
6912 /* Put the wildmenu just above the command line. If there is
6913 * no room, scroll the screen one line up. */
6914 if (cmdline_row == Rows - 1)
6915 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02006916 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 ++msg_scrolled;
6918 }
6919 else
6920 {
6921 ++cmdline_row;
6922 ++row;
6923 }
6924 wild_menu_showing = WM_SCROLLED;
6925 }
6926 else
6927 {
6928 /* Create status line if needed by setting 'laststatus' to 2.
6929 * Set 'winminheight' to zero to avoid that the window is
6930 * resized. */
6931 if (lastwin->w_status_height == 0)
6932 {
6933 save_p_ls = p_ls;
6934 save_p_wmh = p_wmh;
6935 p_ls = 2;
6936 p_wmh = 0;
6937 last_status(FALSE);
6938 }
6939 wild_menu_showing = WM_SHOWN;
6940 }
6941 }
6942
6943 screen_puts(buf, row, 0, attr);
6944 if (selstart != NULL && highlight)
6945 {
6946 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006947 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 }
6949
6950 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6951 }
6952
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 vim_free(buf);
6955}
6956#endif
6957
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958/*
6959 * Redraw the status line of window wp.
6960 *
6961 * If inversion is possible we use it. Else '=' characters are used.
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006962 * If "ignore_pum" is TRUE, also redraw statusline when the popup menu is
6963 * displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 */
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006965 static void
Bram Moolenaar829adb72018-06-24 19:24:03 +02006966win_redr_status(win_T *wp, int ignore_pum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967{
6968 int row;
6969 char_u *p;
6970 int len;
6971 int fillchar;
6972 int attr;
6973 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006974 static int busy = FALSE;
6975
6976 /* It's possible to get here recursively when 'statusline' (indirectly)
6977 * invokes ":redrawstatus". Simply ignore the call then. */
6978 if (busy)
6979 return;
6980 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981
6982 wp->w_redr_status = FALSE;
6983 if (wp->w_status_height == 0)
6984 {
6985 /* no status line, can only be last window */
6986 redraw_cmdline = TRUE;
6987 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006988 else if (!redrawing()
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006989 // don't update status line when popup menu is visible and may be
6990 // drawn over it, unless it will be redrawn later
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006991 || (!ignore_pum && pum_visible()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 {
6993 /* Don't redraw right now, do it later. */
6994 wp->w_redr_status = TRUE;
6995 }
6996#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006997 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998 {
6999 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007000 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 }
7002#endif
7003 else
7004 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007005 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007007 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 p = NameBuff;
7009 len = (int)STRLEN(p);
7010
Bram Moolenaard85f2712017-07-28 21:51:57 +02007011 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012#ifdef FEAT_QUICKFIX
7013 || wp->w_p_pvw
7014#endif
7015 || bufIsChanged(wp->w_buffer)
7016 || wp->w_buffer->b_p_ro)
7017 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02007018 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00007020 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 len += (int)STRLEN(p + len);
7022 }
7023#ifdef FEAT_QUICKFIX
7024 if (wp->w_p_pvw)
7025 {
7026 STRCPY(p + len, _("[Preview]"));
7027 len += (int)STRLEN(p + len);
7028 }
7029#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02007030 if (bufIsChanged(wp->w_buffer)
7031#ifdef FEAT_TERMINAL
7032 && !bt_terminal(wp->w_buffer)
7033#endif
7034 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035 {
7036 STRCPY(p + len, "[+]");
7037 len += 3;
7038 }
7039 if (wp->w_buffer->b_p_ro)
7040 {
Bram Moolenaar23584032013-06-07 20:17:11 +02007041 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01007042 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 }
7044
Bram Moolenaar02631462017-09-22 15:20:32 +02007045 this_ru_col = ru_col - (Columns - wp->w_width);
7046 if (this_ru_col < (wp->w_width + 1) / 2)
7047 this_ru_col = (wp->w_width + 1) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048 if (this_ru_col <= 1)
7049 {
7050 p = (char_u *)"<"; /* No room for file name! */
7051 len = 1;
7052 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01007053 else if (has_mbyte)
7054 {
7055 int clen = 0, i;
7056
7057 /* Count total number of display cells. */
7058 clen = mb_string2cells(p, -1);
7059
7060 /* Find first character that will fit.
7061 * Going from start to end is much faster for DBCS. */
7062 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
7063 i += (*mb_ptr2len)(p + i))
7064 clen -= (*mb_ptr2cells)(p + i);
7065 len = clen;
7066 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 {
Bram Moolenaara12a1612019-01-24 16:39:02 +01007068 p = p + i - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069 *p = '<';
Bram Moolenaara12a1612019-01-24 16:39:02 +01007070 ++len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 }
7072
Bram Moolenaara12a1612019-01-24 16:39:02 +01007073 }
7074 else if (len > this_ru_col - 1)
7075 {
7076 p += len - (this_ru_col - 1);
7077 *p = '<';
7078 len = this_ru_col - 1;
7079 }
7080
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar53f81742017-09-22 14:35:51 +02007082 screen_puts(p, row, wp->w_wincol, attr);
7083 screen_fill(row, row + 1, len + wp->w_wincol,
7084 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007086 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
7088 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007089 - 1 + wp->w_wincol), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090
7091#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +02007092 win_redr_ruler(wp, TRUE, ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093#endif
7094 }
7095
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 /*
7097 * May need to draw the character below the vertical separator.
7098 */
7099 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
7100 {
7101 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007102 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103 else
7104 fillchar = fillchar_vsep(&attr);
7105 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
7106 attr);
7107 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007108 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109}
7110
Bram Moolenaar238a5642006-02-21 22:12:05 +00007111#ifdef FEAT_STL_OPT
7112/*
7113 * Redraw the status line according to 'statusline' and take care of any
7114 * errors encountered.
7115 */
7116 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007117redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00007118{
Bram Moolenaar362f3562009-11-03 16:20:34 +00007119 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02007120 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007121
7122 /* When called recursively return. This can happen when the statusline
7123 * contains an expression that triggers a redraw. */
7124 if (entered)
7125 return;
7126 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007127
Bram Moolenaara742e082016-04-05 21:10:38 +02007128 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007129 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02007130 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007131 {
7132 /* When there is an error disable the statusline, otherwise the
7133 * display is messed up with errors and a redraw triggers the problem
7134 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007135 set_string_option_direct((char_u *)"statusline", -1,
7136 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007137 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007138 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007139 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007140 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007141}
7142#endif
7143
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144/*
7145 * Return TRUE if the status line of window "wp" is connected to the status
7146 * line of the window right of it. If not, then it's a vertical separator.
7147 * Only call if (wp->w_vsep_width != 0).
7148 */
7149 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007150stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151{
7152 frame_T *fr;
7153
7154 fr = wp->w_frame;
7155 while (fr->fr_parent != NULL)
7156 {
7157 if (fr->fr_parent->fr_layout == FR_COL)
7158 {
7159 if (fr->fr_next != NULL)
7160 break;
7161 }
7162 else
7163 {
7164 if (fr->fr_next != NULL)
7165 return TRUE;
7166 }
7167 fr = fr->fr_parent;
7168 }
7169 return FALSE;
7170}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173/*
7174 * Get the value to show for the language mappings, active 'keymap'.
7175 */
7176 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007177get_keymap_str(
7178 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007179 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007180 char_u *buf, /* buffer for the result */
7181 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182{
7183 char_u *p;
7184
7185 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7186 return FALSE;
7187
7188 {
7189#ifdef FEAT_EVAL
7190 buf_T *old_curbuf = curbuf;
7191 win_T *old_curwin = curwin;
7192 char_u *s;
7193
7194 curbuf = wp->w_buffer;
7195 curwin = wp;
7196 STRCPY(buf, "b:keymap_name"); /* must be writable */
7197 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007198 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 --emsg_skip;
7200 curbuf = old_curbuf;
7201 curwin = old_curwin;
7202 if (p == NULL || *p == NUL)
7203#endif
7204 {
7205#ifdef FEAT_KEYMAP
7206 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7207 p = wp->w_buffer->b_p_keymap;
7208 else
7209#endif
7210 p = (char_u *)"lang";
7211 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007212 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 buf[0] = NUL;
7214#ifdef FEAT_EVAL
7215 vim_free(s);
7216#endif
7217 }
7218 return buf[0] != NUL;
7219}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220
7221#if defined(FEAT_STL_OPT) || defined(PROTO)
7222/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007223 * Redraw the status line or ruler of window "wp".
7224 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225 */
7226 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007227win_redr_custom(
7228 win_T *wp,
7229 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007231 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232 int attr;
7233 int curattr;
7234 int row;
7235 int col = 0;
7236 int maxwidth;
7237 int width;
7238 int n;
7239 int len;
7240 int fillchar;
7241 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007242 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007244 struct stl_hlrec hltab[STL_MAX_ITEM];
7245 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007246 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007247 win_T *ewp;
7248 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249
Bram Moolenaar1d633412013-12-11 15:52:01 +01007250 /* There is a tiny chance that this gets called recursively: When
7251 * redrawing a status line triggers redrawing the ruler or tabline.
7252 * Avoid trouble by not allowing recursion. */
7253 if (entered)
7254 return;
7255 entered = TRUE;
7256
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007258 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007260 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007261 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007262 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007263 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007264 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007265 maxwidth = Columns;
7266# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007267 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007268# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007270 else
7271 {
7272 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007273 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02007274 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007275
7276 if (draw_ruler)
7277 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007278 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007279 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007280 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007281 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007282 if (*++stl == '-')
7283 stl++;
7284 if (atoi((char *)stl))
7285 while (VIM_ISDIGIT(*stl))
7286 stl++;
7287 if (*stl++ != '(')
7288 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007289 }
Bram Moolenaar02631462017-09-22 15:20:32 +02007290 col = ru_col - (Columns - wp->w_width);
7291 if (col < (wp->w_width + 1) / 2)
7292 col = (wp->w_width + 1) / 2;
7293 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007294 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007295 {
7296 row = Rows - 1;
7297 --maxwidth; /* writing in last column may cause scrolling */
7298 fillchar = ' ';
7299 attr = 0;
7300 }
7301
7302# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007303 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007304# endif
7305 }
7306 else
7307 {
7308 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007309 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007310 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007311 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007312# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007313 use_sandbox = was_set_insecurely((char_u *)"statusline",
7314 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007315# endif
7316 }
7317
Bram Moolenaar53f81742017-09-22 14:35:51 +02007318 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007319 }
7320
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007322 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323
Bram Moolenaar61452852011-02-01 18:01:11 +01007324 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7325 * the cursor away and back. */
7326 ewp = wp == NULL ? curwin : wp;
7327 p_crb_save = ewp->w_p_crb;
7328 ewp->w_p_crb = FALSE;
7329
Bram Moolenaar362f3562009-11-03 16:20:34 +00007330 /* Make a copy, because the statusline may include a function call that
7331 * might change the option value and free the memory. */
7332 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007333 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007334 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007335 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007336 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007337 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007339 /* Make all characters printable. */
7340 p = transstr(buf);
7341 if (p != NULL)
7342 {
7343 vim_strncpy(buf, p, sizeof(buf) - 1);
7344 vim_free(p);
7345 }
7346
7347 /* fill up with "fillchar" */
7348 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007349 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351 len += (*mb_char2bytes)(fillchar, buf + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352 ++width;
7353 }
7354 buf[len] = NUL;
7355
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007356 /*
7357 * Draw each snippet with the specified highlighting.
7358 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359 curattr = attr;
7360 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007361 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007363 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364 screen_puts_len(p, len, row, col, curattr);
7365 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007366 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007368 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007370 else if (hltab[n].userhl < 0)
7371 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007372#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007373 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7374 && wp->w_status_height != 0)
7375 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007376 else if (wp != NULL && bt_terminal(wp->w_buffer)
7377 && wp->w_status_height != 0)
7378 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02007379#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007380 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007381 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007383 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384 }
7385 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007386
7387 if (wp == NULL)
7388 {
7389 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7390 col = 0;
7391 len = 0;
7392 p = buf;
7393 fillchar = 0;
7394 for (n = 0; tabtab[n].start != NULL; n++)
7395 {
7396 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7397 while (col < len)
7398 TabPageIdxs[col++] = fillchar;
7399 p = tabtab[n].start;
7400 fillchar = tabtab[n].userhl;
7401 }
7402 while (col < Columns)
7403 TabPageIdxs[col++] = fillchar;
7404 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007405
7406theend:
7407 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408}
7409
7410#endif /* FEAT_STL_OPT */
7411
7412/*
7413 * Output a single character directly to the screen and update ScreenLines.
7414 */
7415 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007416screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418 char_u buf[MB_MAXBYTES + 1];
7419
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007420 if (has_mbyte)
7421 buf[(*mb_char2bytes)(c, buf)] = NUL;
7422 else
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007423 {
7424 buf[0] = c;
7425 buf[1] = NUL;
7426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 screen_puts(buf, row, col, attr);
7428}
7429
7430/*
7431 * Get a single character directly from ScreenLines into "bytes[]".
7432 * Also return its attribute in *attrp;
7433 */
7434 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007435screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436{
7437 unsigned off;
7438
7439 /* safety check */
7440 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7441 {
7442 off = LineOffset[row] + col;
7443 *attrp = ScreenAttrs[off];
7444 bytes[0] = ScreenLines[off];
7445 bytes[1] = NUL;
7446
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447 if (enc_utf8 && ScreenLinesUC[off] != 0)
7448 bytes[utfc_char2bytes(off, bytes)] = NUL;
7449 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7450 {
7451 bytes[0] = ScreenLines[off];
7452 bytes[1] = ScreenLines2[off];
7453 bytes[2] = NUL;
7454 }
7455 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7456 {
7457 bytes[1] = ScreenLines[off + 1];
7458 bytes[2] = NUL;
7459 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 }
7461}
7462
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007463/*
7464 * Return TRUE if composing characters for screen posn "off" differs from
7465 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007466 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007467 */
7468 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007469screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007470{
7471 int i;
7472
7473 for (i = 0; i < Screen_mco; ++i)
7474 {
7475 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7476 return TRUE;
7477 if (u8cc[i] == 0)
7478 break;
7479 }
7480 return FALSE;
7481}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007482
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483/*
7484 * Put string '*text' on the screen at position 'row' and 'col', with
7485 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7486 * Note: only outputs within one row, message is truncated at screen boundary!
7487 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7488 */
7489 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007490screen_puts(
7491 char_u *text,
7492 int row,
7493 int col,
7494 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495{
7496 screen_puts_len(text, -1, row, col, attr);
7497}
7498
7499/*
7500 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7501 * a NUL.
7502 */
7503 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007504screen_puts_len(
7505 char_u *text,
7506 int textlen,
7507 int row,
7508 int col,
7509 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510{
7511 unsigned off;
7512 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007513 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514 int c;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007515 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516 int mbyte_blen = 1;
7517 int mbyte_cells = 1;
7518 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007519 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520 int clear_next_cell = FALSE;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007521#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007523 int pc, nc, nc1;
7524 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007526 int force_redraw_this;
7527 int force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007528 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529
Bram Moolenaar0b4c9ed2019-06-03 22:04:23 +02007530 // Safety check. The check for negative row and column is to fix issue
7531 // #4102. TODO: find out why row/col could be negative.
7532 if (ScreenLines == NULL
7533 || row >= screen_Rows || row < 0
7534 || col >= screen_Columns || col < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007536 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537
Bram Moolenaarc236c162008-07-13 17:41:49 +00007538 /* When drawing over the right halve of a double-wide char clear out the
7539 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007540 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaara12a1612019-01-24 16:39:02 +01007541#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00007542 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01007543#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007544 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007545 {
7546 ScreenLines[off - 1] = ' ';
7547 ScreenAttrs[off - 1] = 0;
7548 if (enc_utf8)
7549 {
7550 ScreenLinesUC[off - 1] = 0;
7551 ScreenLinesC[0][off - 1] = 0;
7552 }
7553 /* redraw the previous cell, make it empty */
7554 screen_char(off - 1, row, col - 1);
7555 /* force the cell at "col" to be redrawn */
7556 force_redraw_next = TRUE;
7557 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007558
Bram Moolenaar367329b2007-08-30 11:53:22 +00007559 max_off = LineOffset[row] + screen_Columns;
Bram Moolenaara064ac82007-08-05 18:10:54 +00007560 while (col < screen_Columns
7561 && (len < 0 || (int)(ptr - text) < len)
7562 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563 {
7564 c = *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565 /* check if this is the first byte of a multibyte */
7566 if (has_mbyte)
7567 {
7568 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007569 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007571 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7573 mbyte_cells = 1;
7574 else if (enc_dbcs != 0)
7575 mbyte_cells = mbyte_blen;
7576 else /* enc_utf8 */
7577 {
7578 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007579 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580 (int)((text + len) - ptr));
7581 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007582 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007584#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7586 {
7587 /* Do Arabic shaping. */
7588 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7589 {
7590 /* Past end of string to be displayed. */
7591 nc = NUL;
7592 nc1 = NUL;
7593 }
7594 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007595 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007596 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7597 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007598 nc1 = pcc[0];
7599 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600 pc = prev_c;
7601 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007602 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603 }
7604 else
7605 prev_c = u8c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007606#endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007607 if (col + mbyte_cells > screen_Columns)
7608 {
7609 /* Only 1 cell left, but character requires 2 cells:
7610 * display a '>' in the last column to avoid wrapping. */
7611 c = '>';
7612 mbyte_cells = 1;
7613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007614 }
7615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007617 force_redraw_this = force_redraw_next;
7618 force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007619
7620 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621 || (mbyte_cells == 2
7622 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7623 || (enc_dbcs == DBCS_JPNU
7624 && c == 0x8e
7625 && ScreenLines2[off] != ptr[1])
7626 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007627 && (ScreenLinesUC[off] !=
7628 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7629 || (ScreenLinesUC[off] != 0
7630 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007631 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007632 || exmode_active;
7633
Bram Moolenaar24a5ac52019-06-08 19:01:18 +02007634 if ((need_redraw || force_redraw_this)
7635#ifdef FEAT_TEXT_PROP
Bram Moolenaarc662ec92019-06-23 00:15:57 +02007636 && !blocked_by_popup(row, col)
Bram Moolenaar24a5ac52019-06-08 19:01:18 +02007637#endif
7638 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639 {
7640#if defined(FEAT_GUI) || defined(UNIX)
7641 /* The bold trick makes a single row of pixels appear in the next
7642 * character. When a bold character is removed, the next
7643 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007644 * and for some xterms. */
7645 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646# ifdef FEAT_GUI
7647 gui.in_use
7648# endif
7649# if defined(FEAT_GUI) && defined(UNIX)
7650 ||
7651# endif
7652# ifdef UNIX
7653 term_is_xterm
7654# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007655 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007657 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007659 if (n > HL_ALL)
7660 n = syn_attr2attr(n);
7661 if (n & HL_BOLD)
7662 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663 }
7664#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665 /* When at the end of the text and overwriting a two-cell
7666 * character with a one-cell character, need to clear the next
7667 * cell. Also when overwriting the left halve of a two-cell char
7668 * with the right halve of a two-cell char. Do this only once
7669 * (mb_off2cells() may return 2 on the right halve). */
7670 if (clear_next_cell)
7671 clear_next_cell = FALSE;
7672 else if (has_mbyte
7673 && (len < 0 ? ptr[mbyte_blen] == NUL
7674 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007675 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007677 && (*mb_off2cells)(off, max_off) == 1
7678 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679 clear_next_cell = TRUE;
7680
7681 /* Make sure we never leave a second byte of a double-byte behind,
7682 * it confuses mb_off2cells(). */
7683 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007684 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007686 && (*mb_off2cells)(off, max_off) == 1
7687 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688 ScreenLines[off + mbyte_blen] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689 ScreenLines[off] = c;
7690 ScreenAttrs[off] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691 if (enc_utf8)
7692 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007693 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694 ScreenLinesUC[off] = 0;
7695 else
7696 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007697 int i;
7698
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007700 for (i = 0; i < Screen_mco; ++i)
7701 {
7702 ScreenLinesC[i][off] = u8cc[i];
7703 if (u8cc[i] == 0)
7704 break;
7705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706 }
7707 if (mbyte_cells == 2)
7708 {
7709 ScreenLines[off + 1] = 0;
7710 ScreenAttrs[off + 1] = attr;
7711 }
7712 screen_char(off, row, col);
7713 }
7714 else if (mbyte_cells == 2)
7715 {
7716 ScreenLines[off + 1] = ptr[1];
7717 ScreenAttrs[off + 1] = attr;
7718 screen_char_2(off, row, col);
7719 }
7720 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7721 {
7722 ScreenLines2[off] = ptr[1];
7723 screen_char(off, row, col);
7724 }
7725 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726 screen_char(off, row, col);
7727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728 if (has_mbyte)
7729 {
7730 off += mbyte_cells;
7731 col += mbyte_cells;
7732 ptr += mbyte_blen;
7733 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007734 {
7735 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007737 len = -1;
7738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739 }
7740 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741 {
7742 ++off;
7743 ++col;
7744 ++ptr;
7745 }
7746 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007747
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007748 /* If we detected the next character needs to be redrawn, but the text
7749 * doesn't extend up to there, update the character here. */
7750 if (force_redraw_next && col < screen_Columns)
7751 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007752 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7753 screen_char_2(off, row, col);
7754 else
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007755 screen_char(off, row, col);
7756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757}
7758
7759#ifdef FEAT_SEARCH_EXTRA
7760/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007761 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762 */
7763 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007764start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765{
7766 if (p_hls && !no_hlsearch)
7767 {
7768 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007769 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007770# ifdef FEAT_RELTIME
7771 /* Set the time limit to 'redrawtime'. */
7772 profile_setlimit(p_rdt, &search_hl.tm);
7773# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774 }
7775}
7776
7777/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007778 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779 */
7780 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007781end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782{
7783 if (search_hl.rm.regprog != NULL)
7784 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007785 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786 search_hl.rm.regprog = NULL;
7787 }
7788}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02007789#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02007790
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007792screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793{
7794 attrentry_T *aep = NULL;
7795
7796 screen_attr = attr;
7797 if (full_screen
Bram Moolenaar4f974752019-02-17 17:44:42 +01007798#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799 && termcap_active
7800#endif
7801 )
7802 {
7803#ifdef FEAT_GUI
7804 if (gui.in_use)
7805 {
7806 char buf[20];
7807
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007808 /* The GUI handles this internally. */
7809 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810 OUT_STR(buf);
7811 }
7812 else
7813#endif
7814 {
7815 if (attr > HL_ALL) /* special HL attr. */
7816 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007817 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818 aep = syn_cterm_attr2entry(attr);
7819 else
7820 aep = syn_term_attr2entry(attr);
7821 if (aep == NULL) /* did ":syntax clear" */
7822 attr = 0;
7823 else
7824 attr = aep->ae_attr;
7825 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01007826 if ((attr & HL_BOLD) && *T_MD != NUL) /* bold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007828 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007829#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007830 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
7831 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
7832 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007833#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007834 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007835 /* If the Normal FG color has BOLD attribute and the new HL
7836 * has a FG color defined, clear BOLD. */
7837 out_str(T_ME);
Bram Moolenaar45a00002017-12-22 21:12:34 +01007838 if ((attr & HL_STANDOUT) && *T_SO != NUL) /* standout */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839 out_str(T_SO);
Bram Moolenaar45a00002017-12-22 21:12:34 +01007840 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) /* undercurl */
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01007841 out_str(T_UCS);
7842 if (((attr & HL_UNDERLINE) /* underline or undercurl */
Bram Moolenaar45a00002017-12-22 21:12:34 +01007843 || ((attr & HL_UNDERCURL) && *T_UCS == NUL))
7844 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845 out_str(T_US);
Bram Moolenaar45a00002017-12-22 21:12:34 +01007846 if ((attr & HL_ITALIC) && *T_CZH != NUL) /* italic */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847 out_str(T_CZH);
Bram Moolenaar45a00002017-12-22 21:12:34 +01007848 if ((attr & HL_INVERSE) && *T_MR != NUL) /* inverse (reverse) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849 out_str(T_MR);
Bram Moolenaar45a00002017-12-22 21:12:34 +01007850 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) /* strike */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02007851 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852
7853 /*
7854 * Output the color or start string after bold etc., in case the
7855 * bold etc. override the color setting.
7856 */
7857 if (aep != NULL)
7858 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007859#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007860 /* When 'termguicolors' is set but fg or bg is unset,
7861 * fall back to the cterm colors. This helps for SpellBad,
7862 * where the GUI uses a red undercurl. */
7863 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007865 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007866 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007867 }
7868 else
7869#endif
7870 if (t_colors > 1)
7871 {
7872 if (aep->ae_u.cterm.fg_color)
7873 term_fg_color(aep->ae_u.cterm.fg_color - 1);
7874 }
7875#ifdef FEAT_TERMGUICOLORS
7876 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
7877 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007878 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007879 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880 }
7881 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007882#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007883 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007885 if (aep->ae_u.cterm.bg_color)
7886 term_bg_color(aep->ae_u.cterm.bg_color - 1);
7887 }
7888
Bram Moolenaarf708ac52018-03-12 21:48:32 +01007889 if (!IS_CTERM)
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007890 {
7891 if (aep->ae_u.term.start != NULL)
7892 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893 }
7894 }
7895 }
7896 }
7897}
7898
7899 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007900screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901{
7902 int do_ME = FALSE; /* output T_ME code */
7903
7904 if (screen_attr != 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01007905#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906 && termcap_active
7907#endif
7908 )
7909 {
7910#ifdef FEAT_GUI
7911 if (gui.in_use)
7912 {
7913 char buf[20];
7914
7915 /* use internal GUI code */
7916 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
7917 OUT_STR(buf);
7918 }
7919 else
7920#endif
7921 {
7922 if (screen_attr > HL_ALL) /* special HL attr. */
7923 {
7924 attrentry_T *aep;
7925
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007926 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 {
7928 /*
7929 * Assume that t_me restores the original colors!
7930 */
7931 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007932 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007933#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007934 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
7935 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
7936 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007937#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007938 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007939#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007940 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
7941 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
7942 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007943#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007944 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945 do_ME = TRUE;
7946 }
7947 else
7948 {
7949 aep = syn_term_attr2entry(screen_attr);
7950 if (aep != NULL && aep->ae_u.term.stop != NULL)
7951 {
7952 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
7953 do_ME = TRUE;
7954 else
7955 out_str(aep->ae_u.term.stop);
7956 }
7957 }
7958 if (aep == NULL) /* did ":syntax clear" */
7959 screen_attr = 0;
7960 else
7961 screen_attr = aep->ae_attr;
7962 }
7963
7964 /*
7965 * Often all ending-codes are equal to T_ME. Avoid outputting the
7966 * same sequence several times.
7967 */
7968 if (screen_attr & HL_STANDOUT)
7969 {
7970 if (STRCMP(T_SE, T_ME) == 0)
7971 do_ME = TRUE;
7972 else
7973 out_str(T_SE);
7974 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01007975 if ((screen_attr & HL_UNDERCURL) && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01007976 {
7977 if (STRCMP(T_UCE, T_ME) == 0)
7978 do_ME = TRUE;
7979 else
7980 out_str(T_UCE);
7981 }
7982 if ((screen_attr & HL_UNDERLINE)
Bram Moolenaar45a00002017-12-22 21:12:34 +01007983 || ((screen_attr & HL_UNDERCURL) && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984 {
7985 if (STRCMP(T_UE, T_ME) == 0)
7986 do_ME = TRUE;
7987 else
7988 out_str(T_UE);
7989 }
7990 if (screen_attr & HL_ITALIC)
7991 {
7992 if (STRCMP(T_CZR, T_ME) == 0)
7993 do_ME = TRUE;
7994 else
7995 out_str(T_CZR);
7996 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02007997 if (screen_attr & HL_STRIKETHROUGH)
7998 {
7999 if (STRCMP(T_STE, T_ME) == 0)
8000 do_ME = TRUE;
8001 else
8002 out_str(T_STE);
8003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8005 out_str(T_ME);
8006
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008007#ifdef FEAT_TERMGUICOLORS
8008 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008010 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008011 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008012 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008013 term_bg_rgb_color(cterm_normal_bg_gui_color);
8014 }
8015 else
8016#endif
8017 {
8018 if (t_colors > 1)
8019 {
8020 /* set Normal cterm colors */
8021 if (cterm_normal_fg_color != 0)
8022 term_fg_color(cterm_normal_fg_color - 1);
8023 if (cterm_normal_bg_color != 0)
8024 term_bg_color(cterm_normal_bg_color - 1);
8025 if (cterm_normal_fg_bold)
8026 out_str(T_MD);
8027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028 }
8029 }
8030 }
8031 screen_attr = 0;
8032}
8033
8034/*
8035 * Reset the colors for a cterm. Used when leaving Vim.
8036 * The machine specific code may override this again.
8037 */
8038 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008039reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008041 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042 {
8043 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008044#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008045 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8046 || cterm_normal_bg_gui_color != INVALCOLOR)
8047 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008048#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008050#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051 {
8052 out_str(T_OP);
8053 screen_attr = -1;
8054 }
8055 if (cterm_normal_fg_bold)
8056 {
8057 out_str(T_ME);
8058 screen_attr = -1;
8059 }
8060 }
8061}
8062
8063/*
8064 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8065 * using the attributes from ScreenAttrs["off"].
8066 */
8067 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008068screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069{
8070 int attr;
8071
8072 /* Check for illegal values, just in case (could happen just after
8073 * resizing). */
8074 if (row >= screen_Rows || col >= screen_Columns)
8075 return;
8076
Bram Moolenaar33796b32019-06-08 16:01:13 +02008077 // Skip if under the popup menu.
8078 // Popup windows with zindex higher than POPUPMENU_ZINDEX go on top.
8079 if (pum_under_menu(row, col)
Bram Moolenaare2c453d2019-08-21 14:37:09 +02008080#ifdef FEAT_TEXT_PROP
Bram Moolenaar33796b32019-06-08 16:01:13 +02008081 && screen_zindex <= POPUPMENU_ZINDEX
Bram Moolenaare2c453d2019-08-21 14:37:09 +02008082#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02008083 )
Bram Moolenaarae654382019-01-17 21:09:05 +01008084 return;
Bram Moolenaar33796b32019-06-08 16:01:13 +02008085#ifdef FEAT_TEXT_PROP
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008086 if (blocked_by_popup(row, col))
Bram Moolenaar33796b32019-06-08 16:01:13 +02008087 return;
8088#endif
8089
Bram Moolenaar494838a2015-02-10 19:20:37 +01008090 /* Outputting a character in the last cell on the screen may scroll the
8091 * screen up. Only do it when the "xn" termcap property is set, otherwise
8092 * mark the character invalid (update it when scrolled up). */
8093 if (*T_XN == NUL
8094 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095#ifdef FEAT_RIGHTLEFT
8096 /* account for first command-line character in rightleft mode */
8097 && !cmdmsg_rl
8098#endif
8099 )
8100 {
8101 ScreenAttrs[off] = (sattr_T)-1;
8102 return;
8103 }
8104
8105 /*
8106 * Stop highlighting first, so it's easier to move the cursor.
8107 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108 if (screen_char_attr != 0)
8109 attr = screen_char_attr;
8110 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111 attr = ScreenAttrs[off];
8112 if (screen_attr != attr)
8113 screen_stop_highlight();
8114
8115 windgoto(row, col);
8116
8117 if (screen_attr != attr)
8118 screen_start_highlight(attr);
8119
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120 if (enc_utf8 && ScreenLinesUC[off] != 0)
8121 {
8122 char_u buf[MB_MAXBYTES + 1];
8123
Bram Moolenaarcb070082016-04-02 22:14:51 +02008124 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008125 {
8126 if (*p_ambw == 'd'
Bram Moolenaara12a1612019-01-24 16:39:02 +01008127#ifdef FEAT_GUI
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008128 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008129#endif
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008130 )
8131 {
8132 /* Clear the two screen cells. If the character is actually
8133 * single width it won't change the second cell. */
8134 out_str((char_u *)" ");
8135 term_windgoto(row, col);
8136 }
8137 /* not sure where the cursor is after drawing the ambiguous width
8138 * character */
Bram Moolenaarcb070082016-04-02 22:14:51 +02008139 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008140 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02008141 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008143
8144 /* Convert the UTF-8 character to bytes and write it. */
8145 buf[utfc_char2bytes(off, buf)] = NUL;
8146 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 }
8148 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152 /* double-byte character in single-width cell */
8153 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8154 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 }
8156
8157 screen_cur_col++;
8158}
8159
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160/*
8161 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8162 * on the screen at position 'row' and 'col'.
8163 * The attributes of the first byte is used for all. This is required to
8164 * output the two bytes of a double-byte character with nothing in between.
8165 */
8166 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008167screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168{
8169 /* Check for illegal values (could be wrong when screen was resized). */
8170 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8171 return;
8172
8173 /* Outputting the last character on the screen may scrollup the screen.
8174 * Don't to it! Mark the character invalid (update it when scrolled up) */
8175 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8176 {
8177 ScreenAttrs[off] = (sattr_T)-1;
8178 return;
8179 }
8180
8181 /* Output the first byte normally (positions the cursor), then write the
8182 * second byte directly. */
8183 screen_char(off, row, col);
8184 out_char(ScreenLines[off + 1]);
8185 ++screen_cur_col;
8186}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008187
Bram Moolenaar071d4272004-06-13 20:20:40 +00008188/*
8189 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8190 * This uses the contents of ScreenLines[] and doesn't change it.
8191 */
8192 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008193screen_draw_rectangle(
8194 int row,
8195 int col,
8196 int height,
8197 int width,
8198 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199{
8200 int r, c;
8201 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008202 int max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008203
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008204 /* Can't use ScreenLines unless initialized */
8205 if (ScreenLines == NULL)
8206 return;
8207
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208 if (invert)
8209 screen_char_attr = HL_INVERSE;
8210 for (r = row; r < row + height; ++r)
8211 {
8212 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008213 max_off = off + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214 for (c = col; c < col + width; ++c)
8215 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00008216 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217 {
8218 screen_char_2(off + c, r, c);
8219 ++c;
8220 }
8221 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222 {
8223 screen_char(off + c, r, c);
Bram Moolenaar367329b2007-08-30 11:53:22 +00008224 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225 ++c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226 }
8227 }
8228 }
8229 screen_char_attr = 0;
8230}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232/*
8233 * Redraw the characters for a vertically split window.
8234 */
8235 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008236redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237{
8238 int col;
8239 int width;
8240
8241# ifdef FEAT_CLIPBOARD
8242 clip_may_clear_selection(row, end - 1);
8243# endif
8244
8245 if (wp == NULL)
8246 {
8247 col = 0;
8248 width = Columns;
8249 }
8250 else
8251 {
8252 col = wp->w_wincol;
8253 width = wp->w_width;
8254 }
8255 screen_draw_rectangle(row, col, end - row, width, FALSE);
8256}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008258 static void
8259space_to_screenline(int off, int attr)
8260{
8261 ScreenLines[off] = ' ';
8262 ScreenAttrs[off] = attr;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008263 if (enc_utf8)
8264 ScreenLinesUC[off] = 0;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008265}
8266
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267/*
8268 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8269 * with character 'c1' in first column followed by 'c2' in the other columns.
8270 * Use attributes 'attr'.
8271 */
8272 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008273screen_fill(
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008274 int start_row,
8275 int end_row,
8276 int start_col,
8277 int end_col,
8278 int c1,
8279 int c2,
8280 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008282 int row;
8283 int col;
8284 int off;
8285 int end_off;
8286 int did_delete;
8287 int c;
8288 int norm_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008290 int force_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291#endif
8292
8293 if (end_row > screen_Rows) /* safety check */
8294 end_row = screen_Rows;
8295 if (end_col > screen_Columns) /* safety check */
8296 end_col = screen_Columns;
8297 if (ScreenLines == NULL
8298 || start_row >= end_row
8299 || start_col >= end_col) /* nothing to do */
8300 return;
8301
8302 /* it's a "normal" terminal when not in a GUI or cterm */
8303 norm_term = (
8304#ifdef FEAT_GUI
8305 !gui.in_use &&
8306#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008307 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308 for (row = start_row; row < end_row; ++row)
8309 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008310 if (has_mbyte
Bram Moolenaara12a1612019-01-24 16:39:02 +01008311#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00008312 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008313#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00008314 )
8315 {
8316 /* When drawing over the right halve of a double-wide char clear
8317 * out the left halve. When drawing over the left halve of a
8318 * double wide-char clear out the right halve. Only needed in a
8319 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008320 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008321 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008322 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008323 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008324 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325 /*
8326 * Try to use delete-line termcap code, when no attributes or in a
8327 * "normal" terminal, where a bold/italic space is just a
8328 * space.
8329 */
8330 did_delete = FALSE;
8331 if (c2 == ' '
8332 && end_col == Columns
8333 && can_clear(T_CE)
8334 && (attr == 0
8335 || (norm_term
8336 && attr <= HL_ALL
8337 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8338 {
8339 /*
8340 * check if we really need to clear something
8341 */
8342 col = start_col;
8343 if (c1 != ' ') /* don't clear first char */
8344 ++col;
8345
8346 off = LineOffset[row] + col;
8347 end_off = LineOffset[row] + end_col;
8348
8349 /* skip blanks (used often, keep it fast!) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350 if (enc_utf8)
8351 while (off < end_off && ScreenLines[off] == ' '
8352 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8353 ++off;
8354 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355 while (off < end_off && ScreenLines[off] == ' '
8356 && ScreenAttrs[off] == 0)
8357 ++off;
8358 if (off < end_off) /* something to be cleared */
8359 {
8360 col = off - LineOffset[row];
8361 screen_stop_highlight();
8362 term_windgoto(row, col);/* clear rest of this screen line */
8363 out_str(T_CE);
8364 screen_start(); /* don't know where cursor is now */
8365 col = end_col - col;
8366 while (col--) /* clear chars in ScreenLines */
8367 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008368 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369 ++off;
8370 }
8371 }
8372 did_delete = TRUE; /* the chars are cleared now */
8373 }
8374
8375 off = LineOffset[row] + start_col;
8376 c = c1;
8377 for (col = start_col; col < end_col; ++col)
8378 {
Bram Moolenaar33796b32019-06-08 16:01:13 +02008379 if ((ScreenLines[off] != c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008380 || (enc_utf8 && (int)ScreenLinesUC[off]
8381 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382 || ScreenAttrs[off] != attr
8383#if defined(FEAT_GUI) || defined(UNIX)
8384 || force_next
8385#endif
8386 )
Bram Moolenaar33796b32019-06-08 16:01:13 +02008387#ifdef FEAT_TEXT_PROP
8388 // Skip if under a(nother) popup.
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008389 && !blocked_by_popup(row, col)
Bram Moolenaar33796b32019-06-08 16:01:13 +02008390#endif
8391 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392 {
8393#if defined(FEAT_GUI) || defined(UNIX)
8394 /* The bold trick may make a single row of pixels appear in
8395 * the next character. When a bold character is removed, the
8396 * next character should be redrawn too. This happens for our
8397 * own GUI and for some xterms. */
8398 if (
8399# ifdef FEAT_GUI
8400 gui.in_use
8401# endif
8402# if defined(FEAT_GUI) && defined(UNIX)
8403 ||
8404# endif
8405# ifdef UNIX
8406 term_is_xterm
8407# endif
8408 )
8409 {
8410 if (ScreenLines[off] != ' '
8411 && (ScreenAttrs[off] > HL_ALL
8412 || ScreenAttrs[off] & HL_BOLD))
8413 force_next = TRUE;
8414 else
8415 force_next = FALSE;
8416 }
8417#endif
8418 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419 if (enc_utf8)
8420 {
8421 if (c >= 0x80)
8422 {
8423 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008424 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008425 }
8426 else
8427 ScreenLinesUC[off] = 0;
8428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429 ScreenAttrs[off] = attr;
8430 if (!did_delete || c != ' ')
8431 screen_char(off, row, col);
8432 }
8433 ++off;
8434 if (col == start_col)
8435 {
8436 if (did_delete)
8437 break;
8438 c = c2;
8439 }
8440 }
8441 if (end_col == Columns)
8442 LineWraps[row] = FALSE;
8443 if (row == Rows - 1) /* overwritten the command line */
8444 {
8445 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02008446 if (start_col == 0 && end_col == Columns
8447 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008449 if (start_col == 0)
8450 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451 }
8452 }
8453}
8454
8455/*
8456 * Check if there should be a delay. Used before clearing or redrawing the
8457 * screen or the command line.
8458 */
8459 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008460check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461{
8462 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8463 && !did_wait_return
8464 && emsg_silent == 0)
8465 {
8466 out_flush();
8467 ui_delay(1000L, TRUE);
8468 emsg_on_display = FALSE;
8469 if (check_msg_scroll)
8470 msg_scroll = FALSE;
8471 }
8472}
8473
8474/*
Bram Moolenaarca57ab52019-04-13 14:53:16 +02008475 * Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect.
8476 */
8477 static void
8478clear_TabPageIdxs(void)
8479{
8480 int scol;
8481
8482 for (scol = 0; scol < Columns; ++scol)
8483 TabPageIdxs[scol] = 0;
8484}
8485
8486/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008488 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489 * Returns TRUE if there is a valid screen to write to.
8490 * Returns FALSE when starting up and screen not initialized yet.
8491 */
8492 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008493screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008495 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496 return (ScreenLines != NULL);
8497}
8498
8499/*
8500 * Resize the shell to Rows and Columns.
8501 * Allocate ScreenLines[] and associated items.
8502 *
8503 * There may be some time between setting Rows and Columns and (re)allocating
8504 * ScreenLines[]. This happens when starting up and when (manually) changing
8505 * the shell size. Always use screen_Rows and screen_Columns to access items
8506 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8507 * final size of the shell is needed.
8508 */
8509 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008510screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511{
8512 int new_row, old_row;
8513#ifdef FEAT_GUI
8514 int old_Rows;
8515#endif
8516 win_T *wp;
8517 int outofmem = FALSE;
8518 int len;
8519 schar_T *new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008521 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008523 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524 sattr_T *new_ScreenAttrs;
8525 unsigned *new_LineOffset;
8526 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008527 short *new_TabPageIdxs;
Bram Moolenaar33796b32019-06-08 16:01:13 +02008528#ifdef FEAT_TEXT_PROP
8529 short *new_popup_mask;
Bram Moolenaar4c063a02019-06-10 21:24:12 +02008530 short *new_popup_mask_next;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008531 char *new_popup_transparent;
Bram Moolenaar33796b32019-06-08 16:01:13 +02008532#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00008533 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008535 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008536 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008538retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539 /*
8540 * Allocation of the screen buffers is done only when the size changes and
8541 * when Rows and Columns have been set and we have started doing full
8542 * screen stuff.
8543 */
8544 if ((ScreenLines != NULL
8545 && Rows == screen_Rows
8546 && Columns == screen_Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547 && enc_utf8 == (ScreenLinesUC != NULL)
8548 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaara12a1612019-01-24 16:39:02 +01008549 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550 || Rows == 0
8551 || Columns == 0
8552 || (!full_screen && ScreenLines == NULL))
8553 return;
8554
8555 /*
8556 * It's possible that we produce an out-of-memory message below, which
8557 * will cause this function to be called again. To break the loop, just
8558 * return here.
8559 */
8560 if (entered)
8561 return;
8562 entered = TRUE;
8563
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008564 /*
8565 * Note that the window sizes are updated before reallocating the arrays,
8566 * thus we must not redraw here!
8567 */
8568 ++RedrawingDisabled;
8569
Bram Moolenaar071d4272004-06-13 20:20:40 +00008570 win_new_shellsize(); /* fit the windows in the new sized shell */
8571
Bram Moolenaar071d4272004-06-13 20:20:40 +00008572 comp_col(); /* recompute columns for shown command and ruler */
8573
8574 /*
8575 * We're changing the size of the screen.
8576 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8577 * - Move lines from the old arrays into the new arrays, clear extra
8578 * lines (unless the screen is going to be cleared).
8579 * - Free the old arrays.
8580 *
8581 * If anything fails, make ScreenLines NULL, so we don't do anything!
8582 * Continuing with the old ScreenLines may result in a crash, because the
8583 * size is wrong.
8584 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008585 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008587 if (aucmd_win != NULL)
8588 win_free_lsize(aucmd_win);
Bram Moolenaar8caaf822019-06-01 18:11:22 +02008589#ifdef FEAT_TEXT_PROP
8590 // global popup windows
8591 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
8592 win_free_lsize(wp);
8593 // tab-local popup windows
8594 FOR_ALL_TABPAGES(tp)
8595 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
8596 win_free_lsize(wp);
8597#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008598
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008599 new_ScreenLines = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
Bram Moolenaar216b7102010-03-23 13:56:59 +01008600 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008601 if (enc_utf8)
8602 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008603 new_ScreenLinesUC = LALLOC_MULT(u8char_T, (Rows + 1) * Columns);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008604 for (i = 0; i < p_mco; ++i)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008605 new_ScreenLinesC[i] = LALLOC_CLEAR_MULT(u8char_T,
8606 (Rows + 1) * Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607 }
8608 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008609 new_ScreenLines2 = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
8610 new_ScreenAttrs = LALLOC_MULT(sattr_T, (Rows + 1) * Columns);
8611 new_LineOffset = LALLOC_MULT(unsigned, Rows);
8612 new_LineWraps = LALLOC_MULT(char_u, Rows);
8613 new_TabPageIdxs = LALLOC_MULT(short, Columns);
Bram Moolenaar33796b32019-06-08 16:01:13 +02008614#ifdef FEAT_TEXT_PROP
8615 new_popup_mask = LALLOC_MULT(short, Rows * Columns);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02008616 new_popup_mask_next = LALLOC_MULT(short, Rows * Columns);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008617 new_popup_transparent = LALLOC_MULT(char, Rows * Columns);
Bram Moolenaar33796b32019-06-08 16:01:13 +02008618#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008620 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621 {
8622 if (win_alloc_lines(wp) == FAIL)
8623 {
8624 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008625 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626 }
8627 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008628 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8629 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008630 outofmem = TRUE;
Bram Moolenaar8caaf822019-06-01 18:11:22 +02008631#ifdef FEAT_TEXT_PROP
8632 // global popup windows
8633 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
8634 if (win_alloc_lines(wp) == FAIL)
8635 {
8636 outofmem = TRUE;
8637 goto give_up;
8638 }
8639 // tab-local popup windows
8640 FOR_ALL_TABPAGES(tp)
8641 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
8642 if (win_alloc_lines(wp) == FAIL)
8643 {
8644 outofmem = TRUE;
8645 goto give_up;
8646 }
8647#endif
8648
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008649give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008651 for (i = 0; i < p_mco; ++i)
8652 if (new_ScreenLinesC[i] == NULL)
8653 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654 if (new_ScreenLines == NULL
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008655 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008657 || new_ScreenAttrs == NULL
8658 || new_LineOffset == NULL
8659 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008660 || new_TabPageIdxs == NULL
Bram Moolenaar33796b32019-06-08 16:01:13 +02008661#ifdef FEAT_TEXT_PROP
8662 || new_popup_mask == NULL
Bram Moolenaar4c063a02019-06-10 21:24:12 +02008663 || new_popup_mask_next == NULL
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008664 || new_popup_transparent == NULL
Bram Moolenaar33796b32019-06-08 16:01:13 +02008665#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008666 || outofmem)
8667 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008668 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008669 {
8670 /* guess the size */
8671 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8672
8673 /* Remember we did this to avoid getting outofmem messages over
8674 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008675 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008676 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01008677 VIM_CLEAR(new_ScreenLines);
Bram Moolenaard23a8232018-02-10 18:45:26 +01008678 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008679 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01008680 VIM_CLEAR(new_ScreenLinesC[i]);
8681 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaard23a8232018-02-10 18:45:26 +01008682 VIM_CLEAR(new_ScreenAttrs);
8683 VIM_CLEAR(new_LineOffset);
8684 VIM_CLEAR(new_LineWraps);
8685 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar33796b32019-06-08 16:01:13 +02008686#ifdef FEAT_TEXT_PROP
8687 VIM_CLEAR(new_popup_mask);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02008688 VIM_CLEAR(new_popup_mask_next);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008689 VIM_CLEAR(new_popup_transparent);
Bram Moolenaar33796b32019-06-08 16:01:13 +02008690#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691 }
8692 else
8693 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008694 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008695
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696 for (new_row = 0; new_row < Rows; ++new_row)
8697 {
8698 new_LineOffset[new_row] = new_row * Columns;
8699 new_LineWraps[new_row] = FALSE;
8700
8701 /*
8702 * If the screen is not going to be cleared, copy as much as
8703 * possible from the old screen to the new one and clear the rest
8704 * (used when resizing the window at the "--more--" prompt or when
8705 * executing an external command, for the GUI).
8706 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008707 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708 {
8709 (void)vim_memset(new_ScreenLines + new_row * Columns,
8710 ' ', (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711 if (enc_utf8)
8712 {
8713 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8714 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008715 for (i = 0; i < p_mco; ++i)
8716 (void)vim_memset(new_ScreenLinesC[i]
8717 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718 0, (size_t)Columns * sizeof(u8char_T));
8719 }
8720 if (enc_dbcs == DBCS_JPNU)
8721 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8722 0, (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8724 0, (size_t)Columns * sizeof(sattr_T));
8725 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008726 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727 {
8728 if (screen_Columns < Columns)
8729 len = screen_Columns;
8730 else
8731 len = Columns;
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008732 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008733 * may be invalid now. Also when p_mco changes. */
8734 if (!(enc_utf8 && ScreenLinesUC == NULL)
8735 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008736 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8737 ScreenLines + LineOffset[old_row],
8738 (size_t)len * sizeof(schar_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008739 if (enc_utf8 && ScreenLinesUC != NULL
8740 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741 {
8742 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8743 ScreenLinesUC + LineOffset[old_row],
8744 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008745 for (i = 0; i < p_mco; ++i)
8746 mch_memmove(new_ScreenLinesC[i]
8747 + new_LineOffset[new_row],
8748 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749 (size_t)len * sizeof(u8char_T));
8750 }
8751 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8752 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8753 ScreenLines2 + LineOffset[old_row],
8754 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8756 ScreenAttrs + LineOffset[old_row],
8757 (size_t)len * sizeof(sattr_T));
8758 }
8759 }
8760 }
8761 /* Use the last line of the screen for the current line. */
8762 current_ScreenLine = new_ScreenLines + Rows * Columns;
Bram Moolenaar6ace95e2019-08-13 23:09:49 +02008763
8764#ifdef FEAT_TEXT_PROP
8765 vim_memset(new_popup_mask, 0, Rows * Columns * sizeof(short));
8766 vim_memset(new_popup_transparent, 0, Rows * Columns * sizeof(char));
8767#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768 }
8769
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008770 free_screenlines();
8771
Bram Moolenaar6ace95e2019-08-13 23:09:49 +02008772 // NOTE: this may result in all pointers to become NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773 ScreenLines = new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008775 for (i = 0; i < p_mco; ++i)
8776 ScreenLinesC[i] = new_ScreenLinesC[i];
8777 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778 ScreenLines2 = new_ScreenLines2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779 ScreenAttrs = new_ScreenAttrs;
8780 LineOffset = new_LineOffset;
8781 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008782 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar33796b32019-06-08 16:01:13 +02008783#ifdef FEAT_TEXT_PROP
8784 popup_mask = new_popup_mask;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008785 popup_mask_next = new_popup_mask_next;
8786 popup_transparent = new_popup_transparent;
Bram Moolenaar33796b32019-06-08 16:01:13 +02008787 popup_mask_refresh = TRUE;
8788#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789
8790 /* It's important that screen_Rows and screen_Columns reflect the actual
8791 * size of ScreenLines[]. Set them before calling anything. */
8792#ifdef FEAT_GUI
8793 old_Rows = screen_Rows;
8794#endif
8795 screen_Rows = Rows;
8796 screen_Columns = Columns;
8797
8798 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008799 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800 screenclear2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801#ifdef FEAT_GUI
8802 else if (gui.in_use
8803 && !gui.starting
8804 && ScreenLines != NULL
8805 && old_Rows != Rows)
8806 {
8807 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8808 /*
8809 * Adjust the position of the cursor, for when executing an external
8810 * command.
8811 */
8812 if (msg_row >= Rows) /* Rows got smaller */
8813 msg_row = Rows - 1; /* put cursor at last row */
8814 else if (Rows > old_Rows) /* Rows got bigger */
8815 msg_row += Rows - old_Rows; /* put cursor in same place */
8816 if (msg_col >= Columns) /* Columns got smaller */
8817 msg_col = Columns - 1; /* put cursor at last column */
8818 }
8819#endif
Bram Moolenaarca57ab52019-04-13 14:53:16 +02008820 clear_TabPageIdxs();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008823 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008824
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008825 /*
8826 * Do not apply autocommands more than 3 times to avoid an endless loop
8827 * in case applying autocommands always changes Rows or Columns.
8828 */
8829 if (starting == 0 && ++retry_count <= 3)
8830 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008831 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008832 /* In rare cases, autocommands may have altered Rows or Columns,
8833 * jump back to check if we need to allocate the screen again. */
8834 goto retry;
8835 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008836}
8837
8838 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008839free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008840{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008841 int i;
8842
Bram Moolenaar33796b32019-06-08 16:01:13 +02008843 VIM_CLEAR(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008844 for (i = 0; i < Screen_mco; ++i)
Bram Moolenaar33796b32019-06-08 16:01:13 +02008845 VIM_CLEAR(ScreenLinesC[i]);
8846 VIM_CLEAR(ScreenLines2);
8847 VIM_CLEAR(ScreenLines);
8848 VIM_CLEAR(ScreenAttrs);
8849 VIM_CLEAR(LineOffset);
8850 VIM_CLEAR(LineWraps);
8851 VIM_CLEAR(TabPageIdxs);
8852#ifdef FEAT_TEXT_PROP
8853 VIM_CLEAR(popup_mask);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02008854 VIM_CLEAR(popup_mask_next);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008855 VIM_CLEAR(popup_transparent);
Bram Moolenaar33796b32019-06-08 16:01:13 +02008856#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008857}
8858
8859 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008860screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861{
8862 check_for_delay(FALSE);
8863 screenalloc(FALSE); /* allocate screen buffers if size changed */
8864 screenclear2(); /* clear the screen */
8865}
8866
8867 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008868screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869{
8870 int i;
8871
8872 if (starting == NO_SCREEN || ScreenLines == NULL
8873#ifdef FEAT_GUI
8874 || (gui.in_use && gui.starting)
8875#endif
8876 )
8877 return;
8878
8879#ifdef FEAT_GUI
8880 if (!gui.in_use)
8881#endif
8882 screen_attr = -1; /* force setting the Normal colors */
8883 screen_stop_highlight(); /* don't want highlighting here */
8884
8885#ifdef FEAT_CLIPBOARD
8886 /* disable selection without redrawing it */
8887 clip_scroll_selection(9999);
8888#endif
8889
8890 /* blank out ScreenLines */
8891 for (i = 0; i < Rows; ++i)
8892 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02008893 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894 LineWraps[i] = FALSE;
8895 }
8896
8897 if (can_clear(T_CL))
8898 {
8899 out_str(T_CL); /* clear the display */
8900 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008901 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008902 }
8903 else
8904 {
8905 /* can't clear the screen, mark all chars with invalid attributes */
8906 for (i = 0; i < Rows; ++i)
8907 lineinvalid(LineOffset[i], (int)Columns);
8908 clear_cmdline = TRUE;
8909 }
8910
8911 screen_cleared = TRUE; /* can use contents of ScreenLines now */
8912
8913 win_rest_invalid(firstwin);
8914 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008915 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916 if (must_redraw == CLEAR) /* no need to clear again */
8917 must_redraw = NOT_VALID;
8918 compute_cmdrow();
8919 msg_row = cmdline_row; /* put cursor on last line for messages */
8920 msg_col = 0;
8921 screen_start(); /* don't know where cursor is now */
8922 msg_scrolled = 0; /* can't scroll back */
8923 msg_didany = FALSE;
8924 msg_didout = FALSE;
8925}
8926
8927/*
8928 * Clear one line in ScreenLines.
8929 */
8930 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02008931lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008932{
8933 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934 if (enc_utf8)
8935 (void)vim_memset(ScreenLinesUC + off, 0,
8936 (size_t)width * sizeof(u8char_T));
Bram Moolenaarcfce7172017-08-17 20:31:48 +02008937 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008938}
8939
8940/*
8941 * Mark one line in ScreenLines invalid by setting the attributes to an
8942 * invalid value.
8943 */
8944 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008945lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946{
8947 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
8948}
8949
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950/*
8951 * Copy part of a Screenline for vertically split window "wp".
8952 */
8953 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008954linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955{
8956 unsigned off_to = LineOffset[to] + wp->w_wincol;
8957 unsigned off_from = LineOffset[from] + wp->w_wincol;
8958
8959 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
8960 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961 if (enc_utf8)
8962 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008963 int i;
8964
Bram Moolenaar071d4272004-06-13 20:20:40 +00008965 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
8966 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008967 for (i = 0; i < p_mco; ++i)
8968 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
8969 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970 }
8971 if (enc_dbcs == DBCS_JPNU)
8972 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
8973 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008974 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
8975 wp->w_width * sizeof(sattr_T));
8976}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977
8978/*
8979 * Return TRUE if clearing with term string "p" would work.
8980 * It can't work when the string is empty or it won't set the right background.
Bram Moolenaar33796b32019-06-08 16:01:13 +02008981 * Don't clear to end-of-line when there are popups, it may cause flicker.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982 */
8983 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008984can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008985{
8986 return (*p != NUL && (t_colors <= 1
8987#ifdef FEAT_GUI
8988 || gui.in_use
8989#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008990#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008991 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02008992 || (!p_tgc && cterm_normal_bg_color == 0)
8993#else
8994 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008995#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02008996 || *T_UT != NUL)
8997#ifdef FEAT_TEXT_PROP
8998 && !(p == T_CE && popup_visible)
8999#endif
9000 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00009001}
9002
9003/*
9004 * Reset cursor position. Use whenever cursor was moved because of outputting
9005 * something directly to the screen (shell commands) or a terminal control
9006 * code.
9007 */
9008 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009009screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009010{
9011 screen_cur_row = screen_cur_col = 9999;
9012}
9013
9014/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009015 * Move the cursor to position "row","col" in the screen.
9016 * This tries to find the most efficient way to move, minimizing the number of
9017 * characters sent to the terminal.
9018 */
9019 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009020windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009021{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009022 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009023 int i;
9024 int plan;
9025 int cost;
9026 int wouldbe_col;
9027 int noinvcurs;
9028 char_u *bs;
9029 int goto_cost;
9030 int attr;
9031
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009032#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009033#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9034
9035#define PLAN_LE 1
9036#define PLAN_CR 2
9037#define PLAN_NL 3
9038#define PLAN_WRITE 4
9039 /* Can't use ScreenLines unless initialized */
9040 if (ScreenLines == NULL)
9041 return;
9042
9043 if (col != screen_cur_col || row != screen_cur_row)
9044 {
9045 /* Check for valid position. */
9046 if (row < 0) /* window without text lines? */
9047 row = 0;
9048 if (row >= screen_Rows)
9049 row = screen_Rows - 1;
9050 if (col >= screen_Columns)
9051 col = screen_Columns - 1;
9052
9053 /* check if no cursor movement is allowed in highlight mode */
9054 if (screen_attr && *T_MS == NUL)
9055 noinvcurs = HIGHL_COST;
9056 else
9057 noinvcurs = 0;
9058 goto_cost = GOTO_COST + noinvcurs;
9059
9060 /*
9061 * Plan how to do the positioning:
9062 * 1. Use CR to move it to column 0, same row.
9063 * 2. Use T_LE to move it a few columns to the left.
9064 * 3. Use NL to move a few lines down, column 0.
9065 * 4. Move a few columns to the right with T_ND or by writing chars.
9066 *
9067 * Don't do this if the cursor went beyond the last column, the cursor
9068 * position is unknown then (some terminals wrap, some don't )
9069 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009070 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009071 * characters to move the cursor to the right.
9072 */
9073 if (row >= screen_cur_row && screen_cur_col < Columns)
9074 {
9075 /*
9076 * If the cursor is in the same row, bigger col, we can use CR
9077 * or T_LE.
9078 */
9079 bs = NULL; /* init for GCC */
9080 attr = screen_attr;
9081 if (row == screen_cur_row && col < screen_cur_col)
9082 {
9083 /* "le" is preferred over "bc", because "bc" is obsolete */
9084 if (*T_LE)
9085 bs = T_LE; /* "cursor left" */
9086 else
9087 bs = T_BC; /* "backspace character (old) */
9088 if (*bs)
9089 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9090 else
9091 cost = 999;
9092 if (col + 1 < cost) /* using CR is less characters */
9093 {
9094 plan = PLAN_CR;
9095 wouldbe_col = 0;
9096 cost = 1; /* CR is just one character */
9097 }
9098 else
9099 {
9100 plan = PLAN_LE;
9101 wouldbe_col = col;
9102 }
9103 if (noinvcurs) /* will stop highlighting */
9104 {
9105 cost += noinvcurs;
9106 attr = 0;
9107 }
9108 }
9109
9110 /*
9111 * If the cursor is above where we want to be, we can use CR LF.
9112 */
9113 else if (row > screen_cur_row)
9114 {
9115 plan = PLAN_NL;
9116 wouldbe_col = 0;
9117 cost = (row - screen_cur_row) * 2; /* CR LF */
9118 if (noinvcurs) /* will stop highlighting */
9119 {
9120 cost += noinvcurs;
9121 attr = 0;
9122 }
9123 }
9124
9125 /*
9126 * If the cursor is in the same row, smaller col, just use write.
9127 */
9128 else
9129 {
9130 plan = PLAN_WRITE;
9131 wouldbe_col = screen_cur_col;
9132 cost = 0;
9133 }
9134
9135 /*
9136 * Check if any characters that need to be written have the
9137 * correct attributes. Also avoid UTF-8 characters.
9138 */
9139 i = col - wouldbe_col;
9140 if (i > 0)
9141 cost += i;
9142 if (cost < goto_cost && i > 0)
9143 {
9144 /*
9145 * Check if the attributes are correct without additionally
9146 * stopping highlighting.
9147 */
9148 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9149 while (i && *p++ == attr)
9150 --i;
9151 if (i != 0)
9152 {
9153 /*
9154 * Try if it works when highlighting is stopped here.
9155 */
9156 if (*--p == 0)
9157 {
9158 cost += noinvcurs;
9159 while (i && *p++ == 0)
9160 --i;
9161 }
9162 if (i != 0)
9163 cost = 999; /* different attributes, don't do it */
9164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009165 if (enc_utf8)
9166 {
9167 /* Don't use an UTF-8 char for positioning, it's slow. */
9168 for (i = wouldbe_col; i < col; ++i)
9169 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9170 {
9171 cost = 999;
9172 break;
9173 }
9174 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009175 }
9176
9177 /*
9178 * We can do it without term_windgoto()!
9179 */
9180 if (cost < goto_cost)
9181 {
9182 if (plan == PLAN_LE)
9183 {
9184 if (noinvcurs)
9185 screen_stop_highlight();
9186 while (screen_cur_col > col)
9187 {
9188 out_str(bs);
9189 --screen_cur_col;
9190 }
9191 }
9192 else if (plan == PLAN_CR)
9193 {
9194 if (noinvcurs)
9195 screen_stop_highlight();
9196 out_char('\r');
9197 screen_cur_col = 0;
9198 }
9199 else if (plan == PLAN_NL)
9200 {
9201 if (noinvcurs)
9202 screen_stop_highlight();
9203 while (screen_cur_row < row)
9204 {
9205 out_char('\n');
9206 ++screen_cur_row;
9207 }
9208 screen_cur_col = 0;
9209 }
9210
9211 i = col - screen_cur_col;
9212 if (i > 0)
9213 {
9214 /*
9215 * Use cursor-right if it's one character only. Avoids
9216 * removing a line of pixels from the last bold char, when
9217 * using the bold trick in the GUI.
9218 */
9219 if (T_ND[0] != NUL && T_ND[1] == NUL)
9220 {
9221 while (i-- > 0)
9222 out_char(*T_ND);
9223 }
9224 else
9225 {
9226 int off;
9227
9228 off = LineOffset[row] + screen_cur_col;
9229 while (i-- > 0)
9230 {
9231 if (ScreenAttrs[off] != screen_attr)
9232 screen_stop_highlight();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009233 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009234 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009235 if (enc_dbcs == DBCS_JPNU
9236 && ScreenLines[off] == 0x8e)
9237 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009238 ++off;
9239 }
9240 }
9241 }
9242 }
9243 }
9244 else
9245 cost = 999;
9246
9247 if (cost >= goto_cost)
9248 {
9249 if (noinvcurs)
9250 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009251 if (row == screen_cur_row && (col > screen_cur_col)
9252 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009253 term_cursor_right(col - screen_cur_col);
9254 else
9255 term_windgoto(row, col);
9256 }
9257 screen_cur_row = row;
9258 screen_cur_col = col;
9259 }
9260}
9261
9262/*
9263 * Set cursor to its position in the current window.
9264 */
9265 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009266setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009267{
Bram Moolenaar987723e2018-03-06 11:43:04 +01009268 setcursor_mayforce(FALSE);
9269}
9270
9271/*
9272 * Set cursor to its position in the current window.
9273 * When "force" is TRUE also when not redrawing.
9274 */
9275 void
9276setcursor_mayforce(int force)
9277{
9278 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009279 {
9280 validate_cursor();
9281 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009282 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009283#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009284 /* With 'rightleft' set and the cursor on a double-wide
9285 * character, position it on the leftmost column. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01009286 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol
9287 - ((has_mbyte
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009288 && (*mb_ptr2cells)(ml_get_cursor()) == 2
Bram Moolenaara12a1612019-01-24 16:39:02 +01009289 && vim_isprintc(gchar_cursor())) ? 2 : 1)) :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009290#endif
9291 curwin->w_wcol));
9292 }
9293}
9294
9295
9296/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009297 * Insert 'line_count' lines at 'row' in window 'wp'.
9298 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9299 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009300 * scrolling.
9301 * Returns FAIL if the lines are not inserted, OK for success.
9302 */
9303 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009304win_ins_lines(
9305 win_T *wp,
9306 int row,
9307 int line_count,
9308 int invalid,
9309 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009310{
9311 int did_delete;
9312 int nextrow;
9313 int lastrow;
9314 int retval;
9315
9316 if (invalid)
9317 wp->w_lines_valid = 0;
9318
9319 if (wp->w_height < 5)
9320 return FAIL;
9321
9322 if (line_count > wp->w_height - row)
9323 line_count = wp->w_height - row;
9324
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009325 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009326 if (retval != MAYBE)
9327 return retval;
9328
9329 /*
9330 * If there is a next window or a status line, we first try to delete the
9331 * lines at the bottom to avoid messing what is after the window.
Bram Moolenaarc363fe12019-08-04 18:13:46 +02009332 * If this fails and there are following windows, don't do anything to
9333 * avoid messing up those windows, better just redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009334 */
9335 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009336 if (wp->w_next != NULL || wp->w_status_height)
9337 {
9338 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009339 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009340 did_delete = TRUE;
9341 else if (wp->w_next)
9342 return FAIL;
9343 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009344 /*
9345 * if no lines deleted, blank the lines that will end up below the window
9346 */
9347 if (!did_delete)
9348 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009349 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009350 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02009351 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009352 lastrow = nextrow + line_count;
9353 if (lastrow > Rows)
9354 lastrow = Rows;
9355 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009356 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009357 ' ', ' ', 0);
9358 }
9359
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009360 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009361 == FAIL)
9362 {
Bram Moolenaarc363fe12019-08-04 18:13:46 +02009363 // deletion will have messed up other windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00009364 if (did_delete)
9365 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009366 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009367 win_rest_invalid(W_NEXT(wp));
9368 }
9369 return FAIL;
9370 }
9371
9372 return OK;
9373}
9374
9375/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009376 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009377 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9378 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9379 * scrolling
9380 * Return OK for success, FAIL if the lines are not deleted.
9381 */
9382 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009383win_del_lines(
9384 win_T *wp,
9385 int row,
9386 int line_count,
9387 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009388 int mayclear,
9389 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009390{
9391 int retval;
9392
9393 if (invalid)
9394 wp->w_lines_valid = 0;
9395
9396 if (line_count > wp->w_height - row)
9397 line_count = wp->w_height - row;
9398
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009399 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009400 if (retval != MAYBE)
9401 return retval;
9402
9403 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009404 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009405 return FAIL;
9406
Bram Moolenaar071d4272004-06-13 20:20:40 +00009407 /*
9408 * If there are windows or status lines below, try to put them at the
9409 * correct place. If we can't do that, they have to be redrawn.
9410 */
9411 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9412 {
9413 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009414 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009415 {
9416 wp->w_redr_status = TRUE;
9417 win_rest_invalid(wp->w_next);
9418 }
9419 }
9420 /*
9421 * If this is the last window and there is no status line, redraw the
9422 * command line later.
9423 */
9424 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009425 redraw_cmdline = TRUE;
9426 return OK;
9427}
9428
9429/*
9430 * Common code for win_ins_lines() and win_del_lines().
9431 * Returns OK or FAIL when the work has been done.
9432 * Returns MAYBE when not finished yet.
9433 */
9434 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009435win_do_lines(
9436 win_T *wp,
9437 int row,
9438 int line_count,
9439 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009440 int del,
9441 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009442{
9443 int retval;
9444
9445 if (!redrawing() || line_count <= 0)
9446 return FAIL;
9447
Bram Moolenaar33796b32019-06-08 16:01:13 +02009448 // When inserting lines would result in loss of command output, just redraw
9449 // the lines.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009450 if (no_win_do_lines_ins && !del)
9451 return FAIL;
9452
Bram Moolenaar33796b32019-06-08 16:01:13 +02009453 // only a few lines left: redraw is faster
Bram Moolenaar4033c552017-09-16 20:54:51 +02009454 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009455 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009456 if (!no_win_do_lines_ins)
Bram Moolenaar33796b32019-06-08 16:01:13 +02009457 screenclear(); // will set wp->w_lines_valid to 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00009458 return FAIL;
9459 }
9460
Bram Moolenaar33796b32019-06-08 16:01:13 +02009461#ifdef FEAT_TEXT_PROP
Bram Moolenaar4c063a02019-06-10 21:24:12 +02009462 // this doesn't work when there are popups visible
Bram Moolenaar33796b32019-06-08 16:01:13 +02009463 if (popup_visible)
9464 return FAIL;
9465#endif
9466
9467 // Delete all remaining lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00009468 if (row + line_count >= wp->w_height)
9469 {
9470 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009471 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009472 ' ', ' ', 0);
9473 return OK;
9474 }
9475
9476 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009477 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009478 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009479 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009480 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009481 if (!no_win_do_lines_ins)
9482 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483
9484 /*
9485 * If the terminal can set a scroll region, use that.
9486 * Always do this in a vertically split window. This will redraw from
9487 * ScreenLines[] when t_CV isn't defined. That's faster than using
9488 * win_line().
9489 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009490 * a character in the lower right corner of the scroll region may cause a
9491 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492 */
Bram Moolenaar02631462017-09-22 15:20:32 +02009493 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009495 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009496 scroll_region_set(wp, row);
9497 if (del)
9498 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009499 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009500 else
9501 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009502 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009504 scroll_region_reset();
9505 return retval;
9506 }
9507
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9509 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009510
9511 return MAYBE;
9512}
9513
9514/*
9515 * window 'wp' and everything after it is messed up, mark it for redraw
9516 */
9517 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009518win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009519{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009520 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009521 {
9522 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009523 wp->w_redr_status = TRUE;
9524 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009525 }
9526 redraw_cmdline = TRUE;
9527}
9528
9529/*
9530 * The rest of the routines in this file perform screen manipulations. The
9531 * given operation is performed physically on the screen. The corresponding
9532 * change is also made to the internal screen image. In this way, the editor
9533 * anticipates the effect of editing changes on the appearance of the screen.
9534 * That way, when we call screenupdate a complete redraw isn't usually
9535 * necessary. Another advantage is that we can keep adding code to anticipate
9536 * screen changes, and in the meantime, everything still works.
9537 */
9538
9539/*
9540 * types for inserting or deleting lines
9541 */
9542#define USE_T_CAL 1
9543#define USE_T_CDL 2
9544#define USE_T_AL 3
9545#define USE_T_CE 4
9546#define USE_T_DL 5
9547#define USE_T_SR 6
9548#define USE_NL 7
9549#define USE_T_CD 8
9550#define USE_REDRAW 9
9551
9552/*
9553 * insert lines on the screen and update ScreenLines[]
9554 * 'end' is the line after the scrolled part. Normally it is Rows.
9555 * When scrolling region used 'off' is the offset from the top for the region.
9556 * 'row' and 'end' are relative to the start of the region.
9557 *
9558 * return FAIL for failure, OK for success.
9559 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009560 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009561screen_ins_lines(
9562 int off,
9563 int row,
9564 int line_count,
9565 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009566 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01009567 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009568{
9569 int i;
9570 int j;
9571 unsigned temp;
9572 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009573 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009574 int type;
9575 int result_empty;
9576 int can_ce = can_clear(T_CE);
9577
9578 /*
9579 * FAIL if
9580 * - there is no valid screen
9581 * - the screen has to be redrawn completely
9582 * - the line count is less than one
9583 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009584 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar33796b32019-06-08 16:01:13 +02009585 * - there is a popup window
Bram Moolenaar071d4272004-06-13 20:20:40 +00009586 */
Bram Moolenaar33796b32019-06-08 16:01:13 +02009587 if (!screen_valid(TRUE)
9588 || line_count <= 0 || line_count > p_ttyscroll
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009589#ifdef FEAT_CLIPBOARD
9590 || (clip_star.state != SELECT_CLEARED
9591 && redrawing_for_callback > 0)
9592#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02009593#ifdef FEAT_TEXT_PROP
9594 || popup_visible
9595#endif
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009596 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009597 return FAIL;
9598
9599 /*
9600 * There are seven ways to insert lines:
9601 * 0. When in a vertically split window and t_CV isn't set, redraw the
9602 * characters from ScreenLines[].
9603 * 1. Use T_CD (clear to end of display) if it exists and the result of
9604 * the insert is just empty lines
9605 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9606 * present or line_count > 1. It looks better if we do all the inserts
9607 * at once.
9608 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9609 * insert is just empty lines and T_CE is not present or line_count >
9610 * 1.
9611 * 4. Use T_AL (insert line) if it exists.
9612 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9613 * just empty lines.
9614 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9615 * just empty lines.
9616 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9617 * the 'da' flag is not set or we have clear line capability.
9618 * 8. redraw the characters from ScreenLines[].
9619 *
9620 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9621 * the scrollbar for the window. It does have insert line, use that if it
9622 * exists.
9623 */
9624 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009625 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9626 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009627 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009628 type = USE_T_CD;
9629 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9630 type = USE_T_CAL;
9631 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9632 type = USE_T_CDL;
9633 else if (*T_AL != NUL)
9634 type = USE_T_AL;
9635 else if (can_ce && result_empty)
9636 type = USE_T_CE;
9637 else if (*T_DL != NUL && result_empty)
9638 type = USE_T_DL;
9639 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9640 type = USE_T_SR;
9641 else
9642 return FAIL;
9643
9644 /*
9645 * For clearing the lines screen_del_lines() is used. This will also take
9646 * care of t_db if necessary.
9647 */
9648 if (type == USE_T_CD || type == USE_T_CDL ||
9649 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009650 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009651
9652 /*
9653 * If text is retained below the screen, first clear or delete as many
9654 * lines at the bottom of the window as are about to be inserted so that
9655 * the deleted lines won't later surface during a screen_del_lines.
9656 */
9657 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009658 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009659
9660#ifdef FEAT_CLIPBOARD
9661 /* Remove a modeless selection when inserting lines halfway the screen
9662 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009663 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009664 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009665 else
9666 clip_scroll_selection(-line_count);
9667#endif
9668
Bram Moolenaar071d4272004-06-13 20:20:40 +00009669#ifdef FEAT_GUI
9670 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9671 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009672 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009673#endif
9674
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009675 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
9676 cursor_col = wp->w_wincol;
9677
Bram Moolenaar071d4272004-06-13 20:20:40 +00009678 if (*T_CCS != NUL) /* cursor relative to region */
9679 cursor_row = row;
9680 else
9681 cursor_row = row + off;
9682
9683 /*
9684 * Shift LineOffset[] line_count down to reflect the inserted lines.
9685 * Clear the inserted lines in ScreenLines[].
9686 */
9687 row += off;
9688 end += off;
9689 for (i = 0; i < line_count; ++i)
9690 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009691 if (wp != NULL && wp->w_width != Columns)
9692 {
9693 /* need to copy part of a line */
9694 j = end - 1 - i;
9695 while ((j -= line_count) >= row)
9696 linecopy(j + line_count, j, wp);
9697 j += line_count;
9698 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009699 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
9700 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701 else
9702 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9703 LineWraps[j] = FALSE;
9704 }
9705 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009706 {
9707 j = end - 1 - i;
9708 temp = LineOffset[j];
9709 while ((j -= line_count) >= row)
9710 {
9711 LineOffset[j + line_count] = LineOffset[j];
9712 LineWraps[j + line_count] = LineWraps[j];
9713 }
9714 LineOffset[j + line_count] = temp;
9715 LineWraps[j + line_count] = FALSE;
9716 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009717 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009718 else
9719 lineinvalid(temp, (int)Columns);
9720 }
9721 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009722
9723 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009724 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009725 if (clear_attr != 0)
9726 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009727
Bram Moolenaar071d4272004-06-13 20:20:40 +00009728 /* redraw the characters */
9729 if (type == USE_REDRAW)
9730 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02009731 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009732 {
9733 term_append_lines(line_count);
9734 screen_start(); /* don't know where cursor is now */
9735 }
9736 else
9737 {
9738 for (i = 0; i < line_count; i++)
9739 {
9740 if (type == USE_T_AL)
9741 {
9742 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009743 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009744 out_str(T_AL);
9745 }
9746 else /* type == USE_T_SR */
9747 out_str(T_SR);
9748 screen_start(); /* don't know where cursor is now */
9749 }
9750 }
9751
9752 /*
9753 * With scroll-reverse and 'da' flag set we need to clear the lines that
9754 * have been scrolled down into the region.
9755 */
9756 if (type == USE_T_SR && *T_DA)
9757 {
9758 for (i = 0; i < line_count; ++i)
9759 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009760 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009761 out_str(T_CE);
9762 screen_start(); /* don't know where cursor is now */
9763 }
9764 }
9765
9766#ifdef FEAT_GUI
9767 gui_can_update_cursor();
9768 if (gui.in_use)
9769 out_flush(); /* always flush after a scroll */
9770#endif
9771 return OK;
9772}
9773
9774/*
Bram Moolenaar107abd22016-08-12 14:08:25 +02009775 * Delete lines on the screen and update ScreenLines[].
9776 * "end" is the line after the scrolled part. Normally it is Rows.
9777 * When scrolling region used "off" is the offset from the top for the region.
9778 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009779 *
9780 * Return OK for success, FAIL if the lines are not deleted.
9781 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009782 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009783screen_del_lines(
9784 int off,
9785 int row,
9786 int line_count,
9787 int end,
9788 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009789 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +01009790 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009791{
9792 int j;
9793 int i;
9794 unsigned temp;
9795 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009796 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009797 int cursor_end;
9798 int result_empty; /* result is empty until end of region */
9799 int can_delete; /* deleting line codes can be used */
9800 int type;
9801
9802 /*
9803 * FAIL if
9804 * - there is no valid screen
9805 * - the screen has to be redrawn completely
9806 * - the line count is less than one
9807 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009808 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009809 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009810 if (!screen_valid(TRUE) || line_count <= 0
9811 || (!force && line_count > p_ttyscroll)
9812#ifdef FEAT_CLIPBOARD
9813 || (clip_star.state != SELECT_CLEARED
9814 && redrawing_for_callback > 0)
9815#endif
9816 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009817 return FAIL;
9818
9819 /*
9820 * Check if the rest of the current region will become empty.
9821 */
9822 result_empty = row + line_count >= end;
9823
9824 /*
9825 * We can delete lines only when 'db' flag not set or when 'ce' option
9826 * available.
9827 */
9828 can_delete = (*T_DB == NUL || can_clear(T_CE));
9829
9830 /*
9831 * There are six ways to delete lines:
9832 * 0. When in a vertically split window and t_CV isn't set, redraw the
9833 * characters from ScreenLines[].
9834 * 1. Use T_CD if it exists and the result is empty.
9835 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9836 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9837 * none of the other ways work.
9838 * 4. Use T_CE (erase line) if the result is empty.
9839 * 5. Use T_DL (delete line) if it exists.
9840 * 6. redraw the characters from ScreenLines[].
9841 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009842 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9843 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009844 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009845 type = USE_T_CD;
9846#if defined(__BEOS__) && defined(BEOS_DR8)
9847 /*
9848 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9849 * its internal termcap... this works okay for tests which test *T_DB !=
9850 * NUL. It has the disadvantage that the user cannot use any :set t_*
9851 * command to get T_DB (back) to empty_option, only :set term=... will do
9852 * the trick...
9853 * Anyway, this hack will hopefully go away with the next OS release.
9854 * (Olaf Seibert)
9855 */
9856 else if (row == 0 && T_DB == empty_option
9857 && (line_count == 1 || *T_CDL == NUL))
9858#else
9859 else if (row == 0 && (
9860#ifndef AMIGA
9861 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9862 * up, so use delete-line command */
9863 line_count == 1 ||
9864#endif
9865 *T_CDL == NUL))
9866#endif
9867 type = USE_NL;
9868 else if (*T_CDL != NUL && line_count > 1 && can_delete)
9869 type = USE_T_CDL;
9870 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +02009871 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009872 type = USE_T_CE;
9873 else if (*T_DL != NUL && can_delete)
9874 type = USE_T_DL;
9875 else if (*T_CDL != NUL && can_delete)
9876 type = USE_T_CDL;
9877 else
9878 return FAIL;
9879
9880#ifdef FEAT_CLIPBOARD
9881 /* Remove a modeless selection when deleting lines halfway the screen or
9882 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009883 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009884 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009885 else
9886 clip_scroll_selection(line_count);
9887#endif
9888
Bram Moolenaar071d4272004-06-13 20:20:40 +00009889#ifdef FEAT_GUI
9890 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9891 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009892 gui_dont_update_cursor(gui.cursor_row >= row + off
9893 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009894#endif
9895
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009896 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
9897 cursor_col = wp->w_wincol;
9898
Bram Moolenaar071d4272004-06-13 20:20:40 +00009899 if (*T_CCS != NUL) /* cursor relative to region */
9900 {
9901 cursor_row = row;
9902 cursor_end = end;
9903 }
9904 else
9905 {
9906 cursor_row = row + off;
9907 cursor_end = end + off;
9908 }
9909
9910 /*
9911 * Now shift LineOffset[] line_count up to reflect the deleted lines.
9912 * Clear the inserted lines in ScreenLines[].
9913 */
9914 row += off;
9915 end += off;
9916 for (i = 0; i < line_count; ++i)
9917 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009918 if (wp != NULL && wp->w_width != Columns)
9919 {
9920 /* need to copy part of a line */
9921 j = row + i;
9922 while ((j += line_count) <= end - 1)
9923 linecopy(j - line_count, j, wp);
9924 j -= line_count;
9925 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009926 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
9927 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009928 else
9929 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9930 LineWraps[j] = FALSE;
9931 }
9932 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009933 {
9934 /* whole width, moving the line pointers is faster */
9935 j = row + i;
9936 temp = LineOffset[j];
9937 while ((j += line_count) <= end - 1)
9938 {
9939 LineOffset[j - line_count] = LineOffset[j];
9940 LineWraps[j - line_count] = LineWraps[j];
9941 }
9942 LineOffset[j - line_count] = temp;
9943 LineWraps[j - line_count] = FALSE;
9944 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009945 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009946 else
9947 lineinvalid(temp, (int)Columns);
9948 }
9949 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009950
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009951 if (screen_attr != clear_attr)
9952 screen_stop_highlight();
9953 if (clear_attr != 0)
9954 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009955
Bram Moolenaar071d4272004-06-13 20:20:40 +00009956 /* redraw the characters */
9957 if (type == USE_REDRAW)
9958 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02009959 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009960 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009961 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009962 out_str(T_CD);
9963 screen_start(); /* don't know where cursor is now */
9964 }
9965 else if (type == USE_T_CDL)
9966 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009967 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009968 term_delete_lines(line_count);
9969 screen_start(); /* don't know where cursor is now */
9970 }
9971 /*
9972 * Deleting lines at top of the screen or scroll region: Just scroll
9973 * the whole screen (scroll region) up by outputting newlines on the
9974 * last line.
9975 */
9976 else if (type == USE_NL)
9977 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009978 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009979 for (i = line_count; --i >= 0; )
9980 out_char('\n'); /* cursor will remain on same line */
9981 }
9982 else
9983 {
9984 for (i = line_count; --i >= 0; )
9985 {
9986 if (type == USE_T_DL)
9987 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009988 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009989 out_str(T_DL); /* delete a line */
9990 }
9991 else /* type == USE_T_CE */
9992 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009993 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009994 out_str(T_CE); /* erase a line */
9995 }
9996 screen_start(); /* don't know where cursor is now */
9997 }
9998 }
9999
10000 /*
10001 * If the 'db' flag is set, we need to clear the lines that have been
10002 * scrolled up at the bottom of the region.
10003 */
10004 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10005 {
10006 for (i = line_count; i > 0; --i)
10007 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010008 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010009 out_str(T_CE); /* erase a line */
10010 screen_start(); /* don't know where cursor is now */
10011 }
10012 }
10013
10014#ifdef FEAT_GUI
10015 gui_can_update_cursor();
10016 if (gui.in_use)
10017 out_flush(); /* always flush after a scroll */
10018#endif
10019
10020 return OK;
10021}
10022
10023/*
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010024 * Return TRUE when postponing displaying the mode message: when not redrawing
10025 * or inside a mapping.
10026 */
10027 int
10028skip_showmode()
10029{
10030 // Call char_avail() only when we are going to show something, because it
10031 // takes a bit of time. redrawing() may also call char_avail_avail().
10032 if (global_busy
10033 || msg_silent != 0
10034 || !redrawing()
10035 || (char_avail() && !KeyTyped))
10036 {
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010037 redraw_mode = TRUE; // show mode later
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010038 return TRUE;
10039 }
10040 return FALSE;
10041}
10042
10043/*
Bram Moolenaar81226e02018-02-20 21:44:45 +010010044 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010045 *
10046 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10047 * If clear_cmdline is FALSE there may be a message there that needs to be
10048 * cleared only if a mode is shown.
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010049 * If redraw_mode is TRUE show or clear the mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010050 * Return the length of the message (0 if no message).
10051 */
10052 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010053showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010054{
10055 int need_clear;
10056 int length = 0;
10057 int do_mode;
10058 int attr;
10059 int nwr_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010060 int sub_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010061
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010062 do_mode = ((p_smd && msg_silent == 0)
10063 && ((State & INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +020010064 || restart_edit != NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010065 || VIsual_active));
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010066 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010067 {
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010068 if (skip_showmode())
10069 return 0; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +000010070
10071 nwr_save = need_wait_return;
10072
10073 /* wait a bit before overwriting an important message */
10074 check_for_delay(FALSE);
10075
10076 /* if the cmdline is more than one line high, erase top lines */
10077 need_clear = clear_cmdline;
10078 if (clear_cmdline && cmdline_row < Rows - 1)
10079 msg_clr_cmdline(); /* will reset clear_cmdline */
10080
10081 /* Position on the last line in the window, column 0 */
10082 msg_pos_mode();
10083 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010084 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010085 if (do_mode)
10086 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010087 msg_puts_attr("--", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010088#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010089 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010090# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010091 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010092# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010093 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010094# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010095 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010096# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar32526b32019-01-19 17:43:09 +010010097 msg_puts_attr(" IM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010098# else
Bram Moolenaar32526b32019-01-19 17:43:09 +010010099 msg_puts_attr(" XIM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010100# endif
10101#endif
10102#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10103 if (gui.in_use)
10104 {
10105 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010106 {
10107 /* HANGUL */
10108 if (enc_utf8)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010109 msg_puts_attr(" \355\225\234\352\270\200", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010110 else
Bram Moolenaar32526b32019-01-19 17:43:09 +010010111 msg_puts_attr(" \307\321\261\333", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010112 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010113 }
10114#endif
Bram Moolenaarea389e92014-05-28 21:40:52 +020010115 /* CTRL-X in Insert mode */
10116 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010117 {
10118 /* These messages can get long, avoid a wrap in a narrow
10119 * window. Prefer showing edit_submode_extra. */
10120 length = (Rows - msg_row) * Columns - 3;
10121 if (edit_submode_extra != NULL)
10122 length -= vim_strsize(edit_submode_extra);
10123 if (length > 0)
10124 {
10125 if (edit_submode_pre != NULL)
10126 length -= vim_strsize(edit_submode_pre);
10127 if (length - vim_strsize(edit_submode) > 0)
10128 {
10129 if (edit_submode_pre != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010130 msg_puts_attr((char *)edit_submode_pre, attr);
10131 msg_puts_attr((char *)edit_submode, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010132 }
10133 if (edit_submode_extra != NULL)
10134 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010135 msg_puts_attr(" ", attr); /* add a space in between */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010136 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010137 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010138 else
10139 sub_attr = attr;
Bram Moolenaar32526b32019-01-19 17:43:09 +010010140 msg_puts_attr((char *)edit_submode_extra, sub_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010141 }
10142 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010143 }
10144 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010145 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010146 if (State & VREPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010147 msg_puts_attr(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010148 else if (State & REPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010149 msg_puts_attr(_(" REPLACE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010150 else if (State & INSERT)
10151 {
10152#ifdef FEAT_RIGHTLEFT
10153 if (p_ri)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010154 msg_puts_attr(_(" REVERSE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010155#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +010010156 msg_puts_attr(_(" INSERT"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010157 }
Bram Moolenaar942b4542018-06-17 16:23:34 +020010158 else if (restart_edit == 'I' || restart_edit == 'A')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010159 msg_puts_attr(_(" (insert)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010160 else if (restart_edit == 'R')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010161 msg_puts_attr(_(" (replace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010162 else if (restart_edit == 'V')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010163 msg_puts_attr(_(" (vreplace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010164#ifdef FEAT_RIGHTLEFT
10165 if (p_hkmap)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010166 msg_puts_attr(_(" Hebrew"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010167#endif
10168#ifdef FEAT_KEYMAP
10169 if (State & LANGMAP)
10170 {
10171# ifdef FEAT_ARABIC
10172 if (curwin->w_p_arab)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010173 msg_puts_attr(_(" Arabic"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010174 else
10175# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010176 if (get_keymap_str(curwin, (char_u *)" (%s)",
10177 NameBuff, MAXPATHL))
Bram Moolenaar32526b32019-01-19 17:43:09 +010010178 msg_puts_attr((char *)NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010179 }
10180#endif
10181 if ((State & INSERT) && p_paste)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010182 msg_puts_attr(_(" (paste)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010183
Bram Moolenaar071d4272004-06-13 20:20:40 +000010184 if (VIsual_active)
10185 {
10186 char *p;
10187
10188 /* Don't concatenate separate words to avoid translation
10189 * problems. */
10190 switch ((VIsual_select ? 4 : 0)
10191 + (VIsual_mode == Ctrl_V) * 2
10192 + (VIsual_mode == 'V'))
10193 {
10194 case 0: p = N_(" VISUAL"); break;
10195 case 1: p = N_(" VISUAL LINE"); break;
10196 case 2: p = N_(" VISUAL BLOCK"); break;
10197 case 4: p = N_(" SELECT"); break;
10198 case 5: p = N_(" SELECT LINE"); break;
10199 default: p = N_(" SELECT BLOCK"); break;
10200 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010201 msg_puts_attr(_(p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010202 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010203 msg_puts_attr(" --", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010204 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010205
Bram Moolenaar071d4272004-06-13 20:20:40 +000010206 need_clear = TRUE;
10207 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010208 if (reg_recording != 0
Bram Moolenaare2c453d2019-08-21 14:37:09 +020010209 && edit_submode == NULL) // otherwise it gets too long
Bram Moolenaar071d4272004-06-13 20:20:40 +000010210 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010211 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010212 need_clear = TRUE;
10213 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010214
10215 mode_displayed = TRUE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010216 if (need_clear || clear_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010217 msg_clr_eos();
10218 msg_didout = FALSE; /* overwrite this message */
10219 length = msg_col;
10220 msg_col = 0;
10221 need_wait_return = nwr_save; /* never ask for hit-return for this */
10222 }
10223 else if (clear_cmdline && msg_silent == 0)
10224 /* Clear the whole command line. Will reset "clear_cmdline". */
10225 msg_clr_cmdline();
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010226 else if (redraw_mode)
10227 {
10228 msg_pos_mode();
10229 msg_clr_eos();
10230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010231
10232#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010233 /* In Visual mode the size of the selected area must be redrawn. */
10234 if (VIsual_active)
10235 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010236
10237 /* If the last window has no status line, the ruler is after the mode
10238 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010239 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +020010240 win_redr_ruler(lastwin, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010241#endif
10242 redraw_cmdline = FALSE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010243 redraw_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010244 clear_cmdline = FALSE;
10245
10246 return length;
10247}
10248
10249/*
10250 * Position for a mode message.
10251 */
10252 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010253msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010254{
10255 msg_col = 0;
10256 msg_row = Rows - 1;
10257}
10258
10259/*
10260 * Delete mode message. Used when ESC is typed which is expected to end
10261 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010262 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010263 */
10264 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010265unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010266{
10267 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010268 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010269 */
10270 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10271 redraw_cmdline = TRUE; /* delete mode later */
10272 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010273 clearmode();
10274}
10275
10276/*
10277 * Clear the mode message.
10278 */
10279 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010280clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010281{
Bram Moolenaar2abad542018-05-19 14:43:45 +020010282 int save_msg_row = msg_row;
10283 int save_msg_col = msg_col;
10284
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010285 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010286 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010287 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010288 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +020010289
10290 msg_col = save_msg_col;
10291 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010292}
10293
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010294 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010295recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010296{
Bram Moolenaar32526b32019-01-19 17:43:09 +010010297 msg_puts_attr(_("recording"), attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010298 if (!shortmess(SHM_RECORDING))
10299 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010300 char s[4];
10301
10302 sprintf(s, " @%c", reg_recording);
10303 msg_puts_attr(s, attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010304 }
10305}
10306
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010307/*
10308 * Draw the tab pages line at the top of the Vim window.
10309 */
Bram Moolenaare12bab32019-01-08 22:02:56 +010010310 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010311draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010312{
10313 int tabcount = 0;
10314 tabpage_T *tp;
10315 int tabwidth;
10316 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010317 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010318 int attr;
10319 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010320 win_T *cwp;
10321 int wincount;
10322 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010323 int c;
10324 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010325 int attr_sel = HL_ATTR(HLF_TPS);
10326 int attr_nosel = HL_ATTR(HLF_TP);
10327 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010328 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010329 int room;
10330 int use_sep_chars = (t_colors < 8
10331#ifdef FEAT_GUI
10332 && !gui.in_use
10333#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010334#ifdef FEAT_TERMGUICOLORS
10335 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010336#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010337 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010338
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010339 if (ScreenLines == NULL)
10340 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010341 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010342
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010343#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010344 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010345 if (gui_use_tabline())
10346 {
10347 gui_update_tabline();
10348 return;
10349 }
10350#endif
10351
10352 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010353 return;
10354
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010355#if defined(FEAT_STL_OPT)
Bram Moolenaarca57ab52019-04-13 14:53:16 +020010356 clear_TabPageIdxs();
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010357
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010358 /* Use the 'tabline' option if it's set. */
10359 if (*p_tal != NUL)
10360 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010361 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010362
10363 /* Check for an error. If there is one we would loop in redrawing the
10364 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010365 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010366 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010367 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010368 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010369 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010370 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010371 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010372 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010373#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010374 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010375 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010376 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010377
Bram Moolenaar238a5642006-02-21 22:12:05 +000010378 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10379 if (tabwidth < 6)
10380 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010381
Bram Moolenaar238a5642006-02-21 22:12:05 +000010382 attr = attr_nosel;
10383 tabcount = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010384 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10385 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010386 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010387 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010388
Bram Moolenaar238a5642006-02-21 22:12:05 +000010389 if (tp->tp_topframe == topframe)
10390 attr = attr_sel;
10391 if (use_sep_chars && col > 0)
10392 screen_putchar('|', 0, col++, attr);
10393
10394 if (tp->tp_topframe != topframe)
10395 attr = attr_nosel;
10396
10397 screen_putchar(' ', 0, col++, attr);
10398
10399 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010400 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010401 cwp = curwin;
10402 wp = firstwin;
10403 }
10404 else
10405 {
10406 cwp = tp->tp_curwin;
10407 wp = tp->tp_firstwin;
10408 }
10409
10410 modified = FALSE;
10411 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10412 if (bufIsChanged(wp->w_buffer))
10413 modified = TRUE;
10414 if (modified || wincount > 1)
10415 {
10416 if (wincount > 1)
10417 {
10418 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010419 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010420 if (col + len >= Columns - 3)
10421 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010422 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010423#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010424 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010425#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010426 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010427#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010428 );
10429 col += len;
10430 }
10431 if (modified)
10432 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10433 screen_putchar(' ', 0, col++, attr);
10434 }
10435
10436 room = scol - col + tabwidth - 1;
10437 if (room > 0)
10438 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010439 /* Get buffer name in NameBuff[] */
10440 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010441 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010442 len = vim_strsize(NameBuff);
10443 p = NameBuff;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010444 if (has_mbyte)
10445 while (len > room)
10446 {
10447 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010448 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010449 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010010450 else if (len > room)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010451 {
10452 p += len - room;
10453 len = room;
10454 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010455 if (len > Columns - col - 1)
10456 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010457
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010458 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010459 col += len;
10460 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010461 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010462
10463 /* Store the tab page number in TabPageIdxs[], so that
10464 * jump_to_mouse() knows where each one is. */
10465 ++tabcount;
10466 while (scol < col)
10467 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010468 }
10469
Bram Moolenaar238a5642006-02-21 22:12:05 +000010470 if (use_sep_chars)
10471 c = '_';
10472 else
10473 c = ' ';
10474 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010475
10476 /* Put an "X" for closing the current tab if there are several. */
10477 if (first_tabpage->tp_next != NULL)
10478 {
10479 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10480 TabPageIdxs[Columns - 1] = -999;
10481 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010482 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010483
10484 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10485 * set. */
10486 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010487}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010488
10489/*
10490 * Get buffer name for "buf" into NameBuff[].
10491 * Takes care of special buffer names and translates special characters.
10492 */
10493 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010494get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010495{
10496 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010497 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010498 else
10499 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10500 trans_characters(NameBuff, MAXPATHL);
10501}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010502
Bram Moolenaar071d4272004-06-13 20:20:40 +000010503/*
10504 * Get the character to use in a status line. Get its attributes in "*attr".
10505 */
10506 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010507fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010508{
10509 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010510
10511#ifdef FEAT_TERMINAL
10512 if (bt_terminal(wp->w_buffer))
10513 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010514 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010515 {
10516 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010517 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010518 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010519 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010520 {
10521 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010522 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010523 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010524 }
10525 else
10526#endif
10527 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010528 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010529 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010530 fill = fill_stl;
10531 }
10532 else
10533 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010534 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010535 fill = fill_stlnc;
10536 }
10537 /* Use fill when there is highlighting, and highlighting of current
10538 * window differs, or the fillchars differ, or this is not the
10539 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010540 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010541 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010542 || (fill_stl != fill_stlnc)))
10543 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010544 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010545 return '^';
10546 return '=';
10547}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010548
Bram Moolenaar071d4272004-06-13 20:20:40 +000010549/*
10550 * Get the character to use in a separator between vertically split windows.
10551 * Get its attributes in "*attr".
10552 */
10553 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010554fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010555{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010556 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010557 if (*attr == 0 && fill_vert == ' ')
10558 return '|';
10559 else
10560 return fill_vert;
10561}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010562
10563/*
10564 * Return TRUE if redrawing should currently be done.
10565 */
10566 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010567redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010568{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010569#ifdef FEAT_EVAL
10570 if (disable_redraw_for_testing)
10571 return 0;
10572 else
10573#endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +020010574 return ((!RedrawingDisabled
10575#ifdef FEAT_EVAL
10576 || ignore_redraw_flag_for_testing
10577#endif
10578 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010579}
10580
10581/*
10582 * Return TRUE if printing messages should currently be done.
10583 */
10584 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010585messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010586{
10587 return (!(p_lz && char_avail() && !KeyTyped));
10588}
10589
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010590#ifdef FEAT_MENU
10591/*
10592 * Draw the window toolbar.
10593 */
10594 static void
10595redraw_win_toolbar(win_T *wp)
10596{
10597 vimmenu_T *menu;
10598 int item_idx = 0;
10599 int item_count = 0;
10600 int col = 0;
10601 int next_col;
10602 int off = (int)(current_ScreenLine - ScreenLines);
10603 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
10604 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
10605
10606 vim_free(wp->w_winbar_items);
10607 for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
10608 ++item_count;
Bram Moolenaarc799fe22019-05-28 23:08:19 +020010609 wp->w_winbar_items = ALLOC_CLEAR_MULT(winbar_item_T, item_count + 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010610
10611 /* TODO: use fewer spaces if there is not enough room */
10612 for (menu = wp->w_winbar->children;
Bram Moolenaar02631462017-09-22 15:20:32 +020010613 menu != NULL && col < wp->w_width; menu = menu->next)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010614 {
10615 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010616 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010617 break;
10618 if (col > 1)
10619 {
10620 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010621 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010622 break;
10623 }
10624
10625 wp->w_winbar_items[item_idx].wb_startcol = col;
10626 space_to_screenline(off + col, button_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010627 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010628 break;
10629
10630 next_col = text_to_screenline(wp, menu->name, col);
10631 while (col < next_col)
10632 {
10633 ScreenAttrs[off + col] = button_attr;
10634 ++col;
10635 }
10636 wp->w_winbar_items[item_idx].wb_endcol = col;
10637 wp->w_winbar_items[item_idx].wb_menu = menu;
10638 ++item_idx;
10639
Bram Moolenaar02631462017-09-22 15:20:32 +020010640 if (col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010641 break;
10642 space_to_screenline(off + col, button_attr);
10643 ++col;
10644 }
Bram Moolenaar02631462017-09-22 15:20:32 +020010645 while (col < wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010646 {
10647 space_to_screenline(off + col, fill_attr);
10648 ++col;
10649 }
10650 wp->w_winbar_items[item_idx].wb_menu = NULL; /* end marker */
10651
Bram Moolenaar02631462017-09-22 15:20:32 +020010652 screen_line(wp->w_winrow, wp->w_wincol, (int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +020010653 (int)wp->w_width, 0);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010654}
10655#endif
Bram Moolenaar491ac282018-06-17 14:47:55 +020010656
Bram Moolenaar071d4272004-06-13 20:20:40 +000010657/*
10658 * Show current status info in ruler and various other places
10659 * If always is FALSE, only show ruler if position has changed.
10660 */
10661 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010662showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010663{
10664 if (!always && !redrawing())
10665 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010666 if (pum_visible())
10667 {
10668 /* Don't redraw right now, do it later. */
10669 curwin->w_redr_status = TRUE;
10670 return;
10671 }
Bram Moolenaar4033c552017-09-16 20:54:51 +020010672#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010673 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar362f3562009-11-03 16:20:34 +000010674 redraw_custom_statusline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010675 else
10676#endif
10677#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +020010678 win_redr_ruler(curwin, always, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010679#endif
10680
10681#ifdef FEAT_TITLE
10682 if (need_maketitle
10683# ifdef FEAT_STL_OPT
10684 || (p_icon && (stl_syntax & STL_IN_ICON))
10685 || (p_title && (stl_syntax & STL_IN_TITLE))
10686# endif
10687 )
10688 maketitle();
10689#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010690 /* Redraw the tab pages line if needed. */
10691 if (redraw_tabline)
10692 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010693}
10694
10695#ifdef FEAT_CMDL_INFO
10696 static void
Bram Moolenaar491ac282018-06-17 14:47:55 +020010697win_redr_ruler(win_T *wp, int always, int ignore_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010698{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010699#define RULER_BUF_LEN 70
10700 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010701 int row;
10702 int fillchar;
10703 int attr;
10704 int empty_line = FALSE;
10705 colnr_T virtcol;
10706 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010707 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010708 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010709 int this_ru_col;
10710 int off = 0;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +010010711 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010712
10713 /* If 'ruler' off or redrawing disabled, don't do anything */
10714 if (!p_ru)
10715 return;
10716
10717 /*
10718 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10719 * after deleting lines, before cursor.lnum is corrected.
10720 */
10721 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10722 return;
10723
Bram Moolenaare2c453d2019-08-21 14:37:09 +020010724 // Don't draw the ruler while doing insert-completion, it might overwrite
10725 // the (long) mode message.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010726 if (wp == lastwin && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010727 if (edit_submode != NULL)
10728 return;
Bram Moolenaar491ac282018-06-17 14:47:55 +020010729 // Don't draw the ruler when the popup menu is visible, it may overlap.
10730 // Except when the popup menu will be redrawn anyway.
10731 if (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010732 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010733
10734#ifdef FEAT_STL_OPT
10735 if (*p_ruf)
10736 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010737 int save_called_emsg = called_emsg;
10738
10739 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010740 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010741 if (called_emsg)
10742 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010743 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010744 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010745 return;
10746 }
10747#endif
10748
10749 /*
10750 * Check if not in Insert mode and the line is empty (will show "0-1").
10751 */
10752 if (!(State & INSERT)
10753 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10754 empty_line = TRUE;
10755
10756 /*
10757 * Only draw the ruler when something changed.
10758 */
10759 validate_virtcol_win(wp);
10760 if ( redraw_cmdline
10761 || always
10762 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10763 || wp->w_cursor.col != wp->w_ru_cursor.col
10764 || wp->w_virtcol != wp->w_ru_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +000010765 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
Bram Moolenaar071d4272004-06-13 20:20:40 +000010766 || wp->w_topline != wp->w_ru_topline
10767 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10768#ifdef FEAT_DIFF
10769 || wp->w_topfill != wp->w_ru_topfill
10770#endif
10771 || empty_line != wp->w_ru_empty)
10772 {
10773 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010774 if (wp->w_status_height)
10775 {
10776 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010777 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar53f81742017-09-22 14:35:51 +020010778 off = wp->w_wincol;
Bram Moolenaar02631462017-09-22 15:20:32 +020010779 width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010780 }
10781 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010782 {
10783 row = Rows - 1;
10784 fillchar = ' ';
10785 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010786 width = Columns;
10787 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010788 }
10789
10790 /* In list mode virtcol needs to be recomputed */
10791 virtcol = wp->w_virtcol;
10792 if (wp->w_p_list && lcs_tab1 == NUL)
10793 {
10794 wp->w_p_list = FALSE;
10795 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10796 wp->w_p_list = TRUE;
10797 }
10798
10799 /*
10800 * Some sprintfs return the length, some return a pointer.
10801 * To avoid portability problems we use strlen() here.
10802 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010803 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010804 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10805 ? 0L
10806 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010807 len = STRLEN(buffer);
10808 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010809 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10810 (int)virtcol + 1);
10811
10812 /*
10813 * Add a "50%" if there is room for it.
10814 * On the last line, don't print in the last column (scrolls the
10815 * screen up on some terminals).
10816 */
10817 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010818 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010819 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010820 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010821 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010822 this_ru_col = ru_col - (Columns - width);
10823 if (this_ru_col < 0)
10824 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010825 /* Never use more than half the window/screen width, leave the other
10826 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010827 if (this_ru_col < (width + 1) / 2)
10828 this_ru_col = (width + 1) / 2;
10829 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010830 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010010831 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010832 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010833 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010834 if (has_mbyte)
10835 i += (*mb_char2bytes)(fillchar, buffer + i);
10836 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010837 buffer[i++] = fillchar;
10838 ++o;
10839 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010840 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010841 }
10842 /* Truncate at window boundary. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010843 if (has_mbyte)
10844 {
10845 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010846 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010847 {
10848 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010849 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010850 {
10851 buffer[i] = NUL;
10852 break;
10853 }
10854 }
10855 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010010856 else if (this_ru_col + (int)STRLEN(buffer) > width)
Bram Moolenaar4033c552017-09-16 20:54:51 +020010857 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010858
Bram Moolenaar4033c552017-09-16 20:54:51 +020010859 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010860 i = redraw_cmdline;
10861 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020010862 this_ru_col + off + (int)STRLEN(buffer),
10863 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010864 fillchar, fillchar, attr);
10865 /* don't redraw the cmdline because of showing the ruler */
10866 redraw_cmdline = i;
10867 wp->w_ru_cursor = wp->w_cursor;
10868 wp->w_ru_virtcol = wp->w_virtcol;
10869 wp->w_ru_empty = empty_line;
10870 wp->w_ru_topline = wp->w_topline;
10871 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10872#ifdef FEAT_DIFF
10873 wp->w_ru_topfill = wp->w_topfill;
10874#endif
10875 }
10876}
10877#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010878
Bram Moolenaare677df82019-09-02 22:31:11 +020010879/*
10880 * Compute columns for ruler and shown command. 'sc_col' is also used to
10881 * decide what the maximum length of a message on the status line can be.
10882 * If there is a status line for the last window, 'sc_col' is independent
10883 * of 'ru_col'.
10884 */
10885
10886#define COL_RULER 17 // columns needed by standard ruler
10887
10888 void
10889comp_col(void)
10890{
10891#if defined(FEAT_CMDL_INFO)
10892 int last_has_status = (p_ls == 2 || (p_ls == 1 && !ONE_WINDOW));
10893
10894 sc_col = 0;
10895 ru_col = 0;
10896 if (p_ru)
10897 {
10898# ifdef FEAT_STL_OPT
10899 ru_col = (ru_wid ? ru_wid : COL_RULER) + 1;
10900# else
10901 ru_col = COL_RULER + 1;
10902# endif
10903 // no last status line, adjust sc_col
10904 if (!last_has_status)
10905 sc_col = ru_col;
10906 }
10907 if (p_sc)
10908 {
10909 sc_col += SHOWCMD_COLS;
10910 if (!p_ru || last_has_status) // no need for separating space
10911 ++sc_col;
10912 }
10913 sc_col = Columns - sc_col;
10914 ru_col = Columns - ru_col;
10915 if (sc_col <= 0) // screen too narrow, will become a mess
10916 sc_col = 1;
10917 if (ru_col <= 0)
10918 ru_col = 1;
10919#else
10920 sc_col = Columns;
10921 ru_col = Columns;
10922#endif
10923#ifdef FEAT_EVAL
10924 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
10925#endif
10926}
10927
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010928#if defined(FEAT_LINEBREAK) || defined(PROTO)
10929/*
Bram Moolenaar64486672010-05-16 15:46:46 +020010930 * Return the width of the 'number' and 'relativenumber' column.
10931 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010932 * Otherwise it depends on 'numberwidth' and the line count.
10933 */
10934 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010935number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010936{
10937 int n;
10938 linenr_T lnum;
10939
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020010940 if (wp->w_p_rnu && !wp->w_p_nu)
10941 /* cursor line shows "0" */
10942 lnum = wp->w_height;
10943 else
10944 /* cursor line shows absolute line number */
10945 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020010946
Bram Moolenaar6b314672015-03-20 15:42:10 +010010947 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010948 return wp->w_nrwidth_width;
10949 wp->w_nrwidth_line_count = lnum;
10950
10951 n = 0;
10952 do
10953 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010954 lnum /= 10;
10955 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010956 } while (lnum > 0);
10957
10958 /* 'numberwidth' gives the minimal width plus one */
10959 if (n < wp->w_p_nuw - 1)
10960 n = wp->w_p_nuw - 1;
10961
Bram Moolenaare4b407f2019-07-04 11:59:28 +020010962# ifdef FEAT_SIGNS
10963 // If 'signcolumn' is set to 'number' and there is a sign to display, then
10964 // the minimal width for the number column is 2.
10965 if (n < 2 && (wp->w_buffer->b_signlist != NULL)
10966 && (*wp->w_p_scl == 'n' && *(wp->w_p_scl + 1) == 'u'))
10967 n = 2;
10968# endif
10969
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010970 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010010971 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010972 return n;
10973}
10974#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010975
Bram Moolenaar113e1072019-01-20 15:30:40 +010010976#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010977/*
10978 * Return the current cursor column. This is the actual position on the
10979 * screen. First column is 0.
10980 */
10981 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010982screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010983{
10984 return screen_cur_col;
10985}
10986
10987/*
10988 * Return the current cursor row. This is the actual position on the screen.
10989 * First row is 0.
10990 */
10991 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010992screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010993{
10994 return screen_cur_row;
10995}
Bram Moolenaar113e1072019-01-20 15:30:40 +010010996#endif
Bram Moolenaare677df82019-09-02 22:31:11 +020010997
10998/*
10999 * Handle setting 'listchars' or 'fillchars'.
11000 * Returns error message, NULL if it's OK.
11001 */
11002 char *
11003set_chars_option(char_u **varp)
11004{
11005 int round, i, len, entries;
11006 char_u *p, *s;
11007 int c1 = 0, c2 = 0, c3 = 0;
11008 struct charstab
11009 {
11010 int *cp;
11011 char *name;
11012 };
11013 static struct charstab filltab[] =
11014 {
11015 {&fill_stl, "stl"},
11016 {&fill_stlnc, "stlnc"},
11017 {&fill_vert, "vert"},
11018 {&fill_fold, "fold"},
11019 {&fill_diff, "diff"},
11020 };
11021 static struct charstab lcstab[] =
11022 {
11023 {&lcs_eol, "eol"},
11024 {&lcs_ext, "extends"},
11025 {&lcs_nbsp, "nbsp"},
11026 {&lcs_prec, "precedes"},
11027 {&lcs_space, "space"},
11028 {&lcs_tab2, "tab"},
11029 {&lcs_trail, "trail"},
11030#ifdef FEAT_CONCEAL
11031 {&lcs_conceal, "conceal"},
11032#else
11033 {NULL, "conceal"},
11034#endif
11035 };
11036 struct charstab *tab;
11037
11038 if (varp == &p_lcs)
11039 {
11040 tab = lcstab;
11041 entries = sizeof(lcstab) / sizeof(struct charstab);
11042 }
11043 else
11044 {
11045 tab = filltab;
11046 entries = sizeof(filltab) / sizeof(struct charstab);
11047 }
11048
11049 // first round: check for valid value, second round: assign values
11050 for (round = 0; round <= 1; ++round)
11051 {
11052 if (round > 0)
11053 {
11054 // After checking that the value is valid: set defaults: space for
11055 // 'fillchars', NUL for 'listchars'
11056 for (i = 0; i < entries; ++i)
11057 if (tab[i].cp != NULL)
11058 *(tab[i].cp) = (varp == &p_lcs ? NUL : ' ');
11059
11060 if (varp == &p_lcs)
11061 {
11062 lcs_tab1 = NUL;
11063 lcs_tab3 = NUL;
11064 }
11065 else
11066 fill_diff = '-';
11067 }
11068 p = *varp;
11069 while (*p)
11070 {
11071 for (i = 0; i < entries; ++i)
11072 {
11073 len = (int)STRLEN(tab[i].name);
11074 if (STRNCMP(p, tab[i].name, len) == 0
11075 && p[len] == ':'
11076 && p[len + 1] != NUL)
11077 {
11078 c2 = c3 = 0;
11079 s = p + len + 1;
11080 c1 = mb_ptr2char_adv(&s);
11081 if (mb_char2cells(c1) > 1)
11082 continue;
11083 if (tab[i].cp == &lcs_tab2)
11084 {
11085 if (*s == NUL)
11086 continue;
11087 c2 = mb_ptr2char_adv(&s);
11088 if (mb_char2cells(c2) > 1)
11089 continue;
11090 if (!(*s == ',' || *s == NUL))
11091 {
11092 c3 = mb_ptr2char_adv(&s);
11093 if (mb_char2cells(c3) > 1)
11094 continue;
11095 }
11096 }
11097
11098 if (*s == ',' || *s == NUL)
11099 {
11100 if (round)
11101 {
11102 if (tab[i].cp == &lcs_tab2)
11103 {
11104 lcs_tab1 = c1;
11105 lcs_tab2 = c2;
11106 lcs_tab3 = c3;
11107 }
11108 else if (tab[i].cp != NULL)
11109 *(tab[i].cp) = c1;
11110
11111 }
11112 p = s;
11113 break;
11114 }
11115 }
11116 }
11117
11118 if (i == entries)
11119 return e_invarg;
11120 if (*p == ',')
11121 ++p;
11122 }
11123 }
11124
11125 return NULL; // no error
11126}
Bram Moolenaar017ba072019-09-14 21:01:23 +020011127
11128#ifdef FEAT_SYN_HL
11129/*
11130 * Used when 'cursorlineopt' contains "screenline": compute the margins between
11131 * which the highlighting is used.
11132 */
11133 static void
11134margin_columns_win(win_T *wp, int *left_col, int *right_col)
11135{
11136 // cache previous calculations depending on w_virtcol
11137 static int saved_w_virtcol;
11138 static win_T *prev_wp;
11139 static int prev_left_col;
11140 static int prev_right_col;
11141 static int prev_col_off;
11142
11143 int cur_col_off = win_col_off(wp);
11144 int width1;
11145 int width2;
11146
11147 if (saved_w_virtcol == wp->w_virtcol
11148 && prev_wp == wp && prev_col_off == cur_col_off)
11149 {
11150 *right_col = prev_right_col;
11151 *left_col = prev_left_col;
11152 return;
11153 }
11154
11155 width1 = wp->w_width - cur_col_off;
11156 width2 = width1 + win_col_off2(wp);
11157
11158 *left_col = 0;
11159 *right_col = width1;
11160
11161 if (wp->w_virtcol >= (colnr_T)width1)
11162 *right_col = width1 + ((wp->w_virtcol - width1) / width2 + 1) * width2;
11163 if (wp->w_virtcol >= (colnr_T)width1 && width2 > 0)
11164 *left_col = (wp->w_virtcol - width1) / width2 * width2 + width1;
11165
11166 // cache values
11167 prev_left_col = *left_col;
11168 prev_right_col = *right_col;
11169 prev_wp = wp;
11170 saved_w_virtcol = wp->w_virtcol;
11171 prev_col_off = cur_col_off;
11172}
11173#endif