blob: 988626741955f10e95a446a049cf547a195475b1 [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 Moolenaar071d4272004-06-13 20:20:40 +0000107static 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 Moolenaarde993ea2014-06-17 23:18:01 +0200138# define SEARCH_HL_PRIORITY 0
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100139static void start_search_hl(void);
140static void end_search_hl(void);
141static void init_search_hl(win_T *wp);
142static void prepare_search_hl(win_T *wp, linenr_T lnum);
143static void next_search_hl(win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol, matchitem_T *cur);
144static int next_search_hl_pos(match_T *shl, linenr_T lnum, posmatch_T *pos, colnr_T mincol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100146static void screen_char(unsigned off, int row, int col);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100147static void screen_char_2(unsigned off, int row, int col);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100148static void screenclear2(void);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200149static void lineclear(unsigned off, int width, int attr);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100150static void lineinvalid(unsigned off, int width);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200151static 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 +0100152static void win_rest_invalid(win_T *wp);
153static void msg_pos_mode(void);
154static void recording_mode(int attr);
Bram Moolenaar3633cf52017-07-31 22:29:35 +0200155static int fillchar_status(int *attr, win_T *wp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100156static int fillchar_vsep(int *attr);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200157#ifdef FEAT_MENU
158static void redraw_win_toolbar(win_T *wp);
159#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100161static void win_redr_custom(win_T *wp, int draw_ruler);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162#endif
163#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +0200164static void win_redr_ruler(win_T *wp, int always, int ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165#endif
166
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167/* Ugly global: overrule attribute used by screen_char() */
168static int screen_char_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200170#if defined(FEAT_SYN_HL) && defined(FEAT_RELTIME)
171/* Can limit syntax highlight time to 'redrawtime'. */
172# define SYN_TIME_LIMIT 1
173#endif
174
Bram Moolenaarc0aa4822017-07-16 14:04:29 +0200175#ifdef FEAT_RIGHTLEFT
176# define HAS_RIGHTLEFT(x) x
177#else
178# define HAS_RIGHTLEFT(x) FALSE
179#endif
180
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200181// flags for screen_line()
182#define SLF_RIGHTLEFT 1
183#define SLF_POPUP 2
184
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185/*
186 * Redraw the current window later, with update_screen(type).
187 * Set must_redraw only if not already set to a higher value.
Bram Moolenaarae654382019-01-17 21:09:05 +0100188 * E.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189 */
190 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100191redraw_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192{
193 redraw_win_later(curwin, type);
194}
195
196 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100197redraw_win_later(
198 win_T *wp,
199 int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200{
Bram Moolenaar4f198282017-10-23 21:53:30 +0200201 if (!exiting && wp->w_redr_type < type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202 {
203 wp->w_redr_type = type;
204 if (type >= NOT_VALID)
205 wp->w_lines_valid = 0;
206 if (must_redraw < type) /* must_redraw is the maximum of all windows */
207 must_redraw = type;
208 }
209}
210
211/*
212 * Force a complete redraw later. Also resets the highlighting. To be used
213 * after executing a shell command that messes up the screen.
214 */
215 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100216redraw_later_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217{
218 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000219#ifdef FEAT_GUI
220 if (gui.in_use)
221 /* Use a code that will reset gui.highlight_mask in
222 * gui_stop_highlight(). */
223 screen_attr = HL_ALL + 1;
224 else
225#endif
226 /* Use attributes that is very unlikely to appear in text. */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +0200227 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE | HL_STRIKETHROUGH;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228}
229
230/*
231 * Mark all windows to be redrawn later.
232 */
233 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100234redraw_all_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235{
236 win_T *wp;
237
238 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239 redraw_win_later(wp, type);
Bram Moolenaar04b4e1a2019-01-06 22:22:07 +0100240 // This may be needed when switching tabs.
241 if (must_redraw < type)
242 must_redraw = type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243}
244
245/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000246 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247 */
248 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100249redraw_curbuf_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250{
251 redraw_buf_later(curbuf, type);
252}
253
254 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100255redraw_buf_later(buf_T *buf, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256{
257 win_T *wp;
258
259 FOR_ALL_WINDOWS(wp)
260 {
261 if (wp->w_buffer == buf)
262 redraw_win_later(wp, type);
263 }
264}
265
Bram Moolenaar113e1072019-01-20 15:30:40 +0100266#if defined(FEAT_SIGNS) || defined(PROTO)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200267 void
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100268redraw_buf_line_later(buf_T *buf, linenr_T lnum)
269{
270 win_T *wp;
271
272 FOR_ALL_WINDOWS(wp)
273 if (wp->w_buffer == buf && lnum >= wp->w_topline
274 && lnum < wp->w_botline)
275 redrawWinline(wp, lnum);
276}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100277#endif
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100278
Bram Moolenaar113e1072019-01-20 15:30:40 +0100279#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100280 void
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200281redraw_buf_and_status_later(buf_T *buf, int type)
282{
283 win_T *wp;
284
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200285#ifdef FEAT_WILDMENU
Bram Moolenaar86033562017-07-12 20:24:41 +0200286 if (wild_menu_showing != 0)
287 /* Don't redraw while the command line completion is displayed, it
288 * would disappear. */
289 return;
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200290#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200291 FOR_ALL_WINDOWS(wp)
292 {
293 if (wp->w_buffer == buf)
294 {
295 redraw_win_later(wp, type);
296 wp->w_redr_status = TRUE;
297 }
298 }
299}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100300#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200301
Bram Moolenaar113e1072019-01-20 15:30:40 +0100302#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303/*
Bram Moolenaar2951b772013-07-03 12:45:31 +0200304 * Redraw as soon as possible. When the command line is not scrolled redraw
305 * right away and restore what was on the command line.
306 * Return a code indicating what happened.
307 */
308 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100309redraw_asap(int type)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200310{
311 int rows;
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200312 int cols = screen_Columns;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200313 int r;
314 int ret = 0;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200315 schar_T *screenline; /* copy from ScreenLines[] */
316 sattr_T *screenattr; /* copy from ScreenAttrs[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200317 int i;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200318 u8char_T *screenlineUC = NULL; /* copy from ScreenLinesUC[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200319 u8char_T *screenlineC[MAX_MCO]; /* copy from ScreenLinesC[][] */
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200320 schar_T *screenline2 = NULL; /* copy from ScreenLines2[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200321
322 redraw_later(type);
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200323 if (msg_scrolled || (State != NORMAL && State != NORMAL_BUSY) || exiting)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200324 return ret;
325
326 /* Allocate space to save the text displayed in the command line area. */
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200327 rows = screen_Rows - cmdline_row;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200328 screenline = LALLOC_MULT(schar_T, rows * cols);
329 screenattr = LALLOC_MULT(sattr_T, rows * cols);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200330 if (screenline == NULL || screenattr == NULL)
331 ret = 2;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200332 if (enc_utf8)
333 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200334 screenlineUC = LALLOC_MULT(u8char_T, rows * cols);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200335 if (screenlineUC == NULL)
336 ret = 2;
337 for (i = 0; i < p_mco; ++i)
338 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200339 screenlineC[i] = LALLOC_MULT(u8char_T, rows * cols);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200340 if (screenlineC[i] == NULL)
341 ret = 2;
342 }
343 }
344 if (enc_dbcs == DBCS_JPNU)
345 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200346 screenline2 = LALLOC_MULT(schar_T, rows * cols);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200347 if (screenline2 == NULL)
348 ret = 2;
349 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200350
351 if (ret != 2)
352 {
353 /* Save the text displayed in the command line area. */
354 for (r = 0; r < rows; ++r)
355 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200356 mch_memmove(screenline + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200357 ScreenLines + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200358 (size_t)cols * sizeof(schar_T));
359 mch_memmove(screenattr + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200360 ScreenAttrs + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200361 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200362 if (enc_utf8)
363 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200364 mch_memmove(screenlineUC + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200365 ScreenLinesUC + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200366 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200367 for (i = 0; i < p_mco; ++i)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200368 mch_memmove(screenlineC[i] + r * cols,
369 ScreenLinesC[i] + LineOffset[cmdline_row + r],
370 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200371 }
372 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200373 mch_memmove(screenline2 + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200374 ScreenLines2 + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200375 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200376 }
377
378 update_screen(0);
379 ret = 3;
380
381 if (must_redraw == 0)
382 {
383 int off = (int)(current_ScreenLine - ScreenLines);
384
385 /* Restore the text displayed in the command line area. */
386 for (r = 0; r < rows; ++r)
387 {
388 mch_memmove(current_ScreenLine,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200389 screenline + r * cols,
390 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200391 mch_memmove(ScreenAttrs + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200392 screenattr + r * cols,
393 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200394 if (enc_utf8)
395 {
396 mch_memmove(ScreenLinesUC + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200397 screenlineUC + r * cols,
398 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200399 for (i = 0; i < p_mco; ++i)
400 mch_memmove(ScreenLinesC[i] + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200401 screenlineC[i] + r * cols,
402 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200403 }
404 if (enc_dbcs == DBCS_JPNU)
405 mch_memmove(ScreenLines2 + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200406 screenline2 + r * cols,
407 (size_t)cols * sizeof(schar_T));
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200408 screen_line(cmdline_row + r, 0, cols, cols, 0);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200409 }
410 ret = 4;
411 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200412 }
413
414 vim_free(screenline);
415 vim_free(screenattr);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200416 if (enc_utf8)
417 {
418 vim_free(screenlineUC);
419 for (i = 0; i < p_mco; ++i)
420 vim_free(screenlineC[i]);
421 }
422 if (enc_dbcs == DBCS_JPNU)
423 vim_free(screenline2);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200424
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200425 /* Show the intro message when appropriate. */
426 maybe_intro_message();
427
428 setcursor();
429
Bram Moolenaar2951b772013-07-03 12:45:31 +0200430 return ret;
431}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100432#endif
Bram Moolenaar2951b772013-07-03 12:45:31 +0200433
434/*
Bram Moolenaar975b5272016-03-15 23:10:59 +0100435 * Invoked after an asynchronous callback is called.
436 * If an echo command was used the cursor needs to be put back where
437 * it belongs. If highlighting was changed a redraw is needed.
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200438 * If "call_update_screen" is FALSE don't call update_screen() when at the
439 * command line.
Bram Moolenaar975b5272016-03-15 23:10:59 +0100440 */
441 void
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200442redraw_after_callback(int call_update_screen)
Bram Moolenaar975b5272016-03-15 23:10:59 +0100443{
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200444 ++redrawing_for_callback;
445
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100446 if (State == HITRETURN || State == ASKMORE)
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200447 ; // do nothing
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100448 else if (State & CMDLINE)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200449 {
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200450 // Don't redraw when in prompt_for_number().
451 if (cmdline_row > 0)
452 {
453 // Redrawing only works when the screen didn't scroll. Don't clear
454 // wildmenu entries.
455 if (msg_scrolled == 0
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200456#ifdef FEAT_WILDMENU
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200457 && wild_menu_showing == 0
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200458#endif
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200459 && call_update_screen)
460 update_screen(0);
461
462 // Redraw in the same position, so that the user can continue
463 // editing the command.
464 redrawcmdline_ex(FALSE);
465 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200466 }
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200467 else if (State & (NORMAL | INSERT | TERMINAL))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100468 {
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200469 // keep the command line if possible
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200470 update_screen(VALID_NO_UPDATE);
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100471 setcursor();
472 }
Bram Moolenaar975b5272016-03-15 23:10:59 +0100473 cursor_on();
Bram Moolenaar975b5272016-03-15 23:10:59 +0100474#ifdef FEAT_GUI
Bram Moolenaara338adc2018-01-31 20:51:47 +0100475 if (gui.in_use && !gui_mch_is_blink_off())
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200476 // Don't update the cursor when it is blinking and off to avoid
477 // flicker.
Bram Moolenaara338adc2018-01-31 20:51:47 +0100478 out_flush_cursor(FALSE, FALSE);
479 else
Bram Moolenaar975b5272016-03-15 23:10:59 +0100480#endif
Bram Moolenaaracda04f2018-02-08 09:57:28 +0100481 out_flush();
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200482
483 --redrawing_for_callback;
Bram Moolenaar975b5272016-03-15 23:10:59 +0100484}
485
486/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487 * Changed something in the current window, at buffer line "lnum", that
488 * requires that line and possibly other lines to be redrawn.
489 * Used when entering/leaving Insert mode with the cursor on a folded line.
490 * Used to remove the "$" from a change command.
491 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
492 * may become invalid and the whole window will have to be redrawn.
493 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100495redrawWinline(
Bram Moolenaar90a99792018-09-12 21:52:18 +0200496 win_T *wp,
Bram Moolenaarae12f4b2019-01-09 20:51:04 +0100497 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498{
Bram Moolenaar90a99792018-09-12 21:52:18 +0200499 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
500 wp->w_redraw_top = lnum;
501 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
502 wp->w_redraw_bot = lnum;
503 redraw_win_later(wp, VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504}
505
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200506/*
507 * To be called when "updating_screen" was set before and now the postponed
508 * side effects may take place.
509 */
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200510 void
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200511after_updating_screen(int may_resize_shell UNUSED)
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200512{
513 updating_screen = FALSE;
514#ifdef FEAT_GUI
515 if (may_resize_shell)
516 gui_may_resize_shell();
517#endif
518#ifdef FEAT_TERMINAL
519 term_check_channel_closed_recently();
520#endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +0200521
522#ifdef HAVE_DROP_FILE
523 // If handle_drop() was called while updating_screen was TRUE need to
524 // handle the drop now.
525 handle_any_postponed_drop();
526#endif
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200527}
528
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529/*
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200530 * Update all windows that are editing the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531 */
532 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100533update_curbuf(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534{
535 redraw_curbuf_later(type);
536 update_screen(type);
537}
538
539/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 * Based on the current value of curwin->w_topline, transfer a screenfull
541 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
Bram Moolenaar072412e2017-09-13 22:11:35 +0200542 * Return OK when the screen was updated, FAIL if it was not done.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200544 int
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200545update_screen(int type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546{
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200547 int type = type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548 win_T *wp;
549 static int did_intro = FALSE;
550#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
551 int did_one;
552#endif
Bram Moolenaar144445d2016-07-08 21:41:54 +0200553#ifdef FEAT_GUI
Bram Moolenaar107abd22016-08-12 14:08:25 +0200554 int did_undraw = FALSE;
Bram Moolenaar1f3601e2019-04-26 20:33:00 +0200555 int gui_cursor_col = 0;
556 int gui_cursor_row = 0;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200557#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200558 int no_update = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000560 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 if (!screen_valid(TRUE))
Bram Moolenaar072412e2017-09-13 22:11:35 +0200562 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200564 if (type == VALID_NO_UPDATE)
565 {
566 no_update = TRUE;
567 type = 0;
568 }
569
Bram Moolenaara3347722019-05-11 21:14:24 +0200570#ifdef FEAT_EVAL
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200571 {
572 buf_T *buf;
573
574 // Before updating the screen, notify any listeners of changed text.
575 FOR_ALL_BUFFERS(buf)
576 invoke_listeners(buf);
577 }
Bram Moolenaara3347722019-05-11 21:14:24 +0200578#endif
579
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 if (must_redraw)
581 {
582 if (type < must_redraw) /* use maximal type */
583 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000584
585 /* must_redraw is reset here, so that when we run into some weird
586 * reason to redraw while busy redrawing (e.g., asynchronous
587 * scrolling), or update_topline() in win_update() will cause a
588 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 must_redraw = 0;
590 }
591
Bram Moolenaar6eddadf2018-05-06 16:40:16 +0200592 /* May need to update w_lines[]. */
593 if (curwin->w_lines_valid == 0 && type < NOT_VALID
594#ifdef FEAT_TERMINAL
595 && !term_do_update_window(curwin)
596#endif
597 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 type = NOT_VALID;
599
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000600 /* Postpone the redrawing when it's not needed and when being called
601 * recursively. */
602 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 {
604 redraw_later(type); /* remember type for next time */
605 must_redraw = type;
606 if (type > INVERTED_ALL)
607 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200608 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 }
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
617 updating_screen = TRUE;
618#ifdef FEAT_SYN_HL
619 ++display_tick; /* let syntax code know we're in a next round of
620 * display updating */
621#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200622 if (no_update)
623 ++no_win_do_lines_ins;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624
625 /*
626 * if the screen was scrolled up when displaying a message, scroll it down
627 */
628 if (msg_scrolled)
629 {
630 clear_cmdline = TRUE;
631 if (msg_scrolled > Rows - 5) /* clearing is faster */
632 type = CLEAR;
633 else if (type != CLEAR)
634 {
635 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200636 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, 0, NULL)
637 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 type = CLEAR;
639 FOR_ALL_WINDOWS(wp)
640 {
Bram Moolenaar98fb65c2019-06-02 20:33:32 +0200641 if (wp->w_winrow < msg_scrolled)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 {
643 if (W_WINROW(wp) + wp->w_height > msg_scrolled
644 && wp->w_redr_type < REDRAW_TOP
645 && wp->w_lines_valid > 0
646 && wp->w_topline == wp->w_lines[0].wl_lnum)
647 {
648 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
649 wp->w_redr_type = REDRAW_TOP;
650 }
651 else
652 {
653 wp->w_redr_type = NOT_VALID;
Bram Moolenaare0de17d2017-09-24 16:24:34 +0200654 if (W_WINROW(wp) + wp->w_height + wp->w_status_height
655 <= msg_scrolled)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 }
658 }
659 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200660 if (!no_update)
661 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000662 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 }
664 msg_scrolled = 0;
665 need_wait_return = FALSE;
666 }
667
668 /* reset cmdline_row now (may have been changed temporarily) */
669 compute_cmdrow();
670
671 /* Check for changed highlighting */
672 if (need_highlight_changed)
673 highlight_changed();
674
675 if (type == CLEAR) /* first clear screen */
676 {
677 screenclear(); /* will reset clear_cmdline */
678 type = NOT_VALID;
Bram Moolenaar9f5f7bf2017-06-28 20:45:26 +0200679 /* must_redraw may be set indirectly, avoid another redraw later */
680 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 }
682
683 if (clear_cmdline) /* going to clear cmdline (done below) */
684 check_for_delay(FALSE);
685
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000686#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200687 /* Force redraw when width of 'number' or 'relativenumber' column
688 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000689 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200690 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
691 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000692 curwin->w_redr_type = NOT_VALID;
693#endif
694
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695 /*
696 * Only start redrawing if there is really something to do.
697 */
698 if (type == INVERTED)
699 update_curswant();
700 if (curwin->w_redr_type < type
701 && !((type == VALID
702 && curwin->w_lines[0].wl_valid
703#ifdef FEAT_DIFF
704 && curwin->w_topfill == curwin->w_old_topfill
705 && curwin->w_botfill == curwin->w_old_botfill
706#endif
707 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000709 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
711 && curwin->w_old_visual_mode == VIsual_mode
712 && (curwin->w_valid & VALID_VIRTCOL)
713 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 ))
715 curwin->w_redr_type = type;
716
Bram Moolenaar5a305422006-04-28 22:38:25 +0000717 /* Redraw the tab pages line if needed. */
718 if (redraw_tabline || type >= NOT_VALID)
719 draw_tabline();
Bram Moolenaar5a305422006-04-28 22:38:25 +0000720
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721#ifdef FEAT_SYN_HL
722 /*
723 * Correct stored syntax highlighting info for changes in each displayed
724 * buffer. Each buffer must only be done once.
725 */
726 FOR_ALL_WINDOWS(wp)
727 {
728 if (wp->w_buffer->b_mod_set)
729 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 win_T *wwp;
731
732 /* Check if we already did this buffer. */
733 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
734 if (wwp->w_buffer == wp->w_buffer)
735 break;
Bram Moolenaar4033c552017-09-16 20:54:51 +0200736 if (wwp == wp && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 syn_stack_apply_changes(wp->w_buffer);
738 }
739 }
740#endif
741
742 /*
743 * Go from top to bottom through the windows, redrawing the ones that need
744 * it.
745 */
746#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
747 did_one = FALSE;
748#endif
749#ifdef FEAT_SEARCH_EXTRA
750 search_hl.rm.regprog = NULL;
751#endif
752 FOR_ALL_WINDOWS(wp)
753 {
754 if (wp->w_redr_type != 0)
755 {
756 cursor_off();
757#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
758 if (!did_one)
759 {
760 did_one = TRUE;
761# ifdef FEAT_SEARCH_EXTRA
762 start_search_hl();
763# endif
764# ifdef FEAT_CLIPBOARD
765 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200766 if (clip_star.available && clip_isautosel_star())
767 clip_update_selection(&clip_star);
768 if (clip_plus.available && clip_isautosel_plus())
769 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770# endif
771#ifdef FEAT_GUI
772 /* Remove the cursor before starting to do anything, because
773 * scrolling may make it difficult to redraw the text under
774 * it. */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200775 if (gui.in_use && wp == curwin)
Bram Moolenaar144445d2016-07-08 21:41:54 +0200776 {
777 gui_cursor_col = gui.cursor_col;
778 gui_cursor_row = gui.cursor_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 gui_undraw_cursor();
Bram Moolenaar107abd22016-08-12 14:08:25 +0200780 did_undraw = TRUE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782#endif
783 }
784#endif
785 win_update(wp);
786 }
787
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 /* redraw status line after the window to minimize cursor movement */
789 if (wp->w_redr_status)
790 {
791 cursor_off();
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200792 win_redr_status(wp, TRUE); // any popup menu will be redrawn below
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 }
795#if defined(FEAT_SEARCH_EXTRA)
796 end_search_hl();
797#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100798#ifdef FEAT_INS_EXPAND
799 /* May need to redraw the popup menu. */
Bram Moolenaar491ac282018-06-17 14:47:55 +0200800 pum_may_redraw();
Bram Moolenaar51971b32013-02-13 12:16:05 +0100801#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 /* Reset b_mod_set flags. Going through all windows is probably faster
804 * than going through all buffers (there could be many buffers). */
Bram Moolenaar29323592016-07-24 22:04:11 +0200805 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 wp->w_buffer->b_mod_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200808 after_updating_screen(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809
810 /* Clear or redraw the command line. Done last, because scrolling may
811 * mess up the command line. */
Bram Moolenaar4c25bd72019-04-20 23:38:07 +0200812 if (clear_cmdline || redraw_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 showmode();
814
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200815 if (no_update)
816 --no_win_do_lines_ins;
817
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200819 if (!did_intro)
820 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 did_intro = TRUE;
822
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200823#ifdef FEAT_TEXT_PROP
Bram Moolenaar988c4332019-06-02 14:12:11 +0200824 // Display popup windows on top of the windows.
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200825 update_popups(win_update);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200826#endif
827
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828#ifdef FEAT_GUI
829 /* Redraw the cursor and update the scrollbars when all screen updating is
830 * done. */
831 if (gui.in_use)
832 {
Bram Moolenaar107abd22016-08-12 14:08:25 +0200833 if (did_undraw && !gui_mch_is_blink_off())
Bram Moolenaar144445d2016-07-08 21:41:54 +0200834 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100835 mch_disable_flush();
836 out_flush(); /* required before updating the cursor */
837 mch_enable_flush();
838
Bram Moolenaar144445d2016-07-08 21:41:54 +0200839 /* Put the GUI position where the cursor was, gui_update_cursor()
840 * uses that. */
841 gui.col = gui_cursor_col;
842 gui.row = gui_cursor_row;
Bram Moolenaar84dbd492016-10-02 23:09:31 +0200843 gui.col = mb_fix_col(gui.col, gui.row);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara338adc2018-01-31 20:51:47 +0100845 gui_may_flush();
Bram Moolenaar65549bd2016-07-08 22:52:37 +0200846 screen_cur_col = gui.col;
847 screen_cur_row = gui.row;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200848 }
Bram Moolenaara338adc2018-01-31 20:51:47 +0100849 else
850 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 gui_update_scrollbars(FALSE);
852 }
853#endif
Bram Moolenaar072412e2017-09-13 22:11:35 +0200854 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855}
856
Bram Moolenaar1fa8fdd2019-02-25 05:41:15 +0100857#if defined(FEAT_NETBEANS_INTG) || defined(FEAT_GUI)
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100858/*
859 * Prepare for updating one or more windows.
860 * Caller must check for "updating_screen" already set to avoid recursiveness.
861 */
862 static void
863update_prepare(void)
864{
865 cursor_off();
866 updating_screen = TRUE;
867#ifdef FEAT_GUI
868 /* Remove the cursor before starting to do anything, because scrolling may
869 * make it difficult to redraw the text under it. */
870 if (gui.in_use)
871 gui_undraw_cursor();
872#endif
873#ifdef FEAT_SEARCH_EXTRA
874 start_search_hl();
875#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +0200876#ifdef FEAT_TEXT_PROP
877 // Update popup_mask if needed.
878 may_update_popup_mask(0);
879#endif
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100880}
881
882/*
883 * Finish updating one or more windows.
884 */
885 static void
886update_finish(void)
887{
Bram Moolenaar4c25bd72019-04-20 23:38:07 +0200888 if (redraw_cmdline || redraw_mode)
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100889 showmode();
890
891# ifdef FEAT_SEARCH_EXTRA
892 end_search_hl();
893# endif
894
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200895 after_updating_screen(TRUE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100896
897# ifdef FEAT_GUI
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100898 /* Redraw the cursor and update the scrollbars when all screen updating is
899 * done. */
900 if (gui.in_use)
901 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100902 out_flush_cursor(FALSE, FALSE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100903 gui_update_scrollbars(FALSE);
904 }
905# endif
906}
907#endif
908
Bram Moolenaar860cae12010-06-05 23:22:07 +0200909#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200910/*
911 * Return TRUE if the cursor line in window "wp" may be concealed, according
912 * to the 'concealcursor' option.
913 */
914 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100915conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200916{
917 int c;
918
919 if (*wp->w_p_cocu == NUL)
920 return FALSE;
921 if (get_real_state() & VISUAL)
922 c = 'v';
923 else if (State & INSERT)
924 c = 'i';
925 else if (State & NORMAL)
926 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200927 else if (State & CMDLINE)
928 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200929 else
930 return FALSE;
931 return vim_strchr(wp->w_p_cocu, c) != NULL;
932}
933
934/*
935 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
936 */
937 void
Bram Moolenaarb9464822018-05-10 15:09:49 +0200938conceal_check_cursor_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200939{
940 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
941 {
942 need_cursor_line_redraw = TRUE;
943 /* Need to recompute cursor column, e.g., when starting Visual mode
944 * without concealing. */
945 curs_columns(TRUE);
946 }
947}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948#endif
949
Bram Moolenaar113e1072019-01-20 15:30:40 +0100950#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100952update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953{
954 win_T *wp;
955 int doit = FALSE;
956
957# ifdef FEAT_FOLDING
958 win_foldinfo.fi_level = 0;
959# endif
960
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100961 // update/delete a specific sign
962 redraw_buf_line_later(buf, lnum);
963
964 // check if it resulted in the need to redraw a window
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 if (wp->w_redr_type != 0)
967 doit = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100969 /* Return when there is nothing to do, screen updating is already
Bram Moolenaar07920482017-08-01 20:53:30 +0200970 * happening (recursive call), messages on the screen or still starting up.
971 */
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100972 if (!doit || updating_screen
Bram Moolenaar07920482017-08-01 20:53:30 +0200973 || State == ASKMORE || State == HITRETURN
974 || msg_scrolled
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100975#ifdef FEAT_GUI
976 || gui.starting
977#endif
978 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 return;
980
981 /* update all windows that need updating */
982 update_prepare();
983
Bram Moolenaar29323592016-07-24 22:04:11 +0200984 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 {
986 if (wp->w_redr_type != 0)
987 win_update(wp);
988 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200989 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991
992 update_finish();
993}
994#endif
995
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200996/*
997 * Get 'wincolor' attribute for window "wp". If not set and "wp" is a popup
998 * window then get the "Pmenu" highlight attribute.
999 */
Bram Moolenaara540f8a2019-06-14 19:23:57 +02001000 int
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001001get_wcr_attr(win_T *wp)
1002{
1003 int wcr_attr = 0;
1004
1005 if (*wp->w_p_wcr != NUL)
1006 wcr_attr = syn_name2attr(wp->w_p_wcr);
1007#ifdef FEAT_TEXT_PROP
1008 if (bt_popup(wp->w_buffer) && wcr_attr == 0)
1009 wcr_attr = HL_ATTR(HLF_PNI);
1010#endif
1011 return wcr_attr;
1012}
1013
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014#if defined(FEAT_GUI) || defined(PROTO)
1015/*
1016 * Update a single window, its status line and maybe the command line msg.
1017 * Used for the GUI scrollbar.
1018 */
1019 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001020updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021{
Bram Moolenaar19f990e2009-11-25 12:08:03 +00001022 /* return if already busy updating */
1023 if (updating_screen)
1024 return;
1025
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 update_prepare();
1027
1028#ifdef FEAT_CLIPBOARD
1029 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001030 if (clip_star.available && clip_isautosel_star())
1031 clip_update_selection(&clip_star);
1032 if (clip_plus.available && clip_isautosel_plus())
1033 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001035
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001037
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001038 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001039 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001040 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001041
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 if (wp->w_redr_status
1043# ifdef FEAT_CMDL_INFO
1044 || p_ru
1045# endif
1046# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001047 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048# endif
1049 )
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001050 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051
Bram Moolenaar988c4332019-06-02 14:12:11 +02001052#ifdef FEAT_TEXT_PROP
1053 // Display popup windows on top of everything.
Bram Moolenaara540f8a2019-06-14 19:23:57 +02001054 update_popups(win_update);
Bram Moolenaar988c4332019-06-02 14:12:11 +02001055#endif
1056
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 update_finish();
1058}
1059#endif
1060
1061/*
1062 * Update a single window.
1063 *
1064 * This may cause the windows below it also to be redrawn (when clearing the
1065 * screen or scrolling lines).
1066 *
1067 * How the window is redrawn depends on wp->w_redr_type. Each type also
1068 * implies the one below it.
1069 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001070 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1072 * INVERTED redraw the changed part of the Visual area
1073 * INVERTED_ALL redraw the whole Visual area
1074 * VALID 1. scroll up/down to adjust for a changed w_topline
1075 * 2. update lines at the top when scrolled down
1076 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001077 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 * b_mod_top and b_mod_bot.
1079 * - if wp->w_redraw_top non-zero, redraw lines between
1080 * wp->w_redraw_top and wp->w_redr_bot.
1081 * - continue redrawing when syntax status is invalid.
1082 * 4. if scrolled up, update lines at the bottom.
1083 * This results in three areas that may need updating:
1084 * top: from first row to top_end (when scrolled down)
1085 * mid: from mid_start to mid_end (update inversion or changed text)
1086 * bot: from bot_start to last row (when scrolled up)
1087 */
1088 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001089win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090{
1091 buf_T *buf = wp->w_buffer;
1092 int type;
1093 int top_end = 0; /* Below last row of the top area that needs
1094 updating. 0 when no top area updating. */
1095 int mid_start = 999;/* first row of the mid area that needs
1096 updating. 999 when no mid area updating. */
1097 int mid_end = 0; /* Below last row of the mid area that needs
1098 updating. 0 when no mid area updating. */
1099 int bot_start = 999;/* first row of the bot area that needs
1100 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 int scrolled_down = FALSE; /* TRUE when scrolled down when
1102 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001104 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 int top_to_mod = FALSE; /* redraw above mod_top */
1106#endif
1107
1108 int row; /* current window row to display */
1109 linenr_T lnum; /* current buffer lnum to display */
1110 int idx; /* current index in w_lines[] */
1111 int srow; /* starting row of the current line */
1112
1113 int eof = FALSE; /* if TRUE, we hit the end of the file */
1114 int didline = FALSE; /* if TRUE, we finished the last line */
1115 int i;
1116 long j;
1117 static int recursive = FALSE; /* being called recursively */
1118 int old_botline = wp->w_botline;
1119#ifdef FEAT_FOLDING
1120 long fold_count;
1121#endif
1122#ifdef FEAT_SYN_HL
1123 /* remember what happened to the previous line, to know if
1124 * check_visual_highlight() can be used */
1125#define DID_NONE 1 /* didn't update a line */
1126#define DID_LINE 2 /* updated a normal line */
1127#define DID_FOLD 3 /* updated a folded line */
1128 int did_update = DID_NONE;
1129 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1130#endif
1131 linenr_T mod_top = 0;
1132 linenr_T mod_bot = 0;
1133#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1134 int save_got_int;
1135#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001136#ifdef SYN_TIME_LIMIT
1137 proftime_T syntax_tm;
1138#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139
1140 type = wp->w_redr_type;
1141
1142 if (type == NOT_VALID)
1143 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 wp->w_lines_valid = 0;
1146 }
1147
1148 /* Window is zero-height: nothing to draw. */
Bram Moolenaar415a6932017-12-05 20:31:07 +01001149 if (wp->w_height + WINBAR_HEIGHT(wp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 {
1151 wp->w_redr_type = 0;
1152 return;
1153 }
1154
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 /* Window is zero-width: Only need to draw the separator. */
1156 if (wp->w_width == 0)
1157 {
1158 /* draw the vertical separator right of this window */
1159 draw_vsep_win(wp, 0);
1160 wp->w_redr_type = 0;
1161 return;
1162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001164#ifdef FEAT_TERMINAL
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001165 // If this window contains a terminal, redraw works completely differently.
1166 if (term_do_update_window(wp))
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001167 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001168 term_update_window(wp);
Bram Moolenaar181ca992018-02-13 21:19:21 +01001169# ifdef FEAT_MENU
1170 /* Draw the window toolbar, if there is one. */
1171 if (winbar_height(wp) > 0)
1172 redraw_win_toolbar(wp);
1173# endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001174 wp->w_redr_type = 0;
1175 return;
1176 }
1177#endif
1178
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001180 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181#endif
1182
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001183#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001184 /* Force redraw when width of 'number' or 'relativenumber' column
1185 * changes. */
1186 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001187 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001188 {
1189 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001190 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001191 }
1192 else
1193#endif
1194
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1196 {
1197 /*
1198 * When there are both inserted/deleted lines and specific lines to be
1199 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1200 * everything (only happens when redrawing is off for while).
1201 */
1202 type = NOT_VALID;
1203 }
1204 else
1205 {
1206 /*
1207 * Set mod_top to the first line that needs displaying because of
1208 * changes. Set mod_bot to the first line after the changes.
1209 */
1210 mod_top = wp->w_redraw_top;
1211 if (wp->w_redraw_bot != 0)
1212 mod_bot = wp->w_redraw_bot + 1;
1213 else
1214 mod_bot = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 if (buf->b_mod_set)
1216 {
1217 if (mod_top == 0 || mod_top > buf->b_mod_top)
1218 {
1219 mod_top = buf->b_mod_top;
1220#ifdef FEAT_SYN_HL
1221 /* Need to redraw lines above the change that may be included
1222 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001223 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001225 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 if (mod_top < 1)
1227 mod_top = 1;
1228 }
1229#endif
1230 }
1231 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1232 mod_bot = buf->b_mod_bot;
1233
1234#ifdef FEAT_SEARCH_EXTRA
1235 /* When 'hlsearch' is on and using a multi-line search pattern, a
1236 * change in one line may make the Search highlighting in a
1237 * previous line invalid. Simple solution: redraw all visible
1238 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001239 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 */
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 {
1246 cur = wp->w_match_head;
1247 while (cur != NULL)
1248 {
1249 if (cur->match.regprog != NULL
1250 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001251 {
1252 top_to_mod = TRUE;
1253 break;
1254 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001255 cur = cur->next;
1256 }
1257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258#endif
1259 }
1260#ifdef FEAT_FOLDING
1261 if (mod_top != 0 && hasAnyFolding(wp))
1262 {
1263 linenr_T lnumt, lnumb;
1264
1265 /*
1266 * A change in a line can cause lines above it to become folded or
1267 * unfolded. Find the top most buffer line that may be affected.
1268 * If the line was previously folded and displayed, get the first
1269 * line of that fold. If the line is folded now, get the first
1270 * folded line. Use the minimum of these two.
1271 */
1272
1273 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1274 * the line below it. If there is no valid entry, use w_topline.
1275 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1276 * to this line. If there is no valid entry, use MAXLNUM. */
1277 lnumt = wp->w_topline;
1278 lnumb = MAXLNUM;
1279 for (i = 0; i < wp->w_lines_valid; ++i)
1280 if (wp->w_lines[i].wl_valid)
1281 {
1282 if (wp->w_lines[i].wl_lastlnum < mod_top)
1283 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1284 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1285 {
1286 lnumb = wp->w_lines[i].wl_lnum;
1287 /* When there is a fold column it might need updating
1288 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001289 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 ++lnumb;
1291 }
1292 }
1293
1294 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1295 if (mod_top > lnumt)
1296 mod_top = lnumt;
1297
1298 /* Now do the same for the bottom line (one above mod_bot). */
1299 --mod_bot;
1300 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1301 ++mod_bot;
1302 if (mod_bot < lnumb)
1303 mod_bot = lnumb;
1304 }
1305#endif
1306
1307 /* When a change starts above w_topline and the end is below
1308 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001309 * If the end of the change is above w_topline: do like no change was
1310 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 if (mod_top != 0 && mod_top < wp->w_topline)
1312 {
1313 if (mod_bot > wp->w_topline)
1314 mod_top = wp->w_topline;
1315#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001316 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 top_end = 1;
1318#endif
1319 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001320
1321 /* When line numbers are displayed need to redraw all lines below
1322 * inserted/deleted lines. */
1323 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1324 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 }
Bram Moolenaar895d9662019-01-31 21:57:21 +01001326 wp->w_redraw_top = 0; // reset for next time
1327 wp->w_redraw_bot = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328
1329 /*
1330 * When only displaying the lines at the top, set top_end. Used when
1331 * window has scrolled down for msg_scrolled.
1332 */
1333 if (type == REDRAW_TOP)
1334 {
1335 j = 0;
1336 for (i = 0; i < wp->w_lines_valid; ++i)
1337 {
1338 j += wp->w_lines[i].wl_size;
1339 if (j >= wp->w_upd_rows)
1340 {
1341 top_end = j;
1342 break;
1343 }
1344 }
1345 if (top_end == 0)
1346 /* not found (cannot happen?): redraw everything */
1347 type = NOT_VALID;
1348 else
1349 /* top area defined, the rest is VALID */
1350 type = VALID;
1351 }
1352
Bram Moolenaar367329b2007-08-30 11:53:22 +00001353 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001354 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1355 * non-zero and thus not FALSE) will indicate that screenclear() was not
1356 * called. */
1357 if (screen_cleared)
1358 screen_cleared = MAYBE;
1359
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 /*
1361 * If there are no changes on the screen that require a complete redraw,
1362 * handle three cases:
1363 * 1: we are off the top of the screen by a few lines: scroll down
1364 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1365 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1366 * w_lines[] that needs updating.
1367 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001368 if ((type == VALID || type == SOME_VALID
1369 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370#ifdef FEAT_DIFF
1371 && !wp->w_botfill && !wp->w_old_botfill
1372#endif
1373 )
1374 {
1375 if (mod_top != 0 && wp->w_topline == mod_top)
1376 {
1377 /*
1378 * w_topline is the first changed line, the scrolling will be done
1379 * further down.
1380 */
1381 }
1382 else if (wp->w_lines[0].wl_valid
1383 && (wp->w_topline < wp->w_lines[0].wl_lnum
1384#ifdef FEAT_DIFF
1385 || (wp->w_topline == wp->w_lines[0].wl_lnum
1386 && wp->w_topfill > wp->w_old_topfill)
1387#endif
1388 ))
1389 {
1390 /*
1391 * New topline is above old topline: May scroll down.
1392 */
1393#ifdef FEAT_FOLDING
1394 if (hasAnyFolding(wp))
1395 {
1396 linenr_T ln;
1397
1398 /* count the number of lines we are off, counting a sequence
1399 * of folded lines as one */
1400 j = 0;
1401 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1402 {
1403 ++j;
1404 if (j >= wp->w_height - 2)
1405 break;
1406 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1407 }
1408 }
1409 else
1410#endif
1411 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1412 if (j < wp->w_height - 2) /* not too far off */
1413 {
1414 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1415#ifdef FEAT_DIFF
1416 /* insert extra lines for previously invisible filler lines */
1417 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1418 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1419 - wp->w_old_topfill;
1420#endif
1421 if (i < wp->w_height - 2) /* less than a screen off */
1422 {
1423 /*
1424 * Try to insert the correct number of lines.
1425 * If not the last window, delete the lines at the bottom.
1426 * win_ins_lines may fail when the terminal can't do it.
1427 */
1428 if (i > 0)
1429 check_for_delay(FALSE);
1430 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1431 {
1432 if (wp->w_lines_valid != 0)
1433 {
1434 /* Need to update rows that are new, stop at the
1435 * first one that scrolled down. */
1436 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438
1439 /* Move the entries that were scrolled, disable
1440 * the entries for the lines to be redrawn. */
1441 if ((wp->w_lines_valid += j) > wp->w_height)
1442 wp->w_lines_valid = wp->w_height;
1443 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1444 wp->w_lines[idx] = wp->w_lines[idx - j];
1445 while (idx >= 0)
1446 wp->w_lines[idx--].wl_valid = FALSE;
1447 }
1448 }
1449 else
1450 mid_start = 0; /* redraw all lines */
1451 }
1452 else
1453 mid_start = 0; /* redraw all lines */
1454 }
1455 else
1456 mid_start = 0; /* redraw all lines */
1457 }
1458 else
1459 {
1460 /*
1461 * New topline is at or below old topline: May scroll up.
1462 * When topline didn't change, find first entry in w_lines[] that
1463 * needs updating.
1464 */
1465
1466 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1467 j = -1;
1468 row = 0;
1469 for (i = 0; i < wp->w_lines_valid; i++)
1470 {
1471 if (wp->w_lines[i].wl_valid
1472 && wp->w_lines[i].wl_lnum == wp->w_topline)
1473 {
1474 j = i;
1475 break;
1476 }
1477 row += wp->w_lines[i].wl_size;
1478 }
1479 if (j == -1)
1480 {
1481 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1482 * lines */
1483 mid_start = 0;
1484 }
1485 else
1486 {
1487 /*
1488 * Try to delete the correct number of lines.
1489 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1490 */
1491#ifdef FEAT_DIFF
1492 /* If the topline didn't change, delete old filler lines,
1493 * otherwise delete filler lines of the new topline... */
1494 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1495 row += wp->w_old_topfill;
1496 else
1497 row += diff_check_fill(wp, wp->w_topline);
1498 /* ... but don't delete new filler lines. */
1499 row -= wp->w_topfill;
1500#endif
1501 if (row > 0)
1502 {
1503 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001504 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1505 == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 bot_start = wp->w_height - row;
1507 else
1508 mid_start = 0; /* redraw all lines */
1509 }
1510 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1511 {
1512 /*
1513 * Skip the lines (below the deleted lines) that are still
1514 * valid and don't need redrawing. Copy their info
1515 * upwards, to compensate for the deleted lines. Set
1516 * bot_start to the first row that needs redrawing.
1517 */
1518 bot_start = 0;
1519 idx = 0;
1520 for (;;)
1521 {
1522 wp->w_lines[idx] = wp->w_lines[j];
1523 /* stop at line that didn't fit, unless it is still
1524 * valid (no lines deleted) */
1525 if (row > 0 && bot_start + row
1526 + (int)wp->w_lines[j].wl_size > wp->w_height)
1527 {
1528 wp->w_lines_valid = idx + 1;
1529 break;
1530 }
1531 bot_start += wp->w_lines[idx++].wl_size;
1532
1533 /* stop at the last valid entry in w_lines[].wl_size */
1534 if (++j >= wp->w_lines_valid)
1535 {
1536 wp->w_lines_valid = idx;
1537 break;
1538 }
1539 }
1540#ifdef FEAT_DIFF
1541 /* Correct the first entry for filler lines at the top
1542 * when it won't get updated below. */
1543 if (wp->w_p_diff && bot_start > 0)
1544 wp->w_lines[0].wl_size =
1545 plines_win_nofill(wp, wp->w_topline, TRUE)
1546 + wp->w_topfill;
1547#endif
1548 }
1549 }
1550 }
1551
1552 /* When starting redraw in the first line, redraw all lines. When
1553 * there is only one window it's probably faster to clear the screen
1554 * first. */
1555 if (mid_start == 0)
1556 {
1557 mid_end = wp->w_height;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001558 if (ONE_WINDOW)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001559 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001560 /* Clear the screen when it was not done by win_del_lines() or
1561 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1562 * then. */
1563 if (screen_cleared != TRUE)
1564 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001565 /* The screen was cleared, redraw the tab pages line. */
1566 if (redraw_tabline)
1567 draw_tabline();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001570
1571 /* When win_del_lines() or win_ins_lines() caused the screen to be
1572 * cleared (only happens for the first window) or when screenclear()
1573 * was called directly above, "must_redraw" will have been set to
1574 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1575 if (screen_cleared == TRUE)
1576 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 }
1578 else
1579 {
1580 /* Not VALID or INVERTED: redraw all lines. */
1581 mid_start = 0;
1582 mid_end = wp->w_height;
1583 }
1584
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001585 if (type == SOME_VALID)
1586 {
1587 /* SOME_VALID: redraw all lines. */
1588 mid_start = 0;
1589 mid_end = wp->w_height;
1590 type = NOT_VALID;
1591 }
1592
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 /* check if we are updating or removing the inverted part */
1594 if ((VIsual_active && buf == curwin->w_buffer)
1595 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1596 {
1597 linenr_T from, to;
1598
1599 if (VIsual_active)
1600 {
1601 if (VIsual_active
1602 && (VIsual_mode != wp->w_old_visual_mode
1603 || type == INVERTED_ALL))
1604 {
1605 /*
1606 * If the type of Visual selection changed, redraw the whole
1607 * selection. Also when the ownership of the X selection is
1608 * gained or lost.
1609 */
1610 if (curwin->w_cursor.lnum < VIsual.lnum)
1611 {
1612 from = curwin->w_cursor.lnum;
1613 to = VIsual.lnum;
1614 }
1615 else
1616 {
1617 from = VIsual.lnum;
1618 to = curwin->w_cursor.lnum;
1619 }
1620 /* redraw more when the cursor moved as well */
1621 if (wp->w_old_cursor_lnum < from)
1622 from = wp->w_old_cursor_lnum;
1623 if (wp->w_old_cursor_lnum > to)
1624 to = wp->w_old_cursor_lnum;
1625 if (wp->w_old_visual_lnum < from)
1626 from = wp->w_old_visual_lnum;
1627 if (wp->w_old_visual_lnum > to)
1628 to = wp->w_old_visual_lnum;
1629 }
1630 else
1631 {
1632 /*
1633 * Find the line numbers that need to be updated: The lines
1634 * between the old cursor position and the current cursor
1635 * position. Also check if the Visual position changed.
1636 */
1637 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1638 {
1639 from = curwin->w_cursor.lnum;
1640 to = wp->w_old_cursor_lnum;
1641 }
1642 else
1643 {
1644 from = wp->w_old_cursor_lnum;
1645 to = curwin->w_cursor.lnum;
1646 if (from == 0) /* Visual mode just started */
1647 from = to;
1648 }
1649
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001650 if (VIsual.lnum != wp->w_old_visual_lnum
1651 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 {
1653 if (wp->w_old_visual_lnum < from
1654 && wp->w_old_visual_lnum != 0)
1655 from = wp->w_old_visual_lnum;
1656 if (wp->w_old_visual_lnum > to)
1657 to = wp->w_old_visual_lnum;
1658 if (VIsual.lnum < from)
1659 from = VIsual.lnum;
1660 if (VIsual.lnum > to)
1661 to = VIsual.lnum;
1662 }
1663 }
1664
1665 /*
1666 * If in block mode and changed column or curwin->w_curswant:
1667 * update all lines.
1668 * First compute the actual start and end column.
1669 */
1670 if (VIsual_mode == Ctrl_V)
1671 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001672 colnr_T fromc, toc;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001673#if defined(FEAT_LINEBREAK)
Bram Moolenaar404406a2014-10-09 13:24:43 +02001674 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675
Bram Moolenaar404406a2014-10-09 13:24:43 +02001676 if (curwin->w_p_lbr)
1677 ve_flags = VE_ALL;
1678#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001680#if defined(FEAT_LINEBREAK)
Bram Moolenaar404406a2014-10-09 13:24:43 +02001681 ve_flags = save_ve_flags;
1682#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 ++toc;
1684 if (curwin->w_curswant == MAXCOL)
1685 toc = MAXCOL;
1686
1687 if (fromc != wp->w_old_cursor_fcol
1688 || toc != wp->w_old_cursor_lcol)
1689 {
1690 if (from > VIsual.lnum)
1691 from = VIsual.lnum;
1692 if (to < VIsual.lnum)
1693 to = VIsual.lnum;
1694 }
1695 wp->w_old_cursor_fcol = fromc;
1696 wp->w_old_cursor_lcol = toc;
1697 }
1698 }
1699 else
1700 {
1701 /* Use the line numbers of the old Visual area. */
1702 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1703 {
1704 from = wp->w_old_cursor_lnum;
1705 to = wp->w_old_visual_lnum;
1706 }
1707 else
1708 {
1709 from = wp->w_old_visual_lnum;
1710 to = wp->w_old_cursor_lnum;
1711 }
1712 }
1713
1714 /*
1715 * There is no need to update lines above the top of the window.
1716 */
1717 if (from < wp->w_topline)
1718 from = wp->w_topline;
1719
1720 /*
1721 * If we know the value of w_botline, use it to restrict the update to
1722 * the lines that are visible in the window.
1723 */
1724 if (wp->w_valid & VALID_BOTLINE)
1725 {
1726 if (from >= wp->w_botline)
1727 from = wp->w_botline - 1;
1728 if (to >= wp->w_botline)
1729 to = wp->w_botline - 1;
1730 }
1731
1732 /*
1733 * Find the minimal part to be updated.
1734 * Watch out for scrolling that made entries in w_lines[] invalid.
1735 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1736 * top_end; need to redraw from top_end to the "to" line.
1737 * A middle mouse click with a Visual selection may change the text
1738 * above the Visual area and reset wl_valid, do count these for
1739 * mid_end (in srow).
1740 */
1741 if (mid_start > 0)
1742 {
1743 lnum = wp->w_topline;
1744 idx = 0;
1745 srow = 0;
1746 if (scrolled_down)
1747 mid_start = top_end;
1748 else
1749 mid_start = 0;
1750 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1751 {
1752 if (wp->w_lines[idx].wl_valid)
1753 mid_start += wp->w_lines[idx].wl_size;
1754 else if (!scrolled_down)
1755 srow += wp->w_lines[idx].wl_size;
1756 ++idx;
1757# ifdef FEAT_FOLDING
1758 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1759 lnum = wp->w_lines[idx].wl_lnum;
1760 else
1761# endif
1762 ++lnum;
1763 }
1764 srow += mid_start;
1765 mid_end = wp->w_height;
1766 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1767 {
1768 if (wp->w_lines[idx].wl_valid
1769 && wp->w_lines[idx].wl_lnum >= to + 1)
1770 {
1771 /* Only update until first row of this line */
1772 mid_end = srow;
1773 break;
1774 }
1775 srow += wp->w_lines[idx].wl_size;
1776 }
1777 }
1778 }
1779
1780 if (VIsual_active && buf == curwin->w_buffer)
1781 {
1782 wp->w_old_visual_mode = VIsual_mode;
1783 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1784 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001785 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 wp->w_old_curswant = curwin->w_curswant;
1787 }
1788 else
1789 {
1790 wp->w_old_visual_mode = 0;
1791 wp->w_old_cursor_lnum = 0;
1792 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001793 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795
1796#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1797 /* reset got_int, otherwise regexp won't work */
1798 save_got_int = got_int;
1799 got_int = 0;
1800#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001801#ifdef SYN_TIME_LIMIT
1802 /* Set the time limit to 'redrawtime'. */
1803 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02001804 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001805#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806#ifdef FEAT_FOLDING
1807 win_foldinfo.fi_level = 0;
1808#endif
1809
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001810#ifdef FEAT_MENU
1811 /*
1812 * Draw the window toolbar, if there is one.
1813 * TODO: only when needed.
1814 */
1815 if (winbar_height(wp) > 0)
1816 redraw_win_toolbar(wp);
1817#endif
1818
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 /*
1820 * Update all the window rows.
1821 */
1822 idx = 0; /* first entry in w_lines[].wl_size */
1823 row = 0;
1824 srow = 0;
1825 lnum = wp->w_topline; /* first line shown in window */
1826 for (;;)
1827 {
1828 /* stop updating when reached the end of the window (check for _past_
1829 * the end of the window is at the end of the loop) */
1830 if (row == wp->w_height)
1831 {
1832 didline = TRUE;
1833 break;
1834 }
1835
1836 /* stop updating when hit the end of the file */
1837 if (lnum > buf->b_ml.ml_line_count)
1838 {
1839 eof = TRUE;
1840 break;
1841 }
1842
1843 /* Remember the starting row of the line that is going to be dealt
1844 * with. It is used further down when the line doesn't fit. */
1845 srow = row;
1846
1847 /*
1848 * Update a line when it is in an area that needs updating, when it
1849 * has changes or w_lines[idx] is invalid.
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02001850 * "bot_start" may be halfway a wrapped line after using
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 * win_del_lines(), check if the current line includes it.
1852 * When syntax folding is being used, the saved syntax states will
1853 * already have been updated, we can't see where the syntax state is
1854 * the same again, just update until the end of the window.
1855 */
1856 if (row < top_end
1857 || (row >= mid_start && row < mid_end)
1858#ifdef FEAT_SEARCH_EXTRA
1859 || top_to_mod
1860#endif
1861 || idx >= wp->w_lines_valid
1862 || (row + wp->w_lines[idx].wl_size > bot_start)
1863 || (mod_top != 0
1864 && (lnum == mod_top
1865 || (lnum >= mod_top
1866 && (lnum < mod_bot
1867#ifdef FEAT_SYN_HL
1868 || did_update == DID_FOLD
1869 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001870 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 && (
1872# ifdef FEAT_FOLDING
1873 (foldmethodIsSyntax(wp)
1874 && hasAnyFolding(wp)) ||
1875# endif
1876 syntax_check_changed(lnum)))
1877#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001878#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001879 /* match in fixed position might need redraw
1880 * if lines were inserted or deleted */
1881 || (wp->w_match_head != NULL
1882 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001883#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 )))))
1885 {
1886#ifdef FEAT_SEARCH_EXTRA
1887 if (lnum == mod_top)
1888 top_to_mod = FALSE;
1889#endif
1890
1891 /*
1892 * When at start of changed lines: May scroll following lines
1893 * up or down to minimize redrawing.
1894 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001895 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 */
1897 if (lnum == mod_top
1898 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001899 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 {
1901 int old_rows = 0;
1902 int new_rows = 0;
1903 int xtra_rows;
1904 linenr_T l;
1905
1906 /* Count the old number of window rows, using w_lines[], which
1907 * should still contain the sizes for the lines as they are
1908 * currently displayed. */
1909 for (i = idx; i < wp->w_lines_valid; ++i)
1910 {
1911 /* Only valid lines have a meaningful wl_lnum. Invalid
1912 * lines are part of the changed area. */
1913 if (wp->w_lines[i].wl_valid
1914 && wp->w_lines[i].wl_lnum == mod_bot)
1915 break;
1916 old_rows += wp->w_lines[i].wl_size;
1917#ifdef FEAT_FOLDING
1918 if (wp->w_lines[i].wl_valid
1919 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1920 {
1921 /* Must have found the last valid entry above mod_bot.
1922 * Add following invalid entries. */
1923 ++i;
1924 while (i < wp->w_lines_valid
1925 && !wp->w_lines[i].wl_valid)
1926 old_rows += wp->w_lines[i++].wl_size;
1927 break;
1928 }
1929#endif
1930 }
1931
1932 if (i >= wp->w_lines_valid)
1933 {
1934 /* We can't find a valid line below the changed lines,
1935 * need to redraw until the end of the window.
1936 * Inserting/deleting lines has no use. */
1937 bot_start = 0;
1938 }
1939 else
1940 {
1941 /* Able to count old number of rows: Count new window
1942 * rows, and may insert/delete lines */
1943 j = idx;
1944 for (l = lnum; l < mod_bot; ++l)
1945 {
1946#ifdef FEAT_FOLDING
1947 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1948 ++new_rows;
1949 else
1950#endif
1951#ifdef FEAT_DIFF
1952 if (l == wp->w_topline)
1953 new_rows += plines_win_nofill(wp, l, TRUE)
1954 + wp->w_topfill;
1955 else
1956#endif
1957 new_rows += plines_win(wp, l, TRUE);
1958 ++j;
1959 if (new_rows > wp->w_height - row - 2)
1960 {
1961 /* it's getting too much, must redraw the rest */
1962 new_rows = 9999;
1963 break;
1964 }
1965 }
1966 xtra_rows = new_rows - old_rows;
1967 if (xtra_rows < 0)
1968 {
1969 /* May scroll text up. If there is not enough
1970 * remaining text or scrolling fails, must redraw the
1971 * rest. If scrolling works, must redraw the text
1972 * below the scrolled text. */
1973 if (row - xtra_rows >= wp->w_height - 2)
1974 mod_bot = MAXLNUM;
1975 else
1976 {
1977 check_for_delay(FALSE);
1978 if (win_del_lines(wp, row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001979 -xtra_rows, FALSE, FALSE, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 mod_bot = MAXLNUM;
1981 else
1982 bot_start = wp->w_height + xtra_rows;
1983 }
1984 }
1985 else if (xtra_rows > 0)
1986 {
1987 /* May scroll text down. If there is not enough
1988 * remaining text of scrolling fails, must redraw the
1989 * rest. */
1990 if (row + xtra_rows >= wp->w_height - 2)
1991 mod_bot = MAXLNUM;
1992 else
1993 {
1994 check_for_delay(FALSE);
1995 if (win_ins_lines(wp, row + old_rows,
1996 xtra_rows, FALSE, FALSE) == FAIL)
1997 mod_bot = MAXLNUM;
1998 else if (top_end > row + old_rows)
1999 /* Scrolled the part at the top that requires
2000 * updating down. */
2001 top_end += xtra_rows;
2002 }
2003 }
2004
2005 /* When not updating the rest, may need to move w_lines[]
2006 * entries. */
2007 if (mod_bot != MAXLNUM && i != j)
2008 {
2009 if (j < i)
2010 {
2011 int x = row + new_rows;
2012
2013 /* move entries in w_lines[] upwards */
2014 for (;;)
2015 {
2016 /* stop at last valid entry in w_lines[] */
2017 if (i >= wp->w_lines_valid)
2018 {
2019 wp->w_lines_valid = j;
2020 break;
2021 }
2022 wp->w_lines[j] = wp->w_lines[i];
2023 /* stop at a line that won't fit */
2024 if (x + (int)wp->w_lines[j].wl_size
2025 > wp->w_height)
2026 {
2027 wp->w_lines_valid = j + 1;
2028 break;
2029 }
2030 x += wp->w_lines[j++].wl_size;
2031 ++i;
2032 }
2033 if (bot_start > x)
2034 bot_start = x;
2035 }
2036 else /* j > i */
2037 {
2038 /* move entries in w_lines[] downwards */
2039 j -= i;
2040 wp->w_lines_valid += j;
2041 if (wp->w_lines_valid > wp->w_height)
2042 wp->w_lines_valid = wp->w_height;
2043 for (i = wp->w_lines_valid; i - j >= idx; --i)
2044 wp->w_lines[i] = wp->w_lines[i - j];
2045
2046 /* The w_lines[] entries for inserted lines are
2047 * now invalid, but wl_size may be used above.
2048 * Reset to zero. */
2049 while (i >= idx)
2050 {
2051 wp->w_lines[i].wl_size = 0;
2052 wp->w_lines[i--].wl_valid = FALSE;
2053 }
2054 }
2055 }
2056 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 }
2058
2059#ifdef FEAT_FOLDING
2060 /*
2061 * When lines are folded, display one line for all of them.
2062 * Otherwise, display normally (can be several display lines when
2063 * 'wrap' is on).
2064 */
2065 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2066 if (fold_count != 0)
2067 {
2068 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2069 ++row;
2070 --fold_count;
2071 wp->w_lines[idx].wl_folded = TRUE;
2072 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2073# ifdef FEAT_SYN_HL
2074 did_update = DID_FOLD;
2075# endif
2076 }
2077 else
2078#endif
2079 if (idx < wp->w_lines_valid
2080 && wp->w_lines[idx].wl_valid
2081 && wp->w_lines[idx].wl_lnum == lnum
2082 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002083 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar17146962019-05-30 00:12:11 +02002084#ifdef FEAT_TEXT_PROP
2085 && !bt_popup(wp->w_buffer)
2086#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 && srow + wp->w_lines[idx].wl_size > wp->w_height
2088#ifdef FEAT_DIFF
2089 && diff_check_fill(wp, lnum) == 0
2090#endif
2091 )
2092 {
2093 /* This line is not going to fit. Don't draw anything here,
2094 * will draw "@ " lines below. */
2095 row = wp->w_height + 1;
2096 }
2097 else
2098 {
2099#ifdef FEAT_SEARCH_EXTRA
2100 prepare_search_hl(wp, lnum);
2101#endif
2102#ifdef FEAT_SYN_HL
2103 /* Let the syntax stuff know we skipped a few lines. */
2104 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002105 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 syntax_end_parsing(syntax_last_parsed + 1);
2107#endif
2108
2109 /*
2110 * Display one line.
2111 */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002112 row = win_line(wp, lnum, srow, wp->w_height,
2113 mod_top == 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114
2115#ifdef FEAT_FOLDING
2116 wp->w_lines[idx].wl_folded = FALSE;
2117 wp->w_lines[idx].wl_lastlnum = lnum;
2118#endif
2119#ifdef FEAT_SYN_HL
2120 did_update = DID_LINE;
2121 syntax_last_parsed = lnum;
2122#endif
2123 }
2124
2125 wp->w_lines[idx].wl_lnum = lnum;
2126 wp->w_lines[idx].wl_valid = TRUE;
Bram Moolenaar0e19fc02017-10-28 14:45:16 +02002127
2128 /* Past end of the window or end of the screen. Note that after
2129 * resizing wp->w_height may be end up too big. That's a problem
2130 * elsewhere, but prevent a crash here. */
2131 if (row > wp->w_height || row + wp->w_winrow >= Rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 {
2133 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002134 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2136 ++idx;
2137 break;
2138 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002139 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 wp->w_lines[idx].wl_size = row - srow;
2141 ++idx;
2142#ifdef FEAT_FOLDING
2143 lnum += fold_count + 1;
2144#else
2145 ++lnum;
2146#endif
2147 }
2148 else
2149 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002150 if (wp->w_p_rnu)
2151 {
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002152#ifdef FEAT_FOLDING
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002153 // 'relativenumber' set: The text doesn't need to be drawn, but
2154 // the number column nearly always does.
Bram Moolenaar7701f302018-10-02 21:20:32 +02002155 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2156 if (fold_count != 0)
Bram Moolenaar7701f302018-10-02 21:20:32 +02002157 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
Bram Moolenaar7701f302018-10-02 21:20:32 +02002158 else
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002159#endif
Bram Moolenaar7701f302018-10-02 21:20:32 +02002160 (void)win_line(wp, lnum, srow, wp->w_height, TRUE, TRUE);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002161 }
2162
2163 // This line does not need to be drawn, advance to the next one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 row += wp->w_lines[idx++].wl_size;
2165 if (row > wp->w_height) /* past end of screen */
2166 break;
2167#ifdef FEAT_FOLDING
2168 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2169#else
2170 ++lnum;
2171#endif
2172#ifdef FEAT_SYN_HL
2173 did_update = DID_NONE;
2174#endif
2175 }
2176
2177 if (lnum > buf->b_ml.ml_line_count)
2178 {
2179 eof = TRUE;
2180 break;
2181 }
2182 }
2183 /*
2184 * End of loop over all window lines.
2185 */
2186
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002187#ifdef FEAT_VTP
2188 /* Rewrite the character at the end of the screen line. */
2189 if (use_vtp())
2190 {
2191 int i;
2192
2193 for (i = 0; i < Rows; ++i)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002194 if (enc_utf8)
2195 if ((*mb_off2cells)(LineOffset[i] + Columns - 2,
2196 LineOffset[i] + screen_Columns) > 1)
2197 screen_draw_rectangle(i, Columns - 2, 1, 2, FALSE);
2198 else
2199 screen_draw_rectangle(i, Columns - 1, 1, 1, FALSE);
2200 else
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002201 screen_char(LineOffset[i] + Columns - 1, i, Columns - 1);
2202 }
2203#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204
2205 if (idx > wp->w_lines_valid)
2206 wp->w_lines_valid = idx;
2207
2208#ifdef FEAT_SYN_HL
2209 /*
2210 * Let the syntax stuff know we stop parsing here.
2211 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002212 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 syntax_end_parsing(syntax_last_parsed + 1);
2214#endif
2215
2216 /*
2217 * If we didn't hit the end of the file, and we didn't finish the last
2218 * line we were working on, then the line didn't fit.
2219 */
2220 wp->w_empty_rows = 0;
2221#ifdef FEAT_DIFF
2222 wp->w_filler_rows = 0;
2223#endif
2224 if (!eof && !didline)
2225 {
2226 if (lnum == wp->w_topline)
2227 {
2228 /*
2229 * Single line that does not fit!
2230 * Don't overwrite it, it can be edited.
2231 */
2232 wp->w_botline = lnum + 1;
2233 }
2234#ifdef FEAT_DIFF
2235 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2236 {
2237 /* Window ends in filler lines. */
2238 wp->w_botline = lnum;
2239 wp->w_filler_rows = wp->w_height - srow;
2240 }
2241#endif
Bram Moolenaar17146962019-05-30 00:12:11 +02002242#ifdef FEAT_TEXT_PROP
2243 else if (bt_popup(wp->w_buffer))
2244 {
2245 // popup line that doesn't fit is left as-is
2246 wp->w_botline = lnum;
2247 }
2248#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002249 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2250 {
2251 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2252
2253 /*
2254 * Last line isn't finished: Display "@@@" in the last screen line.
2255 */
Bram Moolenaar53f81742017-09-22 14:35:51 +02002256 screen_puts_len((char_u *)"@@", 2, scr_row, wp->w_wincol,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002257 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002258 screen_fill(scr_row, scr_row + 1,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002259 (int)wp->w_wincol + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002260 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002261 set_empty_rows(wp, srow);
2262 wp->w_botline = lnum;
2263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2265 {
2266 /*
2267 * Last line isn't finished: Display "@@@" at the end.
2268 */
2269 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2270 W_WINROW(wp) + wp->w_height,
2271 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002272 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 set_empty_rows(wp, srow);
2274 wp->w_botline = lnum;
2275 }
2276 else
2277 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002278 win_draw_end(wp, '@', ' ', TRUE, srow, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 wp->w_botline = lnum;
2280 }
2281 }
2282 else
2283 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 if (eof) /* we hit the end of the file */
2286 {
2287 wp->w_botline = buf->b_ml.ml_line_count + 1;
2288#ifdef FEAT_DIFF
2289 j = diff_check_fill(wp, wp->w_botline);
2290 if (j > 0 && !wp->w_botfill)
2291 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002292 // Display filler lines at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 if (char2cells(fill_diff) > 1)
2294 i = '-';
2295 else
2296 i = fill_diff;
2297 if (row + j > wp->w_height)
2298 j = wp->w_height - row;
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002299 win_draw_end(wp, i, i, TRUE, row, row + (int)j, HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 row += j;
2301 }
2302#endif
2303 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002304 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 wp->w_botline = lnum;
2306
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002307 // Make sure the rest of the screen is blank
2308 // put '~'s on rows that aren't part of the file.
Bram Moolenaar17146962019-05-30 00:12:11 +02002309 win_draw_end(wp,
2310#ifdef FEAT_TEXT_PROP
2311 bt_popup(wp->w_buffer) ? ' ' :
2312#endif
2313 '~', ' ', FALSE, row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 }
2315
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002316#ifdef SYN_TIME_LIMIT
2317 syn_set_timeout(NULL);
2318#endif
2319
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 /* Reset the type of redrawing required, the window has been updated. */
2321 wp->w_redr_type = 0;
2322#ifdef FEAT_DIFF
2323 wp->w_old_topfill = wp->w_topfill;
2324 wp->w_old_botfill = wp->w_botfill;
2325#endif
2326
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002327 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 {
2329 /*
2330 * There is a trick with w_botline. If we invalidate it on each
2331 * change that might modify it, this will cause a lot of expensive
2332 * calls to plines() in update_topline() each time. Therefore the
2333 * value of w_botline is often approximated, and this value is used to
2334 * compute the value of w_topline. If the value of w_botline was
2335 * wrong, check that the value of w_topline is correct (cursor is on
2336 * the visible part of the text). If it's not, we need to redraw
2337 * again. Mostly this just means scrolling up a few lines, so it
2338 * doesn't look too bad. Only do this for the current window (where
2339 * changes are relevant).
2340 */
2341 wp->w_valid |= VALID_BOTLINE;
2342 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2343 {
2344 recursive = TRUE;
2345 curwin->w_valid &= ~VALID_TOPLINE;
2346 update_topline(); /* may invalidate w_botline again */
2347 if (must_redraw != 0)
2348 {
2349 /* Don't update for changes in buffer again. */
2350 i = curbuf->b_mod_set;
2351 curbuf->b_mod_set = FALSE;
2352 win_update(curwin);
2353 must_redraw = 0;
2354 curbuf->b_mod_set = i;
2355 }
2356 recursive = FALSE;
2357 }
2358 }
2359
2360#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2361 /* restore got_int, unless CTRL-C was hit while redrawing */
2362 if (!got_int)
2363 got_int = save_got_int;
2364#endif
2365}
2366
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367/*
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002368 * Call screen_fill() with the columns adjusted for 'rightleft' if needed.
2369 * Return the new offset.
2370 */
2371 static int
2372screen_fill_end(
2373 win_T *wp,
2374 int c1,
2375 int c2,
2376 int off,
2377 int width,
2378 int row,
2379 int endrow,
2380 int attr)
2381{
2382 int nn = off + width;
2383
2384 if (nn > wp->w_width)
2385 nn = wp->w_width;
2386#ifdef FEAT_RIGHTLEFT
2387 if (wp->w_p_rl)
2388 {
2389 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2390 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - off,
2391 c1, c2, attr);
2392 }
2393 else
2394#endif
2395 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2396 wp->w_wincol + off, (int)wp->w_wincol + nn,
2397 c1, c2, attr);
2398 return nn;
2399}
2400
2401/*
2402 * Clear lines near the end the window and mark the unused lines with "c1".
2403 * use "c2" as the filler character.
2404 * When "draw_margin" is TRUE then draw the sign, fold and number columns.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 */
2406 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002407win_draw_end(
2408 win_T *wp,
2409 int c1,
2410 int c2,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002411 int draw_margin,
Bram Moolenaar05540972016-01-30 20:31:25 +01002412 int row,
2413 int endrow,
2414 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 int n = 0;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002417 int attr = HL_ATTR(hl);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002418 int wcr_attr = get_wcr_attr(wp);
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002419
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002420 attr = hl_combine_attr(wcr_attr, attr);
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002421
2422 if (draw_margin)
2423 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002424#ifdef FEAT_FOLDING
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002425 int fdc = compute_foldcolumn(wp, 0);
2426
2427 if (fdc > 0)
2428 // draw the fold column
2429 n = screen_fill_end(wp, ' ', ' ', n, fdc,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002430 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_FC)));
Bram Moolenaar1c934292015-01-27 16:39:29 +01002431#endif
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002432#ifdef FEAT_SIGNS
2433 if (signcolumn_on(wp))
2434 // draw the sign column
2435 n = screen_fill_end(wp, ' ', ' ', n, 2,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002436 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_SC)));
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002437#endif
2438 if ((wp->w_p_nu || wp->w_p_rnu)
2439 && vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
2440 // draw the number column
2441 n = screen_fill_end(wp, ' ', ' ', n, number_width(wp) + 1,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002442 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_N)));
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444
2445#ifdef FEAT_RIGHTLEFT
2446 if (wp->w_p_rl)
2447 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002449 wp->w_wincol, W_ENDCOL(wp) - 1 - n,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002450 c2, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002452 W_ENDCOL(wp) - 1 - n, W_ENDCOL(wp) - n,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002453 c1, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 }
2455 else
2456#endif
2457 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002459 wp->w_wincol + n, (int)W_ENDCOL(wp),
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002460 c1, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 }
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002462
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 set_empty_rows(wp, row);
2464}
2465
Bram Moolenaar1a384422010-07-14 19:53:30 +02002466#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02002467/*
2468 * Advance **color_cols and return TRUE when there are columns to draw.
2469 */
2470 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002471advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002472{
2473 while (**color_cols >= 0 && vcol > **color_cols)
2474 ++*color_cols;
2475 return (**color_cols >= 0);
2476}
2477#endif
2478
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002479#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
2480/*
2481 * Copy "text" to ScreenLines using "attr".
2482 * Returns the next screen column.
2483 */
2484 static int
2485text_to_screenline(win_T *wp, char_u *text, int col)
2486{
2487 int off = (int)(current_ScreenLine - ScreenLines);
2488
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002489 if (has_mbyte)
2490 {
2491 int cells;
2492 int u8c, u8cc[MAX_MCO];
2493 int i;
2494 int idx;
2495 int c_len;
2496 char_u *p;
2497# ifdef FEAT_ARABIC
2498 int prev_c = 0; /* previous Arabic character */
2499 int prev_c1 = 0; /* first composing char for prev_c */
2500# endif
2501
2502# ifdef FEAT_RIGHTLEFT
2503 if (wp->w_p_rl)
2504 idx = off;
2505 else
2506# endif
2507 idx = off + col;
2508
2509 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2510 for (p = text; *p != NUL; )
2511 {
2512 cells = (*mb_ptr2cells)(p);
2513 c_len = (*mb_ptr2len)(p);
Bram Moolenaar02631462017-09-22 15:20:32 +02002514 if (col + cells > wp->w_width
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002515# ifdef FEAT_RIGHTLEFT
2516 - (wp->w_p_rl ? col : 0)
2517# endif
2518 )
2519 break;
2520 ScreenLines[idx] = *p;
2521 if (enc_utf8)
2522 {
2523 u8c = utfc_ptr2char(p, u8cc);
2524 if (*p < 0x80 && u8cc[0] == 0)
2525 {
2526 ScreenLinesUC[idx] = 0;
2527#ifdef FEAT_ARABIC
2528 prev_c = u8c;
2529#endif
2530 }
2531 else
2532 {
2533#ifdef FEAT_ARABIC
2534 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2535 {
2536 /* Do Arabic shaping. */
2537 int pc, pc1, nc;
2538 int pcc[MAX_MCO];
2539 int firstbyte = *p;
2540
2541 /* The idea of what is the previous and next
2542 * character depends on 'rightleft'. */
2543 if (wp->w_p_rl)
2544 {
2545 pc = prev_c;
2546 pc1 = prev_c1;
2547 nc = utf_ptr2char(p + c_len);
2548 prev_c1 = u8cc[0];
2549 }
2550 else
2551 {
2552 pc = utfc_ptr2char(p + c_len, pcc);
2553 nc = prev_c;
2554 pc1 = pcc[0];
2555 }
2556 prev_c = u8c;
2557
2558 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
2559 pc, pc1, nc);
2560 ScreenLines[idx] = firstbyte;
2561 }
2562 else
2563 prev_c = u8c;
2564#endif
2565 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar9ba61172019-01-24 18:20:17 +01002566 ScreenLinesUC[idx] = u8c;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002567 for (i = 0; i < Screen_mco; ++i)
2568 {
2569 ScreenLinesC[i][idx] = u8cc[i];
2570 if (u8cc[i] == 0)
2571 break;
2572 }
2573 }
2574 if (cells > 1)
2575 ScreenLines[idx + 1] = 0;
2576 }
2577 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2578 /* double-byte single width character */
2579 ScreenLines2[idx] = p[1];
2580 else if (cells > 1)
2581 /* double-width character */
2582 ScreenLines[idx + 1] = p[1];
2583 col += cells;
2584 idx += cells;
2585 p += c_len;
2586 }
2587 }
2588 else
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002589 {
2590 int len = (int)STRLEN(text);
2591
Bram Moolenaar02631462017-09-22 15:20:32 +02002592 if (len > wp->w_width - col)
2593 len = wp->w_width - col;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002594 if (len > 0)
2595 {
2596#ifdef FEAT_RIGHTLEFT
2597 if (wp->w_p_rl)
Bram Moolenaarc6663882019-02-22 19:14:54 +01002598 mch_memmove(current_ScreenLine, text, len);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002599 else
2600#endif
Bram Moolenaarc6663882019-02-22 19:14:54 +01002601 mch_memmove(current_ScreenLine + col, text, len);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002602 col += len;
2603 }
2604 }
2605 return col;
2606}
2607#endif
2608
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609#ifdef FEAT_FOLDING
2610/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002611 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2612 * space is available for window "wp", minus "col".
2613 */
2614 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002615compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002616{
2617 int fdc = wp->w_p_fdc;
2618 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
Bram Moolenaar02631462017-09-22 15:20:32 +02002619 int wwidth = wp->w_width;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002620
2621 if (fdc > wwidth - (col + wmw))
2622 fdc = wwidth - (col + wmw);
2623 return fdc;
2624}
2625
2626/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 * Display one folded line.
2628 */
2629 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002630fold_line(
2631 win_T *wp,
2632 long fold_count,
2633 foldinfo_T *foldinfo,
2634 linenr_T lnum,
2635 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002637 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 pos_T *top, *bot;
2639 linenr_T lnume = lnum + fold_count - 1;
2640 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002641 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 int col;
2644 int txtcol;
2645 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002646 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647
2648 /* Build the fold line:
2649 * 1. Add the cmdwin_type for the command-line window
2650 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002651 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 * 4. Compose the text
2653 * 5. Add the text
2654 * 6. set highlighting for the Visual area an other text
2655 */
2656 col = 0;
2657
2658 /*
2659 * 1. Add the cmdwin_type for the command-line window
2660 * Ignores 'rightleft', this window is never right-left.
2661 */
2662#ifdef FEAT_CMDWIN
2663 if (cmdwin_type != 0 && wp == curwin)
2664 {
2665 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002666 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 if (enc_utf8)
2668 ScreenLinesUC[off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 ++col;
2670 }
2671#endif
2672
2673 /*
2674 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002675 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002677 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 if (fdc > 0)
2679 {
2680 fill_foldcolumn(buf, wp, TRUE, lnum);
2681#ifdef FEAT_RIGHTLEFT
2682 if (wp->w_p_rl)
2683 {
2684 int i;
2685
Bram Moolenaar02631462017-09-22 15:20:32 +02002686 copy_text_attr(off + wp->w_width - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002687 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 /* reverse the fold column */
2689 for (i = 0; i < fdc; ++i)
Bram Moolenaar02631462017-09-22 15:20:32 +02002690 ScreenLines[off + wp->w_width - i - 1 - col] = buf[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 }
2692 else
2693#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002694 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 col += fdc;
2696 }
2697
2698#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6f470022018-04-10 18:47:20 +02002699# define RL_MEMSET(p, v, l) \
2700 do { \
2701 if (wp->w_p_rl) \
2702 for (ri = 0; ri < l; ++ri) \
2703 ScreenAttrs[off + (wp->w_width - (p) - (l)) + ri] = v; \
2704 else \
2705 for (ri = 0; ri < l; ++ri) \
2706 ScreenAttrs[off + (p) + ri] = v; \
2707 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708#else
Bram Moolenaar6f470022018-04-10 18:47:20 +02002709# define RL_MEMSET(p, v, l) \
2710 do { \
2711 for (ri = 0; ri < l; ++ri) \
2712 ScreenAttrs[off + (p) + ri] = v; \
2713 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714#endif
2715
Bram Moolenaar64486672010-05-16 15:46:46 +02002716 /* Set all attributes of the 'number' or 'relativenumber' column and the
2717 * text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002718 RL_MEMSET(col, HL_ATTR(HLF_FL), wp->w_width - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719
2720#ifdef FEAT_SIGNS
2721 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002722 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002724 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 if (len > 0)
2726 {
2727 if (len > 2)
2728 len = 2;
2729# ifdef FEAT_RIGHTLEFT
2730 if (wp->w_p_rl)
2731 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002732 copy_text_attr(off + wp->w_width - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002733 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 else
2735# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002736 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 col += len;
2738 }
2739 }
2740#endif
2741
2742 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002743 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002745 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002747 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 if (len > 0)
2749 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002750 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002751 long num;
2752 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002753
2754 if (len > w + 1)
2755 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002756
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002757 if (wp->w_p_nu && !wp->w_p_rnu)
2758 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002759 num = (long)lnum;
2760 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002761 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002762 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002763 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002764 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002765 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002766 /* 'number' + 'relativenumber': cursor line shows absolute
2767 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002768 num = lnum;
2769 fmt = "%-*ld ";
2770 }
2771 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002772
Bram Moolenaar700e7342013-01-30 12:31:36 +01002773 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774#ifdef FEAT_RIGHTLEFT
2775 if (wp->w_p_rl)
2776 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002777 copy_text_attr(off + wp->w_width - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002778 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 else
2780#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002781 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 col += len;
2783 }
2784 }
2785
2786 /*
2787 * 4. Compose the folded-line string with 'foldtext', if set.
2788 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002789 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790
2791 txtcol = col; /* remember where text starts */
2792
2793 /*
2794 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2795 * Right-left text is put in columns 0 - number-col, normal text is put
2796 * in columns number-col - window-width.
2797 */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002798 col = text_to_screenline(wp, text, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799
2800 /* Fill the rest of the line with the fold filler */
2801#ifdef FEAT_RIGHTLEFT
2802 if (wp->w_p_rl)
2803 col -= txtcol;
2804#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02002805 while (col < wp->w_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806#ifdef FEAT_RIGHTLEFT
2807 - (wp->w_p_rl ? txtcol : 0)
2808#endif
2809 )
2810 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 if (enc_utf8)
2812 {
2813 if (fill_fold >= 0x80)
2814 {
2815 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002816 ScreenLinesC[0][off + col] = 0;
Bram Moolenaaracda04f2018-02-08 09:57:28 +01002817 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 }
2819 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002820 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002822 ScreenLines[off + col] = fill_fold;
2823 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002824 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002826 else
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002827 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 }
2829
2830 if (text != buf)
2831 vim_free(text);
2832
2833 /*
2834 * 6. set highlighting for the Visual area an other text.
2835 * If all folded lines are in the Visual area, highlight the line.
2836 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2838 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002839 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 {
2841 /* Visual is after curwin->w_cursor */
2842 top = &curwin->w_cursor;
2843 bot = &VIsual;
2844 }
2845 else
2846 {
2847 /* Visual is before curwin->w_cursor */
2848 top = &VIsual;
2849 bot = &curwin->w_cursor;
2850 }
2851 if (lnum >= top->lnum
2852 && lnume <= bot->lnum
2853 && (VIsual_mode != 'v'
2854 || ((lnum > top->lnum
2855 || (lnum == top->lnum
2856 && top->col == 0))
2857 && (lnume < bot->lnum
2858 || (lnume == bot->lnum
2859 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002860 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 {
2862 if (VIsual_mode == Ctrl_V)
2863 {
2864 /* Visual block mode: highlight the chars part of the block */
Bram Moolenaar02631462017-09-22 15:20:32 +02002865 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002867 if (wp->w_old_cursor_lcol != MAXCOL
2868 && wp->w_old_cursor_lcol + txtcol
Bram Moolenaar02631462017-09-22 15:20:32 +02002869 < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 len = wp->w_old_cursor_lcol;
2871 else
Bram Moolenaar02631462017-09-22 15:20:32 +02002872 len = wp->w_width - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002873 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002874 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 }
2876 }
2877 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002878 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 /* Set all attributes of the text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002880 RL_MEMSET(txtcol, HL_ATTR(HLF_V), wp->w_width - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 }
2883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002885#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002886 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2887 if (wp->w_p_cc_cols)
2888 {
2889 int i = 0;
2890 int j = wp->w_p_cc_cols[i];
2891 int old_txtcol = txtcol;
2892
2893 while (j > -1)
2894 {
2895 txtcol += j;
2896 if (wp->w_p_wrap)
2897 txtcol -= wp->w_skipcol;
2898 else
2899 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002900 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002901 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002902 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002903 txtcol = old_txtcol;
2904 j = wp->w_p_cc_cols[++i];
2905 }
2906 }
2907
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002908 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002909 if (wp->w_p_cuc)
2910 {
2911 txtcol += wp->w_virtcol;
2912 if (wp->w_p_wrap)
2913 txtcol -= wp->w_skipcol;
2914 else
2915 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002916 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaar85595c52008-10-02 16:04:05 +00002917 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002918 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00002919 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002920#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921
Bram Moolenaar02631462017-09-22 15:20:32 +02002922 screen_line(row + W_WINROW(wp), wp->w_wincol, (int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002923 (int)wp->w_width, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924
2925 /*
2926 * Update w_cline_height and w_cline_folded if the cursor line was
2927 * updated (saves a call to plines() later).
2928 */
2929 if (wp == curwin
2930 && lnum <= curwin->w_cursor.lnum
2931 && lnume >= curwin->w_cursor.lnum)
2932 {
2933 curwin->w_cline_row = row;
2934 curwin->w_cline_height = 1;
2935 curwin->w_cline_folded = TRUE;
2936 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2937 }
2938}
2939
2940/*
2941 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2942 */
2943 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002944copy_text_attr(
2945 int off,
2946 char_u *buf,
2947 int len,
2948 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002950 int i;
2951
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 mch_memmove(ScreenLines + off, buf, (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 if (enc_utf8)
2954 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002955 for (i = 0; i < len; ++i)
2956 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957}
2958
2959/*
2960 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002961 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 */
2963 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002964fill_foldcolumn(
2965 char_u *p,
2966 win_T *wp,
2967 int closed, /* TRUE of FALSE */
2968 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969{
2970 int i = 0;
2971 int level;
2972 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002973 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002974 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975
2976 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002977 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978
2979 level = win_foldinfo.fi_level;
2980 if (level > 0)
2981 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002982 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002983 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002984
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 /* If the column is too narrow, we start at the lowest level that
2986 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002987 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 if (first_level < 1)
2989 first_level = 1;
2990
Bram Moolenaar1c934292015-01-27 16:39:29 +01002991 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 {
2993 if (win_foldinfo.fi_lnum == lnum
2994 && first_level + i >= win_foldinfo.fi_low_level)
2995 p[i] = '-';
2996 else if (first_level == 1)
2997 p[i] = '|';
2998 else if (first_level + i <= 9)
2999 p[i] = '0' + first_level + i;
3000 else
3001 p[i] = '>';
3002 if (first_level + i == level)
3003 break;
3004 }
3005 }
3006 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01003007 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008}
3009#endif /* FEAT_FOLDING */
3010
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01003011#ifdef FEAT_TEXT_PROP
3012static textprop_T *current_text_props = NULL;
3013static buf_T *current_buf = NULL;
3014
3015 static int
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01003016text_prop_compare(const void *s1, const void *s2)
3017{
3018 int idx1, idx2;
3019 proptype_T *pt1, *pt2;
3020 colnr_T col1, col2;
3021
3022 idx1 = *(int *)s1;
3023 idx2 = *(int *)s2;
3024 pt1 = text_prop_type_by_id(current_buf, current_text_props[idx1].tp_type);
3025 pt2 = text_prop_type_by_id(current_buf, current_text_props[idx2].tp_type);
3026 if (pt1 == pt2)
3027 return 0;
3028 if (pt1 == NULL)
3029 return -1;
3030 if (pt2 == NULL)
3031 return 1;
3032 if (pt1->pt_priority != pt2->pt_priority)
3033 return pt1->pt_priority > pt2->pt_priority ? 1 : -1;
3034 col1 = current_text_props[idx1].tp_col;
3035 col2 = current_text_props[idx2].tp_col;
3036 return col1 == col2 ? 0 : col1 > col2 ? 1 : -1;
3037}
3038#endif
3039
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040/*
3041 * Display line "lnum" of window 'wp' on the screen.
3042 * Start at row "startrow", stop when "endrow" is reached.
3043 * wp->w_virtcol needs to be valid.
3044 *
3045 * Return the number of last row the line occupies.
3046 */
3047 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003048win_line(
3049 win_T *wp,
3050 linenr_T lnum,
3051 int startrow,
3052 int endrow,
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003053 int nochange UNUSED, // not updating for changed text
3054 int number_only) // only update the number column
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055{
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003056 int col = 0; /* visual column on screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 unsigned off; /* offset in ScreenLines/ScreenAttrs */
3058 int c = 0; /* init for GCC */
3059 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01003060#ifdef FEAT_LINEBREAK
3061 long vcol_sbr = -1; /* virtual column after showbreak */
3062#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 long vcol_prev = -1; /* "vcol" of previous character */
3064 char_u *line; /* current line */
3065 char_u *ptr; /* current position in "line" */
3066 int row; /* row in the window, excl w_winrow */
3067 int screen_row; /* row on the screen, incl w_winrow */
3068
3069 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
3070 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00003071 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02003072 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 int c_extra = NUL; /* extra chars, all the same */
Bram Moolenaar83a52172019-01-16 22:41:54 +01003074 int c_final = NUL; /* final char, mandatory if set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 int extra_attr = 0; /* attributes when n_extra != 0 */
3076 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
3077 displaying lcs_eol at end-of-line */
3078 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
3079 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
3080
3081 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
3082 int saved_n_extra = 0;
3083 char_u *saved_p_extra = NULL;
3084 int saved_c_extra = 0;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003085 int saved_c_final = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 int saved_char_attr = 0;
3087
3088 int n_attr = 0; /* chars with special attr */
3089 int saved_attr2 = 0; /* char_attr saved for n_attr */
3090 int n_attr3 = 0; /* chars with overruling special attr */
3091 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
3092
3093 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
3094
3095 int fromcol, tocol; /* start/end of inverting */
3096 int fromcol_prev = -2; /* start of inverting after cursor */
3097 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003099 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 pos_T pos;
3101 long v;
3102
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003103 int char_attr = 0; // attributes for next character
3104 int attr_pri = FALSE; // char_attr has priority
3105 int area_highlighting = FALSE; // Visual or incsearch highlighting
3106 // in this line
3107 int vi_attr = 0; // attributes for Visual and incsearch
3108 // highlighting
3109 int wcr_attr = 0; // attributes from 'wincolor'
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003110 int win_attr = 0; // background for whole window, except
3111 // margins and "~" lines.
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003112 int area_attr = 0; // attributes desired by highlighting
3113 int search_attr = 0; // attributes desired by 'hlsearch'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003115 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 int syntax_attr = 0; /* attributes desired by syntax */
3117 int has_syntax = FALSE; /* this buffer has syntax highl. */
3118 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00003119 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02003120 int draw_color_col = FALSE; /* highlight colorcolumn */
3121 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003122#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003123#ifdef FEAT_TEXT_PROP
3124 int text_prop_count;
3125 int text_prop_next = 0; // next text property to use
3126 textprop_T *text_props = NULL;
3127 int *text_prop_idxs = NULL;
3128 int text_props_active = 0;
3129 proptype_T *text_prop_type = NULL;
3130 int text_prop_attr = 0;
Bram Moolenaarde24a872019-05-05 15:48:00 +02003131 int text_prop_combine = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003132#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003133#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003134 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003135# define SPWORDLEN 150
3136 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003137 int nextlinecol = 0; /* column where nextline[] starts */
3138 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003139 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003140 int spell_attr = 0; /* attributes desired by spelling */
3141 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003142 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3143 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003144 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003145 static int cap_col = -1; /* column to check for Cap word */
3146 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003147 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003149 int extra_check = 0; // has extra highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 int multi_attr = 0; /* attributes desired by multibyte */
3151 int mb_l = 1; /* multi-byte byte length */
3152 int mb_c = 0; /* decoded multi-byte character */
3153 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003154 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155#ifdef FEAT_DIFF
3156 int filler_lines; /* nr of filler lines to be drawn */
3157 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003158 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 int change_start = MAXCOL; /* first col of changed area */
3160 int change_end = -1; /* last col of changed area */
3161#endif
3162 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3163#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003164 int need_showbreak = FALSE; /* overlong line, skipping first x
3165 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003167#if defined(FEAT_SIGNS) || defined(FEAT_QUICKFIX) \
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003168 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003170 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171#endif
3172#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003173 matchitem_T *cur; /* points to the match list */
3174 match_T *shl; /* points to search_hl or a match */
3175 int shl_flag; /* flag to indicate whether search_hl
3176 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003177 int pos_inprogress; /* marks that position match search is
3178 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003179 int prevcol_hl_flag; /* flag to indicate whether prevcol
3180 equals startcol of search_hl or one
3181 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182#endif
3183#ifdef FEAT_ARABIC
3184 int prev_c = 0; /* previous Arabic character */
3185 int prev_c1 = 0; /* first composing char for prev_c */
3186#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003187#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003188 int did_line_attr = 0;
3189#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003190#ifdef FEAT_TERMINAL
3191 int get_term_attr = FALSE;
3192#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193
3194 /* draw_state: items that are drawn in sequence: */
3195#define WL_START 0 /* nothing done yet */
3196#ifdef FEAT_CMDWIN
3197# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3198#else
3199# define WL_CMDLINE WL_START
3200#endif
3201#ifdef FEAT_FOLDING
3202# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3203#else
3204# define WL_FOLD WL_CMDLINE
3205#endif
3206#ifdef FEAT_SIGNS
3207# define WL_SIGN WL_FOLD + 1 /* column for signs */
3208#else
3209# define WL_SIGN WL_FOLD /* column for signs */
3210#endif
3211#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003212#ifdef FEAT_LINEBREAK
3213# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003215# define WL_BRI WL_NR
3216#endif
3217#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3218# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3219#else
3220# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221#endif
3222#define WL_LINE WL_SBR + 1 /* text in the line */
3223 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003224#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 int feedback_col = 0;
3226 int feedback_old_attr = -1;
3227#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003228 int screen_line_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229
Bram Moolenaar860cae12010-06-05 23:22:07 +02003230#ifdef FEAT_CONCEAL
3231 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003232 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003233 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003234 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003235 int is_concealing = FALSE;
3236 int boguscols = 0; /* nonexistent columns added to force
3237 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003238 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003239 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003240 int match_conc = 0; /* cchar for match functions */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003241 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003242# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003243# define FIX_FOR_BOGUSCOLS \
3244 { \
3245 n_extra += vcol_off; \
3246 vcol -= vcol_off; \
3247 vcol_off = 0; \
3248 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003249 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003250 boguscols = 0; \
3251 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003252#else
3253# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003254#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255
3256 if (startrow > endrow) /* past the end already! */
3257 return startrow;
3258
3259 row = startrow;
3260 screen_row = row + W_WINROW(wp);
3261
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003262 if (!number_only)
3263 {
3264 /*
3265 * To speed up the loop below, set extra_check when there is linebreak,
3266 * trailing white space and/or syntax processing to be done.
3267 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268#ifdef FEAT_LINEBREAK
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003269 extra_check = wp->w_p_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270#endif
3271#ifdef FEAT_SYN_HL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003272 if (syntax_present(wp) && !wp->w_s->b_syn_error
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003273# ifdef SYN_TIME_LIMIT
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003274 && !wp->w_s->b_syn_slow
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003275# endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003276 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003278 /* Prepare for syntax highlighting in this line. When there is an
3279 * error, stop syntax highlighting. */
3280 save_did_emsg = did_emsg;
3281 did_emsg = FALSE;
3282 syntax_start(wp, lnum);
3283 if (did_emsg)
3284 wp->w_s->b_syn_error = TRUE;
3285 else
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003286 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003287 did_emsg = save_did_emsg;
3288#ifdef SYN_TIME_LIMIT
3289 if (!wp->w_s->b_syn_slow)
3290#endif
3291 {
3292 has_syntax = TRUE;
3293 extra_check = TRUE;
3294 }
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003297
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003298 /* Check for columns to display for 'colorcolumn'. */
3299 color_cols = wp->w_p_cc_cols;
3300 if (color_cols != NULL)
3301 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003302#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003303
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003304#ifdef FEAT_TERMINAL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003305 if (term_show_buffer(wp->w_buffer))
3306 {
3307 extra_check = TRUE;
3308 get_term_attr = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003309 win_attr = term_get_attr(wp->w_buffer, lnum, -1);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003310 }
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003311#endif
3312
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003313#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003314 if (wp->w_p_spell
3315 && *wp->w_s->b_p_spl != NUL
3316 && wp->w_s->b_langp.ga_len > 0
3317 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003318 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003319 /* Prepare for spell checking. */
3320 has_spell = TRUE;
3321 extra_check = TRUE;
3322
Bram Moolenaar7701f302018-10-02 21:20:32 +02003323 /* Get the start of the next line, so that words that wrap to the
3324 * next line are found too: "et<line-break>al.".
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003325 * Trick: skip a few chars for C/shell/Vim comments */
3326 nextline[SPWORDLEN] = NUL;
3327 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3328 {
3329 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3330 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3331 }
3332
Bram Moolenaar7701f302018-10-02 21:20:32 +02003333 /* When a word wrapped from the previous line the start of the
3334 * current line is valid. */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003335 if (lnum == checked_lnum)
3336 cur_checked_col = checked_col;
3337 checked_lnum = 0;
3338
3339 /* When there was a sentence end in the previous line may require a
3340 * word starting with capital in this line. In line 1 always check
3341 * the first word. */
3342 if (lnum != capcol_lnum)
3343 cap_col = -1;
3344 if (lnum == 1)
3345 cap_col = 0;
3346 capcol_lnum = 0;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348#endif
3349
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003350 /*
3351 * handle visual active in this window
3352 */
3353 fromcol = -10;
3354 tocol = MAXCOL;
3355 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003357 /* Visual is after curwin->w_cursor */
3358 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003360 top = &curwin->w_cursor;
3361 bot = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 }
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003363 else /* Visual is before curwin->w_cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003365 top = &VIsual;
3366 bot = &curwin->w_cursor;
3367 }
3368 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
3369 if (VIsual_mode == Ctrl_V) /* block mode */
3370 {
3371 if (lnum_in_visual_area)
3372 {
3373 fromcol = wp->w_old_cursor_fcol;
3374 tocol = wp->w_old_cursor_lcol;
3375 }
3376 }
3377 else /* non-block mode */
3378 {
3379 if (lnum > top->lnum && lnum <= bot->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 fromcol = 0;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003381 else if (lnum == top->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003383 if (VIsual_mode == 'V') /* linewise */
3384 fromcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 else
3386 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003387 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3388 if (gchar_pos(top) == NUL)
3389 tocol = fromcol + 1;
3390 }
3391 }
3392 if (VIsual_mode != 'V' && lnum == bot->lnum)
3393 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003394 if (*p_sel == 'e' && bot->col == 0 && bot->coladd == 0)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003395 {
3396 fromcol = -10;
3397 tocol = MAXCOL;
3398 }
3399 else if (bot->col == MAXCOL)
3400 tocol = MAXCOL;
3401 else
3402 {
3403 pos = *bot;
3404 if (*p_sel == 'e')
3405 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3406 else
3407 {
3408 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3409 ++tocol;
3410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 }
3412 }
3413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003415 /* Check if the character under the cursor should not be inverted */
3416 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003417#ifdef FEAT_GUI
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003418 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003419#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003420 )
3421 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003423 /* if inverting in this line set area_highlighting */
3424 if (fromcol >= 0)
3425 {
3426 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003427 vi_attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003429 if ((clip_star.available && !clip_star.owned
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003430 && clip_isautosel_star())
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003431 || (clip_plus.available && !clip_plus.owned
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003432 && clip_isautosel_plus()))
3433 vi_attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003438 /*
3439 * handle 'incsearch' and ":s///c" highlighting
3440 */
3441 else if (highlight_match
3442 && wp == curwin
3443 && lnum >= curwin->w_cursor.lnum
3444 && lnum <= curwin->w_cursor.lnum + search_match_lines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003446 if (lnum == curwin->w_cursor.lnum)
3447 getvcol(curwin, &(curwin->w_cursor),
Bram Moolenaarc6663882019-02-22 19:14:54 +01003448 (colnr_T *)&fromcol, NULL, NULL);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003449 else
3450 fromcol = 0;
3451 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3452 {
3453 pos.lnum = lnum;
3454 pos.col = search_match_endcol;
3455 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3456 }
3457 else
3458 tocol = MAXCOL;
3459 /* do at least one character; happens when past end of line */
3460 if (fromcol == tocol)
3461 tocol = fromcol + 1;
3462 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003463 vi_attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 }
3466
3467#ifdef FEAT_DIFF
3468 filler_lines = diff_check(wp, lnum);
3469 if (filler_lines < 0)
3470 {
3471 if (filler_lines == -1)
3472 {
3473 if (diff_find_change(wp, lnum, &change_start, &change_end))
3474 diff_hlf = HLF_ADD; /* added line */
3475 else if (change_start == 0)
3476 diff_hlf = HLF_TXD; /* changed text */
3477 else
3478 diff_hlf = HLF_CHD; /* changed line */
3479 }
3480 else
3481 diff_hlf = HLF_ADD; /* added line */
3482 filler_lines = 0;
3483 area_highlighting = TRUE;
3484 }
3485 if (lnum == wp->w_topline)
3486 filler_lines = wp->w_topfill;
3487 filler_todo = filler_lines;
3488#endif
3489
3490#ifdef LINE_ATTR
3491# ifdef FEAT_SIGNS
3492 /* If this line has a sign with line highlighting set line_attr. */
3493 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3494 if (v != 0)
3495 line_attr = sign_get_attr((int)v, TRUE);
3496# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003497# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003499 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003500 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501# endif
3502 if (line_attr != 0)
3503 area_highlighting = TRUE;
3504#endif
3505
3506 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3507 ptr = line;
3508
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003509#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003510 if (has_spell && !number_only)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003511 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003512 /* For checking first word with a capital skip white space. */
3513 if (cap_col == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003514 cap_col = getwhitecols(line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003515
Bram Moolenaar30abd282005-06-22 22:35:10 +00003516 /* To be able to spell-check over line boundaries copy the end of the
3517 * current line into nextline[]. Above the start of the next line was
3518 * copied to nextline[SPWORDLEN]. */
3519 if (nextline[SPWORDLEN] == NUL)
3520 {
3521 /* No next line or it is empty. */
3522 nextlinecol = MAXCOL;
3523 nextline_idx = 0;
3524 }
3525 else
3526 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003527 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003528 if (v < SPWORDLEN)
3529 {
3530 /* Short line, use it completely and append the start of the
3531 * next line. */
3532 nextlinecol = 0;
3533 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003534 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003535 nextline_idx = v + 1;
3536 }
3537 else
3538 {
3539 /* Long line, use only the last SPWORDLEN bytes. */
3540 nextlinecol = v - SPWORDLEN;
3541 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3542 nextline_idx = SPWORDLEN + 1;
3543 }
3544 }
3545 }
3546#endif
3547
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003548 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 {
Bram Moolenaar895d9662019-01-31 21:57:21 +01003550 if (lcs_space || lcs_trail || lcs_nbsp)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003551 extra_check = TRUE;
3552 /* find start of trailing whitespace */
3553 if (lcs_trail)
3554 {
3555 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003556 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003557 --trailcol;
3558 trailcol += (colnr_T) (ptr - line);
3559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 }
3561
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003562 wcr_attr = get_wcr_attr(wp);
3563 if (wcr_attr != 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003564 {
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003565 win_attr = wcr_attr;
3566 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003567 }
3568#ifdef FEAT_TEXT_PROP
3569 if (bt_popup(wp->w_buffer))
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003570 screen_line_flags |= SLF_POPUP;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003571#endif
3572
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 /*
3574 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3575 * first character to be displayed.
3576 */
3577 if (wp->w_p_wrap)
3578 v = wp->w_skipcol;
3579 else
3580 v = wp->w_leftcol;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003581 if (v > 0 && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 char_u *prev_ptr = ptr;
Bram Moolenaara12a1612019-01-24 16:39:02 +01003584
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 while (vcol < v && *ptr != NUL)
3586 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003587 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 vcol += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 prev_ptr = ptr;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003590 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 }
3592
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003593 /* When:
3594 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003595 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003596 * - 'virtualedit' is set, or
3597 * - the visual mode is active,
3598 * the end of the line may be before the start of the displayed part.
3599 */
3600 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003601#ifdef FEAT_SYN_HL
3602 wp->w_p_cuc || draw_color_col ||
3603#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003604 virtual_active() ||
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003605 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 vcol = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607
3608 /* Handle a character that's not completely on the screen: Put ptr at
3609 * that character but skip the first few screen characters. */
3610 if (vcol > v)
3611 {
3612 vcol -= c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 ptr = prev_ptr;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003614 /* If the character fits on the screen, don't need to skip it.
3615 * Except for a TAB. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01003616 if (( (*mb_ptr2cells)(ptr) >= c || *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003617 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 }
3619
3620 /*
3621 * Adjust for when the inverted text is before the screen,
3622 * and when the start of the inverted text is before the screen.
3623 */
3624 if (tocol <= vcol)
3625 fromcol = 0;
3626 else if (fromcol >= 0 && fromcol < vcol)
3627 fromcol = vcol;
3628
3629#ifdef FEAT_LINEBREAK
3630 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3631 if (wp->w_p_wrap)
3632 need_showbreak = TRUE;
3633#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003634#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003635 /* When spell checking a word we need to figure out the start of the
3636 * word and if it's badly spelled or not. */
3637 if (has_spell)
3638 {
3639 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003640 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003641 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003642
3643 pos = wp->w_cursor;
3644 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003645 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003646 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003647
3648 /* spell_move_to() may call ml_get() and make "line" invalid */
3649 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3650 ptr = line + linecol;
3651
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003652 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003653 {
3654 /* no bad word found at line start, don't check until end of a
3655 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003656 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003657 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003658 }
3659 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003660 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003661 /* bad word found, use attributes until end of word */
3662 word_end = wp->w_cursor.col + len + 1;
3663
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003664 /* Turn index into actual attributes. */
3665 if (spell_hlf != HLF_COUNT)
3666 spell_attr = highlight_attr[spell_hlf];
3667 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003668 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003669
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003670# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003671 /* Need to restart syntax highlighting for this line. */
3672 if (has_syntax)
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003673 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003674# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003675 }
3676#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 }
3678
3679 /*
3680 * Correct highlighting for cursor that can't be disabled.
3681 * Avoids having to check this for each character.
3682 */
3683 if (fromcol >= 0)
3684 {
3685 if (noinvcur)
3686 {
3687 if ((colnr_T)fromcol == wp->w_virtcol)
3688 {
3689 /* highlighting starts at cursor, let it start just after the
3690 * cursor */
3691 fromcol_prev = fromcol;
3692 fromcol = -1;
3693 }
3694 else if ((colnr_T)fromcol < wp->w_virtcol)
3695 /* restart highlighting after the cursor */
3696 fromcol_prev = wp->w_virtcol;
3697 }
3698 if (fromcol >= tocol)
3699 fromcol = -1;
3700 }
3701
3702#ifdef FEAT_SEARCH_EXTRA
3703 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003704 * Handle highlighting the last used search pattern and matches.
3705 * Do this for both search_hl and the match list.
Bram Moolenaarac2450a2019-06-09 18:04:28 +02003706 * Do not use search_hl in a popup window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003708 cur = wp->w_match_head;
Bram Moolenaarac2450a2019-06-09 18:04:28 +02003709 shl_flag = (screen_line_flags & SLF_POPUP);
3710 while ((cur != NULL || shl_flag == FALSE) && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003712 if (shl_flag == FALSE)
3713 {
3714 shl = &search_hl;
3715 shl_flag = TRUE;
3716 }
3717 else
3718 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003719 shl->startcol = MAXCOL;
3720 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003722 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003723 v = (long)(ptr - line);
3724 if (cur != NULL)
3725 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003726 next_search_hl(wp, shl, lnum, (colnr_T)v,
3727 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003728
3729 /* Need to get the line again, a multi-line regexp may have made it
3730 * invalid. */
3731 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3732 ptr = line + v;
3733
3734 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003736 if (shl->lnum == lnum)
3737 shl->startcol = shl->rm.startpos[0].col;
3738 else
3739 shl->startcol = 0;
3740 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3741 - shl->rm.startpos[0].lnum)
3742 shl->endcol = shl->rm.endpos[0].col;
3743 else
3744 shl->endcol = MAXCOL;
3745 /* Highlight one character for an empty match. */
3746 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003748 if (has_mbyte && line[shl->endcol] != NUL)
3749 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3750 else
Bram Moolenaarb3414592014-06-17 17:48:32 +02003751 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003753 if ((long)shl->startcol < v) /* match at leftcol */
3754 {
3755 shl->attr_cur = shl->attr;
3756 search_attr = shl->attr;
3757 }
3758 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003760 if (shl != &search_hl && cur != NULL)
3761 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 }
3763#endif
3764
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003765#ifdef FEAT_SYN_HL
Bram Moolenaar8156ed32019-03-09 11:46:15 +01003766 // Cursor line highlighting for 'cursorline' in the current window.
3767 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003768 {
Bram Moolenaar8156ed32019-03-09 11:46:15 +01003769 // Do not show the cursor line when Visual mode is active, because it's
3770 // not clear what is selected then. Do update w_last_cursorline.
3771 if (!(wp == curwin && VIsual_active))
3772 {
3773 line_attr = HL_ATTR(HLF_CUL);
3774 area_highlighting = TRUE;
3775 }
Bram Moolenaarc07ff5c2019-01-30 21:41:14 +01003776 wp->w_last_cursorline = wp->w_cursor.lnum;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003777 }
3778#endif
3779
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003780#ifdef FEAT_TEXT_PROP
3781 {
3782 char_u *prop_start;
3783
3784 text_prop_count = get_text_props(wp->w_buffer, lnum,
3785 &prop_start, FALSE);
3786 if (text_prop_count > 0)
3787 {
3788 // Make a copy of the properties, so that they are properly
3789 // aligned.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003790 text_props = ALLOC_MULT(textprop_T, text_prop_count);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003791 if (text_props != NULL)
3792 mch_memmove(text_props, prop_start,
3793 text_prop_count * sizeof(textprop_T));
3794
3795 // Allocate an array for the indexes.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003796 text_prop_idxs = ALLOC_MULT(int, text_prop_count);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003797 area_highlighting = TRUE;
3798 extra_check = TRUE;
3799 }
3800 }
3801#endif
3802
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003803 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 col = 0;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003805
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806#ifdef FEAT_RIGHTLEFT
3807 if (wp->w_p_rl)
3808 {
3809 /* Rightleft window: process the text in the normal direction, but put
3810 * it in current_ScreenLine[] from right to left. Start at the
3811 * rightmost column of the window. */
Bram Moolenaar02631462017-09-22 15:20:32 +02003812 col = wp->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 off += col;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003814 screen_line_flags |= SLF_RIGHTLEFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 }
3816#endif
3817
3818 /*
3819 * Repeat for the whole displayed line.
3820 */
3821 for (;;)
3822 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003823#ifdef FEAT_CONCEAL
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02003824 int has_match_conc = 0; // match wants to conceal
3825 int did_decrement_ptr = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003826#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 /* Skip this quickly when working on the text. */
3828 if (draw_state != WL_LINE)
3829 {
3830#ifdef FEAT_CMDWIN
3831 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3832 {
3833 draw_state = WL_CMDLINE;
3834 if (cmdwin_type != 0 && wp == curwin)
3835 {
3836 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003838 c_extra = cmdwin_type;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003839 c_final = NUL;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003840 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 }
3842 }
3843#endif
3844
3845#ifdef FEAT_FOLDING
3846 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3847 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003848 int fdc = compute_foldcolumn(wp, 0);
3849
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003851 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003853 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003854 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003855 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003856 p_extra_free = alloc(12 + 1);
3857
3858 if (p_extra_free != NULL)
3859 {
3860 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3861 n_extra = fdc;
3862 p_extra_free[n_extra] = NUL;
3863 p_extra = p_extra_free;
3864 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003865 c_final = NUL;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003866 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_FC));
Bram Moolenaar62706602016-12-09 19:28:48 +01003867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 }
3869 }
3870#endif
3871
3872#ifdef FEAT_SIGNS
3873 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3874 {
3875 draw_state = WL_SIGN;
3876 /* Show the sign column when there are any signs in this
3877 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003878 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003880 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003882 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883# endif
3884
3885 /* Draw two cells with the sign value or blank. */
3886 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01003887 c_final = NUL;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003888 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 n_extra = 2;
3890
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003891 if (row == startrow
3892#ifdef FEAT_DIFF
3893 + filler_lines && filler_todo <= 0
3894#endif
3895 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 {
3897 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3898 SIGN_TEXT);
3899# ifdef FEAT_SIGN_ICONS
3900 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3901 SIGN_ICON);
3902 if (gui.in_use && icon_sign != 0)
3903 {
3904 /* Use the image in this position. */
3905 c_extra = SIGN_BYTE;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003906 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907# ifdef FEAT_NETBEANS_INTG
3908 if (buf_signcount(wp->w_buffer, lnum) > 1)
Bram Moolenaar83a52172019-01-16 22:41:54 +01003909 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 c_extra = MULTISIGN_BYTE;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003911 c_final = NUL;
3912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913# endif
3914 char_attr = icon_sign;
3915 }
3916 else
3917# endif
3918 if (text_sign != 0)
3919 {
3920 p_extra = sign_get_text(text_sign);
3921 if (p_extra != NULL)
3922 {
3923 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003924 c_final = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003925 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 }
3927 char_attr = sign_get_attr(text_sign, FALSE);
3928 }
3929 }
3930 }
3931 }
3932#endif
3933
3934 if (draw_state == WL_NR - 1 && n_extra == 0)
3935 {
3936 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003937 /* Display the absolute or relative line number. After the
3938 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3939 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 && (row == startrow
3941#ifdef FEAT_DIFF
3942 + filler_lines
3943#endif
3944 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3945 {
3946 /* Draw the line number (empty space after wrapping). */
3947 if (row == startrow
3948#ifdef FEAT_DIFF
3949 + filler_lines
3950#endif
3951 )
3952 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003953 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003954 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003955
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003956 if (wp->w_p_nu && !wp->w_p_rnu)
3957 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003958 num = (long)lnum;
3959 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003960 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003961 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003962 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003963 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003964 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003965 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003966 num = lnum;
3967 fmt = "%-*ld ";
3968 }
3969 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003970
Bram Moolenaar700e7342013-01-30 12:31:36 +01003971 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003972 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 if (wp->w_skipcol > 0)
3974 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3975 *p_extra = '-';
3976#ifdef FEAT_RIGHTLEFT
3977 if (wp->w_p_rl) /* reverse line numbers */
Bram Moolenaare73f9112019-03-29 18:29:54 +01003978 {
3979 char_u *p1, *p2;
3980 int t;
3981
3982 // like rl_mirror(), but keep the space at the end
3983 p2 = skiptowhite(extra) - 1;
3984 for (p1 = extra; p1 < p2; ++p1, --p2)
3985 {
3986 t = *p1;
3987 *p1 = *p2;
3988 *p2 = t;
3989 }
3990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991#endif
3992 p_extra = extra;
3993 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003994 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 }
3996 else
Bram Moolenaar83a52172019-01-16 22:41:54 +01003997 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01003999 c_final = NUL;
4000 }
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004001 n_extra = number_width(wp) + 1;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02004002 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_N));
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004003#ifdef FEAT_SYN_HL
4004 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01004005 * the current line differently.
4006 * TODO: Can we use CursorLine instead of CursorLineNr
4007 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004008 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01004009 && lnum == wp->w_cursor.lnum)
Bram Moolenaar193ffd12019-05-25 22:57:30 +02004010 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_CLN));
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004011#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 }
4013 }
4014
Bram Moolenaar597a4222014-06-25 14:39:50 +02004015#ifdef FEAT_LINEBREAK
4016 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
4017 && n_extra == 0 && *p_sbr != NUL)
4018 /* draw indent after showbreak value */
4019 draw_state = WL_BRI;
4020 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
4021 /* After the showbreak, draw the breakindent */
4022 draw_state = WL_BRI - 1;
4023
4024 /* draw 'breakindent': indent wrapped text accordingly */
4025 if (draw_state == WL_BRI - 1 && n_extra == 0)
4026 {
4027 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01004028 /* if need_showbreak is set, breakindent also applies */
4029 if (wp->w_p_bri && n_extra == 0
4030 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004031# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004032 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004033# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004034 )
4035 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01004036 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004037# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004038 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004039 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004040 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004041# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02004042 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4043 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004044 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004045# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02004046 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004047# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02004048 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02004049 c_extra = ' ';
4050 n_extra = get_breakindent_win(wp,
4051 ml_get_buf(wp->w_buffer, lnum, FALSE));
4052 /* Correct end of highlighted area for 'breakindent',
4053 * required when 'linebreak' is also set. */
4054 if (tocol == vcol)
4055 tocol += n_extra;
4056 }
4057 }
4058#endif
4059
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
4061 if (draw_state == WL_SBR - 1 && n_extra == 0)
4062 {
4063 draw_state = WL_SBR;
4064# ifdef FEAT_DIFF
4065 if (filler_todo > 0)
4066 {
4067 /* Draw "deleted" diff line(s). */
4068 if (char2cells(fill_diff) > 1)
Bram Moolenaar83a52172019-01-16 22:41:54 +01004069 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 c_extra = '-';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004071 c_final = NUL;
4072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 else
Bram Moolenaar83a52172019-01-16 22:41:54 +01004074 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 c_extra = fill_diff;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004076 c_final = NUL;
4077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078# ifdef FEAT_RIGHTLEFT
4079 if (wp->w_p_rl)
4080 n_extra = col + 1;
4081 else
4082# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004083 n_extra = wp->w_width - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004084 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 }
4086# endif
4087# ifdef FEAT_LINEBREAK
4088 if (*p_sbr != NUL && need_showbreak)
4089 {
4090 /* Draw 'showbreak' at the start of each broken line. */
4091 p_extra = p_sbr;
4092 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004093 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004095 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004097 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 /* Correct end of highlighted area for 'showbreak',
4099 * required when 'linebreak' is also set. */
4100 if (tocol == vcol)
4101 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004102#ifdef FEAT_SYN_HL
4103 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004104 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004105 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004106 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004107#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 }
4109# endif
4110 }
4111#endif
4112
4113 if (draw_state == WL_LINE - 1 && n_extra == 0)
4114 {
4115 draw_state = WL_LINE;
4116 if (saved_n_extra)
4117 {
4118 /* Continue item from end of wrapped line. */
4119 n_extra = saved_n_extra;
4120 c_extra = saved_c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004121 c_final = saved_c_final;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 p_extra = saved_p_extra;
4123 char_attr = saved_char_attr;
4124 }
4125 else
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004126 char_attr = win_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 }
4128 }
4129
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004130 // When still displaying '$' of change command, stop at cursor.
4131 // When only displaying the (relative) line number and that's done,
4132 // stop here.
4133 if ((dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004134 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135#ifdef FEAT_DIFF
4136 && filler_todo <= 0
4137#endif
4138 )
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004139 || (number_only && draw_state > WL_NR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 {
Bram Moolenaar02631462017-09-22 15:20:32 +02004141 screen_line(screen_row, wp->w_wincol, col, -(int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004142 screen_line_flags);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004143 /* Pretend we have finished updating the window. Except when
4144 * 'cursorcolumn' is set. */
4145#ifdef FEAT_SYN_HL
4146 if (wp->w_p_cuc)
4147 row = wp->w_cline_row + wp->w_cline_height;
4148 else
4149#endif
4150 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 break;
4152 }
4153
Bram Moolenaar637532b2019-01-03 21:44:40 +01004154 if (draw_state == WL_LINE && (area_highlighting
4155#ifdef FEAT_SPELL
4156 || has_spell
4157#endif
4158 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 {
4160 /* handle Visual or match highlighting in this line */
4161 if (vcol == fromcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4163 && (*mb_ptr2cells)(ptr) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004165 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 && vcol < tocol))
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004167 area_attr = vi_attr; /* start highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 else if (area_attr != 0
4169 && (vcol == tocol
4170 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172
4173#ifdef FEAT_SEARCH_EXTRA
4174 if (!n_extra)
4175 {
4176 /*
4177 * Check for start/end of search pattern match.
4178 * After end, check for start/end of next match.
4179 * When another match, have to check for start again.
4180 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004181 * Do this for 'search_hl' and the match list (ordered by
4182 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004184 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004185 cur = wp->w_match_head;
Bram Moolenaara730e552019-06-16 19:05:31 +02004186 shl_flag = FALSE;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004187 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004189 if (shl_flag == FALSE
4190 && ((cur != NULL
4191 && cur->priority > SEARCH_HL_PRIORITY)
4192 || cur == NULL))
4193 {
4194 shl = &search_hl;
4195 shl_flag = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02004196 if (screen_line_flags & SLF_POPUP)
4197 continue; // do not use search_hl
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004198 }
4199 else
4200 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004201 if (cur != NULL)
4202 cur->pos.cur = 0;
4203 pos_inprogress = TRUE;
4204 while (shl->rm.regprog != NULL
4205 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004207 if (shl->startcol != MAXCOL
4208 && v >= (long)shl->startcol
4209 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004211 int tmp_col = v + MB_PTR2LEN(ptr);
4212
4213 if (shl->endcol < tmp_col)
4214 shl->endcol = tmp_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004216#ifdef FEAT_CONCEAL
Bram Moolenaarab62c192019-03-30 16:39:05 +01004217 // Match with the "Conceal" group results in hiding
4218 // the match.
4219 if (cur != NULL
4220 && shl != &search_hl
4221 && syn_name2id((char_u *)"Conceal")
4222 == cur->hlg_id)
Bram Moolenaar6561d522015-07-21 15:48:27 +02004223 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004224 has_match_conc =
4225 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004226 match_conc = cur->conceal_char;
4227 }
4228 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004229 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004230#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004232 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 {
4234 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004235 next_search_hl(wp, shl, lnum, (colnr_T)v,
4236 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004237 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004238 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239
4240 /* Need to get the line again, a multi-line regexp
4241 * may have made it invalid. */
4242 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4243 ptr = line + v;
4244
4245 if (shl->lnum == lnum)
4246 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004247 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004249 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004251 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004253 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 {
4255 /* highlight empty match, try again after
4256 * it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004258 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004259 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004261 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 }
4263
4264 /* Loop to check if the match starts at the
4265 * current position */
4266 continue;
4267 }
4268 }
4269 break;
4270 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004271 if (shl != &search_hl && cur != NULL)
4272 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004274
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004275 /* Use attributes from match with highest priority among
4276 * 'search_hl' and the match list. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004277 cur = wp->w_match_head;
4278 shl_flag = FALSE;
Bram Moolenaara730e552019-06-16 19:05:31 +02004279 search_attr = 0;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004280 while (cur != NULL || shl_flag == FALSE)
4281 {
4282 if (shl_flag == FALSE
4283 && ((cur != NULL
4284 && cur->priority > SEARCH_HL_PRIORITY)
4285 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004286 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004287 shl = &search_hl;
4288 shl_flag = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02004289 if (screen_line_flags & SLF_POPUP)
4290 continue; // do not use search_hl
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004291 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004292 else
4293 shl = &cur->hl;
4294 if (shl->attr_cur != 0)
4295 search_attr = shl->attr_cur;
4296 if (shl != &search_hl && cur != NULL)
4297 cur = cur->next;
4298 }
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004299 /* Only highlight one character after the last column. */
Bram Moolenaar5ece3e32017-10-01 14:35:02 +02004300 if (*ptr == NUL && (did_line_attr >= 1
4301 || (wp->w_p_list && lcs_eol_one == -1)))
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004302 search_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 }
4304#endif
4305
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004307 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004309 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4310 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004312 if (diff_hlf == HLF_TXD && ptr - line > change_end
4313 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004315 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004316 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004317 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 }
4319#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004320
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004321#ifdef FEAT_TEXT_PROP
4322 if (text_props != NULL)
4323 {
4324 int pi;
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004325 int bcol = (int)(ptr - line);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004326
4327 // Check if any active property ends.
4328 for (pi = 0; pi < text_props_active; ++pi)
4329 {
4330 int tpi = text_prop_idxs[pi];
4331
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004332 if (bcol >= text_props[tpi].tp_col - 1
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004333 + text_props[tpi].tp_len)
4334 {
4335 if (pi + 1 < text_props_active)
4336 mch_memmove(text_prop_idxs + pi,
4337 text_prop_idxs + pi + 1,
4338 sizeof(int)
4339 * (text_props_active - (pi + 1)));
4340 --text_props_active;
4341 --pi;
4342 }
4343 }
4344
4345 // Add any text property that starts in this column.
4346 while (text_prop_next < text_prop_count
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004347 && bcol >= text_props[text_prop_next].tp_col - 1)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004348 text_prop_idxs[text_props_active++] = text_prop_next++;
4349
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004350 text_prop_attr = 0;
Bram Moolenaarde24a872019-05-05 15:48:00 +02004351 text_prop_combine = FALSE;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004352 if (text_props_active > 0)
4353 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004354 // Sort the properties on priority and/or starting last.
4355 // Then combine the attributes, highest priority last.
4356 current_text_props = text_props;
4357 current_buf = wp->w_buffer;
4358 qsort((void *)text_prop_idxs, (size_t)text_props_active,
4359 sizeof(int), text_prop_compare);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004360
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004361 for (pi = 0; pi < text_props_active; ++pi)
4362 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004363 int tpi = text_prop_idxs[pi];
Bram Moolenaarde24a872019-05-05 15:48:00 +02004364 proptype_T *pt = text_prop_type_by_id(
4365 wp->w_buffer, text_props[tpi].tp_type);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004366
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004367 if (pt != NULL)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004368 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004369 int pt_attr = syn_id2attr(pt->pt_hl_id);
4370
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004371 text_prop_type = pt;
Bram Moolenaarde24a872019-05-05 15:48:00 +02004372 text_prop_attr =
4373 hl_combine_attr(text_prop_attr, pt_attr);
4374 text_prop_combine = pt->pt_flags & PT_FLAG_COMBINE;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004375 }
4376 }
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004377 }
4378 }
4379#endif
4380
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004381 /* Decide which of the highlight attributes to use. */
4382 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004383#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004384 if (area_attr != 0)
4385 char_attr = hl_combine_attr(line_attr, area_attr);
4386 else if (search_attr != 0)
4387 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004388# ifdef FEAT_TEXT_PROP
4389 else if (text_prop_type != NULL)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02004390 {
4391 char_attr = hl_combine_attr(
4392 line_attr != 0 ? line_attr : win_attr, text_prop_attr);
4393 }
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004394# endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004395 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004396 || vcol < fromcol || vcol_prev < fromcol_prev
4397 || vcol >= tocol))
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004398 // Use line_attr when not in the Visual or 'incsearch' area
4399 // (area_attr may be 0 when "noinvcur" is set).
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004400 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004401#else
4402 if (area_attr != 0)
4403 char_attr = area_attr;
4404 else if (search_attr != 0)
4405 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004406#endif
4407 else
4408 {
4409 attr_pri = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004410#ifdef FEAT_TEXT_PROP
4411 if (text_prop_type != NULL)
Bram Moolenaarde24a872019-05-05 15:48:00 +02004412 {
4413 if (text_prop_combine)
4414 char_attr = hl_combine_attr(
4415 syntax_attr, text_prop_attr);
4416 else
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02004417 char_attr = hl_combine_attr(
4418 win_attr, text_prop_attr);
Bram Moolenaarde24a872019-05-05 15:48:00 +02004419 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004420 else
4421#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004422#ifdef FEAT_SYN_HL
4423 if (has_syntax)
4424 char_attr = syntax_attr;
4425 else
4426#endif
4427 char_attr = 0;
4428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004430 if (char_attr == 0)
4431 char_attr = win_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432
4433 /*
4434 * Get the next character to put on the screen.
4435 */
4436 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004437 * The "p_extra" points to the extra stuff that is inserted to
4438 * represent special characters (non-printable stuff) and other
4439 * things. When all characters are the same, c_extra is used.
Bram Moolenaar83a52172019-01-16 22:41:54 +01004440 * If c_final is set, it will compulsorily be used at the end.
Bram Moolenaara064ac82007-08-05 18:10:54 +00004441 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4442 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4444 */
4445 if (n_extra > 0)
4446 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004447 if (c_extra != NUL || (n_extra == 1 && c_final != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004449 c = (n_extra == 1 && c_final != NUL) ? c_final : c_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004451 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 {
4453 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004454 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004455 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 }
4457 else
4458 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 }
4460 else
4461 {
4462 c = *p_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 if (has_mbyte)
4464 {
4465 mb_c = c;
4466 if (enc_utf8)
4467 {
4468 /* If the UTF-8 character is more than one byte:
4469 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004470 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 mb_utf8 = FALSE;
4472 if (mb_l > n_extra)
4473 mb_l = 1;
4474 else if (mb_l > 1)
4475 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004476 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004478 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 }
4480 }
4481 else
4482 {
4483 /* if this is a DBCS character, put it in "mb_c" */
4484 mb_l = MB_BYTE2LEN(c);
4485 if (mb_l >= n_extra)
4486 mb_l = 1;
4487 else if (mb_l > 1)
4488 mb_c = (c << 8) + p_extra[1];
4489 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004490 if (mb_l == 0) /* at the NUL at end-of-line */
4491 mb_l = 1;
4492
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 /* If a double-width char doesn't fit display a '>' in the
4494 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004495 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496# ifdef FEAT_RIGHTLEFT
4497 wp->w_p_rl ? (col <= 0) :
4498# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004499 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 && (*mb_char2cells)(mb_c) == 2)
4501 {
4502 c = '>';
4503 mb_c = c;
4504 mb_l = 1;
4505 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004506 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 /* put the pointer back to output the double-width
4508 * character at the start of the next line. */
4509 ++n_extra;
4510 --p_extra;
4511 }
4512 else
4513 {
4514 n_extra -= mb_l - 1;
4515 p_extra += mb_l - 1;
4516 }
4517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 ++p_extra;
4519 }
4520 --n_extra;
4521 }
4522 else
4523 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004524#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004525 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004526#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004527
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004528 if (p_extra_free != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01004529 VIM_CLEAR(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 /*
4531 * Get a character from the line itself.
4532 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004533 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004534#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004535 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004536#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 if (has_mbyte)
4538 {
4539 mb_c = c;
4540 if (enc_utf8)
4541 {
4542 /* If the UTF-8 character is more than one byte: Decode it
4543 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004544 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 mb_utf8 = FALSE;
4546 if (mb_l > 1)
4547 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004548 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 /* Overlong encoded ASCII or ASCII with composing char
4550 * is displayed normally, except a NUL. */
4551 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004552 {
4553 c = mb_c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004554#ifdef FEAT_LINEBREAK
Bram Moolenaar88e76882017-02-27 20:33:46 +01004555 c0 = mb_c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004556#endif
Bram Moolenaar88e76882017-02-27 20:33:46 +01004557 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004559
4560 /* At start of the line we can have a composing char.
4561 * Draw it as a space with a composing char. */
4562 if (utf_iscomposing(mb_c))
4563 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004564 int i;
4565
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004566 for (i = Screen_mco - 1; i > 0; --i)
4567 u8cc[i] = u8cc[i - 1];
4568 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004569 mb_c = ' ';
4570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 }
4572
4573 if ((mb_l == 1 && c >= 0x80)
4574 || (mb_l >= 1 && mb_c == 0)
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004575 || (mb_l > 1 && (!vim_isprintc(mb_c))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 {
4577 /*
4578 * Illegal UTF-8 byte: display as <xx>.
4579 * Non-BMP character : display as ? or fullwidth ?.
4580 */
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004581 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004582# ifdef FEAT_RIGHTLEFT
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004583 if (wp->w_p_rl) /* reverse */
4584 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004585# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 p_extra = extra;
4587 c = *p_extra;
4588 mb_c = mb_ptr2char_adv(&p_extra);
4589 mb_utf8 = (c >= 0x80);
4590 n_extra = (int)STRLEN(p_extra);
4591 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004592 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 if (area_attr == 0 && search_attr == 0)
4594 {
4595 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004596 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 saved_attr2 = char_attr; /* save current attr */
4598 }
4599 }
4600 else if (mb_l == 0) /* at the NUL at end-of-line */
4601 mb_l = 1;
4602#ifdef FEAT_ARABIC
4603 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4604 {
4605 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004606 int pc, pc1, nc;
4607 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608
4609 /* The idea of what is the previous and next
4610 * character depends on 'rightleft'. */
4611 if (wp->w_p_rl)
4612 {
4613 pc = prev_c;
4614 pc1 = prev_c1;
4615 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004616 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 }
4618 else
4619 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004620 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004622 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 }
4624 prev_c = mb_c;
4625
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004626 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 }
4628 else
4629 prev_c = mb_c;
4630#endif
4631 }
4632 else /* enc_dbcs */
4633 {
4634 mb_l = MB_BYTE2LEN(c);
4635 if (mb_l == 0) /* at the NUL at end-of-line */
4636 mb_l = 1;
4637 else if (mb_l > 1)
4638 {
4639 /* We assume a second byte below 32 is illegal.
4640 * Hopefully this is OK for all double-byte encodings!
4641 */
4642 if (ptr[1] >= 32)
4643 mb_c = (c << 8) + ptr[1];
4644 else
4645 {
4646 if (ptr[1] == NUL)
4647 {
4648 /* head byte at end of line */
4649 mb_l = 1;
4650 transchar_nonprint(extra, c);
4651 }
4652 else
4653 {
4654 /* illegal tail byte */
4655 mb_l = 2;
4656 STRCPY(extra, "XX");
4657 }
4658 p_extra = extra;
4659 n_extra = (int)STRLEN(extra) - 1;
4660 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004661 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 c = *p_extra++;
4663 if (area_attr == 0 && search_attr == 0)
4664 {
4665 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004666 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 saved_attr2 = char_attr; /* save current attr */
4668 }
4669 mb_c = c;
4670 }
4671 }
4672 }
4673 /* If a double-width char doesn't fit display a '>' in the
4674 * last column; the character is displayed at the start of the
4675 * next line. */
4676 if ((
4677# ifdef FEAT_RIGHTLEFT
4678 wp->w_p_rl ? (col <= 0) :
4679# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004680 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 && (*mb_char2cells)(mb_c) == 2)
4682 {
4683 c = '>';
4684 mb_c = c;
4685 mb_utf8 = FALSE;
4686 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004687 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02004688 // Put pointer back so that the character will be
4689 // displayed at the start of the next line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 --ptr;
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02004691#ifdef FEAT_CONCEAL
4692 did_decrement_ptr = TRUE;
4693#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 }
4695 else if (*ptr != NUL)
4696 ptr += mb_l - 1;
4697
4698 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004699 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004700 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004701 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004704 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004705 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 c = ' ';
4707 if (area_attr == 0 && search_attr == 0)
4708 {
4709 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004710 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 saved_attr2 = char_attr; /* save current attr */
4712 }
4713 mb_c = c;
4714 mb_utf8 = FALSE;
4715 mb_l = 1;
4716 }
4717
4718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 ++ptr;
4720
4721 if (extra_check)
4722 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004723#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004724 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004725#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004726
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004727#ifdef FEAT_TERMINAL
4728 if (get_term_attr)
4729 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02004730 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004731
4732 if (!attr_pri)
4733 char_attr = syntax_attr;
4734 else
4735 char_attr = hl_combine_attr(syntax_attr, char_attr);
4736 }
4737#endif
4738
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004739#ifdef FEAT_SYN_HL
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004740 // Get syntax attribute, unless still at the start of the line
4741 // (double-wide char that doesn't fit).
Bram Moolenaar217ad922005-03-20 22:37:15 +00004742 v = (long)(ptr - line);
4743 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 {
4745 /* Get the syntax attribute for the character. If there
4746 * is an error, disable syntax highlighting. */
4747 save_did_emsg = did_emsg;
4748 did_emsg = FALSE;
4749
Bram Moolenaar217ad922005-03-20 22:37:15 +00004750 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004751# ifdef FEAT_SPELL
4752 has_spell ? &can_spell :
4753# endif
4754 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755
4756 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004757 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004758 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004759 has_syntax = FALSE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004760 syntax_attr = 0;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 else
4763 did_emsg = save_did_emsg;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004764
4765 // combine syntax attribute with 'wincolor'
4766 if (win_attr != 0)
4767 syntax_attr = hl_combine_attr(win_attr, syntax_attr);
4768
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004769#ifdef SYN_TIME_LIMIT
4770 if (wp->w_s->b_syn_slow)
4771 has_syntax = FALSE;
4772#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773
4774 /* Need to get the line again, a multi-line regexp may
4775 * have made it invalid. */
4776 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4777 ptr = line + v;
4778
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004779# ifdef FEAT_TEXT_PROP
Bram Moolenaarde24a872019-05-05 15:48:00 +02004780 // Text properties overrule syntax highlighting or combine.
4781 if (text_prop_attr == 0 || text_prop_combine)
4782# endif
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004783 {
Bram Moolenaarde24a872019-05-05 15:48:00 +02004784 int comb_attr = syntax_attr;
4785# ifdef FEAT_TEXT_PROP
4786 comb_attr = hl_combine_attr(text_prop_attr, comb_attr);
4787# endif
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004788 if (!attr_pri)
Bram Moolenaarde24a872019-05-05 15:48:00 +02004789 char_attr = comb_attr;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004790 else
Bram Moolenaarde24a872019-05-05 15:48:00 +02004791 char_attr = hl_combine_attr(comb_attr, char_attr);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004792 }
Bram Moolenaarc095b282010-07-20 22:33:34 +02004793# ifdef FEAT_CONCEAL
4794 /* no concealing past the end of the line, it interferes
4795 * with line highlighting */
4796 if (c == NUL)
4797 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004798 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004799 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004800# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004802#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004803
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004804#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004805 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004806 * Only do this when there is no syntax highlighting, the
4807 * @Spell cluster is not used or the current syntax item
4808 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004809 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004810 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004811 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004812 if (c != 0 && (
4813# ifdef FEAT_SYN_HL
4814 !has_syntax ||
4815# endif
4816 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004817 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004818 char_u *prev_ptr, *p;
4819 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004820 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaare7566042005-06-17 22:00:15 +00004821 if (has_mbyte)
4822 {
4823 prev_ptr = ptr - mb_l;
4824 v -= mb_l - 1;
4825 }
4826 else
Bram Moolenaare7566042005-06-17 22:00:15 +00004827 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004828
4829 /* Use nextline[] if possible, it has the start of the
4830 * next line concatenated. */
4831 if ((prev_ptr - line) - nextlinecol >= 0)
4832 p = nextline + (prev_ptr - line) - nextlinecol;
4833 else
4834 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004835 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004836 len = spell_check(wp, p, &spell_hlf, &cap_col,
4837 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004838 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004839
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004840 /* In Insert mode only highlight a word that
4841 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004842 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004843 && (State & INSERT) != 0
4844 && wp->w_cursor.lnum == lnum
4845 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004846 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004847 && wp->w_cursor.col < (colnr_T)word_end)
4848 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004849 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004850 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004851 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004852
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004853 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004854 && (p - nextline) + len > nextline_idx)
4855 {
4856 /* Remember that the good word continues at the
4857 * start of the next line. */
4858 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004859 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004860 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004861
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004862 /* Turn index into actual attributes. */
4863 if (spell_hlf != HLF_COUNT)
4864 spell_attr = highlight_attr[spell_hlf];
4865
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004866 if (cap_col > 0)
4867 {
4868 if (p != prev_ptr
4869 && (p - nextline) + cap_col >= nextline_idx)
4870 {
4871 /* Remember that the word in the next line
4872 * must start with a capital. */
4873 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004874 cap_col = (int)((p - nextline) + cap_col
4875 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004876 }
4877 else
4878 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004879 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004880 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004881 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004882 }
4883 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004884 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004885 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004886 char_attr = hl_combine_attr(char_attr, spell_attr);
4887 else
4888 char_attr = hl_combine_attr(spell_attr, char_attr);
4889 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890#endif
4891#ifdef FEAT_LINEBREAK
4892 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004893 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004895 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004896 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 {
Bram Moolenaar4df70292015-03-21 14:20:16 +01004898 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004899 char_u *p = ptr - (mb_off + 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004900
Bram Moolenaar597a4222014-06-25 14:39:50 +02004901 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004902 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004903 NULL) - 1;
Bram Moolenaar02631462017-09-22 15:20:32 +02004904 if (c == TAB && n_extra + col > wp->w_width)
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004905# ifdef FEAT_VARTABS
Bram Moolenaara87b72c2018-06-25 21:24:51 +02004906 n_extra = tabstop_padding(vcol, wp->w_buffer->b_p_ts,
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004907 wp->w_buffer->b_p_vts_array) - 1;
4908# else
Bram Moolenaara3650912014-11-19 13:21:57 +01004909 n_extra = (int)wp->w_buffer->b_p_ts
4910 - vcol % (int)wp->w_buffer->b_p_ts - 1;
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004911# endif
Bram Moolenaara3650912014-11-19 13:21:57 +01004912
Bram Moolenaar4df70292015-03-21 14:20:16 +01004913 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004914 c_final = NUL;
Bram Moolenaar1c465442017-03-12 20:10:05 +01004915 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004916 {
4917#ifdef FEAT_CONCEAL
4918 if (c == TAB)
4919 /* See "Tab alignment" below. */
4920 FIX_FOR_BOGUSCOLS;
4921#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004922 if (!wp->w_p_list)
4923 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 }
4926#endif
4927
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004928 // 'list': Change char 160 to lcs_nbsp and space to lcs_space.
4929 // But not when the character is followed by a composing
4930 // character (use mb_l to check that).
4931 if (wp->w_p_list
4932 && ((((c == 160 && mb_l == 1)
4933 || (mb_utf8
4934 && ((mb_c == 160 && mb_l == 2)
4935 || (mb_c == 0x202f && mb_l == 3))))
4936 && lcs_nbsp)
4937 || (c == ' '
4938 && mb_l == 1
4939 && lcs_space
4940 && ptr - line <= trailcol)))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004941 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004942 c = (c == ' ') ? lcs_space : lcs_nbsp;
4943 if (area_attr == 0 && search_attr == 0)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004944 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004945 n_attr = 1;
4946 extra_attr = HL_ATTR(HLF_8);
4947 saved_attr2 = char_attr; /* save current attr */
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004948 }
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004949 mb_c = c;
4950 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004951 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004952 mb_utf8 = TRUE;
4953 u8cc[0] = 0;
4954 c = 0xc0;
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004955 }
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004956 else
4957 mb_utf8 = FALSE;
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004958 }
4959
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4961 {
4962 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004963 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 {
4965 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004966 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 saved_attr2 = char_attr; /* save current attr */
4968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004970 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 {
4972 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004973 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004974 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 }
4976 else
4977 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 }
4979 }
4980
4981 /*
4982 * Handling of non-printable characters.
4983 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004984 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 {
4986 /*
4987 * when getting a character from the file, we may have to
4988 * turn it into something else on the way to putting it
4989 * into "ScreenLines".
4990 */
4991 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4992 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004993 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004994 long vcol_adjusted = vcol; /* removed showbreak length */
4995#ifdef FEAT_LINEBREAK
4996 /* only adjust the tab_len, when at the first column
4997 * after the showbreak value was drawn */
4998 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4999 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
5000#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 /* tab amount depends on current column */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005002#ifdef FEAT_VARTABS
5003 tab_len = tabstop_padding(vcol_adjusted,
5004 wp->w_buffer->b_p_ts,
5005 wp->w_buffer->b_p_vts_array) - 1;
5006#else
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005007 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005008 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
5009#endif
Bram Moolenaard574ea22015-01-14 19:35:14 +01005010
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005011#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02005012 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005013#endif
5014 /* tab amount depends on current column */
5015 n_extra = tab_len;
5016#ifdef FEAT_LINEBREAK
5017 else
5018 {
5019 char_u *p;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01005020 int len;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005021 int i;
5022 int saved_nextra = n_extra;
5023
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005024#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005025 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005026 /* there are characters to conceal */
5027 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005028 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
5029 */
5030 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
5031 && n_extra > tab_len)
5032 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005033#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005034
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005035 /* if n_extra > 0, it gives the number of chars, to
5036 * use for a tab, else we need to calculate the width
5037 * for a tab */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005038 len = (tab_len * mb_char2len(lcs_tab2));
5039 if (n_extra > 0)
5040 len += n_extra - tab_len;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005041 c = lcs_tab1;
Bram Moolenaar964b3742019-05-24 18:54:09 +02005042 p = alloc(len + 1);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005043 vim_memset(p, ' ', len);
5044 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005045 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005046 p_extra_free = p;
5047 for (i = 0; i < tab_len; i++)
5048 {
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02005049 if (*p == NUL)
5050 {
5051 tab_len = i;
5052 break;
5053 }
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005054 mb_char2bytes(lcs_tab2, p);
5055 p += mb_char2len(lcs_tab2);
5056 n_extra += mb_char2len(lcs_tab2)
5057 - (saved_nextra > 0 ? 1 : 0);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005058 }
5059 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005060#ifdef FEAT_CONCEAL
5061 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
5062 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005063 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005064 n_extra -= vcol_off;
5065#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005066 }
5067#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005068#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005069 {
5070 int vc_saved = vcol_off;
5071
5072 /* Tab alignment should be identical regardless of
5073 * 'conceallevel' value. So tab compensates of all
5074 * previous concealed characters, and thus resets
5075 * vcol_off and boguscols accumulated so far in the
5076 * line. Note that the tab can be longer than
5077 * 'tabstop' when there are concealed characters. */
5078 FIX_FOR_BOGUSCOLS;
5079
5080 /* Make sure, the highlighting for the tab char will be
5081 * correctly set further below (effectively reverts the
5082 * FIX_FOR_BOGSUCOLS macro */
5083 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005084 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005085 tab_len += vc_saved;
5086 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005087#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 if (wp->w_p_list)
5090 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01005091 c = (n_extra == 0 && lcs_tab3) ? lcs_tab3 : lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005092#ifdef FEAT_LINEBREAK
5093 if (wp->w_p_lbr)
5094 c_extra = NUL; /* using p_extra from above */
5095 else
5096#endif
5097 c_extra = lcs_tab2;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005098 c_final = lcs_tab3;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005099 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005100 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 saved_attr2 = char_attr; /* save current attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005103 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 {
5105 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005106 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005107 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 }
5110 else
5111 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01005112 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 c_extra = ' ';
5114 c = ' ';
5115 }
5116 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005117 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02005118 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005119 || ((fromcol >= 0 || fromcol_prev >= 0)
5120 && tocol > vcol
5121 && VIsual_mode != Ctrl_V
5122 && (
5123# ifdef FEAT_RIGHTLEFT
5124 wp->w_p_rl ? (col >= 0) :
5125# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005126 (col < wp->w_width))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005127 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005128 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005129 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02005130 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005132 /* Display a '$' after the line or highlight an extra
5133 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134#if defined(FEAT_DIFF) || defined(LINE_ATTR)
5135 /* For a diff line the highlighting continues after the
5136 * "$". */
5137 if (
5138# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005139 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140# ifdef LINE_ATTR
5141 &&
5142# endif
5143# endif
5144# ifdef LINE_ATTR
5145 line_attr == 0
5146# endif
5147 )
5148#endif
5149 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 /* In virtualedit, visual selections may extend
5151 * beyond end of line. */
5152 if (area_highlighting && virtual_active()
5153 && tocol != MAXCOL && vcol < tocol)
5154 n_extra = 0;
5155 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 {
5157 p_extra = at_end_str;
5158 n_extra = 1;
5159 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005160 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 }
5162 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02005163 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005164 c = lcs_eol;
5165 else
5166 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 lcs_eol_one = -1;
5168 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005169 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005171 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 n_attr = 1;
5173 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005175 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 {
5177 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005178 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005179 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 }
5181 else
5182 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 }
5184 else if (c != NUL)
5185 {
5186 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02005187 if (n_extra == 0)
5188 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189#ifdef FEAT_RIGHTLEFT
5190 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
5191 rl_mirror(p_extra); /* reverse "<12>" */
5192#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005194 c_final = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005195#ifdef FEAT_LINEBREAK
5196 if (wp->w_p_lbr)
5197 {
5198 char_u *p;
5199
5200 c = *p_extra;
Bram Moolenaar964b3742019-05-24 18:54:09 +02005201 p = alloc(n_extra + 1);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005202 vim_memset(p, ' ', n_extra);
5203 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5204 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005205 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005206 p_extra_free = p_extra = p;
5207 }
5208 else
5209#endif
5210 {
5211 n_extra = byte2cells(c) - 1;
5212 c = *p_extra++;
5213 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005214 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 {
5216 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005217 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 saved_attr2 = char_attr; /* save current attr */
5219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 else if (VIsual_active
5223 && (VIsual_mode == Ctrl_V
5224 || VIsual_mode == 'v')
5225 && virtual_active()
5226 && tocol != MAXCOL
5227 && vcol < tocol
5228 && (
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005229#ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 wp->w_p_rl ? (col >= 0) :
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005231#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005232 (col < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 {
5234 c = ' ';
5235 --ptr; /* put it back at the NUL */
5236 }
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005237#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 else if ((
5239# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005240 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005242# ifdef FEAT_TERMINAL
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005243 win_attr != 0 ||
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005244# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 ) && (
5247# ifdef FEAT_RIGHTLEFT
5248 wp->w_p_rl ? (col >= 0) :
5249# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005250 (col
5251# ifdef FEAT_CONCEAL
5252 - boguscols
5253# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005254 < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 {
5256 /* Highlight until the right side of the window */
5257 c = ' ';
5258 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005259
5260 /* Remember we do the char for line highlighting. */
5261 ++did_line_attr;
5262
5263 /* don't do search HL for the rest of the line */
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005264 if (line_attr != 0 && char_attr == search_attr
5265 && (did_line_attr > 1
5266 || (wp->w_p_list && lcs_eol > 0)))
Bram Moolenaar91170f82006-05-05 21:15:17 +00005267 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268# ifdef FEAT_DIFF
5269 if (diff_hlf == HLF_TXD)
5270 {
5271 diff_hlf = HLF_CHD;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005272 if (vi_attr == 0 || char_attr != vi_attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005273 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005274 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005275 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5276 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005277 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 }
5280# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005281# ifdef FEAT_TERMINAL
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005282 if (win_attr != 0)
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005283 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005284 char_attr = win_attr;
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005285 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5286 char_attr = hl_combine_attr(char_attr,
5287 HL_ATTR(HLF_CUL));
5288 }
5289# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 }
5291#endif
5292 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005293
5294#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005295 if ( wp->w_p_cole > 0
5296 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaarab62c192019-03-30 16:39:05 +01005297 conceal_cursor_line(wp))
5298 && ((syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005299 && !(lnum_in_visual_area
5300 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005301 {
5302 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005303 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005304 && (syn_get_sub_char() != NUL || match_conc
5305 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005306 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005307 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005308 /* First time at this concealed item: display one
5309 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005310 if (match_conc)
5311 c = match_conc;
5312 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005313 c = syn_get_sub_char();
5314 else if (lcs_conceal != NUL)
5315 c = lcs_conceal;
5316 else
5317 c = ' ';
5318
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005319 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005320
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005321 if (n_extra > 0)
5322 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005323 vcol += n_extra;
5324 if (wp->w_p_wrap && n_extra > 0)
5325 {
5326# ifdef FEAT_RIGHTLEFT
5327 if (wp->w_p_rl)
5328 {
5329 col -= n_extra;
5330 boguscols -= n_extra;
5331 }
5332 else
5333# endif
5334 {
5335 boguscols += n_extra;
5336 col += n_extra;
5337 }
5338 }
5339 n_extra = 0;
5340 n_attr = 0;
5341 }
5342 else if (n_skip == 0)
5343 {
5344 is_concealing = TRUE;
5345 n_skip = 1;
5346 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005347 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005348 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005349 {
5350 mb_utf8 = TRUE;
5351 u8cc[0] = 0;
5352 c = 0xc0;
5353 }
5354 else
5355 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005356 }
5357 else
5358 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005359 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005360 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005361 }
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02005362
5363 if (n_skip > 0 && did_decrement_ptr)
5364 // not showing the '>', put pointer back to avoid getting stuck
5365 ++ptr;
5366
5367#endif // FEAT_CONCEAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 }
5369
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005370#ifdef FEAT_CONCEAL
5371 /* In the cursor line and we may be concealing characters: correct
5372 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005373 if (!did_wcol && draw_state == WL_LINE
5374 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005375 && conceal_cursor_line(wp)
5376 && (int)wp->w_virtcol <= vcol + n_skip)
5377 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005378# ifdef FEAT_RIGHTLEFT
5379 if (wp->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02005380 wp->w_wcol = wp->w_width - col + boguscols - 1;
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005381 else
5382# endif
5383 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005384 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005385 did_wcol = TRUE;
5386 }
5387#endif
5388
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 /* Don't override visual selection highlighting. */
5390 if (n_attr > 0
5391 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005392 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 char_attr = extra_attr;
5394
Bram Moolenaar81695252004-12-29 20:58:21 +00005395#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 /* XIM don't send preedit_start and preedit_end, but they send
5397 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5398 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005399 if (p_imst == IM_ON_THE_SPOT
5400 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005401 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402 && (State & INSERT)
5403 && !p_imdisable
5404 && im_is_preediting()
5405 && draw_state == WL_LINE)
5406 {
5407 colnr_T tcol;
5408
5409 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005410 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 else
5412 tcol = preedit_end_col;
5413 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5414 {
5415 if (feedback_old_attr < 0)
5416 {
5417 feedback_col = 0;
5418 feedback_old_attr = char_attr;
5419 }
5420 char_attr = im_get_feedback_attr(feedback_col);
5421 if (char_attr < 0)
5422 char_attr = feedback_old_attr;
5423 feedback_col++;
5424 }
5425 else if (feedback_old_attr >= 0)
5426 {
5427 char_attr = feedback_old_attr;
5428 feedback_old_attr = -1;
5429 feedback_col = 0;
5430 }
5431 }
5432#endif
5433 /*
5434 * Handle the case where we are in column 0 but not on the first
5435 * character of the line and the user wants us to show us a
5436 * special character (via 'listchars' option "precedes:<char>".
5437 */
5438 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005439 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5441#ifdef FEAT_DIFF
5442 && filler_todo <= 0
5443#endif
5444 && draw_state > WL_NR
5445 && c != NUL)
5446 {
5447 c = lcs_prec;
5448 lcs_prec_todo = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005449 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5450 {
5451 /* Double-width character being overwritten by the "precedes"
5452 * character, need to fill up half the character. */
5453 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005454 c_final = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005455 n_extra = 1;
5456 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005457 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005458 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005460 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 {
5462 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005463 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005464 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 }
5466 else
5467 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005468 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 {
5470 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005471 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 n_attr3 = 1;
5473 }
5474 }
5475
5476 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005477 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005479 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005480#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005481 || did_line_attr == 1
5482#endif
5483 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005485#ifdef FEAT_SEARCH_EXTRA
5486 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005487
5488 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005489 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005490 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005491#endif
5492
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005493 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 * highlight match at end of line. If it's beyond the last
5495 * char on the screen, just overwrite that one (tricky!) Not
5496 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005497#ifdef FEAT_SEARCH_EXTRA
5498 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005499 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005500 prevcol_hl_flag = TRUE;
5501 else
5502 {
5503 cur = wp->w_match_head;
5504 while (cur != NULL)
5505 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005506 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005507 {
5508 prevcol_hl_flag = TRUE;
5509 break;
5510 }
5511 cur = cur->next;
5512 }
5513 }
5514#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005516 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005517 && (VIsual_mode != Ctrl_V
5518 || lnum == VIsual.lnum
5519 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005520 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521#ifdef FEAT_SEARCH_EXTRA
5522 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005523 || (prevcol_hl_flag == TRUE
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005524# ifdef FEAT_SYN_HL
5525 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
5526 && !(wp == curwin && VIsual_active))
5527# endif
5528# ifdef FEAT_DIFF
5529 && diff_hlf == (hlf_T)0
5530# endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005531# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005532 && did_line_attr <= 1
5533# endif
5534 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535#endif
5536 ))
5537 {
5538 int n = 0;
5539
5540#ifdef FEAT_RIGHTLEFT
5541 if (wp->w_p_rl)
5542 {
5543 if (col < 0)
5544 n = 1;
5545 }
5546 else
5547#endif
5548 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005549 if (col >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 n = -1;
5551 }
5552 if (n != 0)
5553 {
5554 /* At the window boundary, highlight the last character
5555 * instead (better than nothing). */
5556 off += n;
5557 col += n;
5558 }
5559 else
5560 {
5561 /* Add a blank character to highlight. */
5562 ScreenLines[off] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 if (enc_utf8)
5564 ScreenLinesUC[off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 }
5566#ifdef FEAT_SEARCH_EXTRA
5567 if (area_attr == 0)
5568 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005569 /* Use attributes from match with highest priority among
5570 * 'search_hl' and the match list. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005571 cur = wp->w_match_head;
5572 shl_flag = FALSE;
5573 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005574 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005575 if (shl_flag == FALSE
5576 && ((cur != NULL
5577 && cur->priority > SEARCH_HL_PRIORITY)
5578 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005579 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005580 shl = &search_hl;
5581 shl_flag = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02005582 if (screen_line_flags & SLF_POPUP)
5583 continue; // do not use search_hl
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005584 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005585 else
5586 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005587 if ((ptr - line) - 1 == (long)shl->startcol
5588 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005589 char_attr = shl->attr;
5590 if (shl != &search_hl && cur != NULL)
5591 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 }
5594#endif
5595 ScreenAttrs[off] = char_attr;
5596#ifdef FEAT_RIGHTLEFT
5597 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005598 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005600 --off;
5601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 else
5603#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005604 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005606 ++off;
5607 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005608 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005609#ifdef FEAT_SYN_HL
5610 eol_hl_off = 1;
5611#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614
Bram Moolenaar91170f82006-05-05 21:15:17 +00005615 /*
5616 * At end of the text line.
5617 */
5618 if (c == NUL)
5619 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005620#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005621 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005622 if (wp->w_p_wrap)
5623 v = wp->w_skipcol;
5624 else
5625 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005626
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005627 /* check if line ends before left margin */
5628 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005629 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005630#ifdef FEAT_CONCEAL
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01005631 // Get rid of the boguscols now, we want to draw until the right
5632 // edge for 'cursorcolumn'.
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005633 col -= boguscols;
5634 boguscols = 0;
5635#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005636
5637 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005638 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005639
5640 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005641 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5642 && (int)wp->w_virtcol <
Bram Moolenaar02631462017-09-22 15:20:32 +02005643 wp->w_width * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005644 && lnum != wp->w_cursor.lnum)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005645 || draw_color_col
5646 || win_attr != 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005647# ifdef FEAT_RIGHTLEFT
5648 && !wp->w_p_rl
5649# endif
5650 )
5651 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005652 int rightmost_vcol = 0;
5653 int i;
5654
5655 if (wp->w_p_cuc)
5656 rightmost_vcol = wp->w_virtcol;
5657 if (draw_color_col)
5658 /* determine rightmost colorcolumn to possibly draw */
5659 for (i = 0; color_cols[i] >= 0; ++i)
5660 if (rightmost_vcol < color_cols[i])
5661 rightmost_vcol = color_cols[i];
5662
Bram Moolenaar02631462017-09-22 15:20:32 +02005663 while (col < wp->w_width)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005664 {
5665 ScreenLines[off] = ' ';
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005666 if (enc_utf8)
5667 ScreenLinesUC[off] = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005668 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005669 if (draw_color_col)
5670 draw_color_col = advance_color_col(VCOL_HLC,
5671 &color_cols);
5672
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005673 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005674 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005675 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005676 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005677 else
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005678 ScreenAttrs[off++] = win_attr;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005679
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005680 if (VCOL_HLC >= rightmost_vcol && win_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005681 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005682
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005683 ++vcol;
5684 }
5685 }
5686#endif
5687
Bram Moolenaar53f81742017-09-22 14:35:51 +02005688 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005689 (int)wp->w_width, screen_line_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 row++;
5691
5692 /*
5693 * Update w_cline_height and w_cline_folded if the cursor line was
5694 * updated (saves a call to plines() later).
5695 */
5696 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5697 {
5698 curwin->w_cline_row = startrow;
5699 curwin->w_cline_height = row - startrow;
5700#ifdef FEAT_FOLDING
5701 curwin->w_cline_folded = FALSE;
5702#endif
5703 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5704 }
5705
5706 break;
5707 }
5708
Bram Moolenaara5c6a0b2019-05-08 20:20:46 +02005709 // Show "extends" character from 'listchars' if beyond the line end and
5710 // 'list' is set.
5711 if (lcs_ext != NUL
5712 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 && !wp->w_p_wrap
5714#ifdef FEAT_DIFF
5715 && filler_todo <= 0
5716#endif
5717 && (
5718#ifdef FEAT_RIGHTLEFT
5719 wp->w_p_rl ? col == 0 :
5720#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005721 col == wp->w_width - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005723 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5725 {
5726 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005727 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005729 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 {
5731 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005732 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005733 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 }
5735 else
5736 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 }
5738
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005739#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005740 /* advance to the next 'colorcolumn' */
5741 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005742 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005743
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005744 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005745 * highlight the cursor position itself.
5746 * Also highlight the 'colorcolumn' if it is different than
5747 * 'cursorcolumn' */
5748 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005749 if (draw_state == WL_LINE && !lnum_in_visual_area
5750 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005751 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005752 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005753 && lnum != wp->w_cursor.lnum)
5754 {
5755 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005756 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005757 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005758 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005759 {
5760 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005761 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005762 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005763 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005764#endif
5765
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 /*
5767 * Store character to be displayed.
5768 * Skip characters that are left of the screen for 'nowrap'.
5769 */
5770 vcol_prev = vcol;
5771 if (draw_state < WL_LINE || n_skip <= 0)
5772 {
5773 /*
5774 * Store the character.
5775 */
Bram Moolenaara12a1612019-01-24 16:39:02 +01005776#if defined(FEAT_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5778 {
5779 /* A double-wide character is: put first halve in left cell. */
5780 --off;
5781 --col;
5782 }
5783#endif
5784 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005786 {
5787 if ((mb_c & 0xff00) == 0x8e00)
5788 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791 else if (enc_utf8)
5792 {
5793 if (mb_utf8)
5794 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005795 int i;
5796
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005798 if ((c & 0xff) == 0)
5799 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005800 for (i = 0; i < Screen_mco; ++i)
5801 {
5802 ScreenLinesC[i][off] = u8cc[i];
5803 if (u8cc[i] == 0)
5804 break;
5805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 }
5807 else
5808 ScreenLinesUC[off] = 0;
5809 }
5810 if (multi_attr)
5811 {
5812 ScreenAttrs[off] = multi_attr;
5813 multi_attr = 0;
5814 }
5815 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 ScreenAttrs[off] = char_attr;
5817
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5819 {
5820 /* Need to fill two screen columns. */
5821 ++off;
5822 ++col;
5823 if (enc_utf8)
5824 /* UTF-8: Put a 0 in the second screen char. */
5825 ScreenLines[off] = 0;
5826 else
5827 /* DBCS: Put second byte in the second screen char. */
5828 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005829 if (draw_state > WL_NR
5830#ifdef FEAT_DIFF
5831 && filler_todo <= 0
5832#endif
5833 )
5834 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 /* When "tocol" is halfway a character, set it to the end of
5836 * the character, otherwise highlighting won't stop. */
5837 if (tocol == vcol)
5838 ++tocol;
5839#ifdef FEAT_RIGHTLEFT
5840 if (wp->w_p_rl)
5841 {
5842 /* now it's time to backup one cell */
5843 --off;
5844 --col;
5845 }
5846#endif
5847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848#ifdef FEAT_RIGHTLEFT
5849 if (wp->w_p_rl)
5850 {
5851 --off;
5852 --col;
5853 }
5854 else
5855#endif
5856 {
5857 ++off;
5858 ++col;
5859 }
5860 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005861#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005862 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005863 {
5864 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005865 ++vcol_off;
5866 if (n_extra > 0)
5867 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005868 if (wp->w_p_wrap)
5869 {
5870 /*
5871 * Special voodoo required if 'wrap' is on.
5872 *
5873 * Advance the column indicator to force the line
5874 * drawing to wrap early. This will make the line
5875 * take up the same screen space when parts are concealed,
5876 * so that cursor line computations aren't messed up.
5877 *
5878 * To avoid the fictitious advance of 'col' causing
5879 * trailing junk to be written out of the screen line
5880 * we are building, 'boguscols' keeps track of the number
5881 * of bad columns we have advanced.
5882 */
5883 if (n_extra > 0)
5884 {
5885 vcol += n_extra;
5886# ifdef FEAT_RIGHTLEFT
5887 if (wp->w_p_rl)
5888 {
5889 col -= n_extra;
5890 boguscols -= n_extra;
5891 }
5892 else
5893# endif
5894 {
5895 col += n_extra;
5896 boguscols += n_extra;
5897 }
5898 n_extra = 0;
5899 n_attr = 0;
5900 }
5901
5902
Bram Moolenaar860cae12010-06-05 23:22:07 +02005903 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5904 {
5905 /* Need to fill two screen columns. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01005906# ifdef FEAT_RIGHTLEFT
Bram Moolenaar860cae12010-06-05 23:22:07 +02005907 if (wp->w_p_rl)
5908 {
5909 --boguscols;
5910 --col;
5911 }
5912 else
Bram Moolenaara12a1612019-01-24 16:39:02 +01005913# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02005914 {
5915 ++boguscols;
5916 ++col;
5917 }
5918 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005919
5920# ifdef FEAT_RIGHTLEFT
5921 if (wp->w_p_rl)
5922 {
5923 --boguscols;
5924 --col;
5925 }
5926 else
5927# endif
5928 {
5929 ++boguscols;
5930 ++col;
5931 }
5932 }
5933 else
5934 {
5935 if (n_extra > 0)
5936 {
5937 vcol += n_extra;
5938 n_extra = 0;
5939 n_attr = 0;
5940 }
5941 }
5942
5943 }
5944#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945 else
5946 --n_skip;
5947
Bram Moolenaar64486672010-05-16 15:46:46 +02005948 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5949 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005950 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951#ifdef FEAT_DIFF
5952 && filler_todo <= 0
5953#endif
5954 )
5955 ++vcol;
5956
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005957#ifdef FEAT_SYN_HL
5958 if (vcol_save_attr >= 0)
5959 char_attr = vcol_save_attr;
5960#endif
5961
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 /* restore attributes after "predeces" in 'listchars' */
5963 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5964 char_attr = saved_attr3;
5965
5966 /* restore attributes after last 'listchars' or 'number' char */
5967 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5968 char_attr = saved_attr2;
5969
5970 /*
5971 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005972 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973 */
5974 if ((
5975#ifdef FEAT_RIGHTLEFT
5976 wp->w_p_rl ? (col < 0) :
5977#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005978 (col >= wp->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979 && (*ptr != NUL
5980#ifdef FEAT_DIFF
5981 || filler_todo > 0
5982#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005983 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5985 )
5986 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005987#ifdef FEAT_CONCEAL
Bram Moolenaar53f81742017-09-22 14:35:51 +02005988 screen_line(screen_row, wp->w_wincol, col - boguscols,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005989 (int)wp->w_width, screen_line_flags);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005990 boguscols = 0;
5991#else
Bram Moolenaar53f81742017-09-22 14:35:51 +02005992 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005993 (int)wp->w_width, screen_line_flags);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005994#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995 ++row;
5996 ++screen_row;
5997
5998 /* When not wrapping and finished diff lines, or when displayed
5999 * '$' and highlighting until last column, break here. */
6000 if ((!wp->w_p_wrap
6001#ifdef FEAT_DIFF
6002 && filler_todo <= 0
6003#endif
6004 ) || lcs_eol_one == -1)
6005 break;
6006
6007 /* When the window is too narrow draw all "@" lines. */
6008 if (draw_state != WL_LINE
6009#ifdef FEAT_DIFF
6010 && filler_todo <= 0
6011#endif
6012 )
6013 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01006014 win_draw_end(wp, '@', ' ', TRUE, row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016 row = endrow;
6017 }
6018
6019 /* When line got too long for screen break here. */
6020 if (row == endrow)
6021 {
6022 ++row;
6023 break;
6024 }
6025
6026 if (screen_cur_row == screen_row - 1
6027#ifdef FEAT_DIFF
6028 && filler_todo <= 0
6029#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02006030 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031 {
6032 /* Remember that the line wraps, used for modeless copy. */
6033 LineWraps[screen_row - 1] = TRUE;
6034
6035 /*
6036 * Special trick to make copy/paste of wrapped lines work with
6037 * xterm/screen: write an extra character beyond the end of
6038 * the line. This will work with all terminal types
6039 * (regardless of the xn,am settings).
6040 * Only do this on a fast tty.
6041 * Only do this if the cursor is on the current line
6042 * (something has been written in it).
6043 * Don't do this for the GUI.
6044 * Don't do this for double-width characters.
6045 * Don't do this for a window not at the right screen border.
6046 */
6047 if (p_tf
6048#ifdef FEAT_GUI
6049 && !gui.in_use
6050#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00006052 && ((*mb_off2cells)(LineOffset[screen_row],
6053 LineOffset[screen_row] + screen_Columns)
6054 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00006056 + (int)Columns - 2,
6057 LineOffset[screen_row] + screen_Columns)
Bram Moolenaara12a1612019-01-24 16:39:02 +01006058 == 2)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 {
6060 /* First make sure we are at the end of the screen line,
6061 * then output the same character again to let the
6062 * terminal know about the wrap. If the terminal doesn't
6063 * auto-wrap, we overwrite the character. */
Bram Moolenaar02631462017-09-22 15:20:32 +02006064 if (screen_cur_col != wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065 screen_char(LineOffset[screen_row - 1]
6066 + (unsigned)Columns - 1,
6067 screen_row - 1, (int)(Columns - 1));
6068
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069 /* When there is a multi-byte character, just output a
6070 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00006071 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
6072 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073 out_char(' ');
6074 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075 out_char(ScreenLines[LineOffset[screen_row - 1]
6076 + (Columns - 1)]);
6077 /* force a redraw of the first char on the next line */
6078 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
6079 screen_start(); /* don't know where cursor is now */
6080 }
6081 }
6082
6083 col = 0;
6084 off = (unsigned)(current_ScreenLine - ScreenLines);
6085#ifdef FEAT_RIGHTLEFT
6086 if (wp->w_p_rl)
6087 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006088 col = wp->w_width - 1; /* col is not used if breaking! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089 off += col;
6090 }
6091#endif
6092
6093 /* reset the drawing state for the start of a wrapped line */
6094 draw_state = WL_START;
6095 saved_n_extra = n_extra;
6096 saved_p_extra = p_extra;
6097 saved_c_extra = c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01006098 saved_c_final = c_final;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099 saved_char_attr = char_attr;
6100 n_extra = 0;
6101 lcs_prec_todo = lcs_prec;
6102#ifdef FEAT_LINEBREAK
6103# ifdef FEAT_DIFF
6104 if (filler_todo <= 0)
6105# endif
6106 need_showbreak = TRUE;
6107#endif
6108#ifdef FEAT_DIFF
6109 --filler_todo;
6110 /* When the filler lines are actually below the last line of the
6111 * file, don't draw the line itself, break here. */
6112 if (filler_todo == 0 && wp->w_botfill)
6113 break;
6114#endif
6115 }
6116
6117 } /* for every character in the line */
6118
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006119#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006120 /* After an empty line check first word for capital. */
6121 if (*skipwhite(line) == NUL)
6122 {
6123 capcol_lnum = lnum + 1;
6124 cap_col = 0;
6125 }
6126#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006127#ifdef FEAT_TEXT_PROP
6128 vim_free(text_props);
6129 vim_free(text_prop_idxs);
6130#endif
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006131
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01006132 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133 return row;
6134}
6135
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006136/*
6137 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006138 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006139 */
6140 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006141comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006142{
6143 int i;
6144
6145 for (i = 0; i < Screen_mco; ++i)
6146 {
6147 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
6148 return TRUE;
6149 if (ScreenLinesC[i][off_from] == 0)
6150 break;
6151 }
6152 return FALSE;
6153}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006154
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155/*
6156 * Check whether the given character needs redrawing:
6157 * - the (first byte of the) character is different
6158 * - the attributes are different
6159 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006160 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 */
6162 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006163char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164{
6165 if (cols > 0
6166 && ((ScreenLines[off_from] != ScreenLines[off_to]
6167 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 || (enc_dbcs != 0
6169 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
6170 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
6171 ? ScreenLines2[off_from] != ScreenLines2[off_to]
6172 : (cols > 1 && ScreenLines[off_from + 1]
6173 != ScreenLines[off_to + 1])))
6174 || (enc_utf8
6175 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6176 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006177 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006178 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6179 && ScreenLines[off_from + 1]
Bram Moolenaara12a1612019-01-24 16:39:02 +01006180 != ScreenLines[off_to + 1])))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181 return TRUE;
6182 return FALSE;
6183}
6184
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006185#if defined(FEAT_TERMINAL) || defined(PROTO)
6186/*
6187 * Return the index in ScreenLines[] for the current screen line.
6188 */
6189 int
6190screen_get_current_line_off()
6191{
6192 return (int)(current_ScreenLine - ScreenLines);
6193}
6194#endif
6195
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196/*
6197 * Move one "cooked" screen line to the screen, but only the characters that
6198 * have actually changed. Handle insert/delete character.
6199 * "coloff" gives the first column on the screen for this line.
6200 * "endcol" gives the columns where valid characters are.
6201 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6202 * needs to be cleared, negative otherwise.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006203 * "flags" can have bits:
6204 * SLF_POPUP popup window
6205 * SLF_RIGHTLEFT rightleft window:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6207 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6208 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006209 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006210screen_line(
6211 int row,
6212 int coloff,
6213 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006214 int clear_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006215 int flags UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216{
6217 unsigned off_from;
6218 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006219 unsigned max_off_from;
6220 unsigned max_off_to;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 int force = FALSE; /* force update rest of the line */
6224 int redraw_this /* bool: does character need redraw? */
6225#ifdef FEAT_GUI
6226 = TRUE /* For GUI when while-loop empty */
6227#endif
6228 ;
6229 int redraw_next; /* redraw_this for next character */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 int clear_next = FALSE;
6231 int char_cells; /* 1: normal char */
6232 /* 2: occupies two display cells */
6233# define CHAR_CELLS char_cells
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006235 /* Check for illegal row and col, just in case. */
6236 if (row >= Rows)
6237 row = Rows - 1;
6238 if (endcol > Columns)
6239 endcol = Columns;
6240
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241# ifdef FEAT_CLIPBOARD
6242 clip_may_clear_selection(row, row);
6243# endif
6244
6245 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6246 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006247 max_off_from = off_from + screen_Columns;
6248 max_off_to = LineOffset[row] + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249
6250#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006251 if (flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252 {
6253 /* Clear rest first, because it's left of the text. */
6254 if (clear_width > 0)
6255 {
6256 while (col <= endcol && ScreenLines[off_to] == ' '
6257 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006258 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 {
6260 ++off_to;
6261 ++col;
6262 }
6263 if (col <= endcol)
6264 screen_fill(row, row + 1, col + coloff,
6265 endcol + coloff + 1, ' ', ' ', 0);
6266 }
6267 col = endcol + 1;
6268 off_to = LineOffset[row] + col + coloff;
6269 off_from += col;
6270 endcol = (clear_width > 0 ? clear_width : -clear_width);
6271 }
6272#endif /* FEAT_RIGHTLEFT */
6273
6274 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6275
6276 while (col < endcol)
6277 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006279 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 else
6281 char_cells = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282
6283 redraw_this = redraw_next;
6284 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6285 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6286
6287#ifdef FEAT_GUI
6288 /* If the next character was bold, then redraw the current character to
6289 * remove any pixels that might have spilt over into us. This only
6290 * happens in the GUI.
6291 */
6292 if (redraw_next && gui.in_use)
6293 {
6294 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006295 if (hl > HL_ALL)
6296 hl = syn_attr2attr(hl);
6297 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298 redraw_this = TRUE;
6299 }
6300#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02006301#ifdef FEAT_TEXT_PROP
6302 // Skip if under a(nother) popup.
6303 if (popup_mask[row * screen_Columns + col + coloff] > screen_zindex)
6304 redraw_this = FALSE;
6305#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306
6307 if (redraw_this)
6308 {
6309 /*
6310 * Special handling when 'xs' termcap flag set (hpterm):
6311 * Attributes for characters are stored at the position where the
6312 * cursor is when writing the highlighting code. The
6313 * start-highlighting code must be written with the cursor on the
6314 * first highlighted character. The stop-highlighting code must
6315 * be written with the cursor just after the last highlighted
6316 * character.
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01006317 * Overwriting a character doesn't remove its highlighting. Need
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 * to clear the rest of the line, and force redrawing it
6319 * completely.
6320 */
6321 if ( p_wiv
6322 && !force
6323#ifdef FEAT_GUI
6324 && !gui.in_use
6325#endif
6326 && ScreenAttrs[off_to] != 0
6327 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6328 {
6329 /*
6330 * Need to remove highlighting attributes here.
6331 */
6332 windgoto(row, col + coloff);
6333 out_str(T_CE); /* clear rest of this screen line */
6334 screen_start(); /* don't know where cursor is now */
6335 force = TRUE; /* force redraw of rest of the line */
6336 redraw_next = TRUE; /* or else next char would miss out */
6337
6338 /*
6339 * If the previous character was highlighted, need to stop
6340 * highlighting at this character.
6341 */
6342 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6343 {
6344 screen_attr = ScreenAttrs[off_to - 1];
6345 term_windgoto(row, col + coloff);
6346 screen_stop_highlight();
6347 }
6348 else
6349 screen_attr = 0; /* highlighting has stopped */
6350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351 if (enc_dbcs != 0)
6352 {
6353 /* Check if overwriting a double-byte with a single-byte or
6354 * the other way around requires another character to be
6355 * redrawn. For UTF-8 this isn't needed, because comparing
6356 * ScreenLinesUC[] is sufficient. */
6357 if (char_cells == 1
6358 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006359 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360 {
6361 /* Writing a single-cell character over a double-cell
6362 * character: need to redraw the next cell. */
6363 ScreenLines[off_to + 1] = 0;
6364 redraw_next = TRUE;
6365 }
6366 else if (char_cells == 2
6367 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006368 && (*mb_off2cells)(off_to, max_off_to) == 1
6369 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370 {
6371 /* Writing the second half of a double-cell character over
6372 * a double-cell character: need to redraw the second
6373 * cell. */
6374 ScreenLines[off_to + 2] = 0;
6375 redraw_next = TRUE;
6376 }
6377
6378 if (enc_dbcs == DBCS_JPNU)
6379 ScreenLines2[off_to] = ScreenLines2[off_from];
6380 }
6381 /* When writing a single-width character over a double-width
6382 * character and at the end of the redrawn text, need to clear out
6383 * the right halve of the old character.
6384 * Also required when writing the right halve of a double-width
6385 * char over the left halve of an existing one. */
6386 if (has_mbyte && col + char_cells == endcol
6387 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006388 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006390 && (*mb_off2cells)(off_to, max_off_to) == 1
6391 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392 clear_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393
6394 ScreenLines[off_to] = ScreenLines[off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395 if (enc_utf8)
6396 {
6397 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6398 if (ScreenLinesUC[off_from] != 0)
6399 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006400 int i;
6401
6402 for (i = 0; i < Screen_mco; ++i)
6403 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404 }
6405 }
6406 if (char_cells == 2)
6407 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408
6409#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006410 /* The bold trick makes a single column of pixels appear in the
6411 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412 * character should be redrawn too. This happens for our own GUI
6413 * and for some xterms. */
6414 if (
6415# ifdef FEAT_GUI
6416 gui.in_use
6417# endif
6418# if defined(FEAT_GUI) && defined(UNIX)
6419 ||
6420# endif
6421# ifdef UNIX
6422 term_is_xterm
6423# endif
6424 )
6425 {
6426 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006427 if (hl > HL_ALL)
6428 hl = syn_attr2attr(hl);
6429 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 redraw_next = TRUE;
6431 }
6432#endif
6433 ScreenAttrs[off_to] = ScreenAttrs[off_from];
Bram Moolenaara12a1612019-01-24 16:39:02 +01006434
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006435 /* For simplicity set the attributes of second half of a
6436 * double-wide character equal to the first half. */
6437 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006439
6440 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443 screen_char(off_to, row, col + coloff);
6444 }
6445 else if ( p_wiv
6446#ifdef FEAT_GUI
6447 && !gui.in_use
6448#endif
6449 && col + coloff > 0)
6450 {
6451 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6452 {
6453 /*
6454 * Don't output stop-highlight when moving the cursor, it will
6455 * stop the highlighting when it should continue.
6456 */
6457 screen_attr = 0;
6458 }
6459 else if (screen_attr != 0)
6460 screen_stop_highlight();
6461 }
6462
6463 off_to += CHAR_CELLS;
6464 off_from += CHAR_CELLS;
6465 col += CHAR_CELLS;
6466 }
6467
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 if (clear_next)
6469 {
6470 /* Clear the second half of a double-wide character of which the left
6471 * half was overwritten with a single-wide character. */
6472 ScreenLines[off_to] = ' ';
6473 if (enc_utf8)
6474 ScreenLinesUC[off_to] = 0;
6475 screen_char(off_to, row, col + coloff);
6476 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477
6478 if (clear_width > 0
6479#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006480 && !(flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481#endif
6482 )
6483 {
6484#ifdef FEAT_GUI
6485 int startCol = col;
6486#endif
6487
6488 /* blank out the rest of the line */
6489 while (col < clear_width && ScreenLines[off_to] == ' '
6490 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006491 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 {
6493 ++off_to;
6494 ++col;
6495 }
6496 if (col < clear_width)
6497 {
6498#ifdef FEAT_GUI
6499 /*
6500 * In the GUI, clearing the rest of the line may leave pixels
6501 * behind if the first character cleared was bold. Some bold
6502 * fonts spill over the left. In this case we redraw the previous
6503 * character too. If we didn't skip any blanks above, then we
6504 * only redraw if the character wasn't already redrawn anyway.
6505 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006506 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507 {
6508 hl = ScreenAttrs[off_to];
6509 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006510 {
6511 int prev_cells = 1;
Bram Moolenaara12a1612019-01-24 16:39:02 +01006512
Bram Moolenaar9c697322006-10-09 20:11:17 +00006513 if (enc_utf8)
6514 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6515 * that its width is 2. */
6516 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6517 else if (enc_dbcs != 0)
6518 {
6519 /* find previous character by counting from first
6520 * column and get its width. */
6521 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006522 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006523
6524 while (off < off_to)
6525 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006526 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006527 off += prev_cells;
6528 }
6529 }
6530
6531 if (enc_dbcs != 0 && prev_cells > 1)
6532 screen_char_2(off_to - prev_cells, row,
6533 col + coloff - prev_cells);
6534 else
Bram Moolenaar9c697322006-10-09 20:11:17 +00006535 screen_char(off_to - prev_cells, row,
6536 col + coloff - prev_cells);
6537 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 }
6539#endif
6540 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6541 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542 off_to += clear_width - col;
6543 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544 }
6545 }
6546
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006547 if (clear_width > 0
6548#ifdef FEAT_TEXT_PROP
6549 && !(flags & SLF_POPUP) // no separator for popup window
6550#endif
6551 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006553 // For a window that has a right neighbor, draw the separator char
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006554 // right of the window contents. But not on top of a popup window.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006555 if (coloff + col < Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006557#ifdef FEAT_TEXT_PROP
6558 if (popup_mask[row * screen_Columns + col + coloff]
6559 <= screen_zindex)
6560#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006562 int c;
6563
6564 c = fillchar_vsep(&hl);
6565 if (ScreenLines[off_to] != (schar_T)c
6566 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6567 != (c >= 0x80 ? c : 0))
6568 || ScreenAttrs[off_to] != hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006570 ScreenLines[off_to] = c;
6571 ScreenAttrs[off_to] = hl;
6572 if (enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006574 if (c >= 0x80)
6575 {
6576 ScreenLinesUC[off_to] = c;
6577 ScreenLinesC[0][off_to] = 0;
6578 }
6579 else
6580 ScreenLinesUC[off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581 }
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006582 screen_char(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584 }
6585 }
6586 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587 LineWraps[row] = FALSE;
6588 }
6589}
6590
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006591#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006593 * Mirror text "str" for right-left displaying.
6594 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006596 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006597rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598{
6599 char_u *p1, *p2;
6600 int t;
6601
6602 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6603 {
6604 t = *p1;
6605 *p1 = *p2;
6606 *p2 = t;
6607 }
6608}
6609#endif
6610
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611/*
6612 * mark all status lines for redraw; used after first :cd
6613 */
6614 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006615status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616{
6617 win_T *wp;
6618
Bram Moolenaar29323592016-07-24 22:04:11 +02006619 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620 if (wp->w_status_height)
6621 {
6622 wp->w_redr_status = TRUE;
6623 redraw_later(VALID);
6624 }
6625}
6626
6627/*
6628 * mark all status lines of the current buffer for redraw
6629 */
6630 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006631status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632{
6633 win_T *wp;
6634
Bram Moolenaar29323592016-07-24 22:04:11 +02006635 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6637 {
6638 wp->w_redr_status = TRUE;
6639 redraw_later(VALID);
6640 }
6641}
6642
6643/*
6644 * Redraw all status lines that need to be redrawn.
6645 */
6646 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006647redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648{
6649 win_T *wp;
6650
Bram Moolenaar29323592016-07-24 22:04:11 +02006651 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006653 win_redr_status(wp, FALSE);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006654 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006655 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657
Bram Moolenaar4033c552017-09-16 20:54:51 +02006658#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659/*
6660 * Redraw all status lines at the bottom of frame "frp".
6661 */
6662 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006663win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664{
6665 if (frp->fr_layout == FR_LEAF)
6666 frp->fr_win->w_redr_status = TRUE;
6667 else if (frp->fr_layout == FR_ROW)
6668 {
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006669 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 win_redraw_last_status(frp);
6671 }
6672 else /* frp->fr_layout == FR_COL */
6673 {
6674 frp = frp->fr_child;
6675 while (frp->fr_next != NULL)
6676 frp = frp->fr_next;
6677 win_redraw_last_status(frp);
6678 }
6679}
6680#endif
6681
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682/*
6683 * Draw the verticap separator right of window "wp" starting with line "row".
6684 */
6685 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006686draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687{
6688 int hl;
6689 int c;
6690
6691 if (wp->w_vsep_width)
6692 {
6693 /* draw the vertical separator right of this window */
6694 c = fillchar_vsep(&hl);
6695 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6696 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6697 c, ' ', hl);
6698 }
6699}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700
6701#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006702static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703
6704/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006705 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706 */
6707 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006708status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709{
6710 int len = 0;
6711
6712#ifdef FEAT_MENU
6713 int emenu = (xp->xp_context == EXPAND_MENUS
6714 || xp->xp_context == EXPAND_MENUNAMES);
6715
6716 /* Check for menu separators - replace with '|'. */
6717 if (emenu && menu_is_separator(s))
6718 return 1;
6719#endif
6720
6721 while (*s != NUL)
6722 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006723 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006724 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006725 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726 }
6727
6728 return len;
6729}
6730
6731/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006732 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006733 * These are backslashes used for escaping. Do show backslashes in help tags.
6734 */
6735 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006736skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006737{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006738 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006739#ifdef FEAT_MENU
6740 || ((xp->xp_context == EXPAND_MENUS
6741 || xp->xp_context == EXPAND_MENUNAMES)
6742 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6743#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006744 )
6745 {
6746#ifndef BACKSLASH_IN_FILENAME
6747 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6748 return 2;
6749#endif
6750 return 1;
6751 }
6752 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006753}
6754
6755/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756 * Show wildchar matches in the status line.
6757 * Show at least the "match" item.
6758 * We start at item 'first_match' in the list and show all matches that fit.
6759 *
6760 * If inversion is possible we use it. Else '=' characters are used.
6761 */
6762 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006763win_redr_status_matches(
6764 expand_T *xp,
6765 int num_matches,
6766 char_u **matches, /* list of matches */
6767 int match,
6768 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769{
6770#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6771 int row;
6772 char_u *buf;
6773 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006774 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775 int fillchar;
6776 int attr;
6777 int i;
6778 int highlight = TRUE;
6779 char_u *selstart = NULL;
6780 int selstart_col = 0;
6781 char_u *selend = NULL;
6782 static int first_match = 0;
6783 int add_left = FALSE;
6784 char_u *s;
6785#ifdef FEAT_MENU
6786 int emenu;
6787#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789
6790 if (matches == NULL) /* interrupted completion? */
6791 return;
6792
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006793 if (has_mbyte)
Bram Moolenaar964b3742019-05-24 18:54:09 +02006794 buf = alloc(Columns * MB_MAXBYTES + 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006795 else
Bram Moolenaar964b3742019-05-24 18:54:09 +02006796 buf = alloc(Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 if (buf == NULL)
6798 return;
6799
6800 if (match == -1) /* don't show match but original text */
6801 {
6802 match = 0;
6803 highlight = FALSE;
6804 }
6805 /* count 1 for the ending ">" */
6806 clen = status_match_len(xp, L_MATCH(match)) + 3;
6807 if (match == 0)
6808 first_match = 0;
6809 else if (match < first_match)
6810 {
6811 /* jumping left, as far as we can go */
6812 first_match = match;
6813 add_left = TRUE;
6814 }
6815 else
6816 {
6817 /* check if match fits on the screen */
6818 for (i = first_match; i < match; ++i)
6819 clen += status_match_len(xp, L_MATCH(i)) + 2;
6820 if (first_match > 0)
6821 clen += 2;
6822 /* jumping right, put match at the left */
6823 if ((long)clen > Columns)
6824 {
6825 first_match = match;
6826 /* if showing the last match, we can add some on the left */
6827 clen = 2;
6828 for (i = match; i < num_matches; ++i)
6829 {
6830 clen += status_match_len(xp, L_MATCH(i)) + 2;
6831 if ((long)clen >= Columns)
6832 break;
6833 }
6834 if (i == num_matches)
6835 add_left = TRUE;
6836 }
6837 }
6838 if (add_left)
6839 while (first_match > 0)
6840 {
6841 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6842 if ((long)clen >= Columns)
6843 break;
6844 --first_match;
6845 }
6846
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006847 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848
6849 if (first_match == 0)
6850 {
6851 *buf = NUL;
6852 len = 0;
6853 }
6854 else
6855 {
6856 STRCPY(buf, "< ");
6857 len = 2;
6858 }
6859 clen = len;
6860
6861 i = first_match;
6862 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6863 {
6864 if (i == match)
6865 {
6866 selstart = buf + len;
6867 selstart_col = clen;
6868 }
6869
6870 s = L_MATCH(i);
6871 /* Check for menu separators - replace with '|' */
6872#ifdef FEAT_MENU
6873 emenu = (xp->xp_context == EXPAND_MENUS
6874 || xp->xp_context == EXPAND_MENUNAMES);
6875 if (emenu && menu_is_separator(s))
6876 {
6877 STRCPY(buf + len, transchar('|'));
6878 l = (int)STRLEN(buf + len);
6879 len += l;
6880 clen += l;
6881 }
6882 else
6883#endif
6884 for ( ; *s != NUL; ++s)
6885 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006886 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 clen += ptr2cells(s);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006888 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 {
6890 STRNCPY(buf + len, s, l);
6891 s += l - 1;
6892 len += l;
6893 }
6894 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895 {
6896 STRCPY(buf + len, transchar_byte(*s));
6897 len += (int)STRLEN(buf + len);
6898 }
6899 }
6900 if (i == match)
6901 selend = buf + len;
6902
6903 *(buf + len++) = ' ';
6904 *(buf + len++) = ' ';
6905 clen += 2;
6906 if (++i == num_matches)
6907 break;
6908 }
6909
6910 if (i != num_matches)
6911 {
6912 *(buf + len++) = '>';
6913 ++clen;
6914 }
6915
6916 buf[len] = NUL;
6917
6918 row = cmdline_row - 1;
6919 if (row >= 0)
6920 {
6921 if (wild_menu_showing == 0)
6922 {
6923 if (msg_scrolled > 0)
6924 {
6925 /* Put the wildmenu just above the command line. If there is
6926 * no room, scroll the screen one line up. */
6927 if (cmdline_row == Rows - 1)
6928 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02006929 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930 ++msg_scrolled;
6931 }
6932 else
6933 {
6934 ++cmdline_row;
6935 ++row;
6936 }
6937 wild_menu_showing = WM_SCROLLED;
6938 }
6939 else
6940 {
6941 /* Create status line if needed by setting 'laststatus' to 2.
6942 * Set 'winminheight' to zero to avoid that the window is
6943 * resized. */
6944 if (lastwin->w_status_height == 0)
6945 {
6946 save_p_ls = p_ls;
6947 save_p_wmh = p_wmh;
6948 p_ls = 2;
6949 p_wmh = 0;
6950 last_status(FALSE);
6951 }
6952 wild_menu_showing = WM_SHOWN;
6953 }
6954 }
6955
6956 screen_puts(buf, row, 0, attr);
6957 if (selstart != NULL && highlight)
6958 {
6959 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006960 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961 }
6962
6963 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6964 }
6965
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967 vim_free(buf);
6968}
6969#endif
6970
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971/*
6972 * Redraw the status line of window wp.
6973 *
6974 * If inversion is possible we use it. Else '=' characters are used.
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006975 * If "ignore_pum" is TRUE, also redraw statusline when the popup menu is
6976 * displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 */
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006978 static void
Bram Moolenaar829adb72018-06-24 19:24:03 +02006979win_redr_status(win_T *wp, int ignore_pum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980{
6981 int row;
6982 char_u *p;
6983 int len;
6984 int fillchar;
6985 int attr;
6986 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006987 static int busy = FALSE;
6988
6989 /* It's possible to get here recursively when 'statusline' (indirectly)
6990 * invokes ":redrawstatus". Simply ignore the call then. */
6991 if (busy)
6992 return;
6993 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994
6995 wp->w_redr_status = FALSE;
6996 if (wp->w_status_height == 0)
6997 {
6998 /* no status line, can only be last window */
6999 redraw_cmdline = TRUE;
7000 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00007001 else if (!redrawing()
7002#ifdef FEAT_INS_EXPAND
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007003 // don't update status line when popup menu is visible and may be
7004 // drawn over it, unless it will be redrawn later
7005 || (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00007006#endif
7007 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 {
7009 /* Don't redraw right now, do it later. */
7010 wp->w_redr_status = TRUE;
7011 }
7012#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007013 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 {
7015 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007016 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017 }
7018#endif
7019 else
7020 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007021 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007023 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 p = NameBuff;
7025 len = (int)STRLEN(p);
7026
Bram Moolenaard85f2712017-07-28 21:51:57 +02007027 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028#ifdef FEAT_QUICKFIX
7029 || wp->w_p_pvw
7030#endif
7031 || bufIsChanged(wp->w_buffer)
7032 || wp->w_buffer->b_p_ro)
7033 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02007034 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00007036 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 len += (int)STRLEN(p + len);
7038 }
7039#ifdef FEAT_QUICKFIX
7040 if (wp->w_p_pvw)
7041 {
7042 STRCPY(p + len, _("[Preview]"));
7043 len += (int)STRLEN(p + len);
7044 }
7045#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02007046 if (bufIsChanged(wp->w_buffer)
7047#ifdef FEAT_TERMINAL
7048 && !bt_terminal(wp->w_buffer)
7049#endif
7050 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051 {
7052 STRCPY(p + len, "[+]");
7053 len += 3;
7054 }
7055 if (wp->w_buffer->b_p_ro)
7056 {
Bram Moolenaar23584032013-06-07 20:17:11 +02007057 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01007058 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 }
7060
Bram Moolenaar02631462017-09-22 15:20:32 +02007061 this_ru_col = ru_col - (Columns - wp->w_width);
7062 if (this_ru_col < (wp->w_width + 1) / 2)
7063 this_ru_col = (wp->w_width + 1) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 if (this_ru_col <= 1)
7065 {
7066 p = (char_u *)"<"; /* No room for file name! */
7067 len = 1;
7068 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01007069 else if (has_mbyte)
7070 {
7071 int clen = 0, i;
7072
7073 /* Count total number of display cells. */
7074 clen = mb_string2cells(p, -1);
7075
7076 /* Find first character that will fit.
7077 * Going from start to end is much faster for DBCS. */
7078 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
7079 i += (*mb_ptr2len)(p + i))
7080 clen -= (*mb_ptr2cells)(p + i);
7081 len = clen;
7082 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083 {
Bram Moolenaara12a1612019-01-24 16:39:02 +01007084 p = p + i - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085 *p = '<';
Bram Moolenaara12a1612019-01-24 16:39:02 +01007086 ++len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087 }
7088
Bram Moolenaara12a1612019-01-24 16:39:02 +01007089 }
7090 else if (len > this_ru_col - 1)
7091 {
7092 p += len - (this_ru_col - 1);
7093 *p = '<';
7094 len = this_ru_col - 1;
7095 }
7096
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar53f81742017-09-22 14:35:51 +02007098 screen_puts(p, row, wp->w_wincol, attr);
7099 screen_fill(row, row + 1, len + wp->w_wincol,
7100 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007102 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
7104 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007105 - 1 + wp->w_wincol), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106
7107#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +02007108 win_redr_ruler(wp, TRUE, ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109#endif
7110 }
7111
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112 /*
7113 * May need to draw the character below the vertical separator.
7114 */
7115 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
7116 {
7117 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007118 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119 else
7120 fillchar = fillchar_vsep(&attr);
7121 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
7122 attr);
7123 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007124 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125}
7126
Bram Moolenaar238a5642006-02-21 22:12:05 +00007127#ifdef FEAT_STL_OPT
7128/*
7129 * Redraw the status line according to 'statusline' and take care of any
7130 * errors encountered.
7131 */
7132 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007133redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00007134{
Bram Moolenaar362f3562009-11-03 16:20:34 +00007135 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02007136 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007137
7138 /* When called recursively return. This can happen when the statusline
7139 * contains an expression that triggers a redraw. */
7140 if (entered)
7141 return;
7142 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007143
Bram Moolenaara742e082016-04-05 21:10:38 +02007144 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007145 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02007146 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007147 {
7148 /* When there is an error disable the statusline, otherwise the
7149 * display is messed up with errors and a redraw triggers the problem
7150 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007151 set_string_option_direct((char_u *)"statusline", -1,
7152 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007153 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007154 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007155 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007156 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007157}
7158#endif
7159
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160/*
7161 * Return TRUE if the status line of window "wp" is connected to the status
7162 * line of the window right of it. If not, then it's a vertical separator.
7163 * Only call if (wp->w_vsep_width != 0).
7164 */
7165 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007166stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167{
7168 frame_T *fr;
7169
7170 fr = wp->w_frame;
7171 while (fr->fr_parent != NULL)
7172 {
7173 if (fr->fr_parent->fr_layout == FR_COL)
7174 {
7175 if (fr->fr_next != NULL)
7176 break;
7177 }
7178 else
7179 {
7180 if (fr->fr_next != NULL)
7181 return TRUE;
7182 }
7183 fr = fr->fr_parent;
7184 }
7185 return FALSE;
7186}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189/*
7190 * Get the value to show for the language mappings, active 'keymap'.
7191 */
7192 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007193get_keymap_str(
7194 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007195 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007196 char_u *buf, /* buffer for the result */
7197 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198{
7199 char_u *p;
7200
7201 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7202 return FALSE;
7203
7204 {
7205#ifdef FEAT_EVAL
7206 buf_T *old_curbuf = curbuf;
7207 win_T *old_curwin = curwin;
7208 char_u *s;
7209
7210 curbuf = wp->w_buffer;
7211 curwin = wp;
7212 STRCPY(buf, "b:keymap_name"); /* must be writable */
7213 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007214 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215 --emsg_skip;
7216 curbuf = old_curbuf;
7217 curwin = old_curwin;
7218 if (p == NULL || *p == NUL)
7219#endif
7220 {
7221#ifdef FEAT_KEYMAP
7222 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7223 p = wp->w_buffer->b_p_keymap;
7224 else
7225#endif
7226 p = (char_u *)"lang";
7227 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007228 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229 buf[0] = NUL;
7230#ifdef FEAT_EVAL
7231 vim_free(s);
7232#endif
7233 }
7234 return buf[0] != NUL;
7235}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236
7237#if defined(FEAT_STL_OPT) || defined(PROTO)
7238/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007239 * Redraw the status line or ruler of window "wp".
7240 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241 */
7242 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007243win_redr_custom(
7244 win_T *wp,
7245 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007247 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248 int attr;
7249 int curattr;
7250 int row;
7251 int col = 0;
7252 int maxwidth;
7253 int width;
7254 int n;
7255 int len;
7256 int fillchar;
7257 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007258 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007260 struct stl_hlrec hltab[STL_MAX_ITEM];
7261 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007262 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007263 win_T *ewp;
7264 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265
Bram Moolenaar1d633412013-12-11 15:52:01 +01007266 /* There is a tiny chance that this gets called recursively: When
7267 * redrawing a status line triggers redrawing the ruler or tabline.
7268 * Avoid trouble by not allowing recursion. */
7269 if (entered)
7270 return;
7271 entered = TRUE;
7272
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007274 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007276 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007277 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007278 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007279 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007280 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007281 maxwidth = Columns;
7282# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007283 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007284# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007286 else
7287 {
7288 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007289 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02007290 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007291
7292 if (draw_ruler)
7293 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007294 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007295 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007296 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007297 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007298 if (*++stl == '-')
7299 stl++;
7300 if (atoi((char *)stl))
7301 while (VIM_ISDIGIT(*stl))
7302 stl++;
7303 if (*stl++ != '(')
7304 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007305 }
Bram Moolenaar02631462017-09-22 15:20:32 +02007306 col = ru_col - (Columns - wp->w_width);
7307 if (col < (wp->w_width + 1) / 2)
7308 col = (wp->w_width + 1) / 2;
7309 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007310 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007311 {
7312 row = Rows - 1;
7313 --maxwidth; /* writing in last column may cause scrolling */
7314 fillchar = ' ';
7315 attr = 0;
7316 }
7317
7318# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007319 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007320# endif
7321 }
7322 else
7323 {
7324 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007325 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007326 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007327 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007328# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007329 use_sandbox = was_set_insecurely((char_u *)"statusline",
7330 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007331# endif
7332 }
7333
Bram Moolenaar53f81742017-09-22 14:35:51 +02007334 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007335 }
7336
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007338 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339
Bram Moolenaar61452852011-02-01 18:01:11 +01007340 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7341 * the cursor away and back. */
7342 ewp = wp == NULL ? curwin : wp;
7343 p_crb_save = ewp->w_p_crb;
7344 ewp->w_p_crb = FALSE;
7345
Bram Moolenaar362f3562009-11-03 16:20:34 +00007346 /* Make a copy, because the statusline may include a function call that
7347 * might change the option value and free the memory. */
7348 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007349 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007350 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007351 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007352 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007353 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007355 /* Make all characters printable. */
7356 p = transstr(buf);
7357 if (p != NULL)
7358 {
7359 vim_strncpy(buf, p, sizeof(buf) - 1);
7360 vim_free(p);
7361 }
7362
7363 /* fill up with "fillchar" */
7364 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007365 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 len += (*mb_char2bytes)(fillchar, buf + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368 ++width;
7369 }
7370 buf[len] = NUL;
7371
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007372 /*
7373 * Draw each snippet with the specified highlighting.
7374 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 curattr = attr;
7376 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007377 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007379 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 screen_puts_len(p, len, row, col, curattr);
7381 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007382 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007384 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007386 else if (hltab[n].userhl < 0)
7387 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007388#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007389 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7390 && wp->w_status_height != 0)
7391 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007392 else if (wp != NULL && bt_terminal(wp->w_buffer)
7393 && wp->w_status_height != 0)
7394 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02007395#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007396 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007397 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007399 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400 }
7401 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007402
7403 if (wp == NULL)
7404 {
7405 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7406 col = 0;
7407 len = 0;
7408 p = buf;
7409 fillchar = 0;
7410 for (n = 0; tabtab[n].start != NULL; n++)
7411 {
7412 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7413 while (col < len)
7414 TabPageIdxs[col++] = fillchar;
7415 p = tabtab[n].start;
7416 fillchar = tabtab[n].userhl;
7417 }
7418 while (col < Columns)
7419 TabPageIdxs[col++] = fillchar;
7420 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007421
7422theend:
7423 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424}
7425
7426#endif /* FEAT_STL_OPT */
7427
7428/*
7429 * Output a single character directly to the screen and update ScreenLines.
7430 */
7431 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007432screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434 char_u buf[MB_MAXBYTES + 1];
7435
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007436 if (has_mbyte)
7437 buf[(*mb_char2bytes)(c, buf)] = NUL;
7438 else
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007439 {
7440 buf[0] = c;
7441 buf[1] = NUL;
7442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443 screen_puts(buf, row, col, attr);
7444}
7445
7446/*
7447 * Get a single character directly from ScreenLines into "bytes[]".
7448 * Also return its attribute in *attrp;
7449 */
7450 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007451screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452{
7453 unsigned off;
7454
7455 /* safety check */
7456 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7457 {
7458 off = LineOffset[row] + col;
7459 *attrp = ScreenAttrs[off];
7460 bytes[0] = ScreenLines[off];
7461 bytes[1] = NUL;
7462
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 if (enc_utf8 && ScreenLinesUC[off] != 0)
7464 bytes[utfc_char2bytes(off, bytes)] = NUL;
7465 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7466 {
7467 bytes[0] = ScreenLines[off];
7468 bytes[1] = ScreenLines2[off];
7469 bytes[2] = NUL;
7470 }
7471 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7472 {
7473 bytes[1] = ScreenLines[off + 1];
7474 bytes[2] = NUL;
7475 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476 }
7477}
7478
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007479/*
7480 * Return TRUE if composing characters for screen posn "off" differs from
7481 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007482 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007483 */
7484 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007485screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007486{
7487 int i;
7488
7489 for (i = 0; i < Screen_mco; ++i)
7490 {
7491 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7492 return TRUE;
7493 if (u8cc[i] == 0)
7494 break;
7495 }
7496 return FALSE;
7497}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007498
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499/*
7500 * Put string '*text' on the screen at position 'row' and 'col', with
7501 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7502 * Note: only outputs within one row, message is truncated at screen boundary!
7503 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7504 */
7505 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007506screen_puts(
7507 char_u *text,
7508 int row,
7509 int col,
7510 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511{
7512 screen_puts_len(text, -1, row, col, attr);
7513}
7514
7515/*
7516 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7517 * a NUL.
7518 */
7519 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007520screen_puts_len(
7521 char_u *text,
7522 int textlen,
7523 int row,
7524 int col,
7525 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526{
7527 unsigned off;
7528 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007529 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530 int c;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007531 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532 int mbyte_blen = 1;
7533 int mbyte_cells = 1;
7534 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007535 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536 int clear_next_cell = FALSE;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007537#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007539 int pc, nc, nc1;
7540 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007542 int force_redraw_this;
7543 int force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007544 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007545
Bram Moolenaar0b4c9ed2019-06-03 22:04:23 +02007546 // Safety check. The check for negative row and column is to fix issue
7547 // #4102. TODO: find out why row/col could be negative.
7548 if (ScreenLines == NULL
7549 || row >= screen_Rows || row < 0
7550 || col >= screen_Columns || col < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007552 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553
Bram Moolenaarc236c162008-07-13 17:41:49 +00007554 /* When drawing over the right halve of a double-wide char clear out the
7555 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007556 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaara12a1612019-01-24 16:39:02 +01007557#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00007558 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01007559#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007560 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007561 {
7562 ScreenLines[off - 1] = ' ';
7563 ScreenAttrs[off - 1] = 0;
7564 if (enc_utf8)
7565 {
7566 ScreenLinesUC[off - 1] = 0;
7567 ScreenLinesC[0][off - 1] = 0;
7568 }
7569 /* redraw the previous cell, make it empty */
7570 screen_char(off - 1, row, col - 1);
7571 /* force the cell at "col" to be redrawn */
7572 force_redraw_next = TRUE;
7573 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007574
Bram Moolenaar367329b2007-08-30 11:53:22 +00007575 max_off = LineOffset[row] + screen_Columns;
Bram Moolenaara064ac82007-08-05 18:10:54 +00007576 while (col < screen_Columns
7577 && (len < 0 || (int)(ptr - text) < len)
7578 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579 {
7580 c = *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581 /* check if this is the first byte of a multibyte */
7582 if (has_mbyte)
7583 {
7584 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007585 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007587 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7589 mbyte_cells = 1;
7590 else if (enc_dbcs != 0)
7591 mbyte_cells = mbyte_blen;
7592 else /* enc_utf8 */
7593 {
7594 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007595 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596 (int)((text + len) - ptr));
7597 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007598 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007600#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7602 {
7603 /* Do Arabic shaping. */
7604 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7605 {
7606 /* Past end of string to be displayed. */
7607 nc = NUL;
7608 nc1 = NUL;
7609 }
7610 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007611 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007612 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7613 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007614 nc1 = pcc[0];
7615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616 pc = prev_c;
7617 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007618 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619 }
7620 else
7621 prev_c = u8c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007622#endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007623 if (col + mbyte_cells > screen_Columns)
7624 {
7625 /* Only 1 cell left, but character requires 2 cells:
7626 * display a '>' in the last column to avoid wrapping. */
7627 c = '>';
7628 mbyte_cells = 1;
7629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630 }
7631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007633 force_redraw_this = force_redraw_next;
7634 force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007635
7636 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637 || (mbyte_cells == 2
7638 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7639 || (enc_dbcs == DBCS_JPNU
7640 && c == 0x8e
7641 && ScreenLines2[off] != ptr[1])
7642 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007643 && (ScreenLinesUC[off] !=
7644 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7645 || (ScreenLinesUC[off] != 0
7646 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007648 || exmode_active;
7649
Bram Moolenaar24a5ac52019-06-08 19:01:18 +02007650 if ((need_redraw || force_redraw_this)
7651#ifdef FEAT_TEXT_PROP
7652 && popup_mask[row * screen_Columns + col] <= screen_zindex
7653#endif
7654 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655 {
7656#if defined(FEAT_GUI) || defined(UNIX)
7657 /* The bold trick makes a single row of pixels appear in the next
7658 * character. When a bold character is removed, the next
7659 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007660 * and for some xterms. */
7661 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662# ifdef FEAT_GUI
7663 gui.in_use
7664# endif
7665# if defined(FEAT_GUI) && defined(UNIX)
7666 ||
7667# endif
7668# ifdef UNIX
7669 term_is_xterm
7670# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007671 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007673 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007675 if (n > HL_ALL)
7676 n = syn_attr2attr(n);
7677 if (n & HL_BOLD)
7678 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679 }
7680#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681 /* When at the end of the text and overwriting a two-cell
7682 * character with a one-cell character, need to clear the next
7683 * cell. Also when overwriting the left halve of a two-cell char
7684 * with the right halve of a two-cell char. Do this only once
7685 * (mb_off2cells() may return 2 on the right halve). */
7686 if (clear_next_cell)
7687 clear_next_cell = FALSE;
7688 else if (has_mbyte
7689 && (len < 0 ? ptr[mbyte_blen] == NUL
7690 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007691 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007693 && (*mb_off2cells)(off, max_off) == 1
7694 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695 clear_next_cell = TRUE;
7696
7697 /* Make sure we never leave a second byte of a double-byte behind,
7698 * it confuses mb_off2cells(). */
7699 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007700 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007702 && (*mb_off2cells)(off, max_off) == 1
7703 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704 ScreenLines[off + mbyte_blen] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705 ScreenLines[off] = c;
7706 ScreenAttrs[off] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707 if (enc_utf8)
7708 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007709 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710 ScreenLinesUC[off] = 0;
7711 else
7712 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007713 int i;
7714
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007716 for (i = 0; i < Screen_mco; ++i)
7717 {
7718 ScreenLinesC[i][off] = u8cc[i];
7719 if (u8cc[i] == 0)
7720 break;
7721 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722 }
7723 if (mbyte_cells == 2)
7724 {
7725 ScreenLines[off + 1] = 0;
7726 ScreenAttrs[off + 1] = attr;
7727 }
7728 screen_char(off, row, col);
7729 }
7730 else if (mbyte_cells == 2)
7731 {
7732 ScreenLines[off + 1] = ptr[1];
7733 ScreenAttrs[off + 1] = attr;
7734 screen_char_2(off, row, col);
7735 }
7736 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7737 {
7738 ScreenLines2[off] = ptr[1];
7739 screen_char(off, row, col);
7740 }
7741 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742 screen_char(off, row, col);
7743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744 if (has_mbyte)
7745 {
7746 off += mbyte_cells;
7747 col += mbyte_cells;
7748 ptr += mbyte_blen;
7749 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007750 {
7751 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007753 len = -1;
7754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755 }
7756 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 {
7758 ++off;
7759 ++col;
7760 ++ptr;
7761 }
7762 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007763
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007764 /* If we detected the next character needs to be redrawn, but the text
7765 * doesn't extend up to there, update the character here. */
7766 if (force_redraw_next && col < screen_Columns)
7767 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007768 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7769 screen_char_2(off, row, col);
7770 else
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007771 screen_char(off, row, col);
7772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773}
7774
7775#ifdef FEAT_SEARCH_EXTRA
7776/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007777 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778 */
7779 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007780start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781{
7782 if (p_hls && !no_hlsearch)
7783 {
7784 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007785 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007786# ifdef FEAT_RELTIME
7787 /* Set the time limit to 'redrawtime'. */
7788 profile_setlimit(p_rdt, &search_hl.tm);
7789# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790 }
7791}
7792
7793/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007794 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795 */
7796 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007797end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798{
7799 if (search_hl.rm.regprog != NULL)
7800 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007801 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802 search_hl.rm.regprog = NULL;
7803 }
7804}
7805
7806/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007807 * Init for calling prepare_search_hl().
7808 */
7809 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007810init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007811{
7812 matchitem_T *cur;
7813
7814 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7815 * match */
7816 cur = wp->w_match_head;
7817 while (cur != NULL)
7818 {
7819 cur->hl.rm = cur->match;
7820 if (cur->hlg_id == 0)
7821 cur->hl.attr = 0;
7822 else
7823 cur->hl.attr = syn_id2attr(cur->hlg_id);
7824 cur->hl.buf = wp->w_buffer;
7825 cur->hl.lnum = 0;
7826 cur->hl.first_lnum = 0;
7827# ifdef FEAT_RELTIME
7828 /* Set the time limit to 'redrawtime'. */
7829 profile_setlimit(p_rdt, &(cur->hl.tm));
7830# endif
7831 cur = cur->next;
7832 }
7833 search_hl.buf = wp->w_buffer;
7834 search_hl.lnum = 0;
7835 search_hl.first_lnum = 0;
7836 /* time limit is set at the toplevel, for all windows */
7837}
7838
7839/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840 * Advance to the match in window "wp" line "lnum" or past it.
7841 */
7842 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007843prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007845 matchitem_T *cur; /* points to the match list */
7846 match_T *shl; /* points to search_hl or a match */
7847 int shl_flag; /* flag to indicate whether search_hl
7848 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007849 int pos_inprogress; /* marks that position match search is
7850 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851 int n;
7852
7853 /*
7854 * When using a multi-line pattern, start searching at the top
7855 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007856 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007858 cur = wp->w_match_head;
7859 shl_flag = FALSE;
7860 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007862 if (shl_flag == FALSE)
7863 {
7864 shl = &search_hl;
7865 shl_flag = TRUE;
7866 }
7867 else
7868 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869 if (shl->rm.regprog != NULL
7870 && shl->lnum == 0
7871 && re_multiline(shl->rm.regprog))
7872 {
7873 if (shl->first_lnum == 0)
7874 {
7875# ifdef FEAT_FOLDING
7876 for (shl->first_lnum = lnum;
7877 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7878 if (hasFoldingWin(wp, shl->first_lnum - 1,
7879 NULL, NULL, TRUE, NULL))
7880 break;
7881# else
7882 shl->first_lnum = wp->w_topline;
7883# endif
7884 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007885 if (cur != NULL)
7886 cur->pos.cur = 0;
7887 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007889 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7890 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02007892 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
7893 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007894 pos_inprogress = cur == NULL || cur->pos.cur == 0
7895 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896 if (shl->lnum != 0)
7897 {
7898 shl->first_lnum = shl->lnum
7899 + shl->rm.endpos[0].lnum
7900 - shl->rm.startpos[0].lnum;
7901 n = shl->rm.endpos[0].col;
7902 }
7903 else
7904 {
7905 ++shl->first_lnum;
7906 n = 0;
7907 }
7908 }
7909 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007910 if (shl != &search_hl && cur != NULL)
7911 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912 }
7913}
7914
7915/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007916 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007917 * Uses shl->buf.
7918 * Sets shl->lnum and shl->rm contents.
7919 * Note: Assumes a previous match is always before "lnum", unless
7920 * shl->lnum is zero.
7921 * Careful: Any pointers for buffer lines will become invalid.
7922 */
7923 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007924next_search_hl(
7925 win_T *win,
7926 match_T *shl, /* points to search_hl or a match */
7927 linenr_T lnum,
7928 colnr_T mincol, /* minimal column for a match */
7929 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930{
7931 linenr_T l;
7932 colnr_T matchcol;
7933 long nmatched;
Bram Moolenaarbcf94422018-06-23 14:21:42 +02007934 int save_called_emsg = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935
Bram Moolenaarb0acacd2018-08-11 16:40:43 +02007936 // for :{range}s/pat only highlight inside the range
7937 if (lnum < search_first_line || lnum > search_last_line)
7938 {
7939 shl->lnum = 0;
7940 return;
7941 }
7942
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943 if (shl->lnum != 0)
7944 {
7945 /* Check for three situations:
7946 * 1. If the "lnum" is below a previous match, start a new search.
7947 * 2. If the previous match includes "mincol", use it.
7948 * 3. Continue after the previous match.
7949 */
7950 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7951 if (lnum > l)
7952 shl->lnum = 0;
7953 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7954 return;
7955 }
7956
7957 /*
7958 * Repeat searching for a match until one is found that includes "mincol"
7959 * or none is found in this line.
7960 */
7961 called_emsg = FALSE;
7962 for (;;)
7963 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007964#ifdef FEAT_RELTIME
7965 /* Stop searching after passing the time limit. */
7966 if (profile_passed_limit(&(shl->tm)))
7967 {
7968 shl->lnum = 0; /* no match found in time */
7969 break;
7970 }
7971#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972 /* Three situations:
7973 * 1. No useful previous match: search from start of line.
7974 * 2. Not Vi compatible or empty match: continue at next character.
7975 * Break the loop if this is beyond the end of the line.
7976 * 3. Vi compatible searching: continue at end of previous match.
7977 */
7978 if (shl->lnum == 0)
7979 matchcol = 0;
7980 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7981 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007982 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007984 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007985
7986 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007987 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007988 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007990 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991 shl->lnum = 0;
7992 break;
7993 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007994 if (has_mbyte)
7995 matchcol += mb_ptr2len(ml);
7996 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007997 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998 }
7999 else
8000 matchcol = shl->rm.endpos[0].col;
8001
8002 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008003 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008005 /* Remember whether shl->rm is using a copy of the regprog in
8006 * cur->match. */
8007 int regprog_is_copy = (shl != &search_hl && cur != NULL
8008 && shl == &cur->hl
8009 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008010 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008011
Bram Moolenaarb3414592014-06-17 17:48:32 +02008012 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
8013 matchcol,
8014#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008015 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02008016#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008017 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02008018#endif
8019 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008020 /* Copy the regprog, in case it got freed and recompiled. */
8021 if (regprog_is_copy)
8022 cur->match.regprog = cur->hl.rm.regprog;
8023
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008024 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008025 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02008026 /* Error while handling regexp: stop using this regexp. */
8027 if (shl == &search_hl)
8028 {
8029 /* don't free regprog in the match list, it's a copy */
8030 vim_regfree(shl->rm.regprog);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008031 set_no_hlsearch(TRUE);
Bram Moolenaarb3414592014-06-17 17:48:32 +02008032 }
8033 shl->rm.regprog = NULL;
8034 shl->lnum = 0;
8035 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
8036 message */
8037 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008038 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02008039 }
8040 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008041 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02008042 else
8043 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044 if (nmatched == 0)
8045 {
8046 shl->lnum = 0; /* no match found */
8047 break;
8048 }
8049 if (shl->rm.startpos[0].lnum > 0
8050 || shl->rm.startpos[0].col >= mincol
8051 || nmatched > 1
8052 || shl->rm.endpos[0].col > mincol)
8053 {
8054 shl->lnum += shl->rm.startpos[0].lnum;
8055 break; /* useful match found */
8056 }
8057 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +02008058
8059 // Restore called_emsg for assert_fails().
8060 called_emsg = save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062
Bram Moolenaar85077472016-10-16 14:35:48 +02008063/*
8064 * If there is a match fill "shl" and return one.
8065 * Return zero otherwise.
8066 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008067 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01008068next_search_hl_pos(
8069 match_T *shl, /* points to a match */
8070 linenr_T lnum,
8071 posmatch_T *posmatch, /* match positions */
8072 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008073{
8074 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02008075 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008076
Bram Moolenaarb3414592014-06-17 17:48:32 +02008077 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
8078 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008079 llpos_T *pos = &posmatch->pos[i];
8080
8081 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008082 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02008083 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008084 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008085 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008086 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008087 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008088 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008089 /* if this match comes before the one at "found" then swap
8090 * them */
8091 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008092 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008093 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008094
Bram Moolenaar85077472016-10-16 14:35:48 +02008095 *pos = posmatch->pos[found];
8096 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008097 }
8098 }
8099 else
Bram Moolenaar85077472016-10-16 14:35:48 +02008100 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008101 }
8102 }
8103 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02008104 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008105 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008106 colnr_T start = posmatch->pos[found].col == 0
8107 ? 0 : posmatch->pos[found].col - 1;
8108 colnr_T end = posmatch->pos[found].col == 0
8109 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008110
Bram Moolenaar85077472016-10-16 14:35:48 +02008111 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008112 shl->rm.startpos[0].lnum = 0;
8113 shl->rm.startpos[0].col = start;
8114 shl->rm.endpos[0].lnum = 0;
8115 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02008116 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02008117 posmatch->cur = found + 1;
8118 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008119 }
Bram Moolenaar85077472016-10-16 14:35:48 +02008120 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008121}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02008122#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02008123
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008125screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126{
8127 attrentry_T *aep = NULL;
8128
8129 screen_attr = attr;
8130 if (full_screen
Bram Moolenaar4f974752019-02-17 17:44:42 +01008131#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132 && termcap_active
8133#endif
8134 )
8135 {
8136#ifdef FEAT_GUI
8137 if (gui.in_use)
8138 {
8139 char buf[20];
8140
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008141 /* The GUI handles this internally. */
8142 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 OUT_STR(buf);
8144 }
8145 else
8146#endif
8147 {
8148 if (attr > HL_ALL) /* special HL attr. */
8149 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008150 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151 aep = syn_cterm_attr2entry(attr);
8152 else
8153 aep = syn_term_attr2entry(attr);
8154 if (aep == NULL) /* did ":syntax clear" */
8155 attr = 0;
8156 else
8157 attr = aep->ae_attr;
8158 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008159 if ((attr & HL_BOLD) && *T_MD != NUL) /* bold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008161 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008162#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008163 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8164 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8165 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008166#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008167 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008168 /* If the Normal FG color has BOLD attribute and the new HL
8169 * has a FG color defined, clear BOLD. */
8170 out_str(T_ME);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008171 if ((attr & HL_STANDOUT) && *T_SO != NUL) /* standout */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172 out_str(T_SO);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008173 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) /* undercurl */
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008174 out_str(T_UCS);
8175 if (((attr & HL_UNDERLINE) /* underline or undercurl */
Bram Moolenaar45a00002017-12-22 21:12:34 +01008176 || ((attr & HL_UNDERCURL) && *T_UCS == NUL))
8177 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178 out_str(T_US);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008179 if ((attr & HL_ITALIC) && *T_CZH != NUL) /* italic */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180 out_str(T_CZH);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008181 if ((attr & HL_INVERSE) && *T_MR != NUL) /* inverse (reverse) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182 out_str(T_MR);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008183 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) /* strike */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008184 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185
8186 /*
8187 * Output the color or start string after bold etc., in case the
8188 * bold etc. override the color setting.
8189 */
8190 if (aep != NULL)
8191 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008192#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008193 /* When 'termguicolors' is set but fg or bg is unset,
8194 * fall back to the cterm colors. This helps for SpellBad,
8195 * where the GUI uses a red undercurl. */
8196 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008198 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008199 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008200 }
8201 else
8202#endif
8203 if (t_colors > 1)
8204 {
8205 if (aep->ae_u.cterm.fg_color)
8206 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8207 }
8208#ifdef FEAT_TERMGUICOLORS
8209 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
8210 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008211 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008212 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213 }
8214 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008215#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008216 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008218 if (aep->ae_u.cterm.bg_color)
8219 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8220 }
8221
Bram Moolenaarf708ac52018-03-12 21:48:32 +01008222 if (!IS_CTERM)
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008223 {
8224 if (aep->ae_u.term.start != NULL)
8225 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226 }
8227 }
8228 }
8229 }
8230}
8231
8232 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008233screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234{
8235 int do_ME = FALSE; /* output T_ME code */
8236
8237 if (screen_attr != 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01008238#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239 && termcap_active
8240#endif
8241 )
8242 {
8243#ifdef FEAT_GUI
8244 if (gui.in_use)
8245 {
8246 char buf[20];
8247
8248 /* use internal GUI code */
8249 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8250 OUT_STR(buf);
8251 }
8252 else
8253#endif
8254 {
8255 if (screen_attr > HL_ALL) /* special HL attr. */
8256 {
8257 attrentry_T *aep;
8258
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008259 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260 {
8261 /*
8262 * Assume that t_me restores the original colors!
8263 */
8264 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008265 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008266#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008267 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8268 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8269 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008270#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008271 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008272#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008273 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
8274 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
8275 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008276#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008277 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008278 do_ME = TRUE;
8279 }
8280 else
8281 {
8282 aep = syn_term_attr2entry(screen_attr);
8283 if (aep != NULL && aep->ae_u.term.stop != NULL)
8284 {
8285 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8286 do_ME = TRUE;
8287 else
8288 out_str(aep->ae_u.term.stop);
8289 }
8290 }
8291 if (aep == NULL) /* did ":syntax clear" */
8292 screen_attr = 0;
8293 else
8294 screen_attr = aep->ae_attr;
8295 }
8296
8297 /*
8298 * Often all ending-codes are equal to T_ME. Avoid outputting the
8299 * same sequence several times.
8300 */
8301 if (screen_attr & HL_STANDOUT)
8302 {
8303 if (STRCMP(T_SE, T_ME) == 0)
8304 do_ME = TRUE;
8305 else
8306 out_str(T_SE);
8307 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008308 if ((screen_attr & HL_UNDERCURL) && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008309 {
8310 if (STRCMP(T_UCE, T_ME) == 0)
8311 do_ME = TRUE;
8312 else
8313 out_str(T_UCE);
8314 }
8315 if ((screen_attr & HL_UNDERLINE)
Bram Moolenaar45a00002017-12-22 21:12:34 +01008316 || ((screen_attr & HL_UNDERCURL) && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 {
8318 if (STRCMP(T_UE, T_ME) == 0)
8319 do_ME = TRUE;
8320 else
8321 out_str(T_UE);
8322 }
8323 if (screen_attr & HL_ITALIC)
8324 {
8325 if (STRCMP(T_CZR, T_ME) == 0)
8326 do_ME = TRUE;
8327 else
8328 out_str(T_CZR);
8329 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008330 if (screen_attr & HL_STRIKETHROUGH)
8331 {
8332 if (STRCMP(T_STE, T_ME) == 0)
8333 do_ME = TRUE;
8334 else
8335 out_str(T_STE);
8336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8338 out_str(T_ME);
8339
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008340#ifdef FEAT_TERMGUICOLORS
8341 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008343 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008344 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008345 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008346 term_bg_rgb_color(cterm_normal_bg_gui_color);
8347 }
8348 else
8349#endif
8350 {
8351 if (t_colors > 1)
8352 {
8353 /* set Normal cterm colors */
8354 if (cterm_normal_fg_color != 0)
8355 term_fg_color(cterm_normal_fg_color - 1);
8356 if (cterm_normal_bg_color != 0)
8357 term_bg_color(cterm_normal_bg_color - 1);
8358 if (cterm_normal_fg_bold)
8359 out_str(T_MD);
8360 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361 }
8362 }
8363 }
8364 screen_attr = 0;
8365}
8366
8367/*
8368 * Reset the colors for a cterm. Used when leaving Vim.
8369 * The machine specific code may override this again.
8370 */
8371 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008372reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008374 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375 {
8376 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008377#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008378 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8379 || cterm_normal_bg_gui_color != INVALCOLOR)
8380 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008381#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008383#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384 {
8385 out_str(T_OP);
8386 screen_attr = -1;
8387 }
8388 if (cterm_normal_fg_bold)
8389 {
8390 out_str(T_ME);
8391 screen_attr = -1;
8392 }
8393 }
8394}
8395
8396/*
8397 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8398 * using the attributes from ScreenAttrs["off"].
8399 */
8400 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008401screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402{
8403 int attr;
8404
8405 /* Check for illegal values, just in case (could happen just after
8406 * resizing). */
8407 if (row >= screen_Rows || col >= screen_Columns)
8408 return;
8409
Bram Moolenaarae654382019-01-17 21:09:05 +01008410#ifdef FEAT_INS_EXPAND
Bram Moolenaar33796b32019-06-08 16:01:13 +02008411 // Skip if under the popup menu.
8412 // Popup windows with zindex higher than POPUPMENU_ZINDEX go on top.
8413 if (pum_under_menu(row, col)
8414# ifdef FEAT_TEXT_PROP
8415 && screen_zindex <= POPUPMENU_ZINDEX
8416# endif
8417 )
Bram Moolenaarae654382019-01-17 21:09:05 +01008418 return;
8419#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02008420#ifdef FEAT_TEXT_PROP
8421 // Skip if under a(nother) popup.
8422 if (popup_mask[row * screen_Columns + col] > screen_zindex)
8423 return;
8424#endif
8425
Bram Moolenaar494838a2015-02-10 19:20:37 +01008426 /* Outputting a character in the last cell on the screen may scroll the
8427 * screen up. Only do it when the "xn" termcap property is set, otherwise
8428 * mark the character invalid (update it when scrolled up). */
8429 if (*T_XN == NUL
8430 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431#ifdef FEAT_RIGHTLEFT
8432 /* account for first command-line character in rightleft mode */
8433 && !cmdmsg_rl
8434#endif
8435 )
8436 {
8437 ScreenAttrs[off] = (sattr_T)-1;
8438 return;
8439 }
8440
8441 /*
8442 * Stop highlighting first, so it's easier to move the cursor.
8443 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444 if (screen_char_attr != 0)
8445 attr = screen_char_attr;
8446 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447 attr = ScreenAttrs[off];
8448 if (screen_attr != attr)
8449 screen_stop_highlight();
8450
8451 windgoto(row, col);
8452
8453 if (screen_attr != attr)
8454 screen_start_highlight(attr);
8455
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456 if (enc_utf8 && ScreenLinesUC[off] != 0)
8457 {
8458 char_u buf[MB_MAXBYTES + 1];
8459
Bram Moolenaarcb070082016-04-02 22:14:51 +02008460 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008461 {
8462 if (*p_ambw == 'd'
Bram Moolenaara12a1612019-01-24 16:39:02 +01008463#ifdef FEAT_GUI
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008464 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008465#endif
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008466 )
8467 {
8468 /* Clear the two screen cells. If the character is actually
8469 * single width it won't change the second cell. */
8470 out_str((char_u *)" ");
8471 term_windgoto(row, col);
8472 }
8473 /* not sure where the cursor is after drawing the ambiguous width
8474 * character */
Bram Moolenaarcb070082016-04-02 22:14:51 +02008475 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008476 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02008477 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008479
8480 /* Convert the UTF-8 character to bytes and write it. */
8481 buf[utfc_char2bytes(off, buf)] = NUL;
8482 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483 }
8484 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488 /* double-byte character in single-width cell */
8489 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8490 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491 }
8492
8493 screen_cur_col++;
8494}
8495
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496/*
8497 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8498 * on the screen at position 'row' and 'col'.
8499 * The attributes of the first byte is used for all. This is required to
8500 * output the two bytes of a double-byte character with nothing in between.
8501 */
8502 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008503screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504{
8505 /* Check for illegal values (could be wrong when screen was resized). */
8506 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8507 return;
8508
8509 /* Outputting the last character on the screen may scrollup the screen.
8510 * Don't to it! Mark the character invalid (update it when scrolled up) */
8511 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8512 {
8513 ScreenAttrs[off] = (sattr_T)-1;
8514 return;
8515 }
8516
8517 /* Output the first byte normally (positions the cursor), then write the
8518 * second byte directly. */
8519 screen_char(off, row, col);
8520 out_char(ScreenLines[off + 1]);
8521 ++screen_cur_col;
8522}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524/*
8525 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8526 * This uses the contents of ScreenLines[] and doesn't change it.
8527 */
8528 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008529screen_draw_rectangle(
8530 int row,
8531 int col,
8532 int height,
8533 int width,
8534 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535{
8536 int r, c;
8537 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008538 int max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008540 /* Can't use ScreenLines unless initialized */
8541 if (ScreenLines == NULL)
8542 return;
8543
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544 if (invert)
8545 screen_char_attr = HL_INVERSE;
8546 for (r = row; r < row + height; ++r)
8547 {
8548 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008549 max_off = off + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550 for (c = col; c < col + width; ++c)
8551 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00008552 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553 {
8554 screen_char_2(off + c, r, c);
8555 ++c;
8556 }
8557 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558 {
8559 screen_char(off + c, r, c);
Bram Moolenaar367329b2007-08-30 11:53:22 +00008560 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561 ++c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562 }
8563 }
8564 }
8565 screen_char_attr = 0;
8566}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568/*
8569 * Redraw the characters for a vertically split window.
8570 */
8571 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008572redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573{
8574 int col;
8575 int width;
8576
8577# ifdef FEAT_CLIPBOARD
8578 clip_may_clear_selection(row, end - 1);
8579# endif
8580
8581 if (wp == NULL)
8582 {
8583 col = 0;
8584 width = Columns;
8585 }
8586 else
8587 {
8588 col = wp->w_wincol;
8589 width = wp->w_width;
8590 }
8591 screen_draw_rectangle(row, col, end - row, width, FALSE);
8592}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008594 static void
8595space_to_screenline(int off, int attr)
8596{
8597 ScreenLines[off] = ' ';
8598 ScreenAttrs[off] = attr;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008599 if (enc_utf8)
8600 ScreenLinesUC[off] = 0;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008601}
8602
Bram Moolenaar071d4272004-06-13 20:20:40 +00008603/*
8604 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8605 * with character 'c1' in first column followed by 'c2' in the other columns.
8606 * Use attributes 'attr'.
8607 */
8608 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008609screen_fill(
8610 int start_row,
8611 int end_row,
8612 int start_col,
8613 int end_col,
8614 int c1,
8615 int c2,
8616 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617{
8618 int row;
8619 int col;
8620 int off;
8621 int end_off;
8622 int did_delete;
8623 int c;
8624 int norm_term;
8625#if defined(FEAT_GUI) || defined(UNIX)
8626 int force_next = FALSE;
8627#endif
8628
8629 if (end_row > screen_Rows) /* safety check */
8630 end_row = screen_Rows;
8631 if (end_col > screen_Columns) /* safety check */
8632 end_col = screen_Columns;
8633 if (ScreenLines == NULL
8634 || start_row >= end_row
8635 || start_col >= end_col) /* nothing to do */
8636 return;
8637
8638 /* it's a "normal" terminal when not in a GUI or cterm */
8639 norm_term = (
8640#ifdef FEAT_GUI
8641 !gui.in_use &&
8642#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008643 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644 for (row = start_row; row < end_row; ++row)
8645 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008646 if (has_mbyte
Bram Moolenaara12a1612019-01-24 16:39:02 +01008647#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00008648 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008649#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00008650 )
8651 {
8652 /* When drawing over the right halve of a double-wide char clear
8653 * out the left halve. When drawing over the left halve of a
8654 * double wide-char clear out the right halve. Only needed in a
8655 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008656 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008657 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008658 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008659 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008661 /*
8662 * Try to use delete-line termcap code, when no attributes or in a
8663 * "normal" terminal, where a bold/italic space is just a
8664 * space.
8665 */
8666 did_delete = FALSE;
8667 if (c2 == ' '
8668 && end_col == Columns
8669 && can_clear(T_CE)
8670 && (attr == 0
8671 || (norm_term
8672 && attr <= HL_ALL
8673 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8674 {
8675 /*
8676 * check if we really need to clear something
8677 */
8678 col = start_col;
8679 if (c1 != ' ') /* don't clear first char */
8680 ++col;
8681
8682 off = LineOffset[row] + col;
8683 end_off = LineOffset[row] + end_col;
8684
8685 /* skip blanks (used often, keep it fast!) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686 if (enc_utf8)
8687 while (off < end_off && ScreenLines[off] == ' '
8688 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8689 ++off;
8690 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691 while (off < end_off && ScreenLines[off] == ' '
8692 && ScreenAttrs[off] == 0)
8693 ++off;
8694 if (off < end_off) /* something to be cleared */
8695 {
8696 col = off - LineOffset[row];
8697 screen_stop_highlight();
8698 term_windgoto(row, col);/* clear rest of this screen line */
8699 out_str(T_CE);
8700 screen_start(); /* don't know where cursor is now */
8701 col = end_col - col;
8702 while (col--) /* clear chars in ScreenLines */
8703 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008704 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705 ++off;
8706 }
8707 }
8708 did_delete = TRUE; /* the chars are cleared now */
8709 }
8710
8711 off = LineOffset[row] + start_col;
8712 c = c1;
8713 for (col = start_col; col < end_col; ++col)
8714 {
Bram Moolenaar33796b32019-06-08 16:01:13 +02008715 if ((ScreenLines[off] != c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008716 || (enc_utf8 && (int)ScreenLinesUC[off]
8717 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718 || ScreenAttrs[off] != attr
8719#if defined(FEAT_GUI) || defined(UNIX)
8720 || force_next
8721#endif
8722 )
Bram Moolenaar33796b32019-06-08 16:01:13 +02008723#ifdef FEAT_TEXT_PROP
8724 // Skip if under a(nother) popup.
8725 && popup_mask[row * screen_Columns + col] <= screen_zindex
8726#endif
8727 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728 {
8729#if defined(FEAT_GUI) || defined(UNIX)
8730 /* The bold trick may make a single row of pixels appear in
8731 * the next character. When a bold character is removed, the
8732 * next character should be redrawn too. This happens for our
8733 * own GUI and for some xterms. */
8734 if (
8735# ifdef FEAT_GUI
8736 gui.in_use
8737# endif
8738# if defined(FEAT_GUI) && defined(UNIX)
8739 ||
8740# endif
8741# ifdef UNIX
8742 term_is_xterm
8743# endif
8744 )
8745 {
8746 if (ScreenLines[off] != ' '
8747 && (ScreenAttrs[off] > HL_ALL
8748 || ScreenAttrs[off] & HL_BOLD))
8749 force_next = TRUE;
8750 else
8751 force_next = FALSE;
8752 }
8753#endif
8754 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755 if (enc_utf8)
8756 {
8757 if (c >= 0x80)
8758 {
8759 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008760 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761 }
8762 else
8763 ScreenLinesUC[off] = 0;
8764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008765 ScreenAttrs[off] = attr;
8766 if (!did_delete || c != ' ')
8767 screen_char(off, row, col);
8768 }
8769 ++off;
8770 if (col == start_col)
8771 {
8772 if (did_delete)
8773 break;
8774 c = c2;
8775 }
8776 }
8777 if (end_col == Columns)
8778 LineWraps[row] = FALSE;
8779 if (row == Rows - 1) /* overwritten the command line */
8780 {
8781 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02008782 if (start_col == 0 && end_col == Columns
8783 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008784 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008785 if (start_col == 0)
8786 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787 }
8788 }
8789}
8790
8791/*
8792 * Check if there should be a delay. Used before clearing or redrawing the
8793 * screen or the command line.
8794 */
8795 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008796check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008797{
8798 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8799 && !did_wait_return
8800 && emsg_silent == 0)
8801 {
8802 out_flush();
8803 ui_delay(1000L, TRUE);
8804 emsg_on_display = FALSE;
8805 if (check_msg_scroll)
8806 msg_scroll = FALSE;
8807 }
8808}
8809
8810/*
Bram Moolenaarca57ab52019-04-13 14:53:16 +02008811 * Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect.
8812 */
8813 static void
8814clear_TabPageIdxs(void)
8815{
8816 int scol;
8817
8818 for (scol = 0; scol < Columns; ++scol)
8819 TabPageIdxs[scol] = 0;
8820}
8821
8822/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008823 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008824 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825 * Returns TRUE if there is a valid screen to write to.
8826 * Returns FALSE when starting up and screen not initialized yet.
8827 */
8828 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008829screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008831 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832 return (ScreenLines != NULL);
8833}
8834
8835/*
8836 * Resize the shell to Rows and Columns.
8837 * Allocate ScreenLines[] and associated items.
8838 *
8839 * There may be some time between setting Rows and Columns and (re)allocating
8840 * ScreenLines[]. This happens when starting up and when (manually) changing
8841 * the shell size. Always use screen_Rows and screen_Columns to access items
8842 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8843 * final size of the shell is needed.
8844 */
8845 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008846screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847{
8848 int new_row, old_row;
8849#ifdef FEAT_GUI
8850 int old_Rows;
8851#endif
8852 win_T *wp;
8853 int outofmem = FALSE;
8854 int len;
8855 schar_T *new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008857 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008859 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860 sattr_T *new_ScreenAttrs;
8861 unsigned *new_LineOffset;
8862 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008863 short *new_TabPageIdxs;
Bram Moolenaar33796b32019-06-08 16:01:13 +02008864#ifdef FEAT_TEXT_PROP
8865 short *new_popup_mask;
Bram Moolenaar4c063a02019-06-10 21:24:12 +02008866 short *new_popup_mask_next;
Bram Moolenaar33796b32019-06-08 16:01:13 +02008867#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00008868 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008870 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008871 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008873retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874 /*
8875 * Allocation of the screen buffers is done only when the size changes and
8876 * when Rows and Columns have been set and we have started doing full
8877 * screen stuff.
8878 */
8879 if ((ScreenLines != NULL
8880 && Rows == screen_Rows
8881 && Columns == screen_Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882 && enc_utf8 == (ScreenLinesUC != NULL)
8883 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaara12a1612019-01-24 16:39:02 +01008884 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885 || Rows == 0
8886 || Columns == 0
8887 || (!full_screen && ScreenLines == NULL))
8888 return;
8889
8890 /*
8891 * It's possible that we produce an out-of-memory message below, which
8892 * will cause this function to be called again. To break the loop, just
8893 * return here.
8894 */
8895 if (entered)
8896 return;
8897 entered = TRUE;
8898
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008899 /*
8900 * Note that the window sizes are updated before reallocating the arrays,
8901 * thus we must not redraw here!
8902 */
8903 ++RedrawingDisabled;
8904
Bram Moolenaar071d4272004-06-13 20:20:40 +00008905 win_new_shellsize(); /* fit the windows in the new sized shell */
8906
Bram Moolenaar071d4272004-06-13 20:20:40 +00008907 comp_col(); /* recompute columns for shown command and ruler */
8908
8909 /*
8910 * We're changing the size of the screen.
8911 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8912 * - Move lines from the old arrays into the new arrays, clear extra
8913 * lines (unless the screen is going to be cleared).
8914 * - Free the old arrays.
8915 *
8916 * If anything fails, make ScreenLines NULL, so we don't do anything!
8917 * Continuing with the old ScreenLines may result in a crash, because the
8918 * size is wrong.
8919 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008920 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008922 if (aucmd_win != NULL)
8923 win_free_lsize(aucmd_win);
Bram Moolenaar8caaf822019-06-01 18:11:22 +02008924#ifdef FEAT_TEXT_PROP
8925 // global popup windows
8926 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
8927 win_free_lsize(wp);
8928 // tab-local popup windows
8929 FOR_ALL_TABPAGES(tp)
8930 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
8931 win_free_lsize(wp);
8932#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008933
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008934 new_ScreenLines = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
Bram Moolenaar216b7102010-03-23 13:56:59 +01008935 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936 if (enc_utf8)
8937 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008938 new_ScreenLinesUC = LALLOC_MULT(u8char_T, (Rows + 1) * Columns);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008939 for (i = 0; i < p_mco; ++i)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008940 new_ScreenLinesC[i] = LALLOC_CLEAR_MULT(u8char_T,
8941 (Rows + 1) * Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942 }
8943 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008944 new_ScreenLines2 = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
8945 new_ScreenAttrs = LALLOC_MULT(sattr_T, (Rows + 1) * Columns);
8946 new_LineOffset = LALLOC_MULT(unsigned, Rows);
8947 new_LineWraps = LALLOC_MULT(char_u, Rows);
8948 new_TabPageIdxs = LALLOC_MULT(short, Columns);
Bram Moolenaar33796b32019-06-08 16:01:13 +02008949#ifdef FEAT_TEXT_PROP
8950 new_popup_mask = LALLOC_MULT(short, Rows * Columns);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02008951 new_popup_mask_next = LALLOC_MULT(short, Rows * Columns);
Bram Moolenaar33796b32019-06-08 16:01:13 +02008952#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008954 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955 {
8956 if (win_alloc_lines(wp) == FAIL)
8957 {
8958 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008959 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960 }
8961 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008962 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8963 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008964 outofmem = TRUE;
Bram Moolenaar8caaf822019-06-01 18:11:22 +02008965#ifdef FEAT_TEXT_PROP
8966 // global popup windows
8967 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
8968 if (win_alloc_lines(wp) == FAIL)
8969 {
8970 outofmem = TRUE;
8971 goto give_up;
8972 }
8973 // tab-local popup windows
8974 FOR_ALL_TABPAGES(tp)
8975 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
8976 if (win_alloc_lines(wp) == FAIL)
8977 {
8978 outofmem = TRUE;
8979 goto give_up;
8980 }
8981#endif
8982
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008983give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008985 for (i = 0; i < p_mco; ++i)
8986 if (new_ScreenLinesC[i] == NULL)
8987 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988 if (new_ScreenLines == NULL
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008989 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008990 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008991 || new_ScreenAttrs == NULL
8992 || new_LineOffset == NULL
8993 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008994 || new_TabPageIdxs == NULL
Bram Moolenaar33796b32019-06-08 16:01:13 +02008995#ifdef FEAT_TEXT_PROP
8996 || new_popup_mask == NULL
Bram Moolenaar4c063a02019-06-10 21:24:12 +02008997 || new_popup_mask_next == NULL
Bram Moolenaar33796b32019-06-08 16:01:13 +02008998#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999 || outofmem)
9000 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009001 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009002 {
9003 /* guess the size */
9004 do_outofmem_msg((long_u)((Rows + 1) * Columns));
9005
9006 /* Remember we did this to avoid getting outofmem messages over
9007 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00009008 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009009 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01009010 VIM_CLEAR(new_ScreenLines);
Bram Moolenaard23a8232018-02-10 18:45:26 +01009011 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009012 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01009013 VIM_CLEAR(new_ScreenLinesC[i]);
9014 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaard23a8232018-02-10 18:45:26 +01009015 VIM_CLEAR(new_ScreenAttrs);
9016 VIM_CLEAR(new_LineOffset);
9017 VIM_CLEAR(new_LineWraps);
9018 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar33796b32019-06-08 16:01:13 +02009019#ifdef FEAT_TEXT_PROP
9020 VIM_CLEAR(new_popup_mask);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02009021 VIM_CLEAR(new_popup_mask_next);
Bram Moolenaar33796b32019-06-08 16:01:13 +02009022#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009023 }
9024 else
9025 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009026 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009027
Bram Moolenaar071d4272004-06-13 20:20:40 +00009028 for (new_row = 0; new_row < Rows; ++new_row)
9029 {
9030 new_LineOffset[new_row] = new_row * Columns;
9031 new_LineWraps[new_row] = FALSE;
9032
9033 /*
9034 * If the screen is not going to be cleared, copy as much as
9035 * possible from the old screen to the new one and clear the rest
9036 * (used when resizing the window at the "--more--" prompt or when
9037 * executing an external command, for the GUI).
9038 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009039 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009040 {
9041 (void)vim_memset(new_ScreenLines + new_row * Columns,
9042 ' ', (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009043 if (enc_utf8)
9044 {
9045 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
9046 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009047 for (i = 0; i < p_mco; ++i)
9048 (void)vim_memset(new_ScreenLinesC[i]
9049 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050 0, (size_t)Columns * sizeof(u8char_T));
9051 }
9052 if (enc_dbcs == DBCS_JPNU)
9053 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
9054 0, (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009055 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
9056 0, (size_t)Columns * sizeof(sattr_T));
9057 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009058 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009059 {
9060 if (screen_Columns < Columns)
9061 len = screen_Columns;
9062 else
9063 len = Columns;
Bram Moolenaarf4d11452005-12-02 00:46:37 +00009064 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009065 * may be invalid now. Also when p_mco changes. */
9066 if (!(enc_utf8 && ScreenLinesUC == NULL)
9067 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00009068 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
9069 ScreenLines + LineOffset[old_row],
9070 (size_t)len * sizeof(schar_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009071 if (enc_utf8 && ScreenLinesUC != NULL
9072 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009073 {
9074 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
9075 ScreenLinesUC + LineOffset[old_row],
9076 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009077 for (i = 0; i < p_mco; ++i)
9078 mch_memmove(new_ScreenLinesC[i]
9079 + new_LineOffset[new_row],
9080 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00009081 (size_t)len * sizeof(u8char_T));
9082 }
9083 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
9084 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
9085 ScreenLines2 + LineOffset[old_row],
9086 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009087 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
9088 ScreenAttrs + LineOffset[old_row],
9089 (size_t)len * sizeof(sattr_T));
9090 }
9091 }
9092 }
9093 /* Use the last line of the screen for the current line. */
9094 current_ScreenLine = new_ScreenLines + Rows * Columns;
9095 }
9096
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009097 free_screenlines();
9098
Bram Moolenaar071d4272004-06-13 20:20:40 +00009099 ScreenLines = new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009100 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009101 for (i = 0; i < p_mco; ++i)
9102 ScreenLinesC[i] = new_ScreenLinesC[i];
9103 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009104 ScreenLines2 = new_ScreenLines2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009105 ScreenAttrs = new_ScreenAttrs;
9106 LineOffset = new_LineOffset;
9107 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009108 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar33796b32019-06-08 16:01:13 +02009109#ifdef FEAT_TEXT_PROP
9110 popup_mask = new_popup_mask;
Bram Moolenaar4c063a02019-06-10 21:24:12 +02009111 popup_mask_next = new_popup_mask_next;
Bram Moolenaar1748c7f2019-06-08 16:55:15 +02009112 vim_memset(popup_mask, 0, Rows * Columns * sizeof(short));
Bram Moolenaar33796b32019-06-08 16:01:13 +02009113 popup_mask_refresh = TRUE;
9114#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009115
9116 /* It's important that screen_Rows and screen_Columns reflect the actual
9117 * size of ScreenLines[]. Set them before calling anything. */
9118#ifdef FEAT_GUI
9119 old_Rows = screen_Rows;
9120#endif
9121 screen_Rows = Rows;
9122 screen_Columns = Columns;
9123
9124 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009125 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009126 screenclear2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009127#ifdef FEAT_GUI
9128 else if (gui.in_use
9129 && !gui.starting
9130 && ScreenLines != NULL
9131 && old_Rows != Rows)
9132 {
9133 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
9134 /*
9135 * Adjust the position of the cursor, for when executing an external
9136 * command.
9137 */
9138 if (msg_row >= Rows) /* Rows got smaller */
9139 msg_row = Rows - 1; /* put cursor at last row */
9140 else if (Rows > old_Rows) /* Rows got bigger */
9141 msg_row += Rows - old_Rows; /* put cursor in same place */
9142 if (msg_col >= Columns) /* Columns got smaller */
9143 msg_col = Columns - 1; /* put cursor at last column */
9144 }
9145#endif
Bram Moolenaarca57ab52019-04-13 14:53:16 +02009146 clear_TabPageIdxs();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009147
Bram Moolenaar071d4272004-06-13 20:20:40 +00009148 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00009149 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009150
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009151 /*
9152 * Do not apply autocommands more than 3 times to avoid an endless loop
9153 * in case applying autocommands always changes Rows or Columns.
9154 */
9155 if (starting == 0 && ++retry_count <= 3)
9156 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009157 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009158 /* In rare cases, autocommands may have altered Rows or Columns,
9159 * jump back to check if we need to allocate the screen again. */
9160 goto retry;
9161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009162}
9163
9164 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009165free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009166{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009167 int i;
9168
Bram Moolenaar33796b32019-06-08 16:01:13 +02009169 VIM_CLEAR(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009170 for (i = 0; i < Screen_mco; ++i)
Bram Moolenaar33796b32019-06-08 16:01:13 +02009171 VIM_CLEAR(ScreenLinesC[i]);
9172 VIM_CLEAR(ScreenLines2);
9173 VIM_CLEAR(ScreenLines);
9174 VIM_CLEAR(ScreenAttrs);
9175 VIM_CLEAR(LineOffset);
9176 VIM_CLEAR(LineWraps);
9177 VIM_CLEAR(TabPageIdxs);
9178#ifdef FEAT_TEXT_PROP
9179 VIM_CLEAR(popup_mask);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02009180 VIM_CLEAR(popup_mask_next);
Bram Moolenaar33796b32019-06-08 16:01:13 +02009181#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009182}
9183
9184 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009185screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009186{
9187 check_for_delay(FALSE);
9188 screenalloc(FALSE); /* allocate screen buffers if size changed */
9189 screenclear2(); /* clear the screen */
9190}
9191
9192 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009193screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009194{
9195 int i;
9196
9197 if (starting == NO_SCREEN || ScreenLines == NULL
9198#ifdef FEAT_GUI
9199 || (gui.in_use && gui.starting)
9200#endif
9201 )
9202 return;
9203
9204#ifdef FEAT_GUI
9205 if (!gui.in_use)
9206#endif
9207 screen_attr = -1; /* force setting the Normal colors */
9208 screen_stop_highlight(); /* don't want highlighting here */
9209
9210#ifdef FEAT_CLIPBOARD
9211 /* disable selection without redrawing it */
9212 clip_scroll_selection(9999);
9213#endif
9214
9215 /* blank out ScreenLines */
9216 for (i = 0; i < Rows; ++i)
9217 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009218 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009219 LineWraps[i] = FALSE;
9220 }
9221
9222 if (can_clear(T_CL))
9223 {
9224 out_str(T_CL); /* clear the display */
9225 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009226 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009227 }
9228 else
9229 {
9230 /* can't clear the screen, mark all chars with invalid attributes */
9231 for (i = 0; i < Rows; ++i)
9232 lineinvalid(LineOffset[i], (int)Columns);
9233 clear_cmdline = TRUE;
9234 }
9235
9236 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9237
9238 win_rest_invalid(firstwin);
9239 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009240 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009241 if (must_redraw == CLEAR) /* no need to clear again */
9242 must_redraw = NOT_VALID;
9243 compute_cmdrow();
9244 msg_row = cmdline_row; /* put cursor on last line for messages */
9245 msg_col = 0;
9246 screen_start(); /* don't know where cursor is now */
9247 msg_scrolled = 0; /* can't scroll back */
9248 msg_didany = FALSE;
9249 msg_didout = FALSE;
9250}
9251
9252/*
9253 * Clear one line in ScreenLines.
9254 */
9255 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009256lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009257{
9258 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009259 if (enc_utf8)
9260 (void)vim_memset(ScreenLinesUC + off, 0,
9261 (size_t)width * sizeof(u8char_T));
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009262 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009263}
9264
9265/*
9266 * Mark one line in ScreenLines invalid by setting the attributes to an
9267 * invalid value.
9268 */
9269 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009270lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009271{
9272 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9273}
9274
Bram Moolenaar071d4272004-06-13 20:20:40 +00009275/*
9276 * Copy part of a Screenline for vertically split window "wp".
9277 */
9278 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009279linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009280{
9281 unsigned off_to = LineOffset[to] + wp->w_wincol;
9282 unsigned off_from = LineOffset[from] + wp->w_wincol;
9283
9284 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9285 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009286 if (enc_utf8)
9287 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009288 int i;
9289
Bram Moolenaar071d4272004-06-13 20:20:40 +00009290 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9291 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009292 for (i = 0; i < p_mco; ++i)
9293 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9294 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009295 }
9296 if (enc_dbcs == DBCS_JPNU)
9297 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9298 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009299 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9300 wp->w_width * sizeof(sattr_T));
9301}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009302
9303/*
9304 * Return TRUE if clearing with term string "p" would work.
9305 * It can't work when the string is empty or it won't set the right background.
Bram Moolenaar33796b32019-06-08 16:01:13 +02009306 * Don't clear to end-of-line when there are popups, it may cause flicker.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009307 */
9308 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009309can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009310{
9311 return (*p != NUL && (t_colors <= 1
9312#ifdef FEAT_GUI
9313 || gui.in_use
9314#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009315#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009316 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009317 || (!p_tgc && cterm_normal_bg_color == 0)
9318#else
9319 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009320#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02009321 || *T_UT != NUL)
9322#ifdef FEAT_TEXT_PROP
9323 && !(p == T_CE && popup_visible)
9324#endif
9325 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00009326}
9327
9328/*
9329 * Reset cursor position. Use whenever cursor was moved because of outputting
9330 * something directly to the screen (shell commands) or a terminal control
9331 * code.
9332 */
9333 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009334screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009335{
9336 screen_cur_row = screen_cur_col = 9999;
9337}
9338
9339/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009340 * Move the cursor to position "row","col" in the screen.
9341 * This tries to find the most efficient way to move, minimizing the number of
9342 * characters sent to the terminal.
9343 */
9344 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009345windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009346{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009347 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009348 int i;
9349 int plan;
9350 int cost;
9351 int wouldbe_col;
9352 int noinvcurs;
9353 char_u *bs;
9354 int goto_cost;
9355 int attr;
9356
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009357#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009358#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9359
9360#define PLAN_LE 1
9361#define PLAN_CR 2
9362#define PLAN_NL 3
9363#define PLAN_WRITE 4
9364 /* Can't use ScreenLines unless initialized */
9365 if (ScreenLines == NULL)
9366 return;
9367
9368 if (col != screen_cur_col || row != screen_cur_row)
9369 {
9370 /* Check for valid position. */
9371 if (row < 0) /* window without text lines? */
9372 row = 0;
9373 if (row >= screen_Rows)
9374 row = screen_Rows - 1;
9375 if (col >= screen_Columns)
9376 col = screen_Columns - 1;
9377
9378 /* check if no cursor movement is allowed in highlight mode */
9379 if (screen_attr && *T_MS == NUL)
9380 noinvcurs = HIGHL_COST;
9381 else
9382 noinvcurs = 0;
9383 goto_cost = GOTO_COST + noinvcurs;
9384
9385 /*
9386 * Plan how to do the positioning:
9387 * 1. Use CR to move it to column 0, same row.
9388 * 2. Use T_LE to move it a few columns to the left.
9389 * 3. Use NL to move a few lines down, column 0.
9390 * 4. Move a few columns to the right with T_ND or by writing chars.
9391 *
9392 * Don't do this if the cursor went beyond the last column, the cursor
9393 * position is unknown then (some terminals wrap, some don't )
9394 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009395 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009396 * characters to move the cursor to the right.
9397 */
9398 if (row >= screen_cur_row && screen_cur_col < Columns)
9399 {
9400 /*
9401 * If the cursor is in the same row, bigger col, we can use CR
9402 * or T_LE.
9403 */
9404 bs = NULL; /* init for GCC */
9405 attr = screen_attr;
9406 if (row == screen_cur_row && col < screen_cur_col)
9407 {
9408 /* "le" is preferred over "bc", because "bc" is obsolete */
9409 if (*T_LE)
9410 bs = T_LE; /* "cursor left" */
9411 else
9412 bs = T_BC; /* "backspace character (old) */
9413 if (*bs)
9414 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9415 else
9416 cost = 999;
9417 if (col + 1 < cost) /* using CR is less characters */
9418 {
9419 plan = PLAN_CR;
9420 wouldbe_col = 0;
9421 cost = 1; /* CR is just one character */
9422 }
9423 else
9424 {
9425 plan = PLAN_LE;
9426 wouldbe_col = col;
9427 }
9428 if (noinvcurs) /* will stop highlighting */
9429 {
9430 cost += noinvcurs;
9431 attr = 0;
9432 }
9433 }
9434
9435 /*
9436 * If the cursor is above where we want to be, we can use CR LF.
9437 */
9438 else if (row > screen_cur_row)
9439 {
9440 plan = PLAN_NL;
9441 wouldbe_col = 0;
9442 cost = (row - screen_cur_row) * 2; /* CR LF */
9443 if (noinvcurs) /* will stop highlighting */
9444 {
9445 cost += noinvcurs;
9446 attr = 0;
9447 }
9448 }
9449
9450 /*
9451 * If the cursor is in the same row, smaller col, just use write.
9452 */
9453 else
9454 {
9455 plan = PLAN_WRITE;
9456 wouldbe_col = screen_cur_col;
9457 cost = 0;
9458 }
9459
9460 /*
9461 * Check if any characters that need to be written have the
9462 * correct attributes. Also avoid UTF-8 characters.
9463 */
9464 i = col - wouldbe_col;
9465 if (i > 0)
9466 cost += i;
9467 if (cost < goto_cost && i > 0)
9468 {
9469 /*
9470 * Check if the attributes are correct without additionally
9471 * stopping highlighting.
9472 */
9473 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9474 while (i && *p++ == attr)
9475 --i;
9476 if (i != 0)
9477 {
9478 /*
9479 * Try if it works when highlighting is stopped here.
9480 */
9481 if (*--p == 0)
9482 {
9483 cost += noinvcurs;
9484 while (i && *p++ == 0)
9485 --i;
9486 }
9487 if (i != 0)
9488 cost = 999; /* different attributes, don't do it */
9489 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009490 if (enc_utf8)
9491 {
9492 /* Don't use an UTF-8 char for positioning, it's slow. */
9493 for (i = wouldbe_col; i < col; ++i)
9494 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9495 {
9496 cost = 999;
9497 break;
9498 }
9499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009500 }
9501
9502 /*
9503 * We can do it without term_windgoto()!
9504 */
9505 if (cost < goto_cost)
9506 {
9507 if (plan == PLAN_LE)
9508 {
9509 if (noinvcurs)
9510 screen_stop_highlight();
9511 while (screen_cur_col > col)
9512 {
9513 out_str(bs);
9514 --screen_cur_col;
9515 }
9516 }
9517 else if (plan == PLAN_CR)
9518 {
9519 if (noinvcurs)
9520 screen_stop_highlight();
9521 out_char('\r');
9522 screen_cur_col = 0;
9523 }
9524 else if (plan == PLAN_NL)
9525 {
9526 if (noinvcurs)
9527 screen_stop_highlight();
9528 while (screen_cur_row < row)
9529 {
9530 out_char('\n');
9531 ++screen_cur_row;
9532 }
9533 screen_cur_col = 0;
9534 }
9535
9536 i = col - screen_cur_col;
9537 if (i > 0)
9538 {
9539 /*
9540 * Use cursor-right if it's one character only. Avoids
9541 * removing a line of pixels from the last bold char, when
9542 * using the bold trick in the GUI.
9543 */
9544 if (T_ND[0] != NUL && T_ND[1] == NUL)
9545 {
9546 while (i-- > 0)
9547 out_char(*T_ND);
9548 }
9549 else
9550 {
9551 int off;
9552
9553 off = LineOffset[row] + screen_cur_col;
9554 while (i-- > 0)
9555 {
9556 if (ScreenAttrs[off] != screen_attr)
9557 screen_stop_highlight();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009559 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009560 if (enc_dbcs == DBCS_JPNU
9561 && ScreenLines[off] == 0x8e)
9562 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009563 ++off;
9564 }
9565 }
9566 }
9567 }
9568 }
9569 else
9570 cost = 999;
9571
9572 if (cost >= goto_cost)
9573 {
9574 if (noinvcurs)
9575 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009576 if (row == screen_cur_row && (col > screen_cur_col)
9577 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009578 term_cursor_right(col - screen_cur_col);
9579 else
9580 term_windgoto(row, col);
9581 }
9582 screen_cur_row = row;
9583 screen_cur_col = col;
9584 }
9585}
9586
9587/*
9588 * Set cursor to its position in the current window.
9589 */
9590 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009591setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009592{
Bram Moolenaar987723e2018-03-06 11:43:04 +01009593 setcursor_mayforce(FALSE);
9594}
9595
9596/*
9597 * Set cursor to its position in the current window.
9598 * When "force" is TRUE also when not redrawing.
9599 */
9600 void
9601setcursor_mayforce(int force)
9602{
9603 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009604 {
9605 validate_cursor();
9606 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009607 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009608#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009609 /* With 'rightleft' set and the cursor on a double-wide
9610 * character, position it on the leftmost column. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01009611 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol
9612 - ((has_mbyte
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009613 && (*mb_ptr2cells)(ml_get_cursor()) == 2
Bram Moolenaara12a1612019-01-24 16:39:02 +01009614 && vim_isprintc(gchar_cursor())) ? 2 : 1)) :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009615#endif
9616 curwin->w_wcol));
9617 }
9618}
9619
9620
9621/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009622 * Insert 'line_count' lines at 'row' in window 'wp'.
9623 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9624 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009625 * scrolling.
9626 * Returns FAIL if the lines are not inserted, OK for success.
9627 */
9628 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009629win_ins_lines(
9630 win_T *wp,
9631 int row,
9632 int line_count,
9633 int invalid,
9634 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009635{
9636 int did_delete;
9637 int nextrow;
9638 int lastrow;
9639 int retval;
9640
9641 if (invalid)
9642 wp->w_lines_valid = 0;
9643
9644 if (wp->w_height < 5)
9645 return FAIL;
9646
9647 if (line_count > wp->w_height - row)
9648 line_count = wp->w_height - row;
9649
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009650 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009651 if (retval != MAYBE)
9652 return retval;
9653
9654 /*
9655 * If there is a next window or a status line, we first try to delete the
9656 * lines at the bottom to avoid messing what is after the window.
9657 * If this fails and there are following windows, don't do anything to avoid
9658 * messing up those windows, better just redraw.
9659 */
9660 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009661 if (wp->w_next != NULL || wp->w_status_height)
9662 {
9663 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009664 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009665 did_delete = TRUE;
9666 else if (wp->w_next)
9667 return FAIL;
9668 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009669 /*
9670 * if no lines deleted, blank the lines that will end up below the window
9671 */
9672 if (!did_delete)
9673 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009674 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009675 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02009676 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677 lastrow = nextrow + line_count;
9678 if (lastrow > Rows)
9679 lastrow = Rows;
9680 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009681 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009682 ' ', ' ', 0);
9683 }
9684
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009685 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009686 == FAIL)
9687 {
9688 /* deletion will have messed up other windows */
9689 if (did_delete)
9690 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009691 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009692 win_rest_invalid(W_NEXT(wp));
9693 }
9694 return FAIL;
9695 }
9696
9697 return OK;
9698}
9699
9700/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009701 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009702 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9703 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9704 * scrolling
9705 * Return OK for success, FAIL if the lines are not deleted.
9706 */
9707 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009708win_del_lines(
9709 win_T *wp,
9710 int row,
9711 int line_count,
9712 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009713 int mayclear,
9714 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009715{
9716 int retval;
9717
9718 if (invalid)
9719 wp->w_lines_valid = 0;
9720
9721 if (line_count > wp->w_height - row)
9722 line_count = wp->w_height - row;
9723
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009724 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009725 if (retval != MAYBE)
9726 return retval;
9727
9728 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009729 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009730 return FAIL;
9731
Bram Moolenaar071d4272004-06-13 20:20:40 +00009732 /*
9733 * If there are windows or status lines below, try to put them at the
9734 * correct place. If we can't do that, they have to be redrawn.
9735 */
9736 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9737 {
9738 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009739 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740 {
9741 wp->w_redr_status = TRUE;
9742 win_rest_invalid(wp->w_next);
9743 }
9744 }
9745 /*
9746 * If this is the last window and there is no status line, redraw the
9747 * command line later.
9748 */
9749 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009750 redraw_cmdline = TRUE;
9751 return OK;
9752}
9753
9754/*
9755 * Common code for win_ins_lines() and win_del_lines().
9756 * Returns OK or FAIL when the work has been done.
9757 * Returns MAYBE when not finished yet.
9758 */
9759 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009760win_do_lines(
9761 win_T *wp,
9762 int row,
9763 int line_count,
9764 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009765 int del,
9766 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009767{
9768 int retval;
9769
9770 if (!redrawing() || line_count <= 0)
9771 return FAIL;
9772
Bram Moolenaar33796b32019-06-08 16:01:13 +02009773 // When inserting lines would result in loss of command output, just redraw
9774 // the lines.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009775 if (no_win_do_lines_ins && !del)
9776 return FAIL;
9777
Bram Moolenaar33796b32019-06-08 16:01:13 +02009778 // only a few lines left: redraw is faster
Bram Moolenaar4033c552017-09-16 20:54:51 +02009779 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009781 if (!no_win_do_lines_ins)
Bram Moolenaar33796b32019-06-08 16:01:13 +02009782 screenclear(); // will set wp->w_lines_valid to 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00009783 return FAIL;
9784 }
9785
Bram Moolenaar33796b32019-06-08 16:01:13 +02009786#ifdef FEAT_TEXT_PROP
Bram Moolenaar4c063a02019-06-10 21:24:12 +02009787 // this doesn't work when there are popups visible
Bram Moolenaar33796b32019-06-08 16:01:13 +02009788 if (popup_visible)
9789 return FAIL;
9790#endif
9791
9792 // Delete all remaining lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00009793 if (row + line_count >= wp->w_height)
9794 {
9795 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009796 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009797 ' ', ' ', 0);
9798 return OK;
9799 }
9800
9801 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009802 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009803 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009804 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009805 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009806 if (!no_win_do_lines_ins)
9807 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009808
9809 /*
9810 * If the terminal can set a scroll region, use that.
9811 * Always do this in a vertically split window. This will redraw from
9812 * ScreenLines[] when t_CV isn't defined. That's faster than using
9813 * win_line().
9814 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009815 * a character in the lower right corner of the scroll region may cause a
9816 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009817 */
Bram Moolenaar02631462017-09-22 15:20:32 +02009818 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009819 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009820 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009821 scroll_region_set(wp, row);
9822 if (del)
9823 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009824 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009825 else
9826 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009827 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009828 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009829 scroll_region_reset();
9830 return retval;
9831 }
9832
Bram Moolenaar071d4272004-06-13 20:20:40 +00009833 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9834 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009835
9836 return MAYBE;
9837}
9838
9839/*
9840 * window 'wp' and everything after it is messed up, mark it for redraw
9841 */
9842 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009843win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009844{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009845 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009846 {
9847 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009848 wp->w_redr_status = TRUE;
9849 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009850 }
9851 redraw_cmdline = TRUE;
9852}
9853
9854/*
9855 * The rest of the routines in this file perform screen manipulations. The
9856 * given operation is performed physically on the screen. The corresponding
9857 * change is also made to the internal screen image. In this way, the editor
9858 * anticipates the effect of editing changes on the appearance of the screen.
9859 * That way, when we call screenupdate a complete redraw isn't usually
9860 * necessary. Another advantage is that we can keep adding code to anticipate
9861 * screen changes, and in the meantime, everything still works.
9862 */
9863
9864/*
9865 * types for inserting or deleting lines
9866 */
9867#define USE_T_CAL 1
9868#define USE_T_CDL 2
9869#define USE_T_AL 3
9870#define USE_T_CE 4
9871#define USE_T_DL 5
9872#define USE_T_SR 6
9873#define USE_NL 7
9874#define USE_T_CD 8
9875#define USE_REDRAW 9
9876
9877/*
9878 * insert lines on the screen and update ScreenLines[]
9879 * 'end' is the line after the scrolled part. Normally it is Rows.
9880 * When scrolling region used 'off' is the offset from the top for the region.
9881 * 'row' and 'end' are relative to the start of the region.
9882 *
9883 * return FAIL for failure, OK for success.
9884 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009885 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009886screen_ins_lines(
9887 int off,
9888 int row,
9889 int line_count,
9890 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009891 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01009892 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009893{
9894 int i;
9895 int j;
9896 unsigned temp;
9897 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009898 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009899 int type;
9900 int result_empty;
9901 int can_ce = can_clear(T_CE);
9902
9903 /*
9904 * FAIL if
9905 * - there is no valid screen
9906 * - the screen has to be redrawn completely
9907 * - the line count is less than one
9908 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009909 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar33796b32019-06-08 16:01:13 +02009910 * - there is a popup window
Bram Moolenaar071d4272004-06-13 20:20:40 +00009911 */
Bram Moolenaar33796b32019-06-08 16:01:13 +02009912 if (!screen_valid(TRUE)
9913 || line_count <= 0 || line_count > p_ttyscroll
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009914#ifdef FEAT_CLIPBOARD
9915 || (clip_star.state != SELECT_CLEARED
9916 && redrawing_for_callback > 0)
9917#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02009918#ifdef FEAT_TEXT_PROP
9919 || popup_visible
9920#endif
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009921 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009922 return FAIL;
9923
9924 /*
9925 * There are seven ways to insert lines:
9926 * 0. When in a vertically split window and t_CV isn't set, redraw the
9927 * characters from ScreenLines[].
9928 * 1. Use T_CD (clear to end of display) if it exists and the result of
9929 * the insert is just empty lines
9930 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9931 * present or line_count > 1. It looks better if we do all the inserts
9932 * at once.
9933 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9934 * insert is just empty lines and T_CE is not present or line_count >
9935 * 1.
9936 * 4. Use T_AL (insert line) if it exists.
9937 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9938 * just empty lines.
9939 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9940 * just empty lines.
9941 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9942 * the 'da' flag is not set or we have clear line capability.
9943 * 8. redraw the characters from ScreenLines[].
9944 *
9945 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9946 * the scrollbar for the window. It does have insert line, use that if it
9947 * exists.
9948 */
9949 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009950 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9951 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009952 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009953 type = USE_T_CD;
9954 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9955 type = USE_T_CAL;
9956 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9957 type = USE_T_CDL;
9958 else if (*T_AL != NUL)
9959 type = USE_T_AL;
9960 else if (can_ce && result_empty)
9961 type = USE_T_CE;
9962 else if (*T_DL != NUL && result_empty)
9963 type = USE_T_DL;
9964 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9965 type = USE_T_SR;
9966 else
9967 return FAIL;
9968
9969 /*
9970 * For clearing the lines screen_del_lines() is used. This will also take
9971 * care of t_db if necessary.
9972 */
9973 if (type == USE_T_CD || type == USE_T_CDL ||
9974 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009975 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009976
9977 /*
9978 * If text is retained below the screen, first clear or delete as many
9979 * lines at the bottom of the window as are about to be inserted so that
9980 * the deleted lines won't later surface during a screen_del_lines.
9981 */
9982 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009983 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009984
9985#ifdef FEAT_CLIPBOARD
9986 /* Remove a modeless selection when inserting lines halfway the screen
9987 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009988 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009989 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009990 else
9991 clip_scroll_selection(-line_count);
9992#endif
9993
Bram Moolenaar071d4272004-06-13 20:20:40 +00009994#ifdef FEAT_GUI
9995 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9996 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009997 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009998#endif
9999
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010000 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10001 cursor_col = wp->w_wincol;
10002
Bram Moolenaar071d4272004-06-13 20:20:40 +000010003 if (*T_CCS != NUL) /* cursor relative to region */
10004 cursor_row = row;
10005 else
10006 cursor_row = row + off;
10007
10008 /*
10009 * Shift LineOffset[] line_count down to reflect the inserted lines.
10010 * Clear the inserted lines in ScreenLines[].
10011 */
10012 row += off;
10013 end += off;
10014 for (i = 0; i < line_count; ++i)
10015 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010016 if (wp != NULL && wp->w_width != Columns)
10017 {
10018 /* need to copy part of a line */
10019 j = end - 1 - i;
10020 while ((j -= line_count) >= row)
10021 linecopy(j + line_count, j, wp);
10022 j += line_count;
10023 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010024 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10025 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010026 else
10027 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10028 LineWraps[j] = FALSE;
10029 }
10030 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010031 {
10032 j = end - 1 - i;
10033 temp = LineOffset[j];
10034 while ((j -= line_count) >= row)
10035 {
10036 LineOffset[j + line_count] = LineOffset[j];
10037 LineWraps[j + line_count] = LineWraps[j];
10038 }
10039 LineOffset[j + line_count] = temp;
10040 LineWraps[j + line_count] = FALSE;
10041 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010042 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010043 else
10044 lineinvalid(temp, (int)Columns);
10045 }
10046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010047
10048 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010049 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010050 if (clear_attr != 0)
10051 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010052
Bram Moolenaar071d4272004-06-13 20:20:40 +000010053 /* redraw the characters */
10054 if (type == USE_REDRAW)
10055 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010056 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057 {
10058 term_append_lines(line_count);
10059 screen_start(); /* don't know where cursor is now */
10060 }
10061 else
10062 {
10063 for (i = 0; i < line_count; i++)
10064 {
10065 if (type == USE_T_AL)
10066 {
10067 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010068 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010069 out_str(T_AL);
10070 }
10071 else /* type == USE_T_SR */
10072 out_str(T_SR);
10073 screen_start(); /* don't know where cursor is now */
10074 }
10075 }
10076
10077 /*
10078 * With scroll-reverse and 'da' flag set we need to clear the lines that
10079 * have been scrolled down into the region.
10080 */
10081 if (type == USE_T_SR && *T_DA)
10082 {
10083 for (i = 0; i < line_count; ++i)
10084 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010085 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010086 out_str(T_CE);
10087 screen_start(); /* don't know where cursor is now */
10088 }
10089 }
10090
10091#ifdef FEAT_GUI
10092 gui_can_update_cursor();
10093 if (gui.in_use)
10094 out_flush(); /* always flush after a scroll */
10095#endif
10096 return OK;
10097}
10098
10099/*
Bram Moolenaar107abd22016-08-12 14:08:25 +020010100 * Delete lines on the screen and update ScreenLines[].
10101 * "end" is the line after the scrolled part. Normally it is Rows.
10102 * When scrolling region used "off" is the offset from the top for the region.
10103 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010104 *
10105 * Return OK for success, FAIL if the lines are not deleted.
10106 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010107 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010108screen_del_lines(
10109 int off,
10110 int row,
10111 int line_count,
10112 int end,
10113 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010114 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +010010115 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010116{
10117 int j;
10118 int i;
10119 unsigned temp;
10120 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010121 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010122 int cursor_end;
10123 int result_empty; /* result is empty until end of region */
10124 int can_delete; /* deleting line codes can be used */
10125 int type;
10126
10127 /*
10128 * FAIL if
10129 * - there is no valid screen
10130 * - the screen has to be redrawn completely
10131 * - the line count is less than one
10132 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010133 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +000010134 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010135 if (!screen_valid(TRUE) || line_count <= 0
10136 || (!force && line_count > p_ttyscroll)
10137#ifdef FEAT_CLIPBOARD
10138 || (clip_star.state != SELECT_CLEARED
10139 && redrawing_for_callback > 0)
10140#endif
10141 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010142 return FAIL;
10143
10144 /*
10145 * Check if the rest of the current region will become empty.
10146 */
10147 result_empty = row + line_count >= end;
10148
10149 /*
10150 * We can delete lines only when 'db' flag not set or when 'ce' option
10151 * available.
10152 */
10153 can_delete = (*T_DB == NUL || can_clear(T_CE));
10154
10155 /*
10156 * There are six ways to delete lines:
10157 * 0. When in a vertically split window and t_CV isn't set, redraw the
10158 * characters from ScreenLines[].
10159 * 1. Use T_CD if it exists and the result is empty.
10160 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
10161 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
10162 * none of the other ways work.
10163 * 4. Use T_CE (erase line) if the result is empty.
10164 * 5. Use T_DL (delete line) if it exists.
10165 * 6. redraw the characters from ScreenLines[].
10166 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010167 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10168 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +020010169 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010170 type = USE_T_CD;
10171#if defined(__BEOS__) && defined(BEOS_DR8)
10172 /*
10173 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
10174 * its internal termcap... this works okay for tests which test *T_DB !=
10175 * NUL. It has the disadvantage that the user cannot use any :set t_*
10176 * command to get T_DB (back) to empty_option, only :set term=... will do
10177 * the trick...
10178 * Anyway, this hack will hopefully go away with the next OS release.
10179 * (Olaf Seibert)
10180 */
10181 else if (row == 0 && T_DB == empty_option
10182 && (line_count == 1 || *T_CDL == NUL))
10183#else
10184 else if (row == 0 && (
10185#ifndef AMIGA
10186 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
10187 * up, so use delete-line command */
10188 line_count == 1 ||
10189#endif
10190 *T_CDL == NUL))
10191#endif
10192 type = USE_NL;
10193 else if (*T_CDL != NUL && line_count > 1 && can_delete)
10194 type = USE_T_CDL;
10195 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +020010196 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010197 type = USE_T_CE;
10198 else if (*T_DL != NUL && can_delete)
10199 type = USE_T_DL;
10200 else if (*T_CDL != NUL && can_delete)
10201 type = USE_T_CDL;
10202 else
10203 return FAIL;
10204
10205#ifdef FEAT_CLIPBOARD
10206 /* Remove a modeless selection when deleting lines halfway the screen or
10207 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010208 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010209 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010210 else
10211 clip_scroll_selection(line_count);
10212#endif
10213
Bram Moolenaar071d4272004-06-13 20:20:40 +000010214#ifdef FEAT_GUI
10215 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10216 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010217 gui_dont_update_cursor(gui.cursor_row >= row + off
10218 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010219#endif
10220
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010221 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10222 cursor_col = wp->w_wincol;
10223
Bram Moolenaar071d4272004-06-13 20:20:40 +000010224 if (*T_CCS != NUL) /* cursor relative to region */
10225 {
10226 cursor_row = row;
10227 cursor_end = end;
10228 }
10229 else
10230 {
10231 cursor_row = row + off;
10232 cursor_end = end + off;
10233 }
10234
10235 /*
10236 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10237 * Clear the inserted lines in ScreenLines[].
10238 */
10239 row += off;
10240 end += off;
10241 for (i = 0; i < line_count; ++i)
10242 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010243 if (wp != NULL && wp->w_width != Columns)
10244 {
10245 /* need to copy part of a line */
10246 j = row + i;
10247 while ((j += line_count) <= end - 1)
10248 linecopy(j - line_count, j, wp);
10249 j -= line_count;
10250 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010251 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10252 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010253 else
10254 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10255 LineWraps[j] = FALSE;
10256 }
10257 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010258 {
10259 /* whole width, moving the line pointers is faster */
10260 j = row + i;
10261 temp = LineOffset[j];
10262 while ((j += line_count) <= end - 1)
10263 {
10264 LineOffset[j - line_count] = LineOffset[j];
10265 LineWraps[j - line_count] = LineWraps[j];
10266 }
10267 LineOffset[j - line_count] = temp;
10268 LineWraps[j - line_count] = FALSE;
10269 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010270 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010271 else
10272 lineinvalid(temp, (int)Columns);
10273 }
10274 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010275
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010276 if (screen_attr != clear_attr)
10277 screen_stop_highlight();
10278 if (clear_attr != 0)
10279 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010280
Bram Moolenaar071d4272004-06-13 20:20:40 +000010281 /* redraw the characters */
10282 if (type == USE_REDRAW)
10283 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010284 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010285 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010286 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010287 out_str(T_CD);
10288 screen_start(); /* don't know where cursor is now */
10289 }
10290 else if (type == USE_T_CDL)
10291 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010292 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010293 term_delete_lines(line_count);
10294 screen_start(); /* don't know where cursor is now */
10295 }
10296 /*
10297 * Deleting lines at top of the screen or scroll region: Just scroll
10298 * the whole screen (scroll region) up by outputting newlines on the
10299 * last line.
10300 */
10301 else if (type == USE_NL)
10302 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010303 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010304 for (i = line_count; --i >= 0; )
10305 out_char('\n'); /* cursor will remain on same line */
10306 }
10307 else
10308 {
10309 for (i = line_count; --i >= 0; )
10310 {
10311 if (type == USE_T_DL)
10312 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010313 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010314 out_str(T_DL); /* delete a line */
10315 }
10316 else /* type == USE_T_CE */
10317 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010318 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010319 out_str(T_CE); /* erase a line */
10320 }
10321 screen_start(); /* don't know where cursor is now */
10322 }
10323 }
10324
10325 /*
10326 * If the 'db' flag is set, we need to clear the lines that have been
10327 * scrolled up at the bottom of the region.
10328 */
10329 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10330 {
10331 for (i = line_count; i > 0; --i)
10332 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010333 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010334 out_str(T_CE); /* erase a line */
10335 screen_start(); /* don't know where cursor is now */
10336 }
10337 }
10338
10339#ifdef FEAT_GUI
10340 gui_can_update_cursor();
10341 if (gui.in_use)
10342 out_flush(); /* always flush after a scroll */
10343#endif
10344
10345 return OK;
10346}
10347
10348/*
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010349 * Return TRUE when postponing displaying the mode message: when not redrawing
10350 * or inside a mapping.
10351 */
10352 int
10353skip_showmode()
10354{
10355 // Call char_avail() only when we are going to show something, because it
10356 // takes a bit of time. redrawing() may also call char_avail_avail().
10357 if (global_busy
10358 || msg_silent != 0
10359 || !redrawing()
10360 || (char_avail() && !KeyTyped))
10361 {
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010362 redraw_mode = TRUE; // show mode later
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010363 return TRUE;
10364 }
10365 return FALSE;
10366}
10367
10368/*
Bram Moolenaar81226e02018-02-20 21:44:45 +010010369 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010370 *
10371 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10372 * If clear_cmdline is FALSE there may be a message there that needs to be
10373 * cleared only if a mode is shown.
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010374 * If redraw_mode is TRUE show or clear the mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010375 * Return the length of the message (0 if no message).
10376 */
10377 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010378showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010379{
10380 int need_clear;
10381 int length = 0;
10382 int do_mode;
10383 int attr;
10384 int nwr_save;
10385#ifdef FEAT_INS_EXPAND
10386 int sub_attr;
10387#endif
10388
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010389 do_mode = ((p_smd && msg_silent == 0)
10390 && ((State & INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +020010391 || restart_edit != NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010392 || VIsual_active));
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010393 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010394 {
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010395 if (skip_showmode())
10396 return 0; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +000010397
10398 nwr_save = need_wait_return;
10399
10400 /* wait a bit before overwriting an important message */
10401 check_for_delay(FALSE);
10402
10403 /* if the cmdline is more than one line high, erase top lines */
10404 need_clear = clear_cmdline;
10405 if (clear_cmdline && cmdline_row < Rows - 1)
10406 msg_clr_cmdline(); /* will reset clear_cmdline */
10407
10408 /* Position on the last line in the window, column 0 */
10409 msg_pos_mode();
10410 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010411 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010412 if (do_mode)
10413 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010414 msg_puts_attr("--", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010415#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010416 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010417# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010418 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010419# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010420 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010421# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010422 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010423# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar32526b32019-01-19 17:43:09 +010010424 msg_puts_attr(" IM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010425# else
Bram Moolenaar32526b32019-01-19 17:43:09 +010010426 msg_puts_attr(" XIM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010427# endif
10428#endif
10429#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10430 if (gui.in_use)
10431 {
10432 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010433 {
10434 /* HANGUL */
10435 if (enc_utf8)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010436 msg_puts_attr(" \355\225\234\352\270\200", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010437 else
Bram Moolenaar32526b32019-01-19 17:43:09 +010010438 msg_puts_attr(" \307\321\261\333", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010440 }
10441#endif
10442#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010443 /* CTRL-X in Insert mode */
10444 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010445 {
10446 /* These messages can get long, avoid a wrap in a narrow
10447 * window. Prefer showing edit_submode_extra. */
10448 length = (Rows - msg_row) * Columns - 3;
10449 if (edit_submode_extra != NULL)
10450 length -= vim_strsize(edit_submode_extra);
10451 if (length > 0)
10452 {
10453 if (edit_submode_pre != NULL)
10454 length -= vim_strsize(edit_submode_pre);
10455 if (length - vim_strsize(edit_submode) > 0)
10456 {
10457 if (edit_submode_pre != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010458 msg_puts_attr((char *)edit_submode_pre, attr);
10459 msg_puts_attr((char *)edit_submode, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010460 }
10461 if (edit_submode_extra != NULL)
10462 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010463 msg_puts_attr(" ", attr); /* add a space in between */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010464 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010465 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010466 else
10467 sub_attr = attr;
Bram Moolenaar32526b32019-01-19 17:43:09 +010010468 msg_puts_attr((char *)edit_submode_extra, sub_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010469 }
10470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010471 }
10472 else
10473#endif
10474 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010475 if (State & VREPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010476 msg_puts_attr(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010477 else if (State & REPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010478 msg_puts_attr(_(" REPLACE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010479 else if (State & INSERT)
10480 {
10481#ifdef FEAT_RIGHTLEFT
10482 if (p_ri)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010483 msg_puts_attr(_(" REVERSE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010484#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +010010485 msg_puts_attr(_(" INSERT"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010486 }
Bram Moolenaar942b4542018-06-17 16:23:34 +020010487 else if (restart_edit == 'I' || restart_edit == 'A')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010488 msg_puts_attr(_(" (insert)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010489 else if (restart_edit == 'R')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010490 msg_puts_attr(_(" (replace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010491 else if (restart_edit == 'V')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010492 msg_puts_attr(_(" (vreplace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010493#ifdef FEAT_RIGHTLEFT
10494 if (p_hkmap)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010495 msg_puts_attr(_(" Hebrew"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010496#endif
10497#ifdef FEAT_KEYMAP
10498 if (State & LANGMAP)
10499 {
10500# ifdef FEAT_ARABIC
10501 if (curwin->w_p_arab)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010502 msg_puts_attr(_(" Arabic"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010503 else
10504# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010505 if (get_keymap_str(curwin, (char_u *)" (%s)",
10506 NameBuff, MAXPATHL))
Bram Moolenaar32526b32019-01-19 17:43:09 +010010507 msg_puts_attr((char *)NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010508 }
10509#endif
10510 if ((State & INSERT) && p_paste)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010511 msg_puts_attr(_(" (paste)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010512
Bram Moolenaar071d4272004-06-13 20:20:40 +000010513 if (VIsual_active)
10514 {
10515 char *p;
10516
10517 /* Don't concatenate separate words to avoid translation
10518 * problems. */
10519 switch ((VIsual_select ? 4 : 0)
10520 + (VIsual_mode == Ctrl_V) * 2
10521 + (VIsual_mode == 'V'))
10522 {
10523 case 0: p = N_(" VISUAL"); break;
10524 case 1: p = N_(" VISUAL LINE"); break;
10525 case 2: p = N_(" VISUAL BLOCK"); break;
10526 case 4: p = N_(" SELECT"); break;
10527 case 5: p = N_(" SELECT LINE"); break;
10528 default: p = N_(" SELECT BLOCK"); break;
10529 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010530 msg_puts_attr(_(p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010531 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010532 msg_puts_attr(" --", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010533 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010534
Bram Moolenaar071d4272004-06-13 20:20:40 +000010535 need_clear = TRUE;
10536 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010537 if (reg_recording != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010538#ifdef FEAT_INS_EXPAND
10539 && edit_submode == NULL /* otherwise it gets too long */
10540#endif
10541 )
10542 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010543 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010544 need_clear = TRUE;
10545 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010546
10547 mode_displayed = TRUE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010548 if (need_clear || clear_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010549 msg_clr_eos();
10550 msg_didout = FALSE; /* overwrite this message */
10551 length = msg_col;
10552 msg_col = 0;
10553 need_wait_return = nwr_save; /* never ask for hit-return for this */
10554 }
10555 else if (clear_cmdline && msg_silent == 0)
10556 /* Clear the whole command line. Will reset "clear_cmdline". */
10557 msg_clr_cmdline();
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010558 else if (redraw_mode)
10559 {
10560 msg_pos_mode();
10561 msg_clr_eos();
10562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010563
10564#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010565 /* In Visual mode the size of the selected area must be redrawn. */
10566 if (VIsual_active)
10567 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010568
10569 /* If the last window has no status line, the ruler is after the mode
10570 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010571 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +020010572 win_redr_ruler(lastwin, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010573#endif
10574 redraw_cmdline = FALSE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010575 redraw_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010576 clear_cmdline = FALSE;
10577
10578 return length;
10579}
10580
10581/*
10582 * Position for a mode message.
10583 */
10584 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010585msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010586{
10587 msg_col = 0;
10588 msg_row = Rows - 1;
10589}
10590
10591/*
10592 * Delete mode message. Used when ESC is typed which is expected to end
10593 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010594 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010595 */
10596 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010597unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010598{
10599 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010600 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010601 */
10602 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10603 redraw_cmdline = TRUE; /* delete mode later */
10604 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010605 clearmode();
10606}
10607
10608/*
10609 * Clear the mode message.
10610 */
10611 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010612clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010613{
Bram Moolenaar2abad542018-05-19 14:43:45 +020010614 int save_msg_row = msg_row;
10615 int save_msg_col = msg_col;
10616
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010617 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010618 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010619 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010620 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +020010621
10622 msg_col = save_msg_col;
10623 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010624}
10625
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010626 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010627recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010628{
Bram Moolenaar32526b32019-01-19 17:43:09 +010010629 msg_puts_attr(_("recording"), attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010630 if (!shortmess(SHM_RECORDING))
10631 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010632 char s[4];
10633
10634 sprintf(s, " @%c", reg_recording);
10635 msg_puts_attr(s, attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010636 }
10637}
10638
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010639/*
10640 * Draw the tab pages line at the top of the Vim window.
10641 */
Bram Moolenaare12bab32019-01-08 22:02:56 +010010642 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010643draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010644{
10645 int tabcount = 0;
10646 tabpage_T *tp;
10647 int tabwidth;
10648 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010649 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010650 int attr;
10651 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010652 win_T *cwp;
10653 int wincount;
10654 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010655 int c;
10656 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010657 int attr_sel = HL_ATTR(HLF_TPS);
10658 int attr_nosel = HL_ATTR(HLF_TP);
10659 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010660 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010661 int room;
10662 int use_sep_chars = (t_colors < 8
10663#ifdef FEAT_GUI
10664 && !gui.in_use
10665#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010666#ifdef FEAT_TERMGUICOLORS
10667 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010668#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010669 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010670
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010671 if (ScreenLines == NULL)
10672 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010673 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010674
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010675#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010676 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010677 if (gui_use_tabline())
10678 {
10679 gui_update_tabline();
10680 return;
10681 }
10682#endif
10683
10684 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010685 return;
10686
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010687#if defined(FEAT_STL_OPT)
Bram Moolenaarca57ab52019-04-13 14:53:16 +020010688 clear_TabPageIdxs();
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010689
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010690 /* Use the 'tabline' option if it's set. */
10691 if (*p_tal != NUL)
10692 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010693 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010694
10695 /* Check for an error. If there is one we would loop in redrawing the
10696 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010697 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010698 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010699 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010700 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010701 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010702 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010703 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010704 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010705#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010706 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010707 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010708 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010709
Bram Moolenaar238a5642006-02-21 22:12:05 +000010710 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10711 if (tabwidth < 6)
10712 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010713
Bram Moolenaar238a5642006-02-21 22:12:05 +000010714 attr = attr_nosel;
10715 tabcount = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010716 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10717 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010718 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010719 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010720
Bram Moolenaar238a5642006-02-21 22:12:05 +000010721 if (tp->tp_topframe == topframe)
10722 attr = attr_sel;
10723 if (use_sep_chars && col > 0)
10724 screen_putchar('|', 0, col++, attr);
10725
10726 if (tp->tp_topframe != topframe)
10727 attr = attr_nosel;
10728
10729 screen_putchar(' ', 0, col++, attr);
10730
10731 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010732 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010733 cwp = curwin;
10734 wp = firstwin;
10735 }
10736 else
10737 {
10738 cwp = tp->tp_curwin;
10739 wp = tp->tp_firstwin;
10740 }
10741
10742 modified = FALSE;
10743 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10744 if (bufIsChanged(wp->w_buffer))
10745 modified = TRUE;
10746 if (modified || wincount > 1)
10747 {
10748 if (wincount > 1)
10749 {
10750 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010751 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010752 if (col + len >= Columns - 3)
10753 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010754 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010755#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010756 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010757#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010758 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010759#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010760 );
10761 col += len;
10762 }
10763 if (modified)
10764 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10765 screen_putchar(' ', 0, col++, attr);
10766 }
10767
10768 room = scol - col + tabwidth - 1;
10769 if (room > 0)
10770 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010771 /* Get buffer name in NameBuff[] */
10772 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010773 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010774 len = vim_strsize(NameBuff);
10775 p = NameBuff;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010776 if (has_mbyte)
10777 while (len > room)
10778 {
10779 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010780 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010781 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010010782 else if (len > room)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010783 {
10784 p += len - room;
10785 len = room;
10786 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010787 if (len > Columns - col - 1)
10788 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010789
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010790 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010791 col += len;
10792 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010793 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010794
10795 /* Store the tab page number in TabPageIdxs[], so that
10796 * jump_to_mouse() knows where each one is. */
10797 ++tabcount;
10798 while (scol < col)
10799 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010800 }
10801
Bram Moolenaar238a5642006-02-21 22:12:05 +000010802 if (use_sep_chars)
10803 c = '_';
10804 else
10805 c = ' ';
10806 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010807
10808 /* Put an "X" for closing the current tab if there are several. */
10809 if (first_tabpage->tp_next != NULL)
10810 {
10811 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10812 TabPageIdxs[Columns - 1] = -999;
10813 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010814 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010815
10816 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10817 * set. */
10818 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010819}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010820
10821/*
10822 * Get buffer name for "buf" into NameBuff[].
10823 * Takes care of special buffer names and translates special characters.
10824 */
10825 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010826get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010827{
10828 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010829 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010830 else
10831 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10832 trans_characters(NameBuff, MAXPATHL);
10833}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010834
Bram Moolenaar071d4272004-06-13 20:20:40 +000010835/*
10836 * Get the character to use in a status line. Get its attributes in "*attr".
10837 */
10838 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010839fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010840{
10841 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010842
10843#ifdef FEAT_TERMINAL
10844 if (bt_terminal(wp->w_buffer))
10845 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010846 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010847 {
10848 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010849 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010850 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010851 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010852 {
10853 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010854 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010855 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010856 }
10857 else
10858#endif
10859 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010860 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010861 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010862 fill = fill_stl;
10863 }
10864 else
10865 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010866 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010867 fill = fill_stlnc;
10868 }
10869 /* Use fill when there is highlighting, and highlighting of current
10870 * window differs, or the fillchars differ, or this is not the
10871 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010872 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010873 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010874 || (fill_stl != fill_stlnc)))
10875 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010876 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010877 return '^';
10878 return '=';
10879}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010880
Bram Moolenaar071d4272004-06-13 20:20:40 +000010881/*
10882 * Get the character to use in a separator between vertically split windows.
10883 * Get its attributes in "*attr".
10884 */
10885 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010886fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010887{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010888 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010889 if (*attr == 0 && fill_vert == ' ')
10890 return '|';
10891 else
10892 return fill_vert;
10893}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010894
10895/*
10896 * Return TRUE if redrawing should currently be done.
10897 */
10898 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010899redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010900{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010901#ifdef FEAT_EVAL
10902 if (disable_redraw_for_testing)
10903 return 0;
10904 else
10905#endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +020010906 return ((!RedrawingDisabled
10907#ifdef FEAT_EVAL
10908 || ignore_redraw_flag_for_testing
10909#endif
10910 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010911}
10912
10913/*
10914 * Return TRUE if printing messages should currently be done.
10915 */
10916 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010917messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010918{
10919 return (!(p_lz && char_avail() && !KeyTyped));
10920}
10921
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010922#ifdef FEAT_MENU
10923/*
10924 * Draw the window toolbar.
10925 */
10926 static void
10927redraw_win_toolbar(win_T *wp)
10928{
10929 vimmenu_T *menu;
10930 int item_idx = 0;
10931 int item_count = 0;
10932 int col = 0;
10933 int next_col;
10934 int off = (int)(current_ScreenLine - ScreenLines);
10935 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
10936 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
10937
10938 vim_free(wp->w_winbar_items);
10939 for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
10940 ++item_count;
Bram Moolenaarc799fe22019-05-28 23:08:19 +020010941 wp->w_winbar_items = ALLOC_CLEAR_MULT(winbar_item_T, item_count + 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010942
10943 /* TODO: use fewer spaces if there is not enough room */
10944 for (menu = wp->w_winbar->children;
Bram Moolenaar02631462017-09-22 15:20:32 +020010945 menu != NULL && col < wp->w_width; menu = menu->next)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010946 {
10947 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010948 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010949 break;
10950 if (col > 1)
10951 {
10952 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010953 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010954 break;
10955 }
10956
10957 wp->w_winbar_items[item_idx].wb_startcol = col;
10958 space_to_screenline(off + col, button_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010959 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010960 break;
10961
10962 next_col = text_to_screenline(wp, menu->name, col);
10963 while (col < next_col)
10964 {
10965 ScreenAttrs[off + col] = button_attr;
10966 ++col;
10967 }
10968 wp->w_winbar_items[item_idx].wb_endcol = col;
10969 wp->w_winbar_items[item_idx].wb_menu = menu;
10970 ++item_idx;
10971
Bram Moolenaar02631462017-09-22 15:20:32 +020010972 if (col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010973 break;
10974 space_to_screenline(off + col, button_attr);
10975 ++col;
10976 }
Bram Moolenaar02631462017-09-22 15:20:32 +020010977 while (col < wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010978 {
10979 space_to_screenline(off + col, fill_attr);
10980 ++col;
10981 }
10982 wp->w_winbar_items[item_idx].wb_menu = NULL; /* end marker */
10983
Bram Moolenaar02631462017-09-22 15:20:32 +020010984 screen_line(wp->w_winrow, wp->w_wincol, (int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +020010985 (int)wp->w_width, 0);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010986}
10987#endif
Bram Moolenaar491ac282018-06-17 14:47:55 +020010988
Bram Moolenaar071d4272004-06-13 20:20:40 +000010989/*
10990 * Show current status info in ruler and various other places
10991 * If always is FALSE, only show ruler if position has changed.
10992 */
10993 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010994showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010995{
10996 if (!always && !redrawing())
10997 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010998#ifdef FEAT_INS_EXPAND
10999 if (pum_visible())
11000 {
11001 /* Don't redraw right now, do it later. */
11002 curwin->w_redr_status = TRUE;
11003 return;
11004 }
11005#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020011006#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000011007 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar362f3562009-11-03 16:20:34 +000011008 redraw_custom_statusline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011009 else
11010#endif
11011#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +020011012 win_redr_ruler(curwin, always, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011013#endif
11014
11015#ifdef FEAT_TITLE
11016 if (need_maketitle
11017# ifdef FEAT_STL_OPT
11018 || (p_icon && (stl_syntax & STL_IN_ICON))
11019 || (p_title && (stl_syntax & STL_IN_TITLE))
11020# endif
11021 )
11022 maketitle();
11023#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000011024 /* Redraw the tab pages line if needed. */
11025 if (redraw_tabline)
11026 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011027}
11028
11029#ifdef FEAT_CMDL_INFO
11030 static void
Bram Moolenaar491ac282018-06-17 14:47:55 +020011031win_redr_ruler(win_T *wp, int always, int ignore_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011032{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011033#define RULER_BUF_LEN 70
11034 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011035 int row;
11036 int fillchar;
11037 int attr;
11038 int empty_line = FALSE;
11039 colnr_T virtcol;
11040 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011041 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011042 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011043 int this_ru_col;
11044 int off = 0;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +010011045 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011046
11047 /* If 'ruler' off or redrawing disabled, don't do anything */
11048 if (!p_ru)
11049 return;
11050
11051 /*
11052 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
11053 * after deleting lines, before cursor.lnum is corrected.
11054 */
11055 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
11056 return;
11057
11058#ifdef FEAT_INS_EXPAND
11059 /* Don't draw the ruler while doing insert-completion, it might overwrite
11060 * the (long) mode message. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011061 if (wp == lastwin && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011062 if (edit_submode != NULL)
11063 return;
Bram Moolenaar491ac282018-06-17 14:47:55 +020011064 // Don't draw the ruler when the popup menu is visible, it may overlap.
11065 // Except when the popup menu will be redrawn anyway.
11066 if (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000011067 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011068#endif
11069
11070#ifdef FEAT_STL_OPT
11071 if (*p_ruf)
11072 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000011073 int save_called_emsg = called_emsg;
11074
11075 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011076 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011077 if (called_emsg)
11078 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011079 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011080 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011081 return;
11082 }
11083#endif
11084
11085 /*
11086 * Check if not in Insert mode and the line is empty (will show "0-1").
11087 */
11088 if (!(State & INSERT)
11089 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
11090 empty_line = TRUE;
11091
11092 /*
11093 * Only draw the ruler when something changed.
11094 */
11095 validate_virtcol_win(wp);
11096 if ( redraw_cmdline
11097 || always
11098 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
11099 || wp->w_cursor.col != wp->w_ru_cursor.col
11100 || wp->w_virtcol != wp->w_ru_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +000011101 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
Bram Moolenaar071d4272004-06-13 20:20:40 +000011102 || wp->w_topline != wp->w_ru_topline
11103 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
11104#ifdef FEAT_DIFF
11105 || wp->w_topfill != wp->w_ru_topfill
11106#endif
11107 || empty_line != wp->w_ru_empty)
11108 {
11109 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011110 if (wp->w_status_height)
11111 {
11112 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011113 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar53f81742017-09-22 14:35:51 +020011114 off = wp->w_wincol;
Bram Moolenaar02631462017-09-22 15:20:32 +020011115 width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011116 }
11117 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011118 {
11119 row = Rows - 1;
11120 fillchar = ' ';
11121 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011122 width = Columns;
11123 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011124 }
11125
11126 /* In list mode virtcol needs to be recomputed */
11127 virtcol = wp->w_virtcol;
11128 if (wp->w_p_list && lcs_tab1 == NUL)
11129 {
11130 wp->w_p_list = FALSE;
11131 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
11132 wp->w_p_list = TRUE;
11133 }
11134
11135 /*
11136 * Some sprintfs return the length, some return a pointer.
11137 * To avoid portability problems we use strlen() here.
11138 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011139 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011140 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
11141 ? 0L
11142 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011143 len = STRLEN(buffer);
11144 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011145 empty_line ? 0 : (int)wp->w_cursor.col + 1,
11146 (int)virtcol + 1);
11147
11148 /*
11149 * Add a "50%" if there is room for it.
11150 * On the last line, don't print in the last column (scrolls the
11151 * screen up on some terminals).
11152 */
11153 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011154 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011155 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011156 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011157 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011158 this_ru_col = ru_col - (Columns - width);
11159 if (this_ru_col < 0)
11160 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011161 /* Never use more than half the window/screen width, leave the other
11162 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011163 if (this_ru_col < (width + 1) / 2)
11164 this_ru_col = (width + 1) / 2;
11165 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011166 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010011167 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011168 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011169 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011170 if (has_mbyte)
11171 i += (*mb_char2bytes)(fillchar, buffer + i);
11172 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011173 buffer[i++] = fillchar;
11174 ++o;
11175 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011176 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011177 }
11178 /* Truncate at window boundary. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011179 if (has_mbyte)
11180 {
11181 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011182 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011183 {
11184 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020011185 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011186 {
11187 buffer[i] = NUL;
11188 break;
11189 }
11190 }
11191 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010011192 else if (this_ru_col + (int)STRLEN(buffer) > width)
Bram Moolenaar4033c552017-09-16 20:54:51 +020011193 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011194
Bram Moolenaar4033c552017-09-16 20:54:51 +020011195 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011196 i = redraw_cmdline;
11197 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020011198 this_ru_col + off + (int)STRLEN(buffer),
11199 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011200 fillchar, fillchar, attr);
11201 /* don't redraw the cmdline because of showing the ruler */
11202 redraw_cmdline = i;
11203 wp->w_ru_cursor = wp->w_cursor;
11204 wp->w_ru_virtcol = wp->w_virtcol;
11205 wp->w_ru_empty = empty_line;
11206 wp->w_ru_topline = wp->w_topline;
11207 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
11208#ifdef FEAT_DIFF
11209 wp->w_ru_topfill = wp->w_topfill;
11210#endif
11211 }
11212}
11213#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011214
11215#if defined(FEAT_LINEBREAK) || defined(PROTO)
11216/*
Bram Moolenaar64486672010-05-16 15:46:46 +020011217 * Return the width of the 'number' and 'relativenumber' column.
11218 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011219 * Otherwise it depends on 'numberwidth' and the line count.
11220 */
11221 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011222number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011223{
11224 int n;
11225 linenr_T lnum;
11226
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020011227 if (wp->w_p_rnu && !wp->w_p_nu)
11228 /* cursor line shows "0" */
11229 lnum = wp->w_height;
11230 else
11231 /* cursor line shows absolute line number */
11232 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020011233
Bram Moolenaar6b314672015-03-20 15:42:10 +010011234 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011235 return wp->w_nrwidth_width;
11236 wp->w_nrwidth_line_count = lnum;
11237
11238 n = 0;
11239 do
11240 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000011241 lnum /= 10;
11242 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011243 } while (lnum > 0);
11244
11245 /* 'numberwidth' gives the minimal width plus one */
11246 if (n < wp->w_p_nuw - 1)
11247 n = wp->w_p_nuw - 1;
11248
11249 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011250 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011251 return n;
11252}
11253#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011254
Bram Moolenaar113e1072019-01-20 15:30:40 +010011255#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011256/*
11257 * Return the current cursor column. This is the actual position on the
11258 * screen. First column is 0.
11259 */
11260 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011261screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011262{
11263 return screen_cur_col;
11264}
11265
11266/*
11267 * Return the current cursor row. This is the actual position on the screen.
11268 * First row is 0.
11269 */
11270 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011271screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011272{
11273 return screen_cur_row;
11274}
Bram Moolenaar113e1072019-01-20 15:30:40 +010011275#endif