blob: 7706b6c586744addd28299464dbeb5d14a91cb7a [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 Moolenaar071d4272004-06-13 20:20:40 +0000181/*
182 * Redraw the current window later, with update_screen(type).
183 * Set must_redraw only if not already set to a higher value.
Bram Moolenaarae654382019-01-17 21:09:05 +0100184 * E.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185 */
186 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100187redraw_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188{
189 redraw_win_later(curwin, type);
190}
191
192 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100193redraw_win_later(
194 win_T *wp,
195 int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196{
Bram Moolenaar4f198282017-10-23 21:53:30 +0200197 if (!exiting && wp->w_redr_type < type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 {
199 wp->w_redr_type = type;
200 if (type >= NOT_VALID)
201 wp->w_lines_valid = 0;
202 if (must_redraw < type) /* must_redraw is the maximum of all windows */
203 must_redraw = type;
204 }
205}
206
207/*
208 * Force a complete redraw later. Also resets the highlighting. To be used
209 * after executing a shell command that messes up the screen.
210 */
211 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100212redraw_later_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213{
214 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000215#ifdef FEAT_GUI
216 if (gui.in_use)
217 /* Use a code that will reset gui.highlight_mask in
218 * gui_stop_highlight(). */
219 screen_attr = HL_ALL + 1;
220 else
221#endif
222 /* Use attributes that is very unlikely to appear in text. */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +0200223 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE | HL_STRIKETHROUGH;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224}
225
226/*
227 * Mark all windows to be redrawn later.
228 */
229 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100230redraw_all_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231{
232 win_T *wp;
233
234 FOR_ALL_WINDOWS(wp)
235 {
236 redraw_win_later(wp, type);
237 }
Bram Moolenaar04b4e1a2019-01-06 22:22:07 +0100238 // This may be needed when switching tabs.
239 if (must_redraw < type)
240 must_redraw = type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241}
242
243/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000244 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 */
246 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100247redraw_curbuf_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248{
249 redraw_buf_later(curbuf, type);
250}
251
252 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100253redraw_buf_later(buf_T *buf, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254{
255 win_T *wp;
256
257 FOR_ALL_WINDOWS(wp)
258 {
259 if (wp->w_buffer == buf)
260 redraw_win_later(wp, type);
261 }
262}
263
Bram Moolenaar113e1072019-01-20 15:30:40 +0100264#if defined(FEAT_SIGNS) || defined(PROTO)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200265 void
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100266redraw_buf_line_later(buf_T *buf, linenr_T lnum)
267{
268 win_T *wp;
269
270 FOR_ALL_WINDOWS(wp)
271 if (wp->w_buffer == buf && lnum >= wp->w_topline
272 && lnum < wp->w_botline)
273 redrawWinline(wp, lnum);
274}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100275#endif
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100276
Bram Moolenaar113e1072019-01-20 15:30:40 +0100277#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100278 void
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200279redraw_buf_and_status_later(buf_T *buf, int type)
280{
281 win_T *wp;
282
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200283#ifdef FEAT_WILDMENU
Bram Moolenaar86033562017-07-12 20:24:41 +0200284 if (wild_menu_showing != 0)
285 /* Don't redraw while the command line completion is displayed, it
286 * would disappear. */
287 return;
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200288#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200289 FOR_ALL_WINDOWS(wp)
290 {
291 if (wp->w_buffer == buf)
292 {
293 redraw_win_later(wp, type);
294 wp->w_redr_status = TRUE;
295 }
296 }
297}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100298#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200299
Bram Moolenaar113e1072019-01-20 15:30:40 +0100300#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301/*
Bram Moolenaar2951b772013-07-03 12:45:31 +0200302 * Redraw as soon as possible. When the command line is not scrolled redraw
303 * right away and restore what was on the command line.
304 * Return a code indicating what happened.
305 */
306 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100307redraw_asap(int type)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200308{
309 int rows;
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200310 int cols = screen_Columns;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200311 int r;
312 int ret = 0;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200313 schar_T *screenline; /* copy from ScreenLines[] */
314 sattr_T *screenattr; /* copy from ScreenAttrs[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200315 int i;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200316 u8char_T *screenlineUC = NULL; /* copy from ScreenLinesUC[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200317 u8char_T *screenlineC[MAX_MCO]; /* copy from ScreenLinesC[][] */
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200318 schar_T *screenline2 = NULL; /* copy from ScreenLines2[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200319
320 redraw_later(type);
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200321 if (msg_scrolled || (State != NORMAL && State != NORMAL_BUSY) || exiting)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200322 return ret;
323
324 /* Allocate space to save the text displayed in the command line area. */
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200325 rows = screen_Rows - cmdline_row;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200326 screenline = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200327 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200328 screenattr = (sattr_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200329 (long_u)(rows * cols * sizeof(sattr_T)), FALSE);
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 {
334 screenlineUC = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200335 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200336 if (screenlineUC == NULL)
337 ret = 2;
338 for (i = 0; i < p_mco; ++i)
339 {
340 screenlineC[i] = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200341 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200342 if (screenlineC[i] == NULL)
343 ret = 2;
344 }
345 }
346 if (enc_dbcs == DBCS_JPNU)
347 {
348 screenline2 = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200349 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200350 if (screenline2 == NULL)
351 ret = 2;
352 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200353
354 if (ret != 2)
355 {
356 /* Save the text displayed in the command line area. */
357 for (r = 0; r < rows; ++r)
358 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200359 mch_memmove(screenline + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200360 ScreenLines + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200361 (size_t)cols * sizeof(schar_T));
362 mch_memmove(screenattr + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200363 ScreenAttrs + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200364 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200365 if (enc_utf8)
366 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200367 mch_memmove(screenlineUC + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200368 ScreenLinesUC + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200369 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200370 for (i = 0; i < p_mco; ++i)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200371 mch_memmove(screenlineC[i] + r * cols,
372 ScreenLinesC[i] + LineOffset[cmdline_row + r],
373 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200374 }
375 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200376 mch_memmove(screenline2 + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200377 ScreenLines2 + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200378 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200379 }
380
381 update_screen(0);
382 ret = 3;
383
384 if (must_redraw == 0)
385 {
386 int off = (int)(current_ScreenLine - ScreenLines);
387
388 /* Restore the text displayed in the command line area. */
389 for (r = 0; r < rows; ++r)
390 {
391 mch_memmove(current_ScreenLine,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200392 screenline + r * cols,
393 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200394 mch_memmove(ScreenAttrs + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200395 screenattr + r * cols,
396 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200397 if (enc_utf8)
398 {
399 mch_memmove(ScreenLinesUC + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200400 screenlineUC + r * cols,
401 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200402 for (i = 0; i < p_mco; ++i)
403 mch_memmove(ScreenLinesC[i] + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200404 screenlineC[i] + r * cols,
405 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200406 }
407 if (enc_dbcs == DBCS_JPNU)
408 mch_memmove(ScreenLines2 + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200409 screenline2 + r * cols,
410 (size_t)cols * sizeof(schar_T));
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200411 screen_line(cmdline_row + r, 0, cols, cols, FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200412 }
413 ret = 4;
414 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200415 }
416
417 vim_free(screenline);
418 vim_free(screenattr);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200419 if (enc_utf8)
420 {
421 vim_free(screenlineUC);
422 for (i = 0; i < p_mco; ++i)
423 vim_free(screenlineC[i]);
424 }
425 if (enc_dbcs == DBCS_JPNU)
426 vim_free(screenline2);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200427
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200428 /* Show the intro message when appropriate. */
429 maybe_intro_message();
430
431 setcursor();
432
Bram Moolenaar2951b772013-07-03 12:45:31 +0200433 return ret;
434}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100435#endif
Bram Moolenaar2951b772013-07-03 12:45:31 +0200436
437/*
Bram Moolenaar975b5272016-03-15 23:10:59 +0100438 * Invoked after an asynchronous callback is called.
439 * If an echo command was used the cursor needs to be put back where
440 * it belongs. If highlighting was changed a redraw is needed.
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200441 * If "call_update_screen" is FALSE don't call update_screen() when at the
442 * command line.
Bram Moolenaar975b5272016-03-15 23:10:59 +0100443 */
444 void
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200445redraw_after_callback(int call_update_screen)
Bram Moolenaar975b5272016-03-15 23:10:59 +0100446{
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200447 ++redrawing_for_callback;
448
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100449 if (State == HITRETURN || State == ASKMORE)
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200450 ; // do nothing
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100451 else if (State & CMDLINE)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200452 {
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200453 // Don't redraw when in prompt_for_number().
454 if (cmdline_row > 0)
455 {
456 // Redrawing only works when the screen didn't scroll. Don't clear
457 // wildmenu entries.
458 if (msg_scrolled == 0
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200459#ifdef FEAT_WILDMENU
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200460 && wild_menu_showing == 0
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200461#endif
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200462 && call_update_screen)
463 update_screen(0);
464
465 // Redraw in the same position, so that the user can continue
466 // editing the command.
467 redrawcmdline_ex(FALSE);
468 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200469 }
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200470 else if (State & (NORMAL | INSERT | TERMINAL))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100471 {
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200472 // keep the command line if possible
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200473 update_screen(VALID_NO_UPDATE);
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100474 setcursor();
475 }
Bram Moolenaar975b5272016-03-15 23:10:59 +0100476 cursor_on();
Bram Moolenaar975b5272016-03-15 23:10:59 +0100477#ifdef FEAT_GUI
Bram Moolenaara338adc2018-01-31 20:51:47 +0100478 if (gui.in_use && !gui_mch_is_blink_off())
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200479 // Don't update the cursor when it is blinking and off to avoid
480 // flicker.
Bram Moolenaara338adc2018-01-31 20:51:47 +0100481 out_flush_cursor(FALSE, FALSE);
482 else
Bram Moolenaar975b5272016-03-15 23:10:59 +0100483#endif
Bram Moolenaaracda04f2018-02-08 09:57:28 +0100484 out_flush();
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200485
486 --redrawing_for_callback;
Bram Moolenaar975b5272016-03-15 23:10:59 +0100487}
488
489/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 * Changed something in the current window, at buffer line "lnum", that
491 * requires that line and possibly other lines to be redrawn.
492 * Used when entering/leaving Insert mode with the cursor on a folded line.
493 * Used to remove the "$" from a change command.
494 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
495 * may become invalid and the whole window will have to be redrawn.
496 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100498redrawWinline(
Bram Moolenaar90a99792018-09-12 21:52:18 +0200499 win_T *wp,
Bram Moolenaarae12f4b2019-01-09 20:51:04 +0100500 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501{
Bram Moolenaar90a99792018-09-12 21:52:18 +0200502 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
503 wp->w_redraw_top = lnum;
504 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
505 wp->w_redraw_bot = lnum;
506 redraw_win_later(wp, VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507}
508
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200509 void
510reset_updating_screen(int may_resize_shell UNUSED)
511{
512 updating_screen = FALSE;
513#ifdef FEAT_GUI
514 if (may_resize_shell)
515 gui_may_resize_shell();
516#endif
517#ifdef FEAT_TERMINAL
518 term_check_channel_closed_recently();
519#endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +0200520
521#ifdef HAVE_DROP_FILE
522 // If handle_drop() was called while updating_screen was TRUE need to
523 // handle the drop now.
524 handle_any_postponed_drop();
525#endif
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200526}
527
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528/*
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200529 * Update all windows that are editing the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 */
531 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100532update_curbuf(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533{
534 redraw_curbuf_later(type);
535 update_screen(type);
536}
537
538/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539 * Based on the current value of curwin->w_topline, transfer a screenfull
540 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
Bram Moolenaar072412e2017-09-13 22:11:35 +0200541 * Return OK when the screen was updated, FAIL if it was not done.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200543 int
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200544update_screen(int type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545{
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200546 int type = type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 win_T *wp;
548 static int did_intro = FALSE;
549#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
550 int did_one;
551#endif
Bram Moolenaar144445d2016-07-08 21:41:54 +0200552#ifdef FEAT_GUI
Bram Moolenaar107abd22016-08-12 14:08:25 +0200553 int did_undraw = FALSE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200554 int gui_cursor_col;
555 int gui_cursor_row;
556#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200557 int no_update = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000559 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560 if (!screen_valid(TRUE))
Bram Moolenaar072412e2017-09-13 22:11:35 +0200561 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200563 if (type == VALID_NO_UPDATE)
564 {
565 no_update = TRUE;
566 type = 0;
567 }
568
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 if (must_redraw)
570 {
571 if (type < must_redraw) /* use maximal type */
572 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000573
574 /* must_redraw is reset here, so that when we run into some weird
575 * reason to redraw while busy redrawing (e.g., asynchronous
576 * scrolling), or update_topline() in win_update() will cause a
577 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 must_redraw = 0;
579 }
580
Bram Moolenaar6eddadf2018-05-06 16:40:16 +0200581 /* May need to update w_lines[]. */
582 if (curwin->w_lines_valid == 0 && type < NOT_VALID
583#ifdef FEAT_TERMINAL
584 && !term_do_update_window(curwin)
585#endif
586 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 type = NOT_VALID;
588
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000589 /* Postpone the redrawing when it's not needed and when being called
590 * recursively. */
591 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 {
593 redraw_later(type); /* remember type for next time */
594 must_redraw = type;
595 if (type > INVERTED_ALL)
596 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200597 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 }
599
600 updating_screen = TRUE;
601#ifdef FEAT_SYN_HL
602 ++display_tick; /* let syntax code know we're in a next round of
603 * display updating */
604#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200605 if (no_update)
606 ++no_win_do_lines_ins;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607
608 /*
609 * if the screen was scrolled up when displaying a message, scroll it down
610 */
611 if (msg_scrolled)
612 {
613 clear_cmdline = TRUE;
614 if (msg_scrolled > Rows - 5) /* clearing is faster */
615 type = CLEAR;
616 else if (type != CLEAR)
617 {
618 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200619 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, 0, NULL)
620 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621 type = CLEAR;
622 FOR_ALL_WINDOWS(wp)
623 {
624 if (W_WINROW(wp) < msg_scrolled)
625 {
626 if (W_WINROW(wp) + wp->w_height > msg_scrolled
627 && wp->w_redr_type < REDRAW_TOP
628 && wp->w_lines_valid > 0
629 && wp->w_topline == wp->w_lines[0].wl_lnum)
630 {
631 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
632 wp->w_redr_type = REDRAW_TOP;
633 }
634 else
635 {
636 wp->w_redr_type = NOT_VALID;
Bram Moolenaare0de17d2017-09-24 16:24:34 +0200637 if (W_WINROW(wp) + wp->w_height + wp->w_status_height
638 <= msg_scrolled)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 }
641 }
642 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200643 if (!no_update)
644 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000645 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 }
647 msg_scrolled = 0;
648 need_wait_return = FALSE;
649 }
650
651 /* reset cmdline_row now (may have been changed temporarily) */
652 compute_cmdrow();
653
654 /* Check for changed highlighting */
655 if (need_highlight_changed)
656 highlight_changed();
657
658 if (type == CLEAR) /* first clear screen */
659 {
660 screenclear(); /* will reset clear_cmdline */
661 type = NOT_VALID;
Bram Moolenaar9f5f7bf2017-06-28 20:45:26 +0200662 /* must_redraw may be set indirectly, avoid another redraw later */
663 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 }
665
666 if (clear_cmdline) /* going to clear cmdline (done below) */
667 check_for_delay(FALSE);
668
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000669#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200670 /* Force redraw when width of 'number' or 'relativenumber' column
671 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000672 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200673 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
674 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000675 curwin->w_redr_type = NOT_VALID;
676#endif
677
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 /*
679 * Only start redrawing if there is really something to do.
680 */
681 if (type == INVERTED)
682 update_curswant();
683 if (curwin->w_redr_type < type
684 && !((type == VALID
685 && curwin->w_lines[0].wl_valid
686#ifdef FEAT_DIFF
687 && curwin->w_topfill == curwin->w_old_topfill
688 && curwin->w_botfill == curwin->w_old_botfill
689#endif
690 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000692 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
694 && curwin->w_old_visual_mode == VIsual_mode
695 && (curwin->w_valid & VALID_VIRTCOL)
696 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 ))
698 curwin->w_redr_type = type;
699
Bram Moolenaar5a305422006-04-28 22:38:25 +0000700 /* Redraw the tab pages line if needed. */
701 if (redraw_tabline || type >= NOT_VALID)
702 draw_tabline();
Bram Moolenaar5a305422006-04-28 22:38:25 +0000703
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704#ifdef FEAT_SYN_HL
705 /*
706 * Correct stored syntax highlighting info for changes in each displayed
707 * buffer. Each buffer must only be done once.
708 */
709 FOR_ALL_WINDOWS(wp)
710 {
711 if (wp->w_buffer->b_mod_set)
712 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 win_T *wwp;
714
715 /* Check if we already did this buffer. */
716 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
717 if (wwp->w_buffer == wp->w_buffer)
718 break;
Bram Moolenaar4033c552017-09-16 20:54:51 +0200719 if (wwp == wp && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 syn_stack_apply_changes(wp->w_buffer);
721 }
722 }
723#endif
724
725 /*
726 * Go from top to bottom through the windows, redrawing the ones that need
727 * it.
728 */
729#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
730 did_one = FALSE;
731#endif
732#ifdef FEAT_SEARCH_EXTRA
733 search_hl.rm.regprog = NULL;
734#endif
735 FOR_ALL_WINDOWS(wp)
736 {
737 if (wp->w_redr_type != 0)
738 {
739 cursor_off();
740#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
741 if (!did_one)
742 {
743 did_one = TRUE;
744# ifdef FEAT_SEARCH_EXTRA
745 start_search_hl();
746# endif
747# ifdef FEAT_CLIPBOARD
748 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200749 if (clip_star.available && clip_isautosel_star())
750 clip_update_selection(&clip_star);
751 if (clip_plus.available && clip_isautosel_plus())
752 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753# endif
754#ifdef FEAT_GUI
755 /* Remove the cursor before starting to do anything, because
756 * scrolling may make it difficult to redraw the text under
757 * it. */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200758 if (gui.in_use && wp == curwin)
Bram Moolenaar144445d2016-07-08 21:41:54 +0200759 {
760 gui_cursor_col = gui.cursor_col;
761 gui_cursor_row = gui.cursor_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 gui_undraw_cursor();
Bram Moolenaar107abd22016-08-12 14:08:25 +0200763 did_undraw = TRUE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765#endif
766 }
767#endif
768 win_update(wp);
769 }
770
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 /* redraw status line after the window to minimize cursor movement */
772 if (wp->w_redr_status)
773 {
774 cursor_off();
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200775 win_redr_status(wp, TRUE); // any popup menu will be redrawn below
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 }
778#if defined(FEAT_SEARCH_EXTRA)
779 end_search_hl();
780#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100781#ifdef FEAT_INS_EXPAND
782 /* May need to redraw the popup menu. */
Bram Moolenaar491ac282018-06-17 14:47:55 +0200783 pum_may_redraw();
Bram Moolenaar51971b32013-02-13 12:16:05 +0100784#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 /* Reset b_mod_set flags. Going through all windows is probably faster
787 * than going through all buffers (there could be many buffers). */
Bram Moolenaar29323592016-07-24 22:04:11 +0200788 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 wp->w_buffer->b_mod_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200791 reset_updating_screen(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792
793 /* Clear or redraw the command line. Done last, because scrolling may
794 * mess up the command line. */
795 if (clear_cmdline || redraw_cmdline)
796 showmode();
797
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200798 if (no_update)
799 --no_win_do_lines_ins;
800
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200802 if (!did_intro)
803 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 did_intro = TRUE;
805
806#ifdef FEAT_GUI
807 /* Redraw the cursor and update the scrollbars when all screen updating is
808 * done. */
809 if (gui.in_use)
810 {
Bram Moolenaar107abd22016-08-12 14:08:25 +0200811 if (did_undraw && !gui_mch_is_blink_off())
Bram Moolenaar144445d2016-07-08 21:41:54 +0200812 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100813 mch_disable_flush();
814 out_flush(); /* required before updating the cursor */
815 mch_enable_flush();
816
Bram Moolenaar144445d2016-07-08 21:41:54 +0200817 /* Put the GUI position where the cursor was, gui_update_cursor()
818 * uses that. */
819 gui.col = gui_cursor_col;
820 gui.row = gui_cursor_row;
Bram Moolenaar84dbd492016-10-02 23:09:31 +0200821 gui.col = mb_fix_col(gui.col, gui.row);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara338adc2018-01-31 20:51:47 +0100823 gui_may_flush();
Bram Moolenaar65549bd2016-07-08 22:52:37 +0200824 screen_cur_col = gui.col;
825 screen_cur_row = gui.row;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200826 }
Bram Moolenaara338adc2018-01-31 20:51:47 +0100827 else
828 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 gui_update_scrollbars(FALSE);
830 }
831#endif
Bram Moolenaar072412e2017-09-13 22:11:35 +0200832 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833}
834
Bram Moolenaar1fa8fdd2019-02-25 05:41:15 +0100835#if defined(FEAT_NETBEANS_INTG) || defined(FEAT_GUI)
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100836/*
837 * Prepare for updating one or more windows.
838 * Caller must check for "updating_screen" already set to avoid recursiveness.
839 */
840 static void
841update_prepare(void)
842{
843 cursor_off();
844 updating_screen = TRUE;
845#ifdef FEAT_GUI
846 /* Remove the cursor before starting to do anything, because scrolling may
847 * make it difficult to redraw the text under it. */
848 if (gui.in_use)
849 gui_undraw_cursor();
850#endif
851#ifdef FEAT_SEARCH_EXTRA
852 start_search_hl();
853#endif
854}
855
856/*
857 * Finish updating one or more windows.
858 */
859 static void
860update_finish(void)
861{
862 if (redraw_cmdline)
863 showmode();
864
865# ifdef FEAT_SEARCH_EXTRA
866 end_search_hl();
867# endif
868
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200869 reset_updating_screen(TRUE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100870
871# ifdef FEAT_GUI
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100872 /* Redraw the cursor and update the scrollbars when all screen updating is
873 * done. */
874 if (gui.in_use)
875 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100876 out_flush_cursor(FALSE, FALSE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100877 gui_update_scrollbars(FALSE);
878 }
879# endif
880}
881#endif
882
Bram Moolenaar860cae12010-06-05 23:22:07 +0200883#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200884/*
885 * Return TRUE if the cursor line in window "wp" may be concealed, according
886 * to the 'concealcursor' option.
887 */
888 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100889conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200890{
891 int c;
892
893 if (*wp->w_p_cocu == NUL)
894 return FALSE;
895 if (get_real_state() & VISUAL)
896 c = 'v';
897 else if (State & INSERT)
898 c = 'i';
899 else if (State & NORMAL)
900 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200901 else if (State & CMDLINE)
902 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200903 else
904 return FALSE;
905 return vim_strchr(wp->w_p_cocu, c) != NULL;
906}
907
908/*
909 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
910 */
911 void
Bram Moolenaarb9464822018-05-10 15:09:49 +0200912conceal_check_cursor_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200913{
914 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
915 {
916 need_cursor_line_redraw = TRUE;
917 /* Need to recompute cursor column, e.g., when starting Visual mode
918 * without concealing. */
919 curs_columns(TRUE);
920 }
921}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922#endif
923
Bram Moolenaar113e1072019-01-20 15:30:40 +0100924#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100926update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927{
928 win_T *wp;
929 int doit = FALSE;
930
931# ifdef FEAT_FOLDING
932 win_foldinfo.fi_level = 0;
933# endif
934
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100935 // update/delete a specific sign
936 redraw_buf_line_later(buf, lnum);
937
938 // check if it resulted in the need to redraw a window
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 if (wp->w_redr_type != 0)
941 doit = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100943 /* Return when there is nothing to do, screen updating is already
Bram Moolenaar07920482017-08-01 20:53:30 +0200944 * happening (recursive call), messages on the screen or still starting up.
945 */
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100946 if (!doit || updating_screen
Bram Moolenaar07920482017-08-01 20:53:30 +0200947 || State == ASKMORE || State == HITRETURN
948 || msg_scrolled
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100949#ifdef FEAT_GUI
950 || gui.starting
951#endif
952 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 return;
954
955 /* update all windows that need updating */
956 update_prepare();
957
Bram Moolenaar29323592016-07-24 22:04:11 +0200958 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 {
960 if (wp->w_redr_type != 0)
961 win_update(wp);
962 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200963 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965
966 update_finish();
967}
968#endif
969
970
971#if defined(FEAT_GUI) || defined(PROTO)
972/*
973 * Update a single window, its status line and maybe the command line msg.
974 * Used for the GUI scrollbar.
975 */
976 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100977updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978{
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000979 /* return if already busy updating */
980 if (updating_screen)
981 return;
982
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 update_prepare();
984
985#ifdef FEAT_CLIPBOARD
986 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200987 if (clip_star.available && clip_isautosel_star())
988 clip_update_selection(&clip_star);
989 if (clip_plus.available && clip_isautosel_plus())
990 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000992
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000994
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000995 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000996 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000997 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000998
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 if (wp->w_redr_status
1000# ifdef FEAT_CMDL_INFO
1001 || p_ru
1002# endif
1003# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001004 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005# endif
1006 )
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001007 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008
1009 update_finish();
1010}
1011#endif
1012
1013/*
1014 * Update a single window.
1015 *
1016 * This may cause the windows below it also to be redrawn (when clearing the
1017 * screen or scrolling lines).
1018 *
1019 * How the window is redrawn depends on wp->w_redr_type. Each type also
1020 * implies the one below it.
1021 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001022 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1024 * INVERTED redraw the changed part of the Visual area
1025 * INVERTED_ALL redraw the whole Visual area
1026 * VALID 1. scroll up/down to adjust for a changed w_topline
1027 * 2. update lines at the top when scrolled down
1028 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001029 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 * b_mod_top and b_mod_bot.
1031 * - if wp->w_redraw_top non-zero, redraw lines between
1032 * wp->w_redraw_top and wp->w_redr_bot.
1033 * - continue redrawing when syntax status is invalid.
1034 * 4. if scrolled up, update lines at the bottom.
1035 * This results in three areas that may need updating:
1036 * top: from first row to top_end (when scrolled down)
1037 * mid: from mid_start to mid_end (update inversion or changed text)
1038 * bot: from bot_start to last row (when scrolled up)
1039 */
1040 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001041win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042{
1043 buf_T *buf = wp->w_buffer;
1044 int type;
1045 int top_end = 0; /* Below last row of the top area that needs
1046 updating. 0 when no top area updating. */
1047 int mid_start = 999;/* first row of the mid area that needs
1048 updating. 999 when no mid area updating. */
1049 int mid_end = 0; /* Below last row of the mid area that needs
1050 updating. 0 when no mid area updating. */
1051 int bot_start = 999;/* first row of the bot area that needs
1052 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 int scrolled_down = FALSE; /* TRUE when scrolled down when
1054 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001056 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 int top_to_mod = FALSE; /* redraw above mod_top */
1058#endif
1059
1060 int row; /* current window row to display */
1061 linenr_T lnum; /* current buffer lnum to display */
1062 int idx; /* current index in w_lines[] */
1063 int srow; /* starting row of the current line */
1064
1065 int eof = FALSE; /* if TRUE, we hit the end of the file */
1066 int didline = FALSE; /* if TRUE, we finished the last line */
1067 int i;
1068 long j;
1069 static int recursive = FALSE; /* being called recursively */
1070 int old_botline = wp->w_botline;
1071#ifdef FEAT_FOLDING
1072 long fold_count;
1073#endif
1074#ifdef FEAT_SYN_HL
1075 /* remember what happened to the previous line, to know if
1076 * check_visual_highlight() can be used */
1077#define DID_NONE 1 /* didn't update a line */
1078#define DID_LINE 2 /* updated a normal line */
1079#define DID_FOLD 3 /* updated a folded line */
1080 int did_update = DID_NONE;
1081 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1082#endif
1083 linenr_T mod_top = 0;
1084 linenr_T mod_bot = 0;
1085#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1086 int save_got_int;
1087#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001088#ifdef SYN_TIME_LIMIT
1089 proftime_T syntax_tm;
1090#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091
1092 type = wp->w_redr_type;
1093
1094 if (type == NOT_VALID)
1095 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 wp->w_lines_valid = 0;
1098 }
1099
1100 /* Window is zero-height: nothing to draw. */
Bram Moolenaar415a6932017-12-05 20:31:07 +01001101 if (wp->w_height + WINBAR_HEIGHT(wp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 {
1103 wp->w_redr_type = 0;
1104 return;
1105 }
1106
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 /* Window is zero-width: Only need to draw the separator. */
1108 if (wp->w_width == 0)
1109 {
1110 /* draw the vertical separator right of this window */
1111 draw_vsep_win(wp, 0);
1112 wp->w_redr_type = 0;
1113 return;
1114 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001116#ifdef FEAT_TERMINAL
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001117 // If this window contains a terminal, redraw works completely differently.
1118 if (term_do_update_window(wp))
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001119 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001120 term_update_window(wp);
Bram Moolenaar181ca992018-02-13 21:19:21 +01001121# ifdef FEAT_MENU
1122 /* Draw the window toolbar, if there is one. */
1123 if (winbar_height(wp) > 0)
1124 redraw_win_toolbar(wp);
1125# endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001126 wp->w_redr_type = 0;
1127 return;
1128 }
1129#endif
1130
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001132 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133#endif
1134
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001135#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001136 /* Force redraw when width of 'number' or 'relativenumber' column
1137 * changes. */
1138 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001139 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001140 {
1141 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001142 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001143 }
1144 else
1145#endif
1146
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1148 {
1149 /*
1150 * When there are both inserted/deleted lines and specific lines to be
1151 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1152 * everything (only happens when redrawing is off for while).
1153 */
1154 type = NOT_VALID;
1155 }
1156 else
1157 {
1158 /*
1159 * Set mod_top to the first line that needs displaying because of
1160 * changes. Set mod_bot to the first line after the changes.
1161 */
1162 mod_top = wp->w_redraw_top;
1163 if (wp->w_redraw_bot != 0)
1164 mod_bot = wp->w_redraw_bot + 1;
1165 else
1166 mod_bot = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 if (buf->b_mod_set)
1168 {
1169 if (mod_top == 0 || mod_top > buf->b_mod_top)
1170 {
1171 mod_top = buf->b_mod_top;
1172#ifdef FEAT_SYN_HL
1173 /* Need to redraw lines above the change that may be included
1174 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001175 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001177 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 if (mod_top < 1)
1179 mod_top = 1;
1180 }
1181#endif
1182 }
1183 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1184 mod_bot = buf->b_mod_bot;
1185
1186#ifdef FEAT_SEARCH_EXTRA
1187 /* When 'hlsearch' is on and using a multi-line search pattern, a
1188 * change in one line may make the Search highlighting in a
1189 * previous line invalid. Simple solution: redraw all visible
1190 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001191 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001193 if (search_hl.rm.regprog != NULL
1194 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001196 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001197 {
1198 cur = wp->w_match_head;
1199 while (cur != NULL)
1200 {
1201 if (cur->match.regprog != NULL
1202 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001203 {
1204 top_to_mod = TRUE;
1205 break;
1206 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001207 cur = cur->next;
1208 }
1209 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210#endif
1211 }
1212#ifdef FEAT_FOLDING
1213 if (mod_top != 0 && hasAnyFolding(wp))
1214 {
1215 linenr_T lnumt, lnumb;
1216
1217 /*
1218 * A change in a line can cause lines above it to become folded or
1219 * unfolded. Find the top most buffer line that may be affected.
1220 * If the line was previously folded and displayed, get the first
1221 * line of that fold. If the line is folded now, get the first
1222 * folded line. Use the minimum of these two.
1223 */
1224
1225 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1226 * the line below it. If there is no valid entry, use w_topline.
1227 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1228 * to this line. If there is no valid entry, use MAXLNUM. */
1229 lnumt = wp->w_topline;
1230 lnumb = MAXLNUM;
1231 for (i = 0; i < wp->w_lines_valid; ++i)
1232 if (wp->w_lines[i].wl_valid)
1233 {
1234 if (wp->w_lines[i].wl_lastlnum < mod_top)
1235 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1236 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1237 {
1238 lnumb = wp->w_lines[i].wl_lnum;
1239 /* When there is a fold column it might need updating
1240 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001241 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 ++lnumb;
1243 }
1244 }
1245
1246 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1247 if (mod_top > lnumt)
1248 mod_top = lnumt;
1249
1250 /* Now do the same for the bottom line (one above mod_bot). */
1251 --mod_bot;
1252 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1253 ++mod_bot;
1254 if (mod_bot < lnumb)
1255 mod_bot = lnumb;
1256 }
1257#endif
1258
1259 /* When a change starts above w_topline and the end is below
1260 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001261 * If the end of the change is above w_topline: do like no change was
1262 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 if (mod_top != 0 && mod_top < wp->w_topline)
1264 {
1265 if (mod_bot > wp->w_topline)
1266 mod_top = wp->w_topline;
1267#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001268 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 top_end = 1;
1270#endif
1271 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001272
1273 /* When line numbers are displayed need to redraw all lines below
1274 * inserted/deleted lines. */
1275 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1276 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 }
Bram Moolenaar895d9662019-01-31 21:57:21 +01001278 wp->w_redraw_top = 0; // reset for next time
1279 wp->w_redraw_bot = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280
1281 /*
1282 * When only displaying the lines at the top, set top_end. Used when
1283 * window has scrolled down for msg_scrolled.
1284 */
1285 if (type == REDRAW_TOP)
1286 {
1287 j = 0;
1288 for (i = 0; i < wp->w_lines_valid; ++i)
1289 {
1290 j += wp->w_lines[i].wl_size;
1291 if (j >= wp->w_upd_rows)
1292 {
1293 top_end = j;
1294 break;
1295 }
1296 }
1297 if (top_end == 0)
1298 /* not found (cannot happen?): redraw everything */
1299 type = NOT_VALID;
1300 else
1301 /* top area defined, the rest is VALID */
1302 type = VALID;
1303 }
1304
Bram Moolenaar367329b2007-08-30 11:53:22 +00001305 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001306 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1307 * non-zero and thus not FALSE) will indicate that screenclear() was not
1308 * called. */
1309 if (screen_cleared)
1310 screen_cleared = MAYBE;
1311
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 /*
1313 * If there are no changes on the screen that require a complete redraw,
1314 * handle three cases:
1315 * 1: we are off the top of the screen by a few lines: scroll down
1316 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1317 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1318 * w_lines[] that needs updating.
1319 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001320 if ((type == VALID || type == SOME_VALID
1321 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322#ifdef FEAT_DIFF
1323 && !wp->w_botfill && !wp->w_old_botfill
1324#endif
1325 )
1326 {
1327 if (mod_top != 0 && wp->w_topline == mod_top)
1328 {
1329 /*
1330 * w_topline is the first changed line, the scrolling will be done
1331 * further down.
1332 */
1333 }
1334 else if (wp->w_lines[0].wl_valid
1335 && (wp->w_topline < wp->w_lines[0].wl_lnum
1336#ifdef FEAT_DIFF
1337 || (wp->w_topline == wp->w_lines[0].wl_lnum
1338 && wp->w_topfill > wp->w_old_topfill)
1339#endif
1340 ))
1341 {
1342 /*
1343 * New topline is above old topline: May scroll down.
1344 */
1345#ifdef FEAT_FOLDING
1346 if (hasAnyFolding(wp))
1347 {
1348 linenr_T ln;
1349
1350 /* count the number of lines we are off, counting a sequence
1351 * of folded lines as one */
1352 j = 0;
1353 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1354 {
1355 ++j;
1356 if (j >= wp->w_height - 2)
1357 break;
1358 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1359 }
1360 }
1361 else
1362#endif
1363 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1364 if (j < wp->w_height - 2) /* not too far off */
1365 {
1366 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1367#ifdef FEAT_DIFF
1368 /* insert extra lines for previously invisible filler lines */
1369 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1370 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1371 - wp->w_old_topfill;
1372#endif
1373 if (i < wp->w_height - 2) /* less than a screen off */
1374 {
1375 /*
1376 * Try to insert the correct number of lines.
1377 * If not the last window, delete the lines at the bottom.
1378 * win_ins_lines may fail when the terminal can't do it.
1379 */
1380 if (i > 0)
1381 check_for_delay(FALSE);
1382 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1383 {
1384 if (wp->w_lines_valid != 0)
1385 {
1386 /* Need to update rows that are new, stop at the
1387 * first one that scrolled down. */
1388 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390
1391 /* Move the entries that were scrolled, disable
1392 * the entries for the lines to be redrawn. */
1393 if ((wp->w_lines_valid += j) > wp->w_height)
1394 wp->w_lines_valid = wp->w_height;
1395 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1396 wp->w_lines[idx] = wp->w_lines[idx - j];
1397 while (idx >= 0)
1398 wp->w_lines[idx--].wl_valid = FALSE;
1399 }
1400 }
1401 else
1402 mid_start = 0; /* redraw all lines */
1403 }
1404 else
1405 mid_start = 0; /* redraw all lines */
1406 }
1407 else
1408 mid_start = 0; /* redraw all lines */
1409 }
1410 else
1411 {
1412 /*
1413 * New topline is at or below old topline: May scroll up.
1414 * When topline didn't change, find first entry in w_lines[] that
1415 * needs updating.
1416 */
1417
1418 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1419 j = -1;
1420 row = 0;
1421 for (i = 0; i < wp->w_lines_valid; i++)
1422 {
1423 if (wp->w_lines[i].wl_valid
1424 && wp->w_lines[i].wl_lnum == wp->w_topline)
1425 {
1426 j = i;
1427 break;
1428 }
1429 row += wp->w_lines[i].wl_size;
1430 }
1431 if (j == -1)
1432 {
1433 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1434 * lines */
1435 mid_start = 0;
1436 }
1437 else
1438 {
1439 /*
1440 * Try to delete the correct number of lines.
1441 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1442 */
1443#ifdef FEAT_DIFF
1444 /* If the topline didn't change, delete old filler lines,
1445 * otherwise delete filler lines of the new topline... */
1446 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1447 row += wp->w_old_topfill;
1448 else
1449 row += diff_check_fill(wp, wp->w_topline);
1450 /* ... but don't delete new filler lines. */
1451 row -= wp->w_topfill;
1452#endif
1453 if (row > 0)
1454 {
1455 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001456 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1457 == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 bot_start = wp->w_height - row;
1459 else
1460 mid_start = 0; /* redraw all lines */
1461 }
1462 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1463 {
1464 /*
1465 * Skip the lines (below the deleted lines) that are still
1466 * valid and don't need redrawing. Copy their info
1467 * upwards, to compensate for the deleted lines. Set
1468 * bot_start to the first row that needs redrawing.
1469 */
1470 bot_start = 0;
1471 idx = 0;
1472 for (;;)
1473 {
1474 wp->w_lines[idx] = wp->w_lines[j];
1475 /* stop at line that didn't fit, unless it is still
1476 * valid (no lines deleted) */
1477 if (row > 0 && bot_start + row
1478 + (int)wp->w_lines[j].wl_size > wp->w_height)
1479 {
1480 wp->w_lines_valid = idx + 1;
1481 break;
1482 }
1483 bot_start += wp->w_lines[idx++].wl_size;
1484
1485 /* stop at the last valid entry in w_lines[].wl_size */
1486 if (++j >= wp->w_lines_valid)
1487 {
1488 wp->w_lines_valid = idx;
1489 break;
1490 }
1491 }
1492#ifdef FEAT_DIFF
1493 /* Correct the first entry for filler lines at the top
1494 * when it won't get updated below. */
1495 if (wp->w_p_diff && bot_start > 0)
1496 wp->w_lines[0].wl_size =
1497 plines_win_nofill(wp, wp->w_topline, TRUE)
1498 + wp->w_topfill;
1499#endif
1500 }
1501 }
1502 }
1503
1504 /* When starting redraw in the first line, redraw all lines. When
1505 * there is only one window it's probably faster to clear the screen
1506 * first. */
1507 if (mid_start == 0)
1508 {
1509 mid_end = wp->w_height;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001510 if (ONE_WINDOW)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001511 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001512 /* Clear the screen when it was not done by win_del_lines() or
1513 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1514 * then. */
1515 if (screen_cleared != TRUE)
1516 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001517 /* The screen was cleared, redraw the tab pages line. */
1518 if (redraw_tabline)
1519 draw_tabline();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001520 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001522
1523 /* When win_del_lines() or win_ins_lines() caused the screen to be
1524 * cleared (only happens for the first window) or when screenclear()
1525 * was called directly above, "must_redraw" will have been set to
1526 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1527 if (screen_cleared == TRUE)
1528 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 }
1530 else
1531 {
1532 /* Not VALID or INVERTED: redraw all lines. */
1533 mid_start = 0;
1534 mid_end = wp->w_height;
1535 }
1536
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001537 if (type == SOME_VALID)
1538 {
1539 /* SOME_VALID: redraw all lines. */
1540 mid_start = 0;
1541 mid_end = wp->w_height;
1542 type = NOT_VALID;
1543 }
1544
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 /* check if we are updating or removing the inverted part */
1546 if ((VIsual_active && buf == curwin->w_buffer)
1547 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1548 {
1549 linenr_T from, to;
1550
1551 if (VIsual_active)
1552 {
1553 if (VIsual_active
1554 && (VIsual_mode != wp->w_old_visual_mode
1555 || type == INVERTED_ALL))
1556 {
1557 /*
1558 * If the type of Visual selection changed, redraw the whole
1559 * selection. Also when the ownership of the X selection is
1560 * gained or lost.
1561 */
1562 if (curwin->w_cursor.lnum < VIsual.lnum)
1563 {
1564 from = curwin->w_cursor.lnum;
1565 to = VIsual.lnum;
1566 }
1567 else
1568 {
1569 from = VIsual.lnum;
1570 to = curwin->w_cursor.lnum;
1571 }
1572 /* redraw more when the cursor moved as well */
1573 if (wp->w_old_cursor_lnum < from)
1574 from = wp->w_old_cursor_lnum;
1575 if (wp->w_old_cursor_lnum > to)
1576 to = wp->w_old_cursor_lnum;
1577 if (wp->w_old_visual_lnum < from)
1578 from = wp->w_old_visual_lnum;
1579 if (wp->w_old_visual_lnum > to)
1580 to = wp->w_old_visual_lnum;
1581 }
1582 else
1583 {
1584 /*
1585 * Find the line numbers that need to be updated: The lines
1586 * between the old cursor position and the current cursor
1587 * position. Also check if the Visual position changed.
1588 */
1589 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1590 {
1591 from = curwin->w_cursor.lnum;
1592 to = wp->w_old_cursor_lnum;
1593 }
1594 else
1595 {
1596 from = wp->w_old_cursor_lnum;
1597 to = curwin->w_cursor.lnum;
1598 if (from == 0) /* Visual mode just started */
1599 from = to;
1600 }
1601
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001602 if (VIsual.lnum != wp->w_old_visual_lnum
1603 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 {
1605 if (wp->w_old_visual_lnum < from
1606 && wp->w_old_visual_lnum != 0)
1607 from = wp->w_old_visual_lnum;
1608 if (wp->w_old_visual_lnum > to)
1609 to = wp->w_old_visual_lnum;
1610 if (VIsual.lnum < from)
1611 from = VIsual.lnum;
1612 if (VIsual.lnum > to)
1613 to = VIsual.lnum;
1614 }
1615 }
1616
1617 /*
1618 * If in block mode and changed column or curwin->w_curswant:
1619 * update all lines.
1620 * First compute the actual start and end column.
1621 */
1622 if (VIsual_mode == Ctrl_V)
1623 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001624 colnr_T fromc, toc;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001625#if defined(FEAT_LINEBREAK)
Bram Moolenaar404406a2014-10-09 13:24:43 +02001626 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627
Bram Moolenaar404406a2014-10-09 13:24:43 +02001628 if (curwin->w_p_lbr)
1629 ve_flags = VE_ALL;
1630#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001632#if defined(FEAT_LINEBREAK)
Bram Moolenaar404406a2014-10-09 13:24:43 +02001633 ve_flags = save_ve_flags;
1634#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 ++toc;
1636 if (curwin->w_curswant == MAXCOL)
1637 toc = MAXCOL;
1638
1639 if (fromc != wp->w_old_cursor_fcol
1640 || toc != wp->w_old_cursor_lcol)
1641 {
1642 if (from > VIsual.lnum)
1643 from = VIsual.lnum;
1644 if (to < VIsual.lnum)
1645 to = VIsual.lnum;
1646 }
1647 wp->w_old_cursor_fcol = fromc;
1648 wp->w_old_cursor_lcol = toc;
1649 }
1650 }
1651 else
1652 {
1653 /* Use the line numbers of the old Visual area. */
1654 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1655 {
1656 from = wp->w_old_cursor_lnum;
1657 to = wp->w_old_visual_lnum;
1658 }
1659 else
1660 {
1661 from = wp->w_old_visual_lnum;
1662 to = wp->w_old_cursor_lnum;
1663 }
1664 }
1665
1666 /*
1667 * There is no need to update lines above the top of the window.
1668 */
1669 if (from < wp->w_topline)
1670 from = wp->w_topline;
1671
1672 /*
1673 * If we know the value of w_botline, use it to restrict the update to
1674 * the lines that are visible in the window.
1675 */
1676 if (wp->w_valid & VALID_BOTLINE)
1677 {
1678 if (from >= wp->w_botline)
1679 from = wp->w_botline - 1;
1680 if (to >= wp->w_botline)
1681 to = wp->w_botline - 1;
1682 }
1683
1684 /*
1685 * Find the minimal part to be updated.
1686 * Watch out for scrolling that made entries in w_lines[] invalid.
1687 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1688 * top_end; need to redraw from top_end to the "to" line.
1689 * A middle mouse click with a Visual selection may change the text
1690 * above the Visual area and reset wl_valid, do count these for
1691 * mid_end (in srow).
1692 */
1693 if (mid_start > 0)
1694 {
1695 lnum = wp->w_topline;
1696 idx = 0;
1697 srow = 0;
1698 if (scrolled_down)
1699 mid_start = top_end;
1700 else
1701 mid_start = 0;
1702 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1703 {
1704 if (wp->w_lines[idx].wl_valid)
1705 mid_start += wp->w_lines[idx].wl_size;
1706 else if (!scrolled_down)
1707 srow += wp->w_lines[idx].wl_size;
1708 ++idx;
1709# ifdef FEAT_FOLDING
1710 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1711 lnum = wp->w_lines[idx].wl_lnum;
1712 else
1713# endif
1714 ++lnum;
1715 }
1716 srow += mid_start;
1717 mid_end = wp->w_height;
1718 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1719 {
1720 if (wp->w_lines[idx].wl_valid
1721 && wp->w_lines[idx].wl_lnum >= to + 1)
1722 {
1723 /* Only update until first row of this line */
1724 mid_end = srow;
1725 break;
1726 }
1727 srow += wp->w_lines[idx].wl_size;
1728 }
1729 }
1730 }
1731
1732 if (VIsual_active && buf == curwin->w_buffer)
1733 {
1734 wp->w_old_visual_mode = VIsual_mode;
1735 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1736 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001737 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 wp->w_old_curswant = curwin->w_curswant;
1739 }
1740 else
1741 {
1742 wp->w_old_visual_mode = 0;
1743 wp->w_old_cursor_lnum = 0;
1744 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001745 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747
1748#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1749 /* reset got_int, otherwise regexp won't work */
1750 save_got_int = got_int;
1751 got_int = 0;
1752#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001753#ifdef SYN_TIME_LIMIT
1754 /* Set the time limit to 'redrawtime'. */
1755 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02001756 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001757#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758#ifdef FEAT_FOLDING
1759 win_foldinfo.fi_level = 0;
1760#endif
1761
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001762#ifdef FEAT_MENU
1763 /*
1764 * Draw the window toolbar, if there is one.
1765 * TODO: only when needed.
1766 */
1767 if (winbar_height(wp) > 0)
1768 redraw_win_toolbar(wp);
1769#endif
1770
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 /*
1772 * Update all the window rows.
1773 */
1774 idx = 0; /* first entry in w_lines[].wl_size */
1775 row = 0;
1776 srow = 0;
1777 lnum = wp->w_topline; /* first line shown in window */
1778 for (;;)
1779 {
1780 /* stop updating when reached the end of the window (check for _past_
1781 * the end of the window is at the end of the loop) */
1782 if (row == wp->w_height)
1783 {
1784 didline = TRUE;
1785 break;
1786 }
1787
1788 /* stop updating when hit the end of the file */
1789 if (lnum > buf->b_ml.ml_line_count)
1790 {
1791 eof = TRUE;
1792 break;
1793 }
1794
1795 /* Remember the starting row of the line that is going to be dealt
1796 * with. It is used further down when the line doesn't fit. */
1797 srow = row;
1798
1799 /*
1800 * Update a line when it is in an area that needs updating, when it
1801 * has changes or w_lines[idx] is invalid.
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02001802 * "bot_start" may be halfway a wrapped line after using
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 * win_del_lines(), check if the current line includes it.
1804 * When syntax folding is being used, the saved syntax states will
1805 * already have been updated, we can't see where the syntax state is
1806 * the same again, just update until the end of the window.
1807 */
1808 if (row < top_end
1809 || (row >= mid_start && row < mid_end)
1810#ifdef FEAT_SEARCH_EXTRA
1811 || top_to_mod
1812#endif
1813 || idx >= wp->w_lines_valid
1814 || (row + wp->w_lines[idx].wl_size > bot_start)
1815 || (mod_top != 0
1816 && (lnum == mod_top
1817 || (lnum >= mod_top
1818 && (lnum < mod_bot
1819#ifdef FEAT_SYN_HL
1820 || did_update == DID_FOLD
1821 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001822 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 && (
1824# ifdef FEAT_FOLDING
1825 (foldmethodIsSyntax(wp)
1826 && hasAnyFolding(wp)) ||
1827# endif
1828 syntax_check_changed(lnum)))
1829#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001830#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001831 /* match in fixed position might need redraw
1832 * if lines were inserted or deleted */
1833 || (wp->w_match_head != NULL
1834 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001835#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 )))))
1837 {
1838#ifdef FEAT_SEARCH_EXTRA
1839 if (lnum == mod_top)
1840 top_to_mod = FALSE;
1841#endif
1842
1843 /*
1844 * When at start of changed lines: May scroll following lines
1845 * up or down to minimize redrawing.
1846 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001847 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 */
1849 if (lnum == mod_top
1850 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001851 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 {
1853 int old_rows = 0;
1854 int new_rows = 0;
1855 int xtra_rows;
1856 linenr_T l;
1857
1858 /* Count the old number of window rows, using w_lines[], which
1859 * should still contain the sizes for the lines as they are
1860 * currently displayed. */
1861 for (i = idx; i < wp->w_lines_valid; ++i)
1862 {
1863 /* Only valid lines have a meaningful wl_lnum. Invalid
1864 * lines are part of the changed area. */
1865 if (wp->w_lines[i].wl_valid
1866 && wp->w_lines[i].wl_lnum == mod_bot)
1867 break;
1868 old_rows += wp->w_lines[i].wl_size;
1869#ifdef FEAT_FOLDING
1870 if (wp->w_lines[i].wl_valid
1871 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1872 {
1873 /* Must have found the last valid entry above mod_bot.
1874 * Add following invalid entries. */
1875 ++i;
1876 while (i < wp->w_lines_valid
1877 && !wp->w_lines[i].wl_valid)
1878 old_rows += wp->w_lines[i++].wl_size;
1879 break;
1880 }
1881#endif
1882 }
1883
1884 if (i >= wp->w_lines_valid)
1885 {
1886 /* We can't find a valid line below the changed lines,
1887 * need to redraw until the end of the window.
1888 * Inserting/deleting lines has no use. */
1889 bot_start = 0;
1890 }
1891 else
1892 {
1893 /* Able to count old number of rows: Count new window
1894 * rows, and may insert/delete lines */
1895 j = idx;
1896 for (l = lnum; l < mod_bot; ++l)
1897 {
1898#ifdef FEAT_FOLDING
1899 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1900 ++new_rows;
1901 else
1902#endif
1903#ifdef FEAT_DIFF
1904 if (l == wp->w_topline)
1905 new_rows += plines_win_nofill(wp, l, TRUE)
1906 + wp->w_topfill;
1907 else
1908#endif
1909 new_rows += plines_win(wp, l, TRUE);
1910 ++j;
1911 if (new_rows > wp->w_height - row - 2)
1912 {
1913 /* it's getting too much, must redraw the rest */
1914 new_rows = 9999;
1915 break;
1916 }
1917 }
1918 xtra_rows = new_rows - old_rows;
1919 if (xtra_rows < 0)
1920 {
1921 /* May scroll text up. If there is not enough
1922 * remaining text or scrolling fails, must redraw the
1923 * rest. If scrolling works, must redraw the text
1924 * below the scrolled text. */
1925 if (row - xtra_rows >= wp->w_height - 2)
1926 mod_bot = MAXLNUM;
1927 else
1928 {
1929 check_for_delay(FALSE);
1930 if (win_del_lines(wp, row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001931 -xtra_rows, FALSE, FALSE, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 mod_bot = MAXLNUM;
1933 else
1934 bot_start = wp->w_height + xtra_rows;
1935 }
1936 }
1937 else if (xtra_rows > 0)
1938 {
1939 /* May scroll text down. If there is not enough
1940 * remaining text of scrolling fails, must redraw the
1941 * rest. */
1942 if (row + xtra_rows >= wp->w_height - 2)
1943 mod_bot = MAXLNUM;
1944 else
1945 {
1946 check_for_delay(FALSE);
1947 if (win_ins_lines(wp, row + old_rows,
1948 xtra_rows, FALSE, FALSE) == FAIL)
1949 mod_bot = MAXLNUM;
1950 else if (top_end > row + old_rows)
1951 /* Scrolled the part at the top that requires
1952 * updating down. */
1953 top_end += xtra_rows;
1954 }
1955 }
1956
1957 /* When not updating the rest, may need to move w_lines[]
1958 * entries. */
1959 if (mod_bot != MAXLNUM && i != j)
1960 {
1961 if (j < i)
1962 {
1963 int x = row + new_rows;
1964
1965 /* move entries in w_lines[] upwards */
1966 for (;;)
1967 {
1968 /* stop at last valid entry in w_lines[] */
1969 if (i >= wp->w_lines_valid)
1970 {
1971 wp->w_lines_valid = j;
1972 break;
1973 }
1974 wp->w_lines[j] = wp->w_lines[i];
1975 /* stop at a line that won't fit */
1976 if (x + (int)wp->w_lines[j].wl_size
1977 > wp->w_height)
1978 {
1979 wp->w_lines_valid = j + 1;
1980 break;
1981 }
1982 x += wp->w_lines[j++].wl_size;
1983 ++i;
1984 }
1985 if (bot_start > x)
1986 bot_start = x;
1987 }
1988 else /* j > i */
1989 {
1990 /* move entries in w_lines[] downwards */
1991 j -= i;
1992 wp->w_lines_valid += j;
1993 if (wp->w_lines_valid > wp->w_height)
1994 wp->w_lines_valid = wp->w_height;
1995 for (i = wp->w_lines_valid; i - j >= idx; --i)
1996 wp->w_lines[i] = wp->w_lines[i - j];
1997
1998 /* The w_lines[] entries for inserted lines are
1999 * now invalid, but wl_size may be used above.
2000 * Reset to zero. */
2001 while (i >= idx)
2002 {
2003 wp->w_lines[i].wl_size = 0;
2004 wp->w_lines[i--].wl_valid = FALSE;
2005 }
2006 }
2007 }
2008 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 }
2010
2011#ifdef FEAT_FOLDING
2012 /*
2013 * When lines are folded, display one line for all of them.
2014 * Otherwise, display normally (can be several display lines when
2015 * 'wrap' is on).
2016 */
2017 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2018 if (fold_count != 0)
2019 {
2020 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2021 ++row;
2022 --fold_count;
2023 wp->w_lines[idx].wl_folded = TRUE;
2024 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2025# ifdef FEAT_SYN_HL
2026 did_update = DID_FOLD;
2027# endif
2028 }
2029 else
2030#endif
2031 if (idx < wp->w_lines_valid
2032 && wp->w_lines[idx].wl_valid
2033 && wp->w_lines[idx].wl_lnum == lnum
2034 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002035 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 && srow + wp->w_lines[idx].wl_size > wp->w_height
2037#ifdef FEAT_DIFF
2038 && diff_check_fill(wp, lnum) == 0
2039#endif
2040 )
2041 {
2042 /* This line is not going to fit. Don't draw anything here,
2043 * will draw "@ " lines below. */
2044 row = wp->w_height + 1;
2045 }
2046 else
2047 {
2048#ifdef FEAT_SEARCH_EXTRA
2049 prepare_search_hl(wp, lnum);
2050#endif
2051#ifdef FEAT_SYN_HL
2052 /* Let the syntax stuff know we skipped a few lines. */
2053 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002054 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 syntax_end_parsing(syntax_last_parsed + 1);
2056#endif
2057
2058 /*
2059 * Display one line.
2060 */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002061 row = win_line(wp, lnum, srow, wp->w_height,
2062 mod_top == 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063
2064#ifdef FEAT_FOLDING
2065 wp->w_lines[idx].wl_folded = FALSE;
2066 wp->w_lines[idx].wl_lastlnum = lnum;
2067#endif
2068#ifdef FEAT_SYN_HL
2069 did_update = DID_LINE;
2070 syntax_last_parsed = lnum;
2071#endif
2072 }
2073
2074 wp->w_lines[idx].wl_lnum = lnum;
2075 wp->w_lines[idx].wl_valid = TRUE;
Bram Moolenaar0e19fc02017-10-28 14:45:16 +02002076
2077 /* Past end of the window or end of the screen. Note that after
2078 * resizing wp->w_height may be end up too big. That's a problem
2079 * elsewhere, but prevent a crash here. */
2080 if (row > wp->w_height || row + wp->w_winrow >= Rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 {
2082 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002083 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2085 ++idx;
2086 break;
2087 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002088 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 wp->w_lines[idx].wl_size = row - srow;
2090 ++idx;
2091#ifdef FEAT_FOLDING
2092 lnum += fold_count + 1;
2093#else
2094 ++lnum;
2095#endif
2096 }
2097 else
2098 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002099 if (wp->w_p_rnu)
2100 {
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002101#ifdef FEAT_FOLDING
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002102 // 'relativenumber' set: The text doesn't need to be drawn, but
2103 // the number column nearly always does.
Bram Moolenaar7701f302018-10-02 21:20:32 +02002104 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2105 if (fold_count != 0)
Bram Moolenaar7701f302018-10-02 21:20:32 +02002106 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
Bram Moolenaar7701f302018-10-02 21:20:32 +02002107 else
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002108#endif
Bram Moolenaar7701f302018-10-02 21:20:32 +02002109 (void)win_line(wp, lnum, srow, wp->w_height, TRUE, TRUE);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002110 }
2111
2112 // This line does not need to be drawn, advance to the next one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 row += wp->w_lines[idx++].wl_size;
2114 if (row > wp->w_height) /* past end of screen */
2115 break;
2116#ifdef FEAT_FOLDING
2117 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2118#else
2119 ++lnum;
2120#endif
2121#ifdef FEAT_SYN_HL
2122 did_update = DID_NONE;
2123#endif
2124 }
2125
2126 if (lnum > buf->b_ml.ml_line_count)
2127 {
2128 eof = TRUE;
2129 break;
2130 }
2131 }
2132 /*
2133 * End of loop over all window lines.
2134 */
2135
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002136#ifdef FEAT_VTP
2137 /* Rewrite the character at the end of the screen line. */
2138 if (use_vtp())
2139 {
2140 int i;
2141
2142 for (i = 0; i < Rows; ++i)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002143 if (enc_utf8)
2144 if ((*mb_off2cells)(LineOffset[i] + Columns - 2,
2145 LineOffset[i] + screen_Columns) > 1)
2146 screen_draw_rectangle(i, Columns - 2, 1, 2, FALSE);
2147 else
2148 screen_draw_rectangle(i, Columns - 1, 1, 1, FALSE);
2149 else
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002150 screen_char(LineOffset[i] + Columns - 1, i, Columns - 1);
2151 }
2152#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153
2154 if (idx > wp->w_lines_valid)
2155 wp->w_lines_valid = idx;
2156
2157#ifdef FEAT_SYN_HL
2158 /*
2159 * Let the syntax stuff know we stop parsing here.
2160 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002161 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 syntax_end_parsing(syntax_last_parsed + 1);
2163#endif
2164
2165 /*
2166 * If we didn't hit the end of the file, and we didn't finish the last
2167 * line we were working on, then the line didn't fit.
2168 */
2169 wp->w_empty_rows = 0;
2170#ifdef FEAT_DIFF
2171 wp->w_filler_rows = 0;
2172#endif
2173 if (!eof && !didline)
2174 {
2175 if (lnum == wp->w_topline)
2176 {
2177 /*
2178 * Single line that does not fit!
2179 * Don't overwrite it, it can be edited.
2180 */
2181 wp->w_botline = lnum + 1;
2182 }
2183#ifdef FEAT_DIFF
2184 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2185 {
2186 /* Window ends in filler lines. */
2187 wp->w_botline = lnum;
2188 wp->w_filler_rows = wp->w_height - srow;
2189 }
2190#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002191 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2192 {
2193 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2194
2195 /*
2196 * Last line isn't finished: Display "@@@" in the last screen line.
2197 */
Bram Moolenaar53f81742017-09-22 14:35:51 +02002198 screen_puts_len((char_u *)"@@", 2, scr_row, wp->w_wincol,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002199 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002200 screen_fill(scr_row, scr_row + 1,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002201 (int)wp->w_wincol + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002202 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002203 set_empty_rows(wp, srow);
2204 wp->w_botline = lnum;
2205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2207 {
2208 /*
2209 * Last line isn't finished: Display "@@@" at the end.
2210 */
2211 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2212 W_WINROW(wp) + wp->w_height,
2213 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002214 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 set_empty_rows(wp, srow);
2216 wp->w_botline = lnum;
2217 }
2218 else
2219 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002220 win_draw_end(wp, '@', ' ', TRUE, srow, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 wp->w_botline = lnum;
2222 }
2223 }
2224 else
2225 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 if (eof) /* we hit the end of the file */
2228 {
2229 wp->w_botline = buf->b_ml.ml_line_count + 1;
2230#ifdef FEAT_DIFF
2231 j = diff_check_fill(wp, wp->w_botline);
2232 if (j > 0 && !wp->w_botfill)
2233 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002234 // Display filler lines at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 if (char2cells(fill_diff) > 1)
2236 i = '-';
2237 else
2238 i = fill_diff;
2239 if (row + j > wp->w_height)
2240 j = wp->w_height - row;
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002241 win_draw_end(wp, i, i, TRUE, row, row + (int)j, HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 row += j;
2243 }
2244#endif
2245 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002246 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 wp->w_botline = lnum;
2248
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002249 // Make sure the rest of the screen is blank
2250 // put '~'s on rows that aren't part of the file.
2251 win_draw_end(wp, '~', ' ', FALSE, row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 }
2253
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002254#ifdef SYN_TIME_LIMIT
2255 syn_set_timeout(NULL);
2256#endif
2257
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 /* Reset the type of redrawing required, the window has been updated. */
2259 wp->w_redr_type = 0;
2260#ifdef FEAT_DIFF
2261 wp->w_old_topfill = wp->w_topfill;
2262 wp->w_old_botfill = wp->w_botfill;
2263#endif
2264
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002265 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 {
2267 /*
2268 * There is a trick with w_botline. If we invalidate it on each
2269 * change that might modify it, this will cause a lot of expensive
2270 * calls to plines() in update_topline() each time. Therefore the
2271 * value of w_botline is often approximated, and this value is used to
2272 * compute the value of w_topline. If the value of w_botline was
2273 * wrong, check that the value of w_topline is correct (cursor is on
2274 * the visible part of the text). If it's not, we need to redraw
2275 * again. Mostly this just means scrolling up a few lines, so it
2276 * doesn't look too bad. Only do this for the current window (where
2277 * changes are relevant).
2278 */
2279 wp->w_valid |= VALID_BOTLINE;
2280 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2281 {
2282 recursive = TRUE;
2283 curwin->w_valid &= ~VALID_TOPLINE;
2284 update_topline(); /* may invalidate w_botline again */
2285 if (must_redraw != 0)
2286 {
2287 /* Don't update for changes in buffer again. */
2288 i = curbuf->b_mod_set;
2289 curbuf->b_mod_set = FALSE;
2290 win_update(curwin);
2291 must_redraw = 0;
2292 curbuf->b_mod_set = i;
2293 }
2294 recursive = FALSE;
2295 }
2296 }
2297
2298#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2299 /* restore got_int, unless CTRL-C was hit while redrawing */
2300 if (!got_int)
2301 got_int = save_got_int;
2302#endif
2303}
2304
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305/*
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002306 * Call screen_fill() with the columns adjusted for 'rightleft' if needed.
2307 * Return the new offset.
2308 */
2309 static int
2310screen_fill_end(
2311 win_T *wp,
2312 int c1,
2313 int c2,
2314 int off,
2315 int width,
2316 int row,
2317 int endrow,
2318 int attr)
2319{
2320 int nn = off + width;
2321
2322 if (nn > wp->w_width)
2323 nn = wp->w_width;
2324#ifdef FEAT_RIGHTLEFT
2325 if (wp->w_p_rl)
2326 {
2327 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2328 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - off,
2329 c1, c2, attr);
2330 }
2331 else
2332#endif
2333 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2334 wp->w_wincol + off, (int)wp->w_wincol + nn,
2335 c1, c2, attr);
2336 return nn;
2337}
2338
2339/*
2340 * Clear lines near the end the window and mark the unused lines with "c1".
2341 * use "c2" as the filler character.
2342 * When "draw_margin" is TRUE then draw the sign, fold and number columns.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 */
2344 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002345win_draw_end(
2346 win_T *wp,
2347 int c1,
2348 int c2,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002349 int draw_margin,
Bram Moolenaar05540972016-01-30 20:31:25 +01002350 int row,
2351 int endrow,
2352 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 int n = 0;
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002355
2356 if (draw_margin)
2357 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002358#ifdef FEAT_FOLDING
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002359 int fdc = compute_foldcolumn(wp, 0);
2360
2361 if (fdc > 0)
2362 // draw the fold column
2363 n = screen_fill_end(wp, ' ', ' ', n, fdc,
2364 row, endrow, HL_ATTR(HLF_FC));
Bram Moolenaar1c934292015-01-27 16:39:29 +01002365#endif
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002366#ifdef FEAT_SIGNS
2367 if (signcolumn_on(wp))
2368 // draw the sign column
2369 n = screen_fill_end(wp, ' ', ' ', n, 2,
2370 row, endrow, HL_ATTR(HLF_SC));
2371#endif
2372 if ((wp->w_p_nu || wp->w_p_rnu)
2373 && vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
2374 // draw the number column
2375 n = screen_fill_end(wp, ' ', ' ', n, number_width(wp) + 1,
2376 row, endrow, HL_ATTR(HLF_N));
2377 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378
2379#ifdef FEAT_RIGHTLEFT
2380 if (wp->w_p_rl)
2381 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002383 wp->w_wincol, W_ENDCOL(wp) - 1 - n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002384 c2, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002386 W_ENDCOL(wp) - 1 - n, W_ENDCOL(wp) - n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002387 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 }
2389 else
2390#endif
2391 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002393 wp->w_wincol + n, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002394 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 }
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002396
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 set_empty_rows(wp, row);
2398}
2399
Bram Moolenaar1a384422010-07-14 19:53:30 +02002400#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02002401/*
2402 * Advance **color_cols and return TRUE when there are columns to draw.
2403 */
2404 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002405advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002406{
2407 while (**color_cols >= 0 && vcol > **color_cols)
2408 ++*color_cols;
2409 return (**color_cols >= 0);
2410}
2411#endif
2412
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002413#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
2414/*
2415 * Copy "text" to ScreenLines using "attr".
2416 * Returns the next screen column.
2417 */
2418 static int
2419text_to_screenline(win_T *wp, char_u *text, int col)
2420{
2421 int off = (int)(current_ScreenLine - ScreenLines);
2422
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002423 if (has_mbyte)
2424 {
2425 int cells;
2426 int u8c, u8cc[MAX_MCO];
2427 int i;
2428 int idx;
2429 int c_len;
2430 char_u *p;
2431# ifdef FEAT_ARABIC
2432 int prev_c = 0; /* previous Arabic character */
2433 int prev_c1 = 0; /* first composing char for prev_c */
2434# endif
2435
2436# ifdef FEAT_RIGHTLEFT
2437 if (wp->w_p_rl)
2438 idx = off;
2439 else
2440# endif
2441 idx = off + col;
2442
2443 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2444 for (p = text; *p != NUL; )
2445 {
2446 cells = (*mb_ptr2cells)(p);
2447 c_len = (*mb_ptr2len)(p);
Bram Moolenaar02631462017-09-22 15:20:32 +02002448 if (col + cells > wp->w_width
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002449# ifdef FEAT_RIGHTLEFT
2450 - (wp->w_p_rl ? col : 0)
2451# endif
2452 )
2453 break;
2454 ScreenLines[idx] = *p;
2455 if (enc_utf8)
2456 {
2457 u8c = utfc_ptr2char(p, u8cc);
2458 if (*p < 0x80 && u8cc[0] == 0)
2459 {
2460 ScreenLinesUC[idx] = 0;
2461#ifdef FEAT_ARABIC
2462 prev_c = u8c;
2463#endif
2464 }
2465 else
2466 {
2467#ifdef FEAT_ARABIC
2468 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2469 {
2470 /* Do Arabic shaping. */
2471 int pc, pc1, nc;
2472 int pcc[MAX_MCO];
2473 int firstbyte = *p;
2474
2475 /* The idea of what is the previous and next
2476 * character depends on 'rightleft'. */
2477 if (wp->w_p_rl)
2478 {
2479 pc = prev_c;
2480 pc1 = prev_c1;
2481 nc = utf_ptr2char(p + c_len);
2482 prev_c1 = u8cc[0];
2483 }
2484 else
2485 {
2486 pc = utfc_ptr2char(p + c_len, pcc);
2487 nc = prev_c;
2488 pc1 = pcc[0];
2489 }
2490 prev_c = u8c;
2491
2492 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
2493 pc, pc1, nc);
2494 ScreenLines[idx] = firstbyte;
2495 }
2496 else
2497 prev_c = u8c;
2498#endif
2499 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar9ba61172019-01-24 18:20:17 +01002500 ScreenLinesUC[idx] = u8c;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002501 for (i = 0; i < Screen_mco; ++i)
2502 {
2503 ScreenLinesC[i][idx] = u8cc[i];
2504 if (u8cc[i] == 0)
2505 break;
2506 }
2507 }
2508 if (cells > 1)
2509 ScreenLines[idx + 1] = 0;
2510 }
2511 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2512 /* double-byte single width character */
2513 ScreenLines2[idx] = p[1];
2514 else if (cells > 1)
2515 /* double-width character */
2516 ScreenLines[idx + 1] = p[1];
2517 col += cells;
2518 idx += cells;
2519 p += c_len;
2520 }
2521 }
2522 else
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002523 {
2524 int len = (int)STRLEN(text);
2525
Bram Moolenaar02631462017-09-22 15:20:32 +02002526 if (len > wp->w_width - col)
2527 len = wp->w_width - col;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002528 if (len > 0)
2529 {
2530#ifdef FEAT_RIGHTLEFT
2531 if (wp->w_p_rl)
Bram Moolenaarc6663882019-02-22 19:14:54 +01002532 mch_memmove(current_ScreenLine, text, len);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002533 else
2534#endif
Bram Moolenaarc6663882019-02-22 19:14:54 +01002535 mch_memmove(current_ScreenLine + col, text, len);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002536 col += len;
2537 }
2538 }
2539 return col;
2540}
2541#endif
2542
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543#ifdef FEAT_FOLDING
2544/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002545 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2546 * space is available for window "wp", minus "col".
2547 */
2548 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002549compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002550{
2551 int fdc = wp->w_p_fdc;
2552 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
Bram Moolenaar02631462017-09-22 15:20:32 +02002553 int wwidth = wp->w_width;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002554
2555 if (fdc > wwidth - (col + wmw))
2556 fdc = wwidth - (col + wmw);
2557 return fdc;
2558}
2559
2560/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 * Display one folded line.
2562 */
2563 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002564fold_line(
2565 win_T *wp,
2566 long fold_count,
2567 foldinfo_T *foldinfo,
2568 linenr_T lnum,
2569 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002571 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 pos_T *top, *bot;
2573 linenr_T lnume = lnum + fold_count - 1;
2574 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002575 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 int col;
2578 int txtcol;
2579 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002580 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581
2582 /* Build the fold line:
2583 * 1. Add the cmdwin_type for the command-line window
2584 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002585 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 * 4. Compose the text
2587 * 5. Add the text
2588 * 6. set highlighting for the Visual area an other text
2589 */
2590 col = 0;
2591
2592 /*
2593 * 1. Add the cmdwin_type for the command-line window
2594 * Ignores 'rightleft', this window is never right-left.
2595 */
2596#ifdef FEAT_CMDWIN
2597 if (cmdwin_type != 0 && wp == curwin)
2598 {
2599 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002600 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 if (enc_utf8)
2602 ScreenLinesUC[off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 ++col;
2604 }
2605#endif
2606
2607 /*
2608 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002609 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002611 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 if (fdc > 0)
2613 {
2614 fill_foldcolumn(buf, wp, TRUE, lnum);
2615#ifdef FEAT_RIGHTLEFT
2616 if (wp->w_p_rl)
2617 {
2618 int i;
2619
Bram Moolenaar02631462017-09-22 15:20:32 +02002620 copy_text_attr(off + wp->w_width - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002621 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 /* reverse the fold column */
2623 for (i = 0; i < fdc; ++i)
Bram Moolenaar02631462017-09-22 15:20:32 +02002624 ScreenLines[off + wp->w_width - i - 1 - col] = buf[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 }
2626 else
2627#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002628 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 col += fdc;
2630 }
2631
2632#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6f470022018-04-10 18:47:20 +02002633# define RL_MEMSET(p, v, l) \
2634 do { \
2635 if (wp->w_p_rl) \
2636 for (ri = 0; ri < l; ++ri) \
2637 ScreenAttrs[off + (wp->w_width - (p) - (l)) + ri] = v; \
2638 else \
2639 for (ri = 0; ri < l; ++ri) \
2640 ScreenAttrs[off + (p) + ri] = v; \
2641 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642#else
Bram Moolenaar6f470022018-04-10 18:47:20 +02002643# define RL_MEMSET(p, v, l) \
2644 do { \
2645 for (ri = 0; ri < l; ++ri) \
2646 ScreenAttrs[off + (p) + ri] = v; \
2647 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648#endif
2649
Bram Moolenaar64486672010-05-16 15:46:46 +02002650 /* Set all attributes of the 'number' or 'relativenumber' column and the
2651 * text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002652 RL_MEMSET(col, HL_ATTR(HLF_FL), wp->w_width - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653
2654#ifdef FEAT_SIGNS
2655 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002656 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002658 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 if (len > 0)
2660 {
2661 if (len > 2)
2662 len = 2;
2663# ifdef FEAT_RIGHTLEFT
2664 if (wp->w_p_rl)
2665 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002666 copy_text_attr(off + wp->w_width - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002667 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 else
2669# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002670 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 col += len;
2672 }
2673 }
2674#endif
2675
2676 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002677 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002679 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002681 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 if (len > 0)
2683 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002684 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002685 long num;
2686 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002687
2688 if (len > w + 1)
2689 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002690
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002691 if (wp->w_p_nu && !wp->w_p_rnu)
2692 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002693 num = (long)lnum;
2694 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002695 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002696 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002697 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002698 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002699 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002700 /* 'number' + 'relativenumber': cursor line shows absolute
2701 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002702 num = lnum;
2703 fmt = "%-*ld ";
2704 }
2705 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002706
Bram Moolenaar700e7342013-01-30 12:31:36 +01002707 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708#ifdef FEAT_RIGHTLEFT
2709 if (wp->w_p_rl)
2710 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002711 copy_text_attr(off + wp->w_width - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002712 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 else
2714#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002715 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 col += len;
2717 }
2718 }
2719
2720 /*
2721 * 4. Compose the folded-line string with 'foldtext', if set.
2722 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002723 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724
2725 txtcol = col; /* remember where text starts */
2726
2727 /*
2728 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2729 * Right-left text is put in columns 0 - number-col, normal text is put
2730 * in columns number-col - window-width.
2731 */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002732 col = text_to_screenline(wp, text, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733
2734 /* Fill the rest of the line with the fold filler */
2735#ifdef FEAT_RIGHTLEFT
2736 if (wp->w_p_rl)
2737 col -= txtcol;
2738#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02002739 while (col < wp->w_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740#ifdef FEAT_RIGHTLEFT
2741 - (wp->w_p_rl ? txtcol : 0)
2742#endif
2743 )
2744 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 if (enc_utf8)
2746 {
2747 if (fill_fold >= 0x80)
2748 {
2749 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002750 ScreenLinesC[0][off + col] = 0;
Bram Moolenaaracda04f2018-02-08 09:57:28 +01002751 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 }
2753 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002754 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002756 ScreenLines[off + col] = fill_fold;
2757 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002758 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002760 else
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002761 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 }
2763
2764 if (text != buf)
2765 vim_free(text);
2766
2767 /*
2768 * 6. set highlighting for the Visual area an other text.
2769 * If all folded lines are in the Visual area, highlight the line.
2770 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2772 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002773 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 {
2775 /* Visual is after curwin->w_cursor */
2776 top = &curwin->w_cursor;
2777 bot = &VIsual;
2778 }
2779 else
2780 {
2781 /* Visual is before curwin->w_cursor */
2782 top = &VIsual;
2783 bot = &curwin->w_cursor;
2784 }
2785 if (lnum >= top->lnum
2786 && lnume <= bot->lnum
2787 && (VIsual_mode != 'v'
2788 || ((lnum > top->lnum
2789 || (lnum == top->lnum
2790 && top->col == 0))
2791 && (lnume < bot->lnum
2792 || (lnume == bot->lnum
2793 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002794 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 {
2796 if (VIsual_mode == Ctrl_V)
2797 {
2798 /* Visual block mode: highlight the chars part of the block */
Bram Moolenaar02631462017-09-22 15:20:32 +02002799 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002801 if (wp->w_old_cursor_lcol != MAXCOL
2802 && wp->w_old_cursor_lcol + txtcol
Bram Moolenaar02631462017-09-22 15:20:32 +02002803 < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 len = wp->w_old_cursor_lcol;
2805 else
Bram Moolenaar02631462017-09-22 15:20:32 +02002806 len = wp->w_width - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002807 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002808 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 }
2810 }
2811 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002812 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 /* Set all attributes of the text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002814 RL_MEMSET(txtcol, HL_ATTR(HLF_V), wp->w_width - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 }
2817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002819#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002820 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2821 if (wp->w_p_cc_cols)
2822 {
2823 int i = 0;
2824 int j = wp->w_p_cc_cols[i];
2825 int old_txtcol = txtcol;
2826
2827 while (j > -1)
2828 {
2829 txtcol += j;
2830 if (wp->w_p_wrap)
2831 txtcol -= wp->w_skipcol;
2832 else
2833 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002834 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002835 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002836 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002837 txtcol = old_txtcol;
2838 j = wp->w_p_cc_cols[++i];
2839 }
2840 }
2841
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002842 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002843 if (wp->w_p_cuc)
2844 {
2845 txtcol += wp->w_virtcol;
2846 if (wp->w_p_wrap)
2847 txtcol -= wp->w_skipcol;
2848 else
2849 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002850 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaar85595c52008-10-02 16:04:05 +00002851 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002852 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00002853 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002854#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855
Bram Moolenaar02631462017-09-22 15:20:32 +02002856 screen_line(row + W_WINROW(wp), wp->w_wincol, (int)wp->w_width,
2857 (int)wp->w_width, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858
2859 /*
2860 * Update w_cline_height and w_cline_folded if the cursor line was
2861 * updated (saves a call to plines() later).
2862 */
2863 if (wp == curwin
2864 && lnum <= curwin->w_cursor.lnum
2865 && lnume >= curwin->w_cursor.lnum)
2866 {
2867 curwin->w_cline_row = row;
2868 curwin->w_cline_height = 1;
2869 curwin->w_cline_folded = TRUE;
2870 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2871 }
2872}
2873
2874/*
2875 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2876 */
2877 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002878copy_text_attr(
2879 int off,
2880 char_u *buf,
2881 int len,
2882 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002884 int i;
2885
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 mch_memmove(ScreenLines + off, buf, (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 if (enc_utf8)
2888 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002889 for (i = 0; i < len; ++i)
2890 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891}
2892
2893/*
2894 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002895 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 */
2897 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002898fill_foldcolumn(
2899 char_u *p,
2900 win_T *wp,
2901 int closed, /* TRUE of FALSE */
2902 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903{
2904 int i = 0;
2905 int level;
2906 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002907 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002908 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909
2910 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002911 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912
2913 level = win_foldinfo.fi_level;
2914 if (level > 0)
2915 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002916 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002917 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002918
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 /* If the column is too narrow, we start at the lowest level that
2920 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002921 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 if (first_level < 1)
2923 first_level = 1;
2924
Bram Moolenaar1c934292015-01-27 16:39:29 +01002925 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 {
2927 if (win_foldinfo.fi_lnum == lnum
2928 && first_level + i >= win_foldinfo.fi_low_level)
2929 p[i] = '-';
2930 else if (first_level == 1)
2931 p[i] = '|';
2932 else if (first_level + i <= 9)
2933 p[i] = '0' + first_level + i;
2934 else
2935 p[i] = '>';
2936 if (first_level + i == level)
2937 break;
2938 }
2939 }
2940 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002941 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942}
2943#endif /* FEAT_FOLDING */
2944
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01002945#ifdef FEAT_TEXT_PROP
2946static textprop_T *current_text_props = NULL;
2947static buf_T *current_buf = NULL;
2948
2949 static int
2950#ifdef __BORLANDC__
2951_RTLENTRYF
2952#endif
2953text_prop_compare(const void *s1, const void *s2)
2954{
2955 int idx1, idx2;
2956 proptype_T *pt1, *pt2;
2957 colnr_T col1, col2;
2958
2959 idx1 = *(int *)s1;
2960 idx2 = *(int *)s2;
2961 pt1 = text_prop_type_by_id(current_buf, current_text_props[idx1].tp_type);
2962 pt2 = text_prop_type_by_id(current_buf, current_text_props[idx2].tp_type);
2963 if (pt1 == pt2)
2964 return 0;
2965 if (pt1 == NULL)
2966 return -1;
2967 if (pt2 == NULL)
2968 return 1;
2969 if (pt1->pt_priority != pt2->pt_priority)
2970 return pt1->pt_priority > pt2->pt_priority ? 1 : -1;
2971 col1 = current_text_props[idx1].tp_col;
2972 col2 = current_text_props[idx2].tp_col;
2973 return col1 == col2 ? 0 : col1 > col2 ? 1 : -1;
2974}
2975#endif
2976
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977/*
2978 * Display line "lnum" of window 'wp' on the screen.
2979 * Start at row "startrow", stop when "endrow" is reached.
2980 * wp->w_virtcol needs to be valid.
2981 *
2982 * Return the number of last row the line occupies.
2983 */
2984 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002985win_line(
2986 win_T *wp,
2987 linenr_T lnum,
2988 int startrow,
2989 int endrow,
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002990 int nochange UNUSED, // not updating for changed text
2991 int number_only) // only update the number column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992{
Bram Moolenaar04e87b72017-02-01 21:23:10 +01002993 int col = 0; /* visual column on screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2995 int c = 0; /* init for GCC */
2996 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01002997#ifdef FEAT_LINEBREAK
2998 long vcol_sbr = -1; /* virtual column after showbreak */
2999#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000 long vcol_prev = -1; /* "vcol" of previous character */
3001 char_u *line; /* current line */
3002 char_u *ptr; /* current position in "line" */
3003 int row; /* row in the window, excl w_winrow */
3004 int screen_row; /* row on the screen, incl w_winrow */
3005
3006 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
3007 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00003008 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02003009 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 int c_extra = NUL; /* extra chars, all the same */
Bram Moolenaar83a52172019-01-16 22:41:54 +01003011 int c_final = NUL; /* final char, mandatory if set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 int extra_attr = 0; /* attributes when n_extra != 0 */
3013 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
3014 displaying lcs_eol at end-of-line */
3015 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
3016 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
3017
3018 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
3019 int saved_n_extra = 0;
3020 char_u *saved_p_extra = NULL;
3021 int saved_c_extra = 0;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003022 int saved_c_final = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023 int saved_char_attr = 0;
3024
3025 int n_attr = 0; /* chars with special attr */
3026 int saved_attr2 = 0; /* char_attr saved for n_attr */
3027 int n_attr3 = 0; /* chars with overruling special attr */
3028 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
3029
3030 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
3031
3032 int fromcol, tocol; /* start/end of inverting */
3033 int fromcol_prev = -2; /* start of inverting after cursor */
3034 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003036 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 pos_T pos;
3038 long v;
3039
3040 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003041 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 int area_highlighting = FALSE; /* Visual or incsearch highlighting
3043 in this line */
3044 int attr = 0; /* attributes for area highlighting */
3045 int area_attr = 0; /* attributes desired by highlighting */
3046 int search_attr = 0; /* attributes desired by 'hlsearch' */
3047#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003048 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 int syntax_attr = 0; /* attributes desired by syntax */
3050 int has_syntax = FALSE; /* this buffer has syntax highl. */
3051 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00003052 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02003053 int draw_color_col = FALSE; /* highlight colorcolumn */
3054 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003055#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003056#ifdef FEAT_TEXT_PROP
3057 int text_prop_count;
3058 int text_prop_next = 0; // next text property to use
3059 textprop_T *text_props = NULL;
3060 int *text_prop_idxs = NULL;
3061 int text_props_active = 0;
3062 proptype_T *text_prop_type = NULL;
3063 int text_prop_attr = 0;
3064#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003065#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003066 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003067# define SPWORDLEN 150
3068 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003069 int nextlinecol = 0; /* column where nextline[] starts */
3070 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003071 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003072 int spell_attr = 0; /* attributes desired by spelling */
3073 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003074 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3075 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003076 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003077 static int cap_col = -1; /* column to check for Cap word */
3078 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003079 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003081 int extra_check = 0; // has extra highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 int multi_attr = 0; /* attributes desired by multibyte */
3083 int mb_l = 1; /* multi-byte byte length */
3084 int mb_c = 0; /* decoded multi-byte character */
3085 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003086 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087#ifdef FEAT_DIFF
3088 int filler_lines; /* nr of filler lines to be drawn */
3089 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003090 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 int change_start = MAXCOL; /* first col of changed area */
3092 int change_end = -1; /* last col of changed area */
3093#endif
3094 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3095#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003096 int need_showbreak = FALSE; /* overlong line, skipping first x
3097 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003099#if defined(FEAT_SIGNS) || defined(FEAT_QUICKFIX) \
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003100 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003102 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103#endif
3104#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003105 matchitem_T *cur; /* points to the match list */
3106 match_T *shl; /* points to search_hl or a match */
3107 int shl_flag; /* flag to indicate whether search_hl
3108 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003109 int pos_inprogress; /* marks that position match search is
3110 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003111 int prevcol_hl_flag; /* flag to indicate whether prevcol
3112 equals startcol of search_hl or one
3113 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114#endif
3115#ifdef FEAT_ARABIC
3116 int prev_c = 0; /* previous Arabic character */
3117 int prev_c1 = 0; /* first composing char for prev_c */
3118#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003119#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003120 int did_line_attr = 0;
3121#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003122#ifdef FEAT_TERMINAL
3123 int get_term_attr = FALSE;
Bram Moolenaar238d43b2017-09-11 22:00:51 +02003124 int term_attr = 0; /* background for terminal window */
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003125#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126
3127 /* draw_state: items that are drawn in sequence: */
3128#define WL_START 0 /* nothing done yet */
3129#ifdef FEAT_CMDWIN
3130# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3131#else
3132# define WL_CMDLINE WL_START
3133#endif
3134#ifdef FEAT_FOLDING
3135# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3136#else
3137# define WL_FOLD WL_CMDLINE
3138#endif
3139#ifdef FEAT_SIGNS
3140# define WL_SIGN WL_FOLD + 1 /* column for signs */
3141#else
3142# define WL_SIGN WL_FOLD /* column for signs */
3143#endif
3144#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003145#ifdef FEAT_LINEBREAK
3146# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003148# define WL_BRI WL_NR
3149#endif
3150#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3151# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3152#else
3153# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154#endif
3155#define WL_LINE WL_SBR + 1 /* text in the line */
3156 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003157#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 int feedback_col = 0;
3159 int feedback_old_attr = -1;
3160#endif
3161
Bram Moolenaar860cae12010-06-05 23:22:07 +02003162#ifdef FEAT_CONCEAL
3163 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003164 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003165 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003166 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003167 int is_concealing = FALSE;
3168 int boguscols = 0; /* nonexistent columns added to force
3169 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003170 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003171 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003172 int match_conc = 0; /* cchar for match functions */
3173 int has_match_conc = 0; /* match wants to conceal */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003174 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003175# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003176# define FIX_FOR_BOGUSCOLS \
3177 { \
3178 n_extra += vcol_off; \
3179 vcol -= vcol_off; \
3180 vcol_off = 0; \
3181 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003182 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003183 boguscols = 0; \
3184 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003185#else
3186# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188
3189 if (startrow > endrow) /* past the end already! */
3190 return startrow;
3191
3192 row = startrow;
3193 screen_row = row + W_WINROW(wp);
3194
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003195 if (!number_only)
3196 {
3197 /*
3198 * To speed up the loop below, set extra_check when there is linebreak,
3199 * trailing white space and/or syntax processing to be done.
3200 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201#ifdef FEAT_LINEBREAK
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003202 extra_check = wp->w_p_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203#endif
3204#ifdef FEAT_SYN_HL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003205 if (syntax_present(wp) && !wp->w_s->b_syn_error
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003206# ifdef SYN_TIME_LIMIT
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003207 && !wp->w_s->b_syn_slow
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003208# endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003209 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003211 /* Prepare for syntax highlighting in this line. When there is an
3212 * error, stop syntax highlighting. */
3213 save_did_emsg = did_emsg;
3214 did_emsg = FALSE;
3215 syntax_start(wp, lnum);
3216 if (did_emsg)
3217 wp->w_s->b_syn_error = TRUE;
3218 else
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003219 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003220 did_emsg = save_did_emsg;
3221#ifdef SYN_TIME_LIMIT
3222 if (!wp->w_s->b_syn_slow)
3223#endif
3224 {
3225 has_syntax = TRUE;
3226 extra_check = TRUE;
3227 }
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003228 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003230
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003231 /* Check for columns to display for 'colorcolumn'. */
3232 color_cols = wp->w_p_cc_cols;
3233 if (color_cols != NULL)
3234 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003235#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003236
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003237#ifdef FEAT_TERMINAL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003238 if (term_show_buffer(wp->w_buffer))
3239 {
3240 extra_check = TRUE;
3241 get_term_attr = TRUE;
3242 term_attr = term_get_attr(wp->w_buffer, lnum, -1);
3243 }
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003244#endif
3245
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003246#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003247 if (wp->w_p_spell
3248 && *wp->w_s->b_p_spl != NUL
3249 && wp->w_s->b_langp.ga_len > 0
3250 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003251 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003252 /* Prepare for spell checking. */
3253 has_spell = TRUE;
3254 extra_check = TRUE;
3255
Bram Moolenaar7701f302018-10-02 21:20:32 +02003256 /* Get the start of the next line, so that words that wrap to the
3257 * next line are found too: "et<line-break>al.".
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003258 * Trick: skip a few chars for C/shell/Vim comments */
3259 nextline[SPWORDLEN] = NUL;
3260 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3261 {
3262 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3263 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3264 }
3265
Bram Moolenaar7701f302018-10-02 21:20:32 +02003266 /* When a word wrapped from the previous line the start of the
3267 * current line is valid. */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003268 if (lnum == checked_lnum)
3269 cur_checked_col = checked_col;
3270 checked_lnum = 0;
3271
3272 /* When there was a sentence end in the previous line may require a
3273 * word starting with capital in this line. In line 1 always check
3274 * the first word. */
3275 if (lnum != capcol_lnum)
3276 cap_col = -1;
3277 if (lnum == 1)
3278 cap_col = 0;
3279 capcol_lnum = 0;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281#endif
3282
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003283 /*
3284 * handle visual active in this window
3285 */
3286 fromcol = -10;
3287 tocol = MAXCOL;
3288 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003290 /* Visual is after curwin->w_cursor */
3291 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003293 top = &curwin->w_cursor;
3294 bot = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 }
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003296 else /* Visual is before curwin->w_cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003298 top = &VIsual;
3299 bot = &curwin->w_cursor;
3300 }
3301 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
3302 if (VIsual_mode == Ctrl_V) /* block mode */
3303 {
3304 if (lnum_in_visual_area)
3305 {
3306 fromcol = wp->w_old_cursor_fcol;
3307 tocol = wp->w_old_cursor_lcol;
3308 }
3309 }
3310 else /* non-block mode */
3311 {
3312 if (lnum > top->lnum && lnum <= bot->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 fromcol = 0;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003314 else if (lnum == top->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003316 if (VIsual_mode == 'V') /* linewise */
3317 fromcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 else
3319 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003320 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3321 if (gchar_pos(top) == NUL)
3322 tocol = fromcol + 1;
3323 }
3324 }
3325 if (VIsual_mode != 'V' && lnum == bot->lnum)
3326 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003327 if (*p_sel == 'e' && bot->col == 0 && bot->coladd == 0)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003328 {
3329 fromcol = -10;
3330 tocol = MAXCOL;
3331 }
3332 else if (bot->col == MAXCOL)
3333 tocol = MAXCOL;
3334 else
3335 {
3336 pos = *bot;
3337 if (*p_sel == 'e')
3338 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3339 else
3340 {
3341 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3342 ++tocol;
3343 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 }
3345 }
3346 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003348 /* Check if the character under the cursor should not be inverted */
3349 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003350#ifdef FEAT_GUI
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003351 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003352#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003353 )
3354 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003356 /* if inverting in this line set area_highlighting */
3357 if (fromcol >= 0)
3358 {
3359 area_highlighting = TRUE;
3360 attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003362 if ((clip_star.available && !clip_star.owned
3363 && clip_isautosel_star())
3364 || (clip_plus.available && !clip_plus.owned
3365 && clip_isautosel_plus()))
3366 attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003371 /*
3372 * handle 'incsearch' and ":s///c" highlighting
3373 */
3374 else if (highlight_match
3375 && wp == curwin
3376 && lnum >= curwin->w_cursor.lnum
3377 && lnum <= curwin->w_cursor.lnum + search_match_lines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003379 if (lnum == curwin->w_cursor.lnum)
3380 getvcol(curwin, &(curwin->w_cursor),
Bram Moolenaarc6663882019-02-22 19:14:54 +01003381 (colnr_T *)&fromcol, NULL, NULL);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003382 else
3383 fromcol = 0;
3384 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3385 {
3386 pos.lnum = lnum;
3387 pos.col = search_match_endcol;
3388 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3389 }
3390 else
3391 tocol = MAXCOL;
3392 /* do at least one character; happens when past end of line */
3393 if (fromcol == tocol)
3394 tocol = fromcol + 1;
3395 area_highlighting = TRUE;
3396 attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 }
3399
3400#ifdef FEAT_DIFF
3401 filler_lines = diff_check(wp, lnum);
3402 if (filler_lines < 0)
3403 {
3404 if (filler_lines == -1)
3405 {
3406 if (diff_find_change(wp, lnum, &change_start, &change_end))
3407 diff_hlf = HLF_ADD; /* added line */
3408 else if (change_start == 0)
3409 diff_hlf = HLF_TXD; /* changed text */
3410 else
3411 diff_hlf = HLF_CHD; /* changed line */
3412 }
3413 else
3414 diff_hlf = HLF_ADD; /* added line */
3415 filler_lines = 0;
3416 area_highlighting = TRUE;
3417 }
3418 if (lnum == wp->w_topline)
3419 filler_lines = wp->w_topfill;
3420 filler_todo = filler_lines;
3421#endif
3422
3423#ifdef LINE_ATTR
3424# ifdef FEAT_SIGNS
3425 /* If this line has a sign with line highlighting set line_attr. */
3426 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3427 if (v != 0)
3428 line_attr = sign_get_attr((int)v, TRUE);
3429# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003430# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003432 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003433 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434# endif
3435 if (line_attr != 0)
3436 area_highlighting = TRUE;
3437#endif
3438
3439 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3440 ptr = line;
3441
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003442#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003443 if (has_spell && !number_only)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003444 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003445 /* For checking first word with a capital skip white space. */
3446 if (cap_col == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003447 cap_col = getwhitecols(line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003448
Bram Moolenaar30abd282005-06-22 22:35:10 +00003449 /* To be able to spell-check over line boundaries copy the end of the
3450 * current line into nextline[]. Above the start of the next line was
3451 * copied to nextline[SPWORDLEN]. */
3452 if (nextline[SPWORDLEN] == NUL)
3453 {
3454 /* No next line or it is empty. */
3455 nextlinecol = MAXCOL;
3456 nextline_idx = 0;
3457 }
3458 else
3459 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003460 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003461 if (v < SPWORDLEN)
3462 {
3463 /* Short line, use it completely and append the start of the
3464 * next line. */
3465 nextlinecol = 0;
3466 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003467 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003468 nextline_idx = v + 1;
3469 }
3470 else
3471 {
3472 /* Long line, use only the last SPWORDLEN bytes. */
3473 nextlinecol = v - SPWORDLEN;
3474 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3475 nextline_idx = SPWORDLEN + 1;
3476 }
3477 }
3478 }
3479#endif
3480
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003481 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 {
Bram Moolenaar895d9662019-01-31 21:57:21 +01003483 if (lcs_space || lcs_trail || lcs_nbsp)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003484 extra_check = TRUE;
3485 /* find start of trailing whitespace */
3486 if (lcs_trail)
3487 {
3488 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003489 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003490 --trailcol;
3491 trailcol += (colnr_T) (ptr - line);
3492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 }
3494
3495 /*
3496 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3497 * first character to be displayed.
3498 */
3499 if (wp->w_p_wrap)
3500 v = wp->w_skipcol;
3501 else
3502 v = wp->w_leftcol;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003503 if (v > 0 && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 char_u *prev_ptr = ptr;
Bram Moolenaara12a1612019-01-24 16:39:02 +01003506
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 while (vcol < v && *ptr != NUL)
3508 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003509 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 vcol += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 prev_ptr = ptr;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003512 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 }
3514
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003515 /* When:
3516 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003517 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003518 * - 'virtualedit' is set, or
3519 * - the visual mode is active,
3520 * the end of the line may be before the start of the displayed part.
3521 */
3522 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003523#ifdef FEAT_SYN_HL
3524 wp->w_p_cuc || draw_color_col ||
3525#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003526 virtual_active() ||
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003527 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003528 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003530 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531
3532 /* Handle a character that's not completely on the screen: Put ptr at
3533 * that character but skip the first few screen characters. */
3534 if (vcol > v)
3535 {
3536 vcol -= c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 ptr = prev_ptr;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003538 /* If the character fits on the screen, don't need to skip it.
3539 * Except for a TAB. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01003540 if (( (*mb_ptr2cells)(ptr) >= c || *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003541 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 }
3543
3544 /*
3545 * Adjust for when the inverted text is before the screen,
3546 * and when the start of the inverted text is before the screen.
3547 */
3548 if (tocol <= vcol)
3549 fromcol = 0;
3550 else if (fromcol >= 0 && fromcol < vcol)
3551 fromcol = vcol;
3552
3553#ifdef FEAT_LINEBREAK
3554 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3555 if (wp->w_p_wrap)
3556 need_showbreak = TRUE;
3557#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003558#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003559 /* When spell checking a word we need to figure out the start of the
3560 * word and if it's badly spelled or not. */
3561 if (has_spell)
3562 {
3563 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003564 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003565 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003566
3567 pos = wp->w_cursor;
3568 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003569 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003570 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003571
3572 /* spell_move_to() may call ml_get() and make "line" invalid */
3573 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3574 ptr = line + linecol;
3575
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003576 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003577 {
3578 /* no bad word found at line start, don't check until end of a
3579 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003580 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003581 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003582 }
3583 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003584 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003585 /* bad word found, use attributes until end of word */
3586 word_end = wp->w_cursor.col + len + 1;
3587
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003588 /* Turn index into actual attributes. */
3589 if (spell_hlf != HLF_COUNT)
3590 spell_attr = highlight_attr[spell_hlf];
3591 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003592 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003593
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003594# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003595 /* Need to restart syntax highlighting for this line. */
3596 if (has_syntax)
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003597 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003598# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003599 }
3600#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 }
3602
3603 /*
3604 * Correct highlighting for cursor that can't be disabled.
3605 * Avoids having to check this for each character.
3606 */
3607 if (fromcol >= 0)
3608 {
3609 if (noinvcur)
3610 {
3611 if ((colnr_T)fromcol == wp->w_virtcol)
3612 {
3613 /* highlighting starts at cursor, let it start just after the
3614 * cursor */
3615 fromcol_prev = fromcol;
3616 fromcol = -1;
3617 }
3618 else if ((colnr_T)fromcol < wp->w_virtcol)
3619 /* restart highlighting after the cursor */
3620 fromcol_prev = wp->w_virtcol;
3621 }
3622 if (fromcol >= tocol)
3623 fromcol = -1;
3624 }
3625
3626#ifdef FEAT_SEARCH_EXTRA
3627 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003628 * Handle highlighting the last used search pattern and matches.
3629 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003631 cur = wp->w_match_head;
3632 shl_flag = FALSE;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003633 while ((cur != NULL || shl_flag == FALSE) && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003635 if (shl_flag == FALSE)
3636 {
3637 shl = &search_hl;
3638 shl_flag = TRUE;
3639 }
3640 else
3641 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003642 shl->startcol = MAXCOL;
3643 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003645 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003646 v = (long)(ptr - line);
3647 if (cur != NULL)
3648 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003649 next_search_hl(wp, shl, lnum, (colnr_T)v,
3650 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003651
3652 /* Need to get the line again, a multi-line regexp may have made it
3653 * invalid. */
3654 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3655 ptr = line + v;
3656
3657 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003659 if (shl->lnum == lnum)
3660 shl->startcol = shl->rm.startpos[0].col;
3661 else
3662 shl->startcol = 0;
3663 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3664 - shl->rm.startpos[0].lnum)
3665 shl->endcol = shl->rm.endpos[0].col;
3666 else
3667 shl->endcol = MAXCOL;
3668 /* Highlight one character for an empty match. */
3669 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003671 if (has_mbyte && line[shl->endcol] != NUL)
3672 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3673 else
Bram Moolenaarb3414592014-06-17 17:48:32 +02003674 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003676 if ((long)shl->startcol < v) /* match at leftcol */
3677 {
3678 shl->attr_cur = shl->attr;
3679 search_attr = shl->attr;
3680 }
3681 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003683 if (shl != &search_hl && cur != NULL)
3684 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 }
3686#endif
3687
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003688#ifdef FEAT_SYN_HL
Bram Moolenaar8156ed32019-03-09 11:46:15 +01003689 // Cursor line highlighting for 'cursorline' in the current window.
3690 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003691 {
Bram Moolenaar8156ed32019-03-09 11:46:15 +01003692 // Do not show the cursor line when Visual mode is active, because it's
3693 // not clear what is selected then. Do update w_last_cursorline.
3694 if (!(wp == curwin && VIsual_active))
3695 {
3696 line_attr = HL_ATTR(HLF_CUL);
3697 area_highlighting = TRUE;
3698 }
Bram Moolenaarc07ff5c2019-01-30 21:41:14 +01003699 wp->w_last_cursorline = wp->w_cursor.lnum;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003700 }
3701#endif
3702
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003703#ifdef FEAT_TEXT_PROP
3704 {
3705 char_u *prop_start;
3706
3707 text_prop_count = get_text_props(wp->w_buffer, lnum,
3708 &prop_start, FALSE);
3709 if (text_prop_count > 0)
3710 {
3711 // Make a copy of the properties, so that they are properly
3712 // aligned.
3713 text_props = (textprop_T *)alloc(
3714 text_prop_count * sizeof(textprop_T));
3715 if (text_props != NULL)
3716 mch_memmove(text_props, prop_start,
3717 text_prop_count * sizeof(textprop_T));
3718
3719 // Allocate an array for the indexes.
3720 text_prop_idxs = (int *)alloc(text_prop_count * sizeof(int));
3721 area_highlighting = TRUE;
3722 extra_check = TRUE;
3723 }
3724 }
3725#endif
3726
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003727 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 col = 0;
3729#ifdef FEAT_RIGHTLEFT
3730 if (wp->w_p_rl)
3731 {
3732 /* Rightleft window: process the text in the normal direction, but put
3733 * it in current_ScreenLine[] from right to left. Start at the
3734 * rightmost column of the window. */
Bram Moolenaar02631462017-09-22 15:20:32 +02003735 col = wp->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 off += col;
3737 }
3738#endif
3739
3740 /*
3741 * Repeat for the whole displayed line.
3742 */
3743 for (;;)
3744 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003745#ifdef FEAT_CONCEAL
Bram Moolenaar4d585022016-04-14 19:50:22 +02003746 has_match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003747#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 /* Skip this quickly when working on the text. */
3749 if (draw_state != WL_LINE)
3750 {
3751#ifdef FEAT_CMDWIN
3752 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3753 {
3754 draw_state = WL_CMDLINE;
3755 if (cmdwin_type != 0 && wp == curwin)
3756 {
3757 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003759 c_extra = cmdwin_type;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003760 c_final = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003761 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 }
3763 }
3764#endif
3765
3766#ifdef FEAT_FOLDING
3767 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3768 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003769 int fdc = compute_foldcolumn(wp, 0);
3770
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003772 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003774 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003775 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003776 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003777 p_extra_free = alloc(12 + 1);
3778
3779 if (p_extra_free != NULL)
3780 {
3781 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3782 n_extra = fdc;
3783 p_extra_free[n_extra] = NUL;
3784 p_extra = p_extra_free;
3785 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003786 c_final = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003787 char_attr = HL_ATTR(HLF_FC);
Bram Moolenaar62706602016-12-09 19:28:48 +01003788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 }
3790 }
3791#endif
3792
3793#ifdef FEAT_SIGNS
3794 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3795 {
3796 draw_state = WL_SIGN;
3797 /* Show the sign column when there are any signs in this
3798 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003799 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003801 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003803 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804# endif
3805
3806 /* Draw two cells with the sign value or blank. */
3807 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01003808 c_final = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003809 char_attr = HL_ATTR(HLF_SC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 n_extra = 2;
3811
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003812 if (row == startrow
3813#ifdef FEAT_DIFF
3814 + filler_lines && filler_todo <= 0
3815#endif
3816 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 {
3818 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3819 SIGN_TEXT);
3820# ifdef FEAT_SIGN_ICONS
3821 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3822 SIGN_ICON);
3823 if (gui.in_use && icon_sign != 0)
3824 {
3825 /* Use the image in this position. */
3826 c_extra = SIGN_BYTE;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003827 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828# ifdef FEAT_NETBEANS_INTG
3829 if (buf_signcount(wp->w_buffer, lnum) > 1)
Bram Moolenaar83a52172019-01-16 22:41:54 +01003830 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 c_extra = MULTISIGN_BYTE;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003832 c_final = NUL;
3833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834# endif
3835 char_attr = icon_sign;
3836 }
3837 else
3838# endif
3839 if (text_sign != 0)
3840 {
3841 p_extra = sign_get_text(text_sign);
3842 if (p_extra != NULL)
3843 {
3844 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003845 c_final = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003846 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 }
3848 char_attr = sign_get_attr(text_sign, FALSE);
3849 }
3850 }
3851 }
3852 }
3853#endif
3854
3855 if (draw_state == WL_NR - 1 && n_extra == 0)
3856 {
3857 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003858 /* Display the absolute or relative line number. After the
3859 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3860 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 && (row == startrow
3862#ifdef FEAT_DIFF
3863 + filler_lines
3864#endif
3865 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3866 {
3867 /* Draw the line number (empty space after wrapping). */
3868 if (row == startrow
3869#ifdef FEAT_DIFF
3870 + filler_lines
3871#endif
3872 )
3873 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003874 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003875 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003876
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003877 if (wp->w_p_nu && !wp->w_p_rnu)
3878 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003879 num = (long)lnum;
3880 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003881 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003882 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003883 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003884 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003885 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003886 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003887 num = lnum;
3888 fmt = "%-*ld ";
3889 }
3890 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003891
Bram Moolenaar700e7342013-01-30 12:31:36 +01003892 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003893 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 if (wp->w_skipcol > 0)
3895 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3896 *p_extra = '-';
3897#ifdef FEAT_RIGHTLEFT
3898 if (wp->w_p_rl) /* reverse line numbers */
Bram Moolenaare73f9112019-03-29 18:29:54 +01003899 {
3900 char_u *p1, *p2;
3901 int t;
3902
3903 // like rl_mirror(), but keep the space at the end
3904 p2 = skiptowhite(extra) - 1;
3905 for (p1 = extra; p1 < p2; ++p1, --p2)
3906 {
3907 t = *p1;
3908 *p1 = *p2;
3909 *p2 = t;
3910 }
3911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912#endif
3913 p_extra = extra;
3914 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003915 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 }
3917 else
Bram Moolenaar83a52172019-01-16 22:41:54 +01003918 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01003920 c_final = NUL;
3921 }
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003922 n_extra = number_width(wp) + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003923 char_attr = HL_ATTR(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003924#ifdef FEAT_SYN_HL
3925 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003926 * the current line differently.
3927 * TODO: Can we use CursorLine instead of CursorLineNr
3928 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003929 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003930 && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003931 char_attr = HL_ATTR(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003932#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 }
3934 }
3935
Bram Moolenaar597a4222014-06-25 14:39:50 +02003936#ifdef FEAT_LINEBREAK
3937 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
3938 && n_extra == 0 && *p_sbr != NUL)
3939 /* draw indent after showbreak value */
3940 draw_state = WL_BRI;
3941 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
3942 /* After the showbreak, draw the breakindent */
3943 draw_state = WL_BRI - 1;
3944
3945 /* draw 'breakindent': indent wrapped text accordingly */
3946 if (draw_state == WL_BRI - 1 && n_extra == 0)
3947 {
3948 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01003949 /* if need_showbreak is set, breakindent also applies */
3950 if (wp->w_p_bri && n_extra == 0
3951 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003952# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003953 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003954# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02003955 )
3956 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01003957 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003958# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003959 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003960 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003961 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003962# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02003963 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3964 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01003965 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003966# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02003967 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003968# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02003969 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003970 c_extra = ' ';
3971 n_extra = get_breakindent_win(wp,
3972 ml_get_buf(wp->w_buffer, lnum, FALSE));
3973 /* Correct end of highlighted area for 'breakindent',
3974 * required when 'linebreak' is also set. */
3975 if (tocol == vcol)
3976 tocol += n_extra;
3977 }
3978 }
3979#endif
3980
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3982 if (draw_state == WL_SBR - 1 && n_extra == 0)
3983 {
3984 draw_state = WL_SBR;
3985# ifdef FEAT_DIFF
3986 if (filler_todo > 0)
3987 {
3988 /* Draw "deleted" diff line(s). */
3989 if (char2cells(fill_diff) > 1)
Bram Moolenaar83a52172019-01-16 22:41:54 +01003990 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 c_extra = '-';
Bram Moolenaar83a52172019-01-16 22:41:54 +01003992 c_final = NUL;
3993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 else
Bram Moolenaar83a52172019-01-16 22:41:54 +01003995 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 c_extra = fill_diff;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003997 c_final = NUL;
3998 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999# ifdef FEAT_RIGHTLEFT
4000 if (wp->w_p_rl)
4001 n_extra = col + 1;
4002 else
4003# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004004 n_extra = wp->w_width - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004005 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 }
4007# endif
4008# ifdef FEAT_LINEBREAK
4009 if (*p_sbr != NUL && need_showbreak)
4010 {
4011 /* Draw 'showbreak' at the start of each broken line. */
4012 p_extra = p_sbr;
4013 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004014 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004016 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004018 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 /* Correct end of highlighted area for 'showbreak',
4020 * required when 'linebreak' is also set. */
4021 if (tocol == vcol)
4022 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004023#ifdef FEAT_SYN_HL
4024 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004025 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004026 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004027 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004028#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 }
4030# endif
4031 }
4032#endif
4033
4034 if (draw_state == WL_LINE - 1 && n_extra == 0)
4035 {
4036 draw_state = WL_LINE;
4037 if (saved_n_extra)
4038 {
4039 /* Continue item from end of wrapped line. */
4040 n_extra = saved_n_extra;
4041 c_extra = saved_c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004042 c_final = saved_c_final;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 p_extra = saved_p_extra;
4044 char_attr = saved_char_attr;
4045 }
4046 else
4047 char_attr = 0;
4048 }
4049 }
4050
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004051 // When still displaying '$' of change command, stop at cursor.
4052 // When only displaying the (relative) line number and that's done,
4053 // stop here.
4054 if ((dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004055 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056#ifdef FEAT_DIFF
4057 && filler_todo <= 0
4058#endif
4059 )
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004060 || (number_only && draw_state > WL_NR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 {
Bram Moolenaar02631462017-09-22 15:20:32 +02004062 screen_line(screen_row, wp->w_wincol, col, -(int)wp->w_width,
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02004063 HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004064 /* Pretend we have finished updating the window. Except when
4065 * 'cursorcolumn' is set. */
4066#ifdef FEAT_SYN_HL
4067 if (wp->w_p_cuc)
4068 row = wp->w_cline_row + wp->w_cline_height;
4069 else
4070#endif
4071 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 break;
4073 }
4074
Bram Moolenaar637532b2019-01-03 21:44:40 +01004075 if (draw_state == WL_LINE && (area_highlighting
4076#ifdef FEAT_SPELL
4077 || has_spell
4078#endif
4079 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 {
4081 /* handle Visual or match highlighting in this line */
4082 if (vcol == fromcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4084 && (*mb_ptr2cells)(ptr) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004086 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 && vcol < tocol))
4088 area_attr = attr; /* start highlighting */
4089 else if (area_attr != 0
4090 && (vcol == tocol
4091 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093
4094#ifdef FEAT_SEARCH_EXTRA
4095 if (!n_extra)
4096 {
4097 /*
4098 * Check for start/end of search pattern match.
4099 * After end, check for start/end of next match.
4100 * When another match, have to check for start again.
4101 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004102 * Do this for 'search_hl' and the match list (ordered by
4103 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004105 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004106 cur = wp->w_match_head;
4107 shl_flag = FALSE;
4108 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004110 if (shl_flag == FALSE
4111 && ((cur != NULL
4112 && cur->priority > SEARCH_HL_PRIORITY)
4113 || cur == NULL))
4114 {
4115 shl = &search_hl;
4116 shl_flag = TRUE;
4117 }
4118 else
4119 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004120 if (cur != NULL)
4121 cur->pos.cur = 0;
4122 pos_inprogress = TRUE;
4123 while (shl->rm.regprog != NULL
4124 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004126 if (shl->startcol != MAXCOL
4127 && v >= (long)shl->startcol
4128 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004130 int tmp_col = v + MB_PTR2LEN(ptr);
4131
4132 if (shl->endcol < tmp_col)
4133 shl->endcol = tmp_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004135#ifdef FEAT_CONCEAL
4136 if (cur != NULL && syn_name2id((char_u *)"Conceal")
4137 == cur->hlg_id)
4138 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004139 has_match_conc =
4140 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004141 match_conc = cur->conceal_char;
4142 }
4143 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004144 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004145#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004147 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 {
4149 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004150 next_search_hl(wp, shl, lnum, (colnr_T)v,
4151 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004152 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004153 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154
4155 /* Need to get the line again, a multi-line regexp
4156 * may have made it invalid. */
4157 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4158 ptr = line + v;
4159
4160 if (shl->lnum == lnum)
4161 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004162 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004164 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004166 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004168 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 {
4170 /* highlight empty match, try again after
4171 * it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004173 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004174 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004176 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 }
4178
4179 /* Loop to check if the match starts at the
4180 * current position */
4181 continue;
4182 }
4183 }
4184 break;
4185 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004186 if (shl != &search_hl && cur != NULL)
4187 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004189
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004190 /* Use attributes from match with highest priority among
4191 * 'search_hl' and the match list. */
4192 search_attr = search_hl.attr_cur;
4193 cur = wp->w_match_head;
4194 shl_flag = FALSE;
4195 while (cur != NULL || shl_flag == FALSE)
4196 {
4197 if (shl_flag == FALSE
4198 && ((cur != NULL
4199 && cur->priority > SEARCH_HL_PRIORITY)
4200 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004201 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004202 shl = &search_hl;
4203 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004204 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004205 else
4206 shl = &cur->hl;
4207 if (shl->attr_cur != 0)
4208 search_attr = shl->attr_cur;
4209 if (shl != &search_hl && cur != NULL)
4210 cur = cur->next;
4211 }
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004212 /* Only highlight one character after the last column. */
Bram Moolenaar5ece3e32017-10-01 14:35:02 +02004213 if (*ptr == NUL && (did_line_attr >= 1
4214 || (wp->w_p_list && lcs_eol_one == -1)))
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004215 search_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 }
4217#endif
4218
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004220 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004222 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4223 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004225 if (diff_hlf == HLF_TXD && ptr - line > change_end
4226 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004228 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004229 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004230 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 }
4232#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004233
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004234#ifdef FEAT_TEXT_PROP
4235 if (text_props != NULL)
4236 {
4237 int pi;
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004238 int bcol = (int)(ptr - line);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004239
4240 // Check if any active property ends.
4241 for (pi = 0; pi < text_props_active; ++pi)
4242 {
4243 int tpi = text_prop_idxs[pi];
4244
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004245 if (bcol >= text_props[tpi].tp_col - 1
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004246 + text_props[tpi].tp_len)
4247 {
4248 if (pi + 1 < text_props_active)
4249 mch_memmove(text_prop_idxs + pi,
4250 text_prop_idxs + pi + 1,
4251 sizeof(int)
4252 * (text_props_active - (pi + 1)));
4253 --text_props_active;
4254 --pi;
4255 }
4256 }
4257
4258 // Add any text property that starts in this column.
4259 while (text_prop_next < text_prop_count
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004260 && bcol >= text_props[text_prop_next].tp_col - 1)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004261 text_prop_idxs[text_props_active++] = text_prop_next++;
4262
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004263 text_prop_attr = 0;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004264 if (text_props_active > 0)
4265 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004266 // Sort the properties on priority and/or starting last.
4267 // Then combine the attributes, highest priority last.
4268 current_text_props = text_props;
4269 current_buf = wp->w_buffer;
4270 qsort((void *)text_prop_idxs, (size_t)text_props_active,
4271 sizeof(int), text_prop_compare);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004272
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004273 for (pi = 0; pi < text_props_active; ++pi)
4274 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004275 int tpi = text_prop_idxs[pi];
4276 proptype_T *pt = text_prop_type_by_id(wp->w_buffer, text_props[tpi].tp_type);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004277
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004278 if (pt != NULL)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004279 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004280 int pt_attr = syn_id2attr(pt->pt_hl_id);
4281
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004282 text_prop_type = pt;
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004283 if (text_prop_attr == 0)
4284 text_prop_attr = pt_attr;
4285 else
4286 text_prop_attr = hl_combine_attr(text_prop_attr, pt_attr);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004287 }
4288 }
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004289 }
4290 }
4291#endif
4292
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004293 /* Decide which of the highlight attributes to use. */
4294 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004295#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004296 if (area_attr != 0)
4297 char_attr = hl_combine_attr(line_attr, area_attr);
4298 else if (search_attr != 0)
4299 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004300 /* Use line_attr when not in the Visual or 'incsearch' area
4301 * (area_attr may be 0 when "noinvcur" is set). */
4302 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004303 || vcol < fromcol || vcol_prev < fromcol_prev
4304 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004305 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004306#else
4307 if (area_attr != 0)
4308 char_attr = area_attr;
4309 else if (search_attr != 0)
4310 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004311#endif
4312 else
4313 {
4314 attr_pri = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004315#ifdef FEAT_TEXT_PROP
4316 if (text_prop_type != NULL)
4317 char_attr = text_prop_attr;
4318 else
4319#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004320#ifdef FEAT_SYN_HL
4321 if (has_syntax)
4322 char_attr = syntax_attr;
4323 else
4324#endif
4325 char_attr = 0;
4326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 }
4328
4329 /*
4330 * Get the next character to put on the screen.
4331 */
4332 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004333 * The "p_extra" points to the extra stuff that is inserted to
4334 * represent special characters (non-printable stuff) and other
4335 * things. When all characters are the same, c_extra is used.
Bram Moolenaar83a52172019-01-16 22:41:54 +01004336 * If c_final is set, it will compulsorily be used at the end.
Bram Moolenaara064ac82007-08-05 18:10:54 +00004337 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4338 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4340 */
4341 if (n_extra > 0)
4342 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004343 if (c_extra != NUL || (n_extra == 1 && c_final != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004345 c = (n_extra == 1 && c_final != NUL) ? c_final : c_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004347 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 {
4349 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004350 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004351 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 }
4353 else
4354 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 }
4356 else
4357 {
4358 c = *p_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 if (has_mbyte)
4360 {
4361 mb_c = c;
4362 if (enc_utf8)
4363 {
4364 /* If the UTF-8 character is more than one byte:
4365 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004366 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 mb_utf8 = FALSE;
4368 if (mb_l > n_extra)
4369 mb_l = 1;
4370 else if (mb_l > 1)
4371 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004372 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004374 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 }
4376 }
4377 else
4378 {
4379 /* if this is a DBCS character, put it in "mb_c" */
4380 mb_l = MB_BYTE2LEN(c);
4381 if (mb_l >= n_extra)
4382 mb_l = 1;
4383 else if (mb_l > 1)
4384 mb_c = (c << 8) + p_extra[1];
4385 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004386 if (mb_l == 0) /* at the NUL at end-of-line */
4387 mb_l = 1;
4388
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 /* If a double-width char doesn't fit display a '>' in the
4390 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004391 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392# ifdef FEAT_RIGHTLEFT
4393 wp->w_p_rl ? (col <= 0) :
4394# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004395 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 && (*mb_char2cells)(mb_c) == 2)
4397 {
4398 c = '>';
4399 mb_c = c;
4400 mb_l = 1;
4401 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004402 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 /* put the pointer back to output the double-width
4404 * character at the start of the next line. */
4405 ++n_extra;
4406 --p_extra;
4407 }
4408 else
4409 {
4410 n_extra -= mb_l - 1;
4411 p_extra += mb_l - 1;
4412 }
4413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 ++p_extra;
4415 }
4416 --n_extra;
4417 }
4418 else
4419 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004420#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004421 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004422#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004423
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004424 if (p_extra_free != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01004425 VIM_CLEAR(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 /*
4427 * Get a character from the line itself.
4428 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004429 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004430#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004431 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004432#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 if (has_mbyte)
4434 {
4435 mb_c = c;
4436 if (enc_utf8)
4437 {
4438 /* If the UTF-8 character is more than one byte: Decode it
4439 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004440 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 mb_utf8 = FALSE;
4442 if (mb_l > 1)
4443 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004444 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 /* Overlong encoded ASCII or ASCII with composing char
4446 * is displayed normally, except a NUL. */
4447 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004448 {
4449 c = mb_c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004450#ifdef FEAT_LINEBREAK
Bram Moolenaar88e76882017-02-27 20:33:46 +01004451 c0 = mb_c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004452#endif
Bram Moolenaar88e76882017-02-27 20:33:46 +01004453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004455
4456 /* At start of the line we can have a composing char.
4457 * Draw it as a space with a composing char. */
4458 if (utf_iscomposing(mb_c))
4459 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004460 int i;
4461
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004462 for (i = Screen_mco - 1; i > 0; --i)
4463 u8cc[i] = u8cc[i - 1];
4464 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004465 mb_c = ' ';
4466 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 }
4468
4469 if ((mb_l == 1 && c >= 0x80)
4470 || (mb_l >= 1 && mb_c == 0)
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004471 || (mb_l > 1 && (!vim_isprintc(mb_c))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 {
4473 /*
4474 * Illegal UTF-8 byte: display as <xx>.
4475 * Non-BMP character : display as ? or fullwidth ?.
4476 */
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004477 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004478# ifdef FEAT_RIGHTLEFT
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004479 if (wp->w_p_rl) /* reverse */
4480 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004481# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 p_extra = extra;
4483 c = *p_extra;
4484 mb_c = mb_ptr2char_adv(&p_extra);
4485 mb_utf8 = (c >= 0x80);
4486 n_extra = (int)STRLEN(p_extra);
4487 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004488 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 if (area_attr == 0 && search_attr == 0)
4490 {
4491 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004492 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 saved_attr2 = char_attr; /* save current attr */
4494 }
4495 }
4496 else if (mb_l == 0) /* at the NUL at end-of-line */
4497 mb_l = 1;
4498#ifdef FEAT_ARABIC
4499 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4500 {
4501 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004502 int pc, pc1, nc;
4503 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504
4505 /* The idea of what is the previous and next
4506 * character depends on 'rightleft'. */
4507 if (wp->w_p_rl)
4508 {
4509 pc = prev_c;
4510 pc1 = prev_c1;
4511 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004512 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 }
4514 else
4515 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004516 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004518 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 }
4520 prev_c = mb_c;
4521
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004522 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 }
4524 else
4525 prev_c = mb_c;
4526#endif
4527 }
4528 else /* enc_dbcs */
4529 {
4530 mb_l = MB_BYTE2LEN(c);
4531 if (mb_l == 0) /* at the NUL at end-of-line */
4532 mb_l = 1;
4533 else if (mb_l > 1)
4534 {
4535 /* We assume a second byte below 32 is illegal.
4536 * Hopefully this is OK for all double-byte encodings!
4537 */
4538 if (ptr[1] >= 32)
4539 mb_c = (c << 8) + ptr[1];
4540 else
4541 {
4542 if (ptr[1] == NUL)
4543 {
4544 /* head byte at end of line */
4545 mb_l = 1;
4546 transchar_nonprint(extra, c);
4547 }
4548 else
4549 {
4550 /* illegal tail byte */
4551 mb_l = 2;
4552 STRCPY(extra, "XX");
4553 }
4554 p_extra = extra;
4555 n_extra = (int)STRLEN(extra) - 1;
4556 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004557 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 c = *p_extra++;
4559 if (area_attr == 0 && search_attr == 0)
4560 {
4561 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004562 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 saved_attr2 = char_attr; /* save current attr */
4564 }
4565 mb_c = c;
4566 }
4567 }
4568 }
4569 /* If a double-width char doesn't fit display a '>' in the
4570 * last column; the character is displayed at the start of the
4571 * next line. */
4572 if ((
4573# ifdef FEAT_RIGHTLEFT
4574 wp->w_p_rl ? (col <= 0) :
4575# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004576 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 && (*mb_char2cells)(mb_c) == 2)
4578 {
4579 c = '>';
4580 mb_c = c;
4581 mb_utf8 = FALSE;
4582 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004583 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 /* Put pointer back so that the character will be
4585 * displayed at the start of the next line. */
4586 --ptr;
4587 }
4588 else if (*ptr != NUL)
4589 ptr += mb_l - 1;
4590
4591 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004592 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004593 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004594 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004597 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004598 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 c = ' ';
4600 if (area_attr == 0 && search_attr == 0)
4601 {
4602 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004603 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 saved_attr2 = char_attr; /* save current attr */
4605 }
4606 mb_c = c;
4607 mb_utf8 = FALSE;
4608 mb_l = 1;
4609 }
4610
4611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 ++ptr;
4613
4614 if (extra_check)
4615 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004616#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004617 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004618#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004619
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004620#ifdef FEAT_TERMINAL
4621 if (get_term_attr)
4622 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02004623 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004624
4625 if (!attr_pri)
4626 char_attr = syntax_attr;
4627 else
4628 char_attr = hl_combine_attr(syntax_attr, char_attr);
4629 }
4630#endif
4631
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004632#ifdef FEAT_SYN_HL
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004633 // Get syntax attribute, unless still at the start of the line
4634 // (double-wide char that doesn't fit).
Bram Moolenaar217ad922005-03-20 22:37:15 +00004635 v = (long)(ptr - line);
4636 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 {
4638 /* Get the syntax attribute for the character. If there
4639 * is an error, disable syntax highlighting. */
4640 save_did_emsg = did_emsg;
4641 did_emsg = FALSE;
4642
Bram Moolenaar217ad922005-03-20 22:37:15 +00004643 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004644# ifdef FEAT_SPELL
4645 has_spell ? &can_spell :
4646# endif
4647 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648
4649 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004650 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004651 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004652 has_syntax = FALSE;
4653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 else
4655 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004656#ifdef SYN_TIME_LIMIT
4657 if (wp->w_s->b_syn_slow)
4658 has_syntax = FALSE;
4659#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660
4661 /* Need to get the line again, a multi-line regexp may
4662 * have made it invalid. */
4663 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4664 ptr = line + v;
4665
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004666# ifdef FEAT_TEXT_PROP
4667 // Text properties overrule syntax highlighting.
4668 if (text_prop_attr == 0)
4669#endif
4670 {
4671 if (!attr_pri)
4672 char_attr = syntax_attr;
4673 else
4674 char_attr = hl_combine_attr(syntax_attr, char_attr);
4675 }
Bram Moolenaarc095b282010-07-20 22:33:34 +02004676# ifdef FEAT_CONCEAL
4677 /* no concealing past the end of the line, it interferes
4678 * with line highlighting */
4679 if (c == NUL)
4680 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004681 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004682 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004683# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004685#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004686
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004687#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004688 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004689 * Only do this when there is no syntax highlighting, the
4690 * @Spell cluster is not used or the current syntax item
4691 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004692 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004693 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004694 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004695 if (c != 0 && (
4696# ifdef FEAT_SYN_HL
4697 !has_syntax ||
4698# endif
4699 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004700 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004701 char_u *prev_ptr, *p;
4702 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004703 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaare7566042005-06-17 22:00:15 +00004704 if (has_mbyte)
4705 {
4706 prev_ptr = ptr - mb_l;
4707 v -= mb_l - 1;
4708 }
4709 else
Bram Moolenaare7566042005-06-17 22:00:15 +00004710 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004711
4712 /* Use nextline[] if possible, it has the start of the
4713 * next line concatenated. */
4714 if ((prev_ptr - line) - nextlinecol >= 0)
4715 p = nextline + (prev_ptr - line) - nextlinecol;
4716 else
4717 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004718 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004719 len = spell_check(wp, p, &spell_hlf, &cap_col,
4720 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004721 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004722
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004723 /* In Insert mode only highlight a word that
4724 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004725 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004726 && (State & INSERT) != 0
4727 && wp->w_cursor.lnum == lnum
4728 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004729 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004730 && wp->w_cursor.col < (colnr_T)word_end)
4731 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004732 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004733 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004734 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004735
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004736 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004737 && (p - nextline) + len > nextline_idx)
4738 {
4739 /* Remember that the good word continues at the
4740 * start of the next line. */
4741 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004742 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004743 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004744
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004745 /* Turn index into actual attributes. */
4746 if (spell_hlf != HLF_COUNT)
4747 spell_attr = highlight_attr[spell_hlf];
4748
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004749 if (cap_col > 0)
4750 {
4751 if (p != prev_ptr
4752 && (p - nextline) + cap_col >= nextline_idx)
4753 {
4754 /* Remember that the word in the next line
4755 * must start with a capital. */
4756 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004757 cap_col = (int)((p - nextline) + cap_col
4758 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004759 }
4760 else
4761 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004762 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004763 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004764 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004765 }
4766 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004767 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004768 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004769 char_attr = hl_combine_attr(char_attr, spell_attr);
4770 else
4771 char_attr = hl_combine_attr(spell_attr, char_attr);
4772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773#endif
4774#ifdef FEAT_LINEBREAK
4775 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004776 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004778 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004779 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 {
Bram Moolenaar4df70292015-03-21 14:20:16 +01004781 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004782 char_u *p = ptr - (mb_off + 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004783
Bram Moolenaar597a4222014-06-25 14:39:50 +02004784 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004785 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004786 NULL) - 1;
Bram Moolenaar02631462017-09-22 15:20:32 +02004787 if (c == TAB && n_extra + col > wp->w_width)
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004788# ifdef FEAT_VARTABS
Bram Moolenaara87b72c2018-06-25 21:24:51 +02004789 n_extra = tabstop_padding(vcol, wp->w_buffer->b_p_ts,
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004790 wp->w_buffer->b_p_vts_array) - 1;
4791# else
Bram Moolenaara3650912014-11-19 13:21:57 +01004792 n_extra = (int)wp->w_buffer->b_p_ts
4793 - vcol % (int)wp->w_buffer->b_p_ts - 1;
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004794# endif
Bram Moolenaara3650912014-11-19 13:21:57 +01004795
Bram Moolenaar4df70292015-03-21 14:20:16 +01004796 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004797 c_final = NUL;
Bram Moolenaar1c465442017-03-12 20:10:05 +01004798 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004799 {
4800#ifdef FEAT_CONCEAL
4801 if (c == TAB)
4802 /* See "Tab alignment" below. */
4803 FIX_FOR_BOGUSCOLS;
4804#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004805 if (!wp->w_p_list)
4806 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 }
4809#endif
4810
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004811 /* 'list': change char 160 to lcs_nbsp and space to lcs_space.
4812 */
4813 if (wp->w_p_list
4814 && (((c == 160
Bram Moolenaara12a1612019-01-24 16:39:02 +01004815 || (mb_utf8 && (mb_c == 160 || mb_c == 0x202f)))
4816 && lcs_nbsp)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004817 || (c == ' ' && lcs_space && ptr - line <= trailcol)))
4818 {
4819 c = (c == ' ') ? lcs_space : lcs_nbsp;
4820 if (area_attr == 0 && search_attr == 0)
4821 {
4822 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004823 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004824 saved_attr2 = char_attr; /* save current attr */
4825 }
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004826 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004827 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004828 {
4829 mb_utf8 = TRUE;
4830 u8cc[0] = 0;
4831 c = 0xc0;
4832 }
4833 else
4834 mb_utf8 = FALSE;
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004835 }
4836
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4838 {
4839 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004840 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 {
4842 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004843 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 saved_attr2 = char_attr; /* save current attr */
4845 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004847 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 {
4849 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004850 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004851 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 }
4853 else
4854 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 }
4856 }
4857
4858 /*
4859 * Handling of non-printable characters.
4860 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004861 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 {
4863 /*
4864 * when getting a character from the file, we may have to
4865 * turn it into something else on the way to putting it
4866 * into "ScreenLines".
4867 */
4868 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4869 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004870 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004871 long vcol_adjusted = vcol; /* removed showbreak length */
4872#ifdef FEAT_LINEBREAK
4873 /* only adjust the tab_len, when at the first column
4874 * after the showbreak value was drawn */
4875 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4876 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4877#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 /* tab amount depends on current column */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004879#ifdef FEAT_VARTABS
4880 tab_len = tabstop_padding(vcol_adjusted,
4881 wp->w_buffer->b_p_ts,
4882 wp->w_buffer->b_p_vts_array) - 1;
4883#else
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004884 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004885 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4886#endif
Bram Moolenaard574ea22015-01-14 19:35:14 +01004887
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004888#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004889 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004890#endif
4891 /* tab amount depends on current column */
4892 n_extra = tab_len;
4893#ifdef FEAT_LINEBREAK
4894 else
4895 {
4896 char_u *p;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01004897 int len;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004898 int i;
4899 int saved_nextra = n_extra;
4900
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004901#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004902 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004903 /* there are characters to conceal */
4904 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004905 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
4906 */
4907 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
4908 && n_extra > tab_len)
4909 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004910#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004911
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004912 /* if n_extra > 0, it gives the number of chars, to
4913 * use for a tab, else we need to calculate the width
4914 * for a tab */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004915 len = (tab_len * mb_char2len(lcs_tab2));
4916 if (n_extra > 0)
4917 len += n_extra - tab_len;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004918 c = lcs_tab1;
4919 p = alloc((unsigned)(len + 1));
4920 vim_memset(p, ' ', len);
4921 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004922 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004923 p_extra_free = p;
4924 for (i = 0; i < tab_len; i++)
4925 {
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004926 if (*p == NUL)
4927 {
4928 tab_len = i;
4929 break;
4930 }
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004931 mb_char2bytes(lcs_tab2, p);
4932 p += mb_char2len(lcs_tab2);
4933 n_extra += mb_char2len(lcs_tab2)
4934 - (saved_nextra > 0 ? 1 : 0);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004935 }
4936 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004937#ifdef FEAT_CONCEAL
4938 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
4939 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004940 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004941 n_extra -= vcol_off;
4942#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004943 }
4944#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004945#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004946 {
4947 int vc_saved = vcol_off;
4948
4949 /* Tab alignment should be identical regardless of
4950 * 'conceallevel' value. So tab compensates of all
4951 * previous concealed characters, and thus resets
4952 * vcol_off and boguscols accumulated so far in the
4953 * line. Note that the tab can be longer than
4954 * 'tabstop' when there are concealed characters. */
4955 FIX_FOR_BOGUSCOLS;
4956
4957 /* Make sure, the highlighting for the tab char will be
4958 * correctly set further below (effectively reverts the
4959 * FIX_FOR_BOGSUCOLS macro */
4960 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004961 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004962 tab_len += vc_saved;
4963 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004964#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 if (wp->w_p_list)
4967 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004968 c = (n_extra == 0 && lcs_tab3) ? lcs_tab3 : lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004969#ifdef FEAT_LINEBREAK
4970 if (wp->w_p_lbr)
4971 c_extra = NUL; /* using p_extra from above */
4972 else
4973#endif
4974 c_extra = lcs_tab2;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004975 c_final = lcs_tab3;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004976 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004977 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 saved_attr2 = char_attr; /* save current attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004980 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 {
4982 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004983 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004984 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 }
4987 else
4988 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004989 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 c_extra = ' ';
4991 c = ' ';
4992 }
4993 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004994 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02004995 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004996 || ((fromcol >= 0 || fromcol_prev >= 0)
4997 && tocol > vcol
4998 && VIsual_mode != Ctrl_V
4999 && (
5000# ifdef FEAT_RIGHTLEFT
5001 wp->w_p_rl ? (col >= 0) :
5002# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005003 (col < wp->w_width))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005004 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005005 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005006 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02005007 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005009 /* Display a '$' after the line or highlight an extra
5010 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011#if defined(FEAT_DIFF) || defined(LINE_ATTR)
5012 /* For a diff line the highlighting continues after the
5013 * "$". */
5014 if (
5015# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005016 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017# ifdef LINE_ATTR
5018 &&
5019# endif
5020# endif
5021# ifdef LINE_ATTR
5022 line_attr == 0
5023# endif
5024 )
5025#endif
5026 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 /* In virtualedit, visual selections may extend
5028 * beyond end of line. */
5029 if (area_highlighting && virtual_active()
5030 && tocol != MAXCOL && vcol < tocol)
5031 n_extra = 0;
5032 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 {
5034 p_extra = at_end_str;
5035 n_extra = 1;
5036 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005037 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 }
5039 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02005040 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005041 c = lcs_eol;
5042 else
5043 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 lcs_eol_one = -1;
5045 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005046 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005048 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 n_attr = 1;
5050 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005052 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 {
5054 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005055 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005056 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 }
5058 else
5059 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 }
5061 else if (c != NUL)
5062 {
5063 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02005064 if (n_extra == 0)
5065 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066#ifdef FEAT_RIGHTLEFT
5067 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
5068 rl_mirror(p_extra); /* reverse "<12>" */
5069#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005071 c_final = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005072#ifdef FEAT_LINEBREAK
5073 if (wp->w_p_lbr)
5074 {
5075 char_u *p;
5076
5077 c = *p_extra;
5078 p = alloc((unsigned)n_extra + 1);
5079 vim_memset(p, ' ', n_extra);
5080 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5081 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005082 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005083 p_extra_free = p_extra = p;
5084 }
5085 else
5086#endif
5087 {
5088 n_extra = byte2cells(c) - 1;
5089 c = *p_extra++;
5090 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005091 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 {
5093 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005094 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 saved_attr2 = char_attr; /* save current attr */
5096 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 else if (VIsual_active
5100 && (VIsual_mode == Ctrl_V
5101 || VIsual_mode == 'v')
5102 && virtual_active()
5103 && tocol != MAXCOL
5104 && vcol < tocol
5105 && (
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005106#ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 wp->w_p_rl ? (col >= 0) :
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005108#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005109 (col < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 {
5111 c = ' ';
5112 --ptr; /* put it back at the NUL */
5113 }
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005114#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 else if ((
5116# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005117 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005119# ifdef FEAT_TERMINAL
5120 term_attr != 0 ||
5121# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 ) && (
5124# ifdef FEAT_RIGHTLEFT
5125 wp->w_p_rl ? (col >= 0) :
5126# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005127 (col
5128# ifdef FEAT_CONCEAL
5129 - boguscols
5130# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005131 < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 {
5133 /* Highlight until the right side of the window */
5134 c = ' ';
5135 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005136
5137 /* Remember we do the char for line highlighting. */
5138 ++did_line_attr;
5139
5140 /* don't do search HL for the rest of the line */
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005141 if (line_attr != 0 && char_attr == search_attr
5142 && (did_line_attr > 1
5143 || (wp->w_p_list && lcs_eol > 0)))
Bram Moolenaar91170f82006-05-05 21:15:17 +00005144 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145# ifdef FEAT_DIFF
5146 if (diff_hlf == HLF_TXD)
5147 {
5148 diff_hlf = HLF_CHD;
5149 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005150 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005151 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005152 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5153 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005154 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 }
5157# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005158# ifdef FEAT_TERMINAL
5159 if (term_attr != 0)
5160 {
5161 char_attr = term_attr;
5162 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5163 char_attr = hl_combine_attr(char_attr,
5164 HL_ATTR(HLF_CUL));
5165 }
5166# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 }
5168#endif
5169 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005170
5171#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005172 if ( wp->w_p_cole > 0
5173 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaar6561d522015-07-21 15:48:27 +02005174 conceal_cursor_line(wp) )
Bram Moolenaar4d585022016-04-14 19:50:22 +02005175 && ( (syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005176 && !(lnum_in_visual_area
5177 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005178 {
5179 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005180 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005181 && (syn_get_sub_char() != NUL || match_conc
5182 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005183 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005184 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005185 /* First time at this concealed item: display one
5186 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005187 if (match_conc)
5188 c = match_conc;
5189 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005190 c = syn_get_sub_char();
5191 else if (lcs_conceal != NUL)
5192 c = lcs_conceal;
5193 else
5194 c = ' ';
5195
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005196 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005197
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005198 if (n_extra > 0)
5199 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005200 vcol += n_extra;
5201 if (wp->w_p_wrap && n_extra > 0)
5202 {
5203# ifdef FEAT_RIGHTLEFT
5204 if (wp->w_p_rl)
5205 {
5206 col -= n_extra;
5207 boguscols -= n_extra;
5208 }
5209 else
5210# endif
5211 {
5212 boguscols += n_extra;
5213 col += n_extra;
5214 }
5215 }
5216 n_extra = 0;
5217 n_attr = 0;
5218 }
5219 else if (n_skip == 0)
5220 {
5221 is_concealing = TRUE;
5222 n_skip = 1;
5223 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005224 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005225 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005226 {
5227 mb_utf8 = TRUE;
5228 u8cc[0] = 0;
5229 c = 0xc0;
5230 }
5231 else
5232 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005233 }
5234 else
5235 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005236 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005237 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005238 }
5239#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 }
5241
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005242#ifdef FEAT_CONCEAL
5243 /* In the cursor line and we may be concealing characters: correct
5244 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005245 if (!did_wcol && draw_state == WL_LINE
5246 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005247 && conceal_cursor_line(wp)
5248 && (int)wp->w_virtcol <= vcol + n_skip)
5249 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005250# ifdef FEAT_RIGHTLEFT
5251 if (wp->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02005252 wp->w_wcol = wp->w_width - col + boguscols - 1;
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005253 else
5254# endif
5255 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005256 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005257 did_wcol = TRUE;
5258 }
5259#endif
5260
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 /* Don't override visual selection highlighting. */
5262 if (n_attr > 0
5263 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005264 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 char_attr = extra_attr;
5266
Bram Moolenaar81695252004-12-29 20:58:21 +00005267#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 /* XIM don't send preedit_start and preedit_end, but they send
5269 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5270 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005271 if (p_imst == IM_ON_THE_SPOT
5272 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005273 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 && (State & INSERT)
5275 && !p_imdisable
5276 && im_is_preediting()
5277 && draw_state == WL_LINE)
5278 {
5279 colnr_T tcol;
5280
5281 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005282 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 else
5284 tcol = preedit_end_col;
5285 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5286 {
5287 if (feedback_old_attr < 0)
5288 {
5289 feedback_col = 0;
5290 feedback_old_attr = char_attr;
5291 }
5292 char_attr = im_get_feedback_attr(feedback_col);
5293 if (char_attr < 0)
5294 char_attr = feedback_old_attr;
5295 feedback_col++;
5296 }
5297 else if (feedback_old_attr >= 0)
5298 {
5299 char_attr = feedback_old_attr;
5300 feedback_old_attr = -1;
5301 feedback_col = 0;
5302 }
5303 }
5304#endif
5305 /*
5306 * Handle the case where we are in column 0 but not on the first
5307 * character of the line and the user wants us to show us a
5308 * special character (via 'listchars' option "precedes:<char>".
5309 */
5310 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005311 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5313#ifdef FEAT_DIFF
5314 && filler_todo <= 0
5315#endif
5316 && draw_state > WL_NR
5317 && c != NUL)
5318 {
5319 c = lcs_prec;
5320 lcs_prec_todo = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005321 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5322 {
5323 /* Double-width character being overwritten by the "precedes"
5324 * character, need to fill up half the character. */
5325 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005326 c_final = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005327 n_extra = 1;
5328 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005329 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005330 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005332 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 {
5334 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005335 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005336 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 }
5338 else
5339 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005340 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 {
5342 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005343 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 n_attr3 = 1;
5345 }
5346 }
5347
5348 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005349 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005351 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005352#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005353 || did_line_attr == 1
5354#endif
5355 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005357#ifdef FEAT_SEARCH_EXTRA
5358 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005359
5360 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005361 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005362 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005363#endif
5364
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005365 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 * highlight match at end of line. If it's beyond the last
5367 * char on the screen, just overwrite that one (tricky!) Not
5368 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005369#ifdef FEAT_SEARCH_EXTRA
5370 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005371 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005372 prevcol_hl_flag = TRUE;
5373 else
5374 {
5375 cur = wp->w_match_head;
5376 while (cur != NULL)
5377 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005378 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005379 {
5380 prevcol_hl_flag = TRUE;
5381 break;
5382 }
5383 cur = cur->next;
5384 }
5385 }
5386#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005388 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005389 && (VIsual_mode != Ctrl_V
5390 || lnum == VIsual.lnum
5391 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005392 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393#ifdef FEAT_SEARCH_EXTRA
5394 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005395 || (prevcol_hl_flag == TRUE
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005396# ifdef FEAT_SYN_HL
5397 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
5398 && !(wp == curwin && VIsual_active))
5399# endif
5400# ifdef FEAT_DIFF
5401 && diff_hlf == (hlf_T)0
5402# endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005403# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005404 && did_line_attr <= 1
5405# endif
5406 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407#endif
5408 ))
5409 {
5410 int n = 0;
5411
5412#ifdef FEAT_RIGHTLEFT
5413 if (wp->w_p_rl)
5414 {
5415 if (col < 0)
5416 n = 1;
5417 }
5418 else
5419#endif
5420 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005421 if (col >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 n = -1;
5423 }
5424 if (n != 0)
5425 {
5426 /* At the window boundary, highlight the last character
5427 * instead (better than nothing). */
5428 off += n;
5429 col += n;
5430 }
5431 else
5432 {
5433 /* Add a blank character to highlight. */
5434 ScreenLines[off] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 if (enc_utf8)
5436 ScreenLinesUC[off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437 }
5438#ifdef FEAT_SEARCH_EXTRA
5439 if (area_attr == 0)
5440 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005441 /* Use attributes from match with highest priority among
5442 * 'search_hl' and the match list. */
5443 char_attr = search_hl.attr;
5444 cur = wp->w_match_head;
5445 shl_flag = FALSE;
5446 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005447 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005448 if (shl_flag == FALSE
5449 && ((cur != NULL
5450 && cur->priority > SEARCH_HL_PRIORITY)
5451 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005452 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005453 shl = &search_hl;
5454 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005455 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005456 else
5457 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005458 if ((ptr - line) - 1 == (long)shl->startcol
5459 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005460 char_attr = shl->attr;
5461 if (shl != &search_hl && cur != NULL)
5462 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 }
5465#endif
5466 ScreenAttrs[off] = char_attr;
5467#ifdef FEAT_RIGHTLEFT
5468 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005469 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005471 --off;
5472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 else
5474#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005475 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005477 ++off;
5478 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005479 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005480#ifdef FEAT_SYN_HL
5481 eol_hl_off = 1;
5482#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005484 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485
Bram Moolenaar91170f82006-05-05 21:15:17 +00005486 /*
5487 * At end of the text line.
5488 */
5489 if (c == NUL)
5490 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005491#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005492 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005493 if (wp->w_p_wrap)
5494 v = wp->w_skipcol;
5495 else
5496 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005497
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005498 /* check if line ends before left margin */
5499 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005500 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005501#ifdef FEAT_CONCEAL
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01005502 // Get rid of the boguscols now, we want to draw until the right
5503 // edge for 'cursorcolumn'.
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005504 col -= boguscols;
5505 boguscols = 0;
5506#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005507
5508 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005509 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005510
5511 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005512 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5513 && (int)wp->w_virtcol <
Bram Moolenaar02631462017-09-22 15:20:32 +02005514 wp->w_width * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005515 && lnum != wp->w_cursor.lnum)
5516 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005517# ifdef FEAT_RIGHTLEFT
5518 && !wp->w_p_rl
5519# endif
5520 )
5521 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005522 int rightmost_vcol = 0;
5523 int i;
5524
5525 if (wp->w_p_cuc)
5526 rightmost_vcol = wp->w_virtcol;
5527 if (draw_color_col)
5528 /* determine rightmost colorcolumn to possibly draw */
5529 for (i = 0; color_cols[i] >= 0; ++i)
5530 if (rightmost_vcol < color_cols[i])
5531 rightmost_vcol = color_cols[i];
5532
Bram Moolenaar02631462017-09-22 15:20:32 +02005533 while (col < wp->w_width)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005534 {
5535 ScreenLines[off] = ' ';
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005536 if (enc_utf8)
5537 ScreenLinesUC[off] = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005538 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005539 if (draw_color_col)
5540 draw_color_col = advance_color_col(VCOL_HLC,
5541 &color_cols);
5542
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005543 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005544 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005545 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005546 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005547 else
5548 ScreenAttrs[off++] = 0;
5549
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005550 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005551 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005552
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005553 ++vcol;
5554 }
5555 }
5556#endif
5557
Bram Moolenaar53f81742017-09-22 14:35:51 +02005558 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02005559 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 row++;
5561
5562 /*
5563 * Update w_cline_height and w_cline_folded if the cursor line was
5564 * updated (saves a call to plines() later).
5565 */
5566 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5567 {
5568 curwin->w_cline_row = startrow;
5569 curwin->w_cline_height = row - startrow;
5570#ifdef FEAT_FOLDING
5571 curwin->w_cline_folded = FALSE;
5572#endif
5573 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5574 }
5575
5576 break;
5577 }
5578
5579 /* line continues beyond line end */
5580 if (lcs_ext
5581 && !wp->w_p_wrap
5582#ifdef FEAT_DIFF
5583 && filler_todo <= 0
5584#endif
5585 && (
5586#ifdef FEAT_RIGHTLEFT
5587 wp->w_p_rl ? col == 0 :
5588#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005589 col == wp->w_width - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005591 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5593 {
5594 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005595 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005597 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 {
5599 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005600 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005601 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 }
5603 else
5604 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 }
5606
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005607#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005608 /* advance to the next 'colorcolumn' */
5609 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005610 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005611
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005612 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005613 * highlight the cursor position itself.
5614 * Also highlight the 'colorcolumn' if it is different than
5615 * 'cursorcolumn' */
5616 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005617 if (draw_state == WL_LINE && !lnum_in_visual_area
5618 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005619 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005620 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005621 && lnum != wp->w_cursor.lnum)
5622 {
5623 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005624 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005625 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005626 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005627 {
5628 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005629 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005630 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005631 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005632#endif
5633
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634 /*
5635 * Store character to be displayed.
5636 * Skip characters that are left of the screen for 'nowrap'.
5637 */
5638 vcol_prev = vcol;
5639 if (draw_state < WL_LINE || n_skip <= 0)
5640 {
5641 /*
5642 * Store the character.
5643 */
Bram Moolenaara12a1612019-01-24 16:39:02 +01005644#if defined(FEAT_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5646 {
5647 /* A double-wide character is: put first halve in left cell. */
5648 --off;
5649 --col;
5650 }
5651#endif
5652 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005654 {
5655 if ((mb_c & 0xff00) == 0x8e00)
5656 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 else if (enc_utf8)
5660 {
5661 if (mb_utf8)
5662 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005663 int i;
5664
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005666 if ((c & 0xff) == 0)
5667 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005668 for (i = 0; i < Screen_mco; ++i)
5669 {
5670 ScreenLinesC[i][off] = u8cc[i];
5671 if (u8cc[i] == 0)
5672 break;
5673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 }
5675 else
5676 ScreenLinesUC[off] = 0;
5677 }
5678 if (multi_attr)
5679 {
5680 ScreenAttrs[off] = multi_attr;
5681 multi_attr = 0;
5682 }
5683 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 ScreenAttrs[off] = char_attr;
5685
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5687 {
5688 /* Need to fill two screen columns. */
5689 ++off;
5690 ++col;
5691 if (enc_utf8)
5692 /* UTF-8: Put a 0 in the second screen char. */
5693 ScreenLines[off] = 0;
5694 else
5695 /* DBCS: Put second byte in the second screen char. */
5696 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005697 if (draw_state > WL_NR
5698#ifdef FEAT_DIFF
5699 && filler_todo <= 0
5700#endif
5701 )
5702 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 /* When "tocol" is halfway a character, set it to the end of
5704 * the character, otherwise highlighting won't stop. */
5705 if (tocol == vcol)
5706 ++tocol;
5707#ifdef FEAT_RIGHTLEFT
5708 if (wp->w_p_rl)
5709 {
5710 /* now it's time to backup one cell */
5711 --off;
5712 --col;
5713 }
5714#endif
5715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716#ifdef FEAT_RIGHTLEFT
5717 if (wp->w_p_rl)
5718 {
5719 --off;
5720 --col;
5721 }
5722 else
5723#endif
5724 {
5725 ++off;
5726 ++col;
5727 }
5728 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005729#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005730 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005731 {
5732 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005733 ++vcol_off;
5734 if (n_extra > 0)
5735 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005736 if (wp->w_p_wrap)
5737 {
5738 /*
5739 * Special voodoo required if 'wrap' is on.
5740 *
5741 * Advance the column indicator to force the line
5742 * drawing to wrap early. This will make the line
5743 * take up the same screen space when parts are concealed,
5744 * so that cursor line computations aren't messed up.
5745 *
5746 * To avoid the fictitious advance of 'col' causing
5747 * trailing junk to be written out of the screen line
5748 * we are building, 'boguscols' keeps track of the number
5749 * of bad columns we have advanced.
5750 */
5751 if (n_extra > 0)
5752 {
5753 vcol += n_extra;
5754# ifdef FEAT_RIGHTLEFT
5755 if (wp->w_p_rl)
5756 {
5757 col -= n_extra;
5758 boguscols -= n_extra;
5759 }
5760 else
5761# endif
5762 {
5763 col += n_extra;
5764 boguscols += n_extra;
5765 }
5766 n_extra = 0;
5767 n_attr = 0;
5768 }
5769
5770
Bram Moolenaar860cae12010-06-05 23:22:07 +02005771 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5772 {
5773 /* Need to fill two screen columns. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01005774# ifdef FEAT_RIGHTLEFT
Bram Moolenaar860cae12010-06-05 23:22:07 +02005775 if (wp->w_p_rl)
5776 {
5777 --boguscols;
5778 --col;
5779 }
5780 else
Bram Moolenaara12a1612019-01-24 16:39:02 +01005781# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02005782 {
5783 ++boguscols;
5784 ++col;
5785 }
5786 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005787
5788# ifdef FEAT_RIGHTLEFT
5789 if (wp->w_p_rl)
5790 {
5791 --boguscols;
5792 --col;
5793 }
5794 else
5795# endif
5796 {
5797 ++boguscols;
5798 ++col;
5799 }
5800 }
5801 else
5802 {
5803 if (n_extra > 0)
5804 {
5805 vcol += n_extra;
5806 n_extra = 0;
5807 n_attr = 0;
5808 }
5809 }
5810
5811 }
5812#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813 else
5814 --n_skip;
5815
Bram Moolenaar64486672010-05-16 15:46:46 +02005816 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5817 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005818 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819#ifdef FEAT_DIFF
5820 && filler_todo <= 0
5821#endif
5822 )
5823 ++vcol;
5824
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005825#ifdef FEAT_SYN_HL
5826 if (vcol_save_attr >= 0)
5827 char_attr = vcol_save_attr;
5828#endif
5829
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 /* restore attributes after "predeces" in 'listchars' */
5831 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5832 char_attr = saved_attr3;
5833
5834 /* restore attributes after last 'listchars' or 'number' char */
5835 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5836 char_attr = saved_attr2;
5837
5838 /*
5839 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005840 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 */
5842 if ((
5843#ifdef FEAT_RIGHTLEFT
5844 wp->w_p_rl ? (col < 0) :
5845#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005846 (col >= wp->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847 && (*ptr != NUL
5848#ifdef FEAT_DIFF
5849 || filler_todo > 0
5850#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005851 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5853 )
5854 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005855#ifdef FEAT_CONCEAL
Bram Moolenaar53f81742017-09-22 14:35:51 +02005856 screen_line(screen_row, wp->w_wincol, col - boguscols,
Bram Moolenaar02631462017-09-22 15:20:32 +02005857 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005858 boguscols = 0;
5859#else
Bram Moolenaar53f81742017-09-22 14:35:51 +02005860 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02005861 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005862#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 ++row;
5864 ++screen_row;
5865
5866 /* When not wrapping and finished diff lines, or when displayed
5867 * '$' and highlighting until last column, break here. */
5868 if ((!wp->w_p_wrap
5869#ifdef FEAT_DIFF
5870 && filler_todo <= 0
5871#endif
5872 ) || lcs_eol_one == -1)
5873 break;
5874
5875 /* When the window is too narrow draw all "@" lines. */
5876 if (draw_state != WL_LINE
5877#ifdef FEAT_DIFF
5878 && filler_todo <= 0
5879#endif
5880 )
5881 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01005882 win_draw_end(wp, '@', ' ', TRUE, row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884 row = endrow;
5885 }
5886
5887 /* When line got too long for screen break here. */
5888 if (row == endrow)
5889 {
5890 ++row;
5891 break;
5892 }
5893
5894 if (screen_cur_row == screen_row - 1
5895#ifdef FEAT_DIFF
5896 && filler_todo <= 0
5897#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005898 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 {
5900 /* Remember that the line wraps, used for modeless copy. */
5901 LineWraps[screen_row - 1] = TRUE;
5902
5903 /*
5904 * Special trick to make copy/paste of wrapped lines work with
5905 * xterm/screen: write an extra character beyond the end of
5906 * the line. This will work with all terminal types
5907 * (regardless of the xn,am settings).
5908 * Only do this on a fast tty.
5909 * Only do this if the cursor is on the current line
5910 * (something has been written in it).
5911 * Don't do this for the GUI.
5912 * Don't do this for double-width characters.
5913 * Don't do this for a window not at the right screen border.
5914 */
5915 if (p_tf
5916#ifdef FEAT_GUI
5917 && !gui.in_use
5918#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005920 && ((*mb_off2cells)(LineOffset[screen_row],
5921 LineOffset[screen_row] + screen_Columns)
5922 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005924 + (int)Columns - 2,
5925 LineOffset[screen_row] + screen_Columns)
Bram Moolenaara12a1612019-01-24 16:39:02 +01005926 == 2)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927 {
5928 /* First make sure we are at the end of the screen line,
5929 * then output the same character again to let the
5930 * terminal know about the wrap. If the terminal doesn't
5931 * auto-wrap, we overwrite the character. */
Bram Moolenaar02631462017-09-22 15:20:32 +02005932 if (screen_cur_col != wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 screen_char(LineOffset[screen_row - 1]
5934 + (unsigned)Columns - 1,
5935 screen_row - 1, (int)(Columns - 1));
5936
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 /* When there is a multi-byte character, just output a
5938 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005939 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5940 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 out_char(' ');
5942 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943 out_char(ScreenLines[LineOffset[screen_row - 1]
5944 + (Columns - 1)]);
5945 /* force a redraw of the first char on the next line */
5946 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5947 screen_start(); /* don't know where cursor is now */
5948 }
5949 }
5950
5951 col = 0;
5952 off = (unsigned)(current_ScreenLine - ScreenLines);
5953#ifdef FEAT_RIGHTLEFT
5954 if (wp->w_p_rl)
5955 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005956 col = wp->w_width - 1; /* col is not used if breaking! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 off += col;
5958 }
5959#endif
5960
5961 /* reset the drawing state for the start of a wrapped line */
5962 draw_state = WL_START;
5963 saved_n_extra = n_extra;
5964 saved_p_extra = p_extra;
5965 saved_c_extra = c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005966 saved_c_final = c_final;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 saved_char_attr = char_attr;
5968 n_extra = 0;
5969 lcs_prec_todo = lcs_prec;
5970#ifdef FEAT_LINEBREAK
5971# ifdef FEAT_DIFF
5972 if (filler_todo <= 0)
5973# endif
5974 need_showbreak = TRUE;
5975#endif
5976#ifdef FEAT_DIFF
5977 --filler_todo;
5978 /* When the filler lines are actually below the last line of the
5979 * file, don't draw the line itself, break here. */
5980 if (filler_todo == 0 && wp->w_botfill)
5981 break;
5982#endif
5983 }
5984
5985 } /* for every character in the line */
5986
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005987#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005988 /* After an empty line check first word for capital. */
5989 if (*skipwhite(line) == NUL)
5990 {
5991 capcol_lnum = lnum + 1;
5992 cap_col = 0;
5993 }
5994#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01005995#ifdef FEAT_TEXT_PROP
5996 vim_free(text_props);
5997 vim_free(text_prop_idxs);
5998#endif
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005999
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01006000 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 return row;
6002}
6003
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006004/*
6005 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006006 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006007 */
6008 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006009comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006010{
6011 int i;
6012
6013 for (i = 0; i < Screen_mco; ++i)
6014 {
6015 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
6016 return TRUE;
6017 if (ScreenLinesC[i][off_from] == 0)
6018 break;
6019 }
6020 return FALSE;
6021}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006022
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023/*
6024 * Check whether the given character needs redrawing:
6025 * - the (first byte of the) character is different
6026 * - the attributes are different
6027 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006028 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029 */
6030 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006031char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032{
6033 if (cols > 0
6034 && ((ScreenLines[off_from] != ScreenLines[off_to]
6035 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036 || (enc_dbcs != 0
6037 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
6038 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
6039 ? ScreenLines2[off_from] != ScreenLines2[off_to]
6040 : (cols > 1 && ScreenLines[off_from + 1]
6041 != ScreenLines[off_to + 1])))
6042 || (enc_utf8
6043 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6044 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006045 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006046 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6047 && ScreenLines[off_from + 1]
Bram Moolenaara12a1612019-01-24 16:39:02 +01006048 != ScreenLines[off_to + 1])))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049 return TRUE;
6050 return FALSE;
6051}
6052
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006053#if defined(FEAT_TERMINAL) || defined(PROTO)
6054/*
6055 * Return the index in ScreenLines[] for the current screen line.
6056 */
6057 int
6058screen_get_current_line_off()
6059{
6060 return (int)(current_ScreenLine - ScreenLines);
6061}
6062#endif
6063
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064/*
6065 * Move one "cooked" screen line to the screen, but only the characters that
6066 * have actually changed. Handle insert/delete character.
6067 * "coloff" gives the first column on the screen for this line.
6068 * "endcol" gives the columns where valid characters are.
6069 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6070 * needs to be cleared, negative otherwise.
6071 * "rlflag" is TRUE in a rightleft window:
6072 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6073 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6074 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006075 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006076screen_line(
6077 int row,
6078 int coloff,
6079 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006080 int clear_width,
6081 int rlflag UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082{
6083 unsigned off_from;
6084 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006085 unsigned max_off_from;
6086 unsigned max_off_to;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089 int force = FALSE; /* force update rest of the line */
6090 int redraw_this /* bool: does character need redraw? */
6091#ifdef FEAT_GUI
6092 = TRUE /* For GUI when while-loop empty */
6093#endif
6094 ;
6095 int redraw_next; /* redraw_this for next character */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 int clear_next = FALSE;
6097 int char_cells; /* 1: normal char */
6098 /* 2: occupies two display cells */
6099# define CHAR_CELLS char_cells
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006101 /* Check for illegal row and col, just in case. */
6102 if (row >= Rows)
6103 row = Rows - 1;
6104 if (endcol > Columns)
6105 endcol = Columns;
6106
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107# ifdef FEAT_CLIPBOARD
6108 clip_may_clear_selection(row, row);
6109# endif
6110
6111 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6112 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006113 max_off_from = off_from + screen_Columns;
6114 max_off_to = LineOffset[row] + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115
6116#ifdef FEAT_RIGHTLEFT
6117 if (rlflag)
6118 {
6119 /* Clear rest first, because it's left of the text. */
6120 if (clear_width > 0)
6121 {
6122 while (col <= endcol && ScreenLines[off_to] == ' '
6123 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006124 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 {
6126 ++off_to;
6127 ++col;
6128 }
6129 if (col <= endcol)
6130 screen_fill(row, row + 1, col + coloff,
6131 endcol + coloff + 1, ' ', ' ', 0);
6132 }
6133 col = endcol + 1;
6134 off_to = LineOffset[row] + col + coloff;
6135 off_from += col;
6136 endcol = (clear_width > 0 ? clear_width : -clear_width);
6137 }
6138#endif /* FEAT_RIGHTLEFT */
6139
6140 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6141
6142 while (col < endcol)
6143 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006145 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 else
6147 char_cells = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148
6149 redraw_this = redraw_next;
6150 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6151 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6152
6153#ifdef FEAT_GUI
6154 /* If the next character was bold, then redraw the current character to
6155 * remove any pixels that might have spilt over into us. This only
6156 * happens in the GUI.
6157 */
6158 if (redraw_next && gui.in_use)
6159 {
6160 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006161 if (hl > HL_ALL)
6162 hl = syn_attr2attr(hl);
6163 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 redraw_this = TRUE;
6165 }
6166#endif
6167
6168 if (redraw_this)
6169 {
6170 /*
6171 * Special handling when 'xs' termcap flag set (hpterm):
6172 * Attributes for characters are stored at the position where the
6173 * cursor is when writing the highlighting code. The
6174 * start-highlighting code must be written with the cursor on the
6175 * first highlighted character. The stop-highlighting code must
6176 * be written with the cursor just after the last highlighted
6177 * character.
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01006178 * Overwriting a character doesn't remove its highlighting. Need
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179 * to clear the rest of the line, and force redrawing it
6180 * completely.
6181 */
6182 if ( p_wiv
6183 && !force
6184#ifdef FEAT_GUI
6185 && !gui.in_use
6186#endif
6187 && ScreenAttrs[off_to] != 0
6188 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6189 {
6190 /*
6191 * Need to remove highlighting attributes here.
6192 */
6193 windgoto(row, col + coloff);
6194 out_str(T_CE); /* clear rest of this screen line */
6195 screen_start(); /* don't know where cursor is now */
6196 force = TRUE; /* force redraw of rest of the line */
6197 redraw_next = TRUE; /* or else next char would miss out */
6198
6199 /*
6200 * If the previous character was highlighted, need to stop
6201 * highlighting at this character.
6202 */
6203 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6204 {
6205 screen_attr = ScreenAttrs[off_to - 1];
6206 term_windgoto(row, col + coloff);
6207 screen_stop_highlight();
6208 }
6209 else
6210 screen_attr = 0; /* highlighting has stopped */
6211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212 if (enc_dbcs != 0)
6213 {
6214 /* Check if overwriting a double-byte with a single-byte or
6215 * the other way around requires another character to be
6216 * redrawn. For UTF-8 this isn't needed, because comparing
6217 * ScreenLinesUC[] is sufficient. */
6218 if (char_cells == 1
6219 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006220 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 {
6222 /* Writing a single-cell character over a double-cell
6223 * character: need to redraw the next cell. */
6224 ScreenLines[off_to + 1] = 0;
6225 redraw_next = TRUE;
6226 }
6227 else if (char_cells == 2
6228 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006229 && (*mb_off2cells)(off_to, max_off_to) == 1
6230 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231 {
6232 /* Writing the second half of a double-cell character over
6233 * a double-cell character: need to redraw the second
6234 * cell. */
6235 ScreenLines[off_to + 2] = 0;
6236 redraw_next = TRUE;
6237 }
6238
6239 if (enc_dbcs == DBCS_JPNU)
6240 ScreenLines2[off_to] = ScreenLines2[off_from];
6241 }
6242 /* When writing a single-width character over a double-width
6243 * character and at the end of the redrawn text, need to clear out
6244 * the right halve of the old character.
6245 * Also required when writing the right halve of a double-width
6246 * char over the left halve of an existing one. */
6247 if (has_mbyte && col + char_cells == endcol
6248 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006249 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006251 && (*mb_off2cells)(off_to, max_off_to) == 1
6252 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253 clear_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254
6255 ScreenLines[off_to] = ScreenLines[off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256 if (enc_utf8)
6257 {
6258 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6259 if (ScreenLinesUC[off_from] != 0)
6260 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006261 int i;
6262
6263 for (i = 0; i < Screen_mco; ++i)
6264 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265 }
6266 }
6267 if (char_cells == 2)
6268 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269
6270#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006271 /* The bold trick makes a single column of pixels appear in the
6272 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 * character should be redrawn too. This happens for our own GUI
6274 * and for some xterms. */
6275 if (
6276# ifdef FEAT_GUI
6277 gui.in_use
6278# endif
6279# if defined(FEAT_GUI) && defined(UNIX)
6280 ||
6281# endif
6282# ifdef UNIX
6283 term_is_xterm
6284# endif
6285 )
6286 {
6287 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006288 if (hl > HL_ALL)
6289 hl = syn_attr2attr(hl);
6290 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291 redraw_next = TRUE;
6292 }
6293#endif
6294 ScreenAttrs[off_to] = ScreenAttrs[off_from];
Bram Moolenaara12a1612019-01-24 16:39:02 +01006295
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006296 /* For simplicity set the attributes of second half of a
6297 * double-wide character equal to the first half. */
6298 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006300
6301 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304 screen_char(off_to, row, col + coloff);
6305 }
6306 else if ( p_wiv
6307#ifdef FEAT_GUI
6308 && !gui.in_use
6309#endif
6310 && col + coloff > 0)
6311 {
6312 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6313 {
6314 /*
6315 * Don't output stop-highlight when moving the cursor, it will
6316 * stop the highlighting when it should continue.
6317 */
6318 screen_attr = 0;
6319 }
6320 else if (screen_attr != 0)
6321 screen_stop_highlight();
6322 }
6323
6324 off_to += CHAR_CELLS;
6325 off_from += CHAR_CELLS;
6326 col += CHAR_CELLS;
6327 }
6328
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329 if (clear_next)
6330 {
6331 /* Clear the second half of a double-wide character of which the left
6332 * half was overwritten with a single-wide character. */
6333 ScreenLines[off_to] = ' ';
6334 if (enc_utf8)
6335 ScreenLinesUC[off_to] = 0;
6336 screen_char(off_to, row, col + coloff);
6337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338
6339 if (clear_width > 0
6340#ifdef FEAT_RIGHTLEFT
6341 && !rlflag
6342#endif
6343 )
6344 {
6345#ifdef FEAT_GUI
6346 int startCol = col;
6347#endif
6348
6349 /* blank out the rest of the line */
6350 while (col < clear_width && ScreenLines[off_to] == ' '
6351 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006352 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 {
6354 ++off_to;
6355 ++col;
6356 }
6357 if (col < clear_width)
6358 {
6359#ifdef FEAT_GUI
6360 /*
6361 * In the GUI, clearing the rest of the line may leave pixels
6362 * behind if the first character cleared was bold. Some bold
6363 * fonts spill over the left. In this case we redraw the previous
6364 * character too. If we didn't skip any blanks above, then we
6365 * only redraw if the character wasn't already redrawn anyway.
6366 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006367 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368 {
6369 hl = ScreenAttrs[off_to];
6370 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006371 {
6372 int prev_cells = 1;
Bram Moolenaara12a1612019-01-24 16:39:02 +01006373
Bram Moolenaar9c697322006-10-09 20:11:17 +00006374 if (enc_utf8)
6375 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6376 * that its width is 2. */
6377 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6378 else if (enc_dbcs != 0)
6379 {
6380 /* find previous character by counting from first
6381 * column and get its width. */
6382 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006383 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006384
6385 while (off < off_to)
6386 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006387 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006388 off += prev_cells;
6389 }
6390 }
6391
6392 if (enc_dbcs != 0 && prev_cells > 1)
6393 screen_char_2(off_to - prev_cells, row,
6394 col + coloff - prev_cells);
6395 else
Bram Moolenaar9c697322006-10-09 20:11:17 +00006396 screen_char(off_to - prev_cells, row,
6397 col + coloff - prev_cells);
6398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 }
6400#endif
6401 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6402 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403 off_to += clear_width - col;
6404 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 }
6406 }
6407
6408 if (clear_width > 0)
6409 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410 /* For a window that's left of another, draw the separator char. */
6411 if (col + coloff < Columns)
6412 {
6413 int c;
6414
6415 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006416 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006417 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6418 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 || ScreenAttrs[off_to] != hl)
6420 {
6421 ScreenLines[off_to] = c;
6422 ScreenAttrs[off_to] = hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423 if (enc_utf8)
6424 {
6425 if (c >= 0x80)
6426 {
6427 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006428 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429 }
6430 else
6431 ScreenLinesUC[off_to] = 0;
6432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433 screen_char(off_to, row, col + coloff);
6434 }
6435 }
6436 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 LineWraps[row] = FALSE;
6438 }
6439}
6440
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006441#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006443 * Mirror text "str" for right-left displaying.
6444 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006446 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006447rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448{
6449 char_u *p1, *p2;
6450 int t;
6451
6452 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6453 {
6454 t = *p1;
6455 *p1 = *p2;
6456 *p2 = t;
6457 }
6458}
6459#endif
6460
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461/*
6462 * mark all status lines for redraw; used after first :cd
6463 */
6464 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006465status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466{
6467 win_T *wp;
6468
Bram Moolenaar29323592016-07-24 22:04:11 +02006469 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470 if (wp->w_status_height)
6471 {
6472 wp->w_redr_status = TRUE;
6473 redraw_later(VALID);
6474 }
6475}
6476
6477/*
6478 * mark all status lines of the current buffer for redraw
6479 */
6480 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006481status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482{
6483 win_T *wp;
6484
Bram Moolenaar29323592016-07-24 22:04:11 +02006485 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6487 {
6488 wp->w_redr_status = TRUE;
6489 redraw_later(VALID);
6490 }
6491}
6492
6493/*
6494 * Redraw all status lines that need to be redrawn.
6495 */
6496 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006497redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498{
6499 win_T *wp;
6500
Bram Moolenaar29323592016-07-24 22:04:11 +02006501 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006503 win_redr_status(wp, FALSE);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006504 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006505 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507
Bram Moolenaar4033c552017-09-16 20:54:51 +02006508#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509/*
6510 * Redraw all status lines at the bottom of frame "frp".
6511 */
6512 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006513win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514{
6515 if (frp->fr_layout == FR_LEAF)
6516 frp->fr_win->w_redr_status = TRUE;
6517 else if (frp->fr_layout == FR_ROW)
6518 {
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006519 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520 win_redraw_last_status(frp);
6521 }
6522 else /* frp->fr_layout == FR_COL */
6523 {
6524 frp = frp->fr_child;
6525 while (frp->fr_next != NULL)
6526 frp = frp->fr_next;
6527 win_redraw_last_status(frp);
6528 }
6529}
6530#endif
6531
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532/*
6533 * Draw the verticap separator right of window "wp" starting with line "row".
6534 */
6535 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006536draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537{
6538 int hl;
6539 int c;
6540
6541 if (wp->w_vsep_width)
6542 {
6543 /* draw the vertical separator right of this window */
6544 c = fillchar_vsep(&hl);
6545 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6546 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6547 c, ' ', hl);
6548 }
6549}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550
6551#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006552static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553
6554/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006555 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556 */
6557 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006558status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559{
6560 int len = 0;
6561
6562#ifdef FEAT_MENU
6563 int emenu = (xp->xp_context == EXPAND_MENUS
6564 || xp->xp_context == EXPAND_MENUNAMES);
6565
6566 /* Check for menu separators - replace with '|'. */
6567 if (emenu && menu_is_separator(s))
6568 return 1;
6569#endif
6570
6571 while (*s != NUL)
6572 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006573 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006574 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006575 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576 }
6577
6578 return len;
6579}
6580
6581/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006582 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006583 * These are backslashes used for escaping. Do show backslashes in help tags.
6584 */
6585 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006586skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006587{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006588 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006589#ifdef FEAT_MENU
6590 || ((xp->xp_context == EXPAND_MENUS
6591 || xp->xp_context == EXPAND_MENUNAMES)
6592 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6593#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006594 )
6595 {
6596#ifndef BACKSLASH_IN_FILENAME
6597 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6598 return 2;
6599#endif
6600 return 1;
6601 }
6602 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006603}
6604
6605/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 * Show wildchar matches in the status line.
6607 * Show at least the "match" item.
6608 * We start at item 'first_match' in the list and show all matches that fit.
6609 *
6610 * If inversion is possible we use it. Else '=' characters are used.
6611 */
6612 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006613win_redr_status_matches(
6614 expand_T *xp,
6615 int num_matches,
6616 char_u **matches, /* list of matches */
6617 int match,
6618 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619{
6620#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6621 int row;
6622 char_u *buf;
6623 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006624 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625 int fillchar;
6626 int attr;
6627 int i;
6628 int highlight = TRUE;
6629 char_u *selstart = NULL;
6630 int selstart_col = 0;
6631 char_u *selend = NULL;
6632 static int first_match = 0;
6633 int add_left = FALSE;
6634 char_u *s;
6635#ifdef FEAT_MENU
6636 int emenu;
6637#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639
6640 if (matches == NULL) /* interrupted completion? */
6641 return;
6642
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006643 if (has_mbyte)
6644 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6645 else
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006646 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 if (buf == NULL)
6648 return;
6649
6650 if (match == -1) /* don't show match but original text */
6651 {
6652 match = 0;
6653 highlight = FALSE;
6654 }
6655 /* count 1 for the ending ">" */
6656 clen = status_match_len(xp, L_MATCH(match)) + 3;
6657 if (match == 0)
6658 first_match = 0;
6659 else if (match < first_match)
6660 {
6661 /* jumping left, as far as we can go */
6662 first_match = match;
6663 add_left = TRUE;
6664 }
6665 else
6666 {
6667 /* check if match fits on the screen */
6668 for (i = first_match; i < match; ++i)
6669 clen += status_match_len(xp, L_MATCH(i)) + 2;
6670 if (first_match > 0)
6671 clen += 2;
6672 /* jumping right, put match at the left */
6673 if ((long)clen > Columns)
6674 {
6675 first_match = match;
6676 /* if showing the last match, we can add some on the left */
6677 clen = 2;
6678 for (i = match; i < num_matches; ++i)
6679 {
6680 clen += status_match_len(xp, L_MATCH(i)) + 2;
6681 if ((long)clen >= Columns)
6682 break;
6683 }
6684 if (i == num_matches)
6685 add_left = TRUE;
6686 }
6687 }
6688 if (add_left)
6689 while (first_match > 0)
6690 {
6691 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6692 if ((long)clen >= Columns)
6693 break;
6694 --first_match;
6695 }
6696
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006697 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698
6699 if (first_match == 0)
6700 {
6701 *buf = NUL;
6702 len = 0;
6703 }
6704 else
6705 {
6706 STRCPY(buf, "< ");
6707 len = 2;
6708 }
6709 clen = len;
6710
6711 i = first_match;
6712 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6713 {
6714 if (i == match)
6715 {
6716 selstart = buf + len;
6717 selstart_col = clen;
6718 }
6719
6720 s = L_MATCH(i);
6721 /* Check for menu separators - replace with '|' */
6722#ifdef FEAT_MENU
6723 emenu = (xp->xp_context == EXPAND_MENUS
6724 || xp->xp_context == EXPAND_MENUNAMES);
6725 if (emenu && menu_is_separator(s))
6726 {
6727 STRCPY(buf + len, transchar('|'));
6728 l = (int)STRLEN(buf + len);
6729 len += l;
6730 clen += l;
6731 }
6732 else
6733#endif
6734 for ( ; *s != NUL; ++s)
6735 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006736 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 clen += ptr2cells(s);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006738 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 {
6740 STRNCPY(buf + len, s, l);
6741 s += l - 1;
6742 len += l;
6743 }
6744 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745 {
6746 STRCPY(buf + len, transchar_byte(*s));
6747 len += (int)STRLEN(buf + len);
6748 }
6749 }
6750 if (i == match)
6751 selend = buf + len;
6752
6753 *(buf + len++) = ' ';
6754 *(buf + len++) = ' ';
6755 clen += 2;
6756 if (++i == num_matches)
6757 break;
6758 }
6759
6760 if (i != num_matches)
6761 {
6762 *(buf + len++) = '>';
6763 ++clen;
6764 }
6765
6766 buf[len] = NUL;
6767
6768 row = cmdline_row - 1;
6769 if (row >= 0)
6770 {
6771 if (wild_menu_showing == 0)
6772 {
6773 if (msg_scrolled > 0)
6774 {
6775 /* Put the wildmenu just above the command line. If there is
6776 * no room, scroll the screen one line up. */
6777 if (cmdline_row == Rows - 1)
6778 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02006779 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780 ++msg_scrolled;
6781 }
6782 else
6783 {
6784 ++cmdline_row;
6785 ++row;
6786 }
6787 wild_menu_showing = WM_SCROLLED;
6788 }
6789 else
6790 {
6791 /* Create status line if needed by setting 'laststatus' to 2.
6792 * Set 'winminheight' to zero to avoid that the window is
6793 * resized. */
6794 if (lastwin->w_status_height == 0)
6795 {
6796 save_p_ls = p_ls;
6797 save_p_wmh = p_wmh;
6798 p_ls = 2;
6799 p_wmh = 0;
6800 last_status(FALSE);
6801 }
6802 wild_menu_showing = WM_SHOWN;
6803 }
6804 }
6805
6806 screen_puts(buf, row, 0, attr);
6807 if (selstart != NULL && highlight)
6808 {
6809 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006810 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 }
6812
6813 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6814 }
6815
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817 vim_free(buf);
6818}
6819#endif
6820
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821/*
6822 * Redraw the status line of window wp.
6823 *
6824 * If inversion is possible we use it. Else '=' characters are used.
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006825 * If "ignore_pum" is TRUE, also redraw statusline when the popup menu is
6826 * displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 */
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006828 static void
Bram Moolenaar829adb72018-06-24 19:24:03 +02006829win_redr_status(win_T *wp, int ignore_pum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830{
6831 int row;
6832 char_u *p;
6833 int len;
6834 int fillchar;
6835 int attr;
6836 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006837 static int busy = FALSE;
6838
6839 /* It's possible to get here recursively when 'statusline' (indirectly)
6840 * invokes ":redrawstatus". Simply ignore the call then. */
6841 if (busy)
6842 return;
6843 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844
6845 wp->w_redr_status = FALSE;
6846 if (wp->w_status_height == 0)
6847 {
6848 /* no status line, can only be last window */
6849 redraw_cmdline = TRUE;
6850 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006851 else if (!redrawing()
6852#ifdef FEAT_INS_EXPAND
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006853 // don't update status line when popup menu is visible and may be
6854 // drawn over it, unless it will be redrawn later
6855 || (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006856#endif
6857 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858 {
6859 /* Don't redraw right now, do it later. */
6860 wp->w_redr_status = TRUE;
6861 }
6862#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006863 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864 {
6865 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006866 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867 }
6868#endif
6869 else
6870 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006871 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006873 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874 p = NameBuff;
6875 len = (int)STRLEN(p);
6876
Bram Moolenaard85f2712017-07-28 21:51:57 +02006877 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878#ifdef FEAT_QUICKFIX
6879 || wp->w_p_pvw
6880#endif
6881 || bufIsChanged(wp->w_buffer)
6882 || wp->w_buffer->b_p_ro)
6883 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02006884 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006886 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 len += (int)STRLEN(p + len);
6888 }
6889#ifdef FEAT_QUICKFIX
6890 if (wp->w_p_pvw)
6891 {
6892 STRCPY(p + len, _("[Preview]"));
6893 len += (int)STRLEN(p + len);
6894 }
6895#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02006896 if (bufIsChanged(wp->w_buffer)
6897#ifdef FEAT_TERMINAL
6898 && !bt_terminal(wp->w_buffer)
6899#endif
6900 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 {
6902 STRCPY(p + len, "[+]");
6903 len += 3;
6904 }
6905 if (wp->w_buffer->b_p_ro)
6906 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006907 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01006908 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909 }
6910
Bram Moolenaar02631462017-09-22 15:20:32 +02006911 this_ru_col = ru_col - (Columns - wp->w_width);
6912 if (this_ru_col < (wp->w_width + 1) / 2)
6913 this_ru_col = (wp->w_width + 1) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914 if (this_ru_col <= 1)
6915 {
6916 p = (char_u *)"<"; /* No room for file name! */
6917 len = 1;
6918 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01006919 else if (has_mbyte)
6920 {
6921 int clen = 0, i;
6922
6923 /* Count total number of display cells. */
6924 clen = mb_string2cells(p, -1);
6925
6926 /* Find first character that will fit.
6927 * Going from start to end is much faster for DBCS. */
6928 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
6929 i += (*mb_ptr2len)(p + i))
6930 clen -= (*mb_ptr2cells)(p + i);
6931 len = clen;
6932 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933 {
Bram Moolenaara12a1612019-01-24 16:39:02 +01006934 p = p + i - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935 *p = '<';
Bram Moolenaara12a1612019-01-24 16:39:02 +01006936 ++len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 }
6938
Bram Moolenaara12a1612019-01-24 16:39:02 +01006939 }
6940 else if (len > this_ru_col - 1)
6941 {
6942 p += len - (this_ru_col - 1);
6943 *p = '<';
6944 len = this_ru_col - 1;
6945 }
6946
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar53f81742017-09-22 14:35:51 +02006948 screen_puts(p, row, wp->w_wincol, attr);
6949 screen_fill(row, row + 1, len + wp->w_wincol,
6950 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02006952 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6954 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
Bram Moolenaar53f81742017-09-22 14:35:51 +02006955 - 1 + wp->w_wincol), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956
6957#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +02006958 win_redr_ruler(wp, TRUE, ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959#endif
6960 }
6961
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962 /*
6963 * May need to draw the character below the vertical separator.
6964 */
6965 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6966 {
6967 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006968 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969 else
6970 fillchar = fillchar_vsep(&attr);
6971 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6972 attr);
6973 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006974 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975}
6976
Bram Moolenaar238a5642006-02-21 22:12:05 +00006977#ifdef FEAT_STL_OPT
6978/*
6979 * Redraw the status line according to 'statusline' and take care of any
6980 * errors encountered.
6981 */
6982 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006983redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00006984{
Bram Moolenaar362f3562009-11-03 16:20:34 +00006985 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02006986 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006987
6988 /* When called recursively return. This can happen when the statusline
6989 * contains an expression that triggers a redraw. */
6990 if (entered)
6991 return;
6992 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006993
Bram Moolenaara742e082016-04-05 21:10:38 +02006994 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006995 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02006996 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006997 {
6998 /* When there is an error disable the statusline, otherwise the
6999 * display is messed up with errors and a redraw triggers the problem
7000 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007001 set_string_option_direct((char_u *)"statusline", -1,
7002 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007003 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007004 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007005 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007006 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007007}
7008#endif
7009
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010/*
7011 * Return TRUE if the status line of window "wp" is connected to the status
7012 * line of the window right of it. If not, then it's a vertical separator.
7013 * Only call if (wp->w_vsep_width != 0).
7014 */
7015 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007016stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017{
7018 frame_T *fr;
7019
7020 fr = wp->w_frame;
7021 while (fr->fr_parent != NULL)
7022 {
7023 if (fr->fr_parent->fr_layout == FR_COL)
7024 {
7025 if (fr->fr_next != NULL)
7026 break;
7027 }
7028 else
7029 {
7030 if (fr->fr_next != NULL)
7031 return TRUE;
7032 }
7033 fr = fr->fr_parent;
7034 }
7035 return FALSE;
7036}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039/*
7040 * Get the value to show for the language mappings, active 'keymap'.
7041 */
7042 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007043get_keymap_str(
7044 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007045 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007046 char_u *buf, /* buffer for the result */
7047 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048{
7049 char_u *p;
7050
7051 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7052 return FALSE;
7053
7054 {
7055#ifdef FEAT_EVAL
7056 buf_T *old_curbuf = curbuf;
7057 win_T *old_curwin = curwin;
7058 char_u *s;
7059
7060 curbuf = wp->w_buffer;
7061 curwin = wp;
7062 STRCPY(buf, "b:keymap_name"); /* must be writable */
7063 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007064 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 --emsg_skip;
7066 curbuf = old_curbuf;
7067 curwin = old_curwin;
7068 if (p == NULL || *p == NUL)
7069#endif
7070 {
7071#ifdef FEAT_KEYMAP
7072 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7073 p = wp->w_buffer->b_p_keymap;
7074 else
7075#endif
7076 p = (char_u *)"lang";
7077 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007078 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 buf[0] = NUL;
7080#ifdef FEAT_EVAL
7081 vim_free(s);
7082#endif
7083 }
7084 return buf[0] != NUL;
7085}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086
7087#if defined(FEAT_STL_OPT) || defined(PROTO)
7088/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007089 * Redraw the status line or ruler of window "wp".
7090 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091 */
7092 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007093win_redr_custom(
7094 win_T *wp,
7095 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007097 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098 int attr;
7099 int curattr;
7100 int row;
7101 int col = 0;
7102 int maxwidth;
7103 int width;
7104 int n;
7105 int len;
7106 int fillchar;
7107 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007108 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007110 struct stl_hlrec hltab[STL_MAX_ITEM];
7111 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007112 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007113 win_T *ewp;
7114 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115
Bram Moolenaar1d633412013-12-11 15:52:01 +01007116 /* There is a tiny chance that this gets called recursively: When
7117 * redrawing a status line triggers redrawing the ruler or tabline.
7118 * Avoid trouble by not allowing recursion. */
7119 if (entered)
7120 return;
7121 entered = TRUE;
7122
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007124 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007126 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007127 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007128 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007129 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007130 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007131 maxwidth = Columns;
7132# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007133 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007134# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007136 else
7137 {
7138 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007139 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02007140 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007141
7142 if (draw_ruler)
7143 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007144 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007145 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007146 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007147 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007148 if (*++stl == '-')
7149 stl++;
7150 if (atoi((char *)stl))
7151 while (VIM_ISDIGIT(*stl))
7152 stl++;
7153 if (*stl++ != '(')
7154 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007155 }
Bram Moolenaar02631462017-09-22 15:20:32 +02007156 col = ru_col - (Columns - wp->w_width);
7157 if (col < (wp->w_width + 1) / 2)
7158 col = (wp->w_width + 1) / 2;
7159 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007160 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007161 {
7162 row = Rows - 1;
7163 --maxwidth; /* writing in last column may cause scrolling */
7164 fillchar = ' ';
7165 attr = 0;
7166 }
7167
7168# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007169 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007170# endif
7171 }
7172 else
7173 {
7174 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007175 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007176 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007177 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007178# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007179 use_sandbox = was_set_insecurely((char_u *)"statusline",
7180 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007181# endif
7182 }
7183
Bram Moolenaar53f81742017-09-22 14:35:51 +02007184 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007185 }
7186
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007188 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189
Bram Moolenaar61452852011-02-01 18:01:11 +01007190 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7191 * the cursor away and back. */
7192 ewp = wp == NULL ? curwin : wp;
7193 p_crb_save = ewp->w_p_crb;
7194 ewp->w_p_crb = FALSE;
7195
Bram Moolenaar362f3562009-11-03 16:20:34 +00007196 /* Make a copy, because the statusline may include a function call that
7197 * might change the option value and free the memory. */
7198 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007199 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007200 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007201 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007202 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007203 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007205 /* Make all characters printable. */
7206 p = transstr(buf);
7207 if (p != NULL)
7208 {
7209 vim_strncpy(buf, p, sizeof(buf) - 1);
7210 vim_free(p);
7211 }
7212
7213 /* fill up with "fillchar" */
7214 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007215 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 len += (*mb_char2bytes)(fillchar, buf + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218 ++width;
7219 }
7220 buf[len] = NUL;
7221
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007222 /*
7223 * Draw each snippet with the specified highlighting.
7224 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225 curattr = attr;
7226 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007227 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007229 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230 screen_puts_len(p, len, row, col, curattr);
7231 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007232 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007234 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007236 else if (hltab[n].userhl < 0)
7237 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007238#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007239 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7240 && wp->w_status_height != 0)
7241 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007242 else if (wp != NULL && bt_terminal(wp->w_buffer)
7243 && wp->w_status_height != 0)
7244 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02007245#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007246 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007247 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007249 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 }
7251 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007252
7253 if (wp == NULL)
7254 {
7255 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7256 col = 0;
7257 len = 0;
7258 p = buf;
7259 fillchar = 0;
7260 for (n = 0; tabtab[n].start != NULL; n++)
7261 {
7262 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7263 while (col < len)
7264 TabPageIdxs[col++] = fillchar;
7265 p = tabtab[n].start;
7266 fillchar = tabtab[n].userhl;
7267 }
7268 while (col < Columns)
7269 TabPageIdxs[col++] = fillchar;
7270 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007271
7272theend:
7273 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274}
7275
7276#endif /* FEAT_STL_OPT */
7277
7278/*
7279 * Output a single character directly to the screen and update ScreenLines.
7280 */
7281 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007282screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284 char_u buf[MB_MAXBYTES + 1];
7285
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007286 if (has_mbyte)
7287 buf[(*mb_char2bytes)(c, buf)] = NUL;
7288 else
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007289 {
7290 buf[0] = c;
7291 buf[1] = NUL;
7292 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 screen_puts(buf, row, col, attr);
7294}
7295
7296/*
7297 * Get a single character directly from ScreenLines into "bytes[]".
7298 * Also return its attribute in *attrp;
7299 */
7300 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007301screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302{
7303 unsigned off;
7304
7305 /* safety check */
7306 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7307 {
7308 off = LineOffset[row] + col;
7309 *attrp = ScreenAttrs[off];
7310 bytes[0] = ScreenLines[off];
7311 bytes[1] = NUL;
7312
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313 if (enc_utf8 && ScreenLinesUC[off] != 0)
7314 bytes[utfc_char2bytes(off, bytes)] = NUL;
7315 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7316 {
7317 bytes[0] = ScreenLines[off];
7318 bytes[1] = ScreenLines2[off];
7319 bytes[2] = NUL;
7320 }
7321 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7322 {
7323 bytes[1] = ScreenLines[off + 1];
7324 bytes[2] = NUL;
7325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326 }
7327}
7328
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007329/*
7330 * Return TRUE if composing characters for screen posn "off" differs from
7331 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007332 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007333 */
7334 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007335screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007336{
7337 int i;
7338
7339 for (i = 0; i < Screen_mco; ++i)
7340 {
7341 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7342 return TRUE;
7343 if (u8cc[i] == 0)
7344 break;
7345 }
7346 return FALSE;
7347}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007348
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349/*
7350 * Put string '*text' on the screen at position 'row' and 'col', with
7351 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7352 * Note: only outputs within one row, message is truncated at screen boundary!
7353 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7354 */
7355 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007356screen_puts(
7357 char_u *text,
7358 int row,
7359 int col,
7360 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361{
7362 screen_puts_len(text, -1, row, col, attr);
7363}
7364
7365/*
7366 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7367 * a NUL.
7368 */
7369 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007370screen_puts_len(
7371 char_u *text,
7372 int textlen,
7373 int row,
7374 int col,
7375 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376{
7377 unsigned off;
7378 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007379 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 int c;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007381 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 int mbyte_blen = 1;
7383 int mbyte_cells = 1;
7384 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007385 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386 int clear_next_cell = FALSE;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007387#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007389 int pc, nc, nc1;
7390 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007392 int force_redraw_this;
7393 int force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007394 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395
7396 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7397 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007398 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399
Bram Moolenaarc236c162008-07-13 17:41:49 +00007400 /* When drawing over the right halve of a double-wide char clear out the
7401 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007402 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaara12a1612019-01-24 16:39:02 +01007403#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00007404 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01007405#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007406 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007407 {
7408 ScreenLines[off - 1] = ' ';
7409 ScreenAttrs[off - 1] = 0;
7410 if (enc_utf8)
7411 {
7412 ScreenLinesUC[off - 1] = 0;
7413 ScreenLinesC[0][off - 1] = 0;
7414 }
7415 /* redraw the previous cell, make it empty */
7416 screen_char(off - 1, row, col - 1);
7417 /* force the cell at "col" to be redrawn */
7418 force_redraw_next = TRUE;
7419 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007420
Bram Moolenaar367329b2007-08-30 11:53:22 +00007421 max_off = LineOffset[row] + screen_Columns;
Bram Moolenaara064ac82007-08-05 18:10:54 +00007422 while (col < screen_Columns
7423 && (len < 0 || (int)(ptr - text) < len)
7424 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425 {
7426 c = *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 /* check if this is the first byte of a multibyte */
7428 if (has_mbyte)
7429 {
7430 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007431 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007433 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7435 mbyte_cells = 1;
7436 else if (enc_dbcs != 0)
7437 mbyte_cells = mbyte_blen;
7438 else /* enc_utf8 */
7439 {
7440 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007441 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442 (int)((text + len) - ptr));
7443 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007444 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007446#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7448 {
7449 /* Do Arabic shaping. */
7450 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7451 {
7452 /* Past end of string to be displayed. */
7453 nc = NUL;
7454 nc1 = NUL;
7455 }
7456 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007457 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007458 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7459 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007460 nc1 = pcc[0];
7461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462 pc = prev_c;
7463 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007464 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465 }
7466 else
7467 prev_c = u8c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007468#endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007469 if (col + mbyte_cells > screen_Columns)
7470 {
7471 /* Only 1 cell left, but character requires 2 cells:
7472 * display a '>' in the last column to avoid wrapping. */
7473 c = '>';
7474 mbyte_cells = 1;
7475 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476 }
7477 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007479 force_redraw_this = force_redraw_next;
7480 force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007481
7482 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 || (mbyte_cells == 2
7484 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7485 || (enc_dbcs == DBCS_JPNU
7486 && c == 0x8e
7487 && ScreenLines2[off] != ptr[1])
7488 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007489 && (ScreenLinesUC[off] !=
7490 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7491 || (ScreenLinesUC[off] != 0
7492 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007494 || exmode_active;
7495
Bram Moolenaara12a1612019-01-24 16:39:02 +01007496 if (need_redraw || force_redraw_this)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497 {
7498#if defined(FEAT_GUI) || defined(UNIX)
7499 /* The bold trick makes a single row of pixels appear in the next
7500 * character. When a bold character is removed, the next
7501 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007502 * and for some xterms. */
7503 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504# ifdef FEAT_GUI
7505 gui.in_use
7506# endif
7507# if defined(FEAT_GUI) && defined(UNIX)
7508 ||
7509# endif
7510# ifdef UNIX
7511 term_is_xterm
7512# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007513 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007515 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007517 if (n > HL_ALL)
7518 n = syn_attr2attr(n);
7519 if (n & HL_BOLD)
7520 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521 }
7522#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523 /* When at the end of the text and overwriting a two-cell
7524 * character with a one-cell character, need to clear the next
7525 * cell. Also when overwriting the left halve of a two-cell char
7526 * with the right halve of a two-cell char. Do this only once
7527 * (mb_off2cells() may return 2 on the right halve). */
7528 if (clear_next_cell)
7529 clear_next_cell = FALSE;
7530 else if (has_mbyte
7531 && (len < 0 ? ptr[mbyte_blen] == NUL
7532 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007533 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007535 && (*mb_off2cells)(off, max_off) == 1
7536 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537 clear_next_cell = TRUE;
7538
7539 /* Make sure we never leave a second byte of a double-byte behind,
7540 * it confuses mb_off2cells(). */
7541 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007542 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007544 && (*mb_off2cells)(off, max_off) == 1
7545 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546 ScreenLines[off + mbyte_blen] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547 ScreenLines[off] = c;
7548 ScreenAttrs[off] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549 if (enc_utf8)
7550 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007551 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552 ScreenLinesUC[off] = 0;
7553 else
7554 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007555 int i;
7556
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007558 for (i = 0; i < Screen_mco; ++i)
7559 {
7560 ScreenLinesC[i][off] = u8cc[i];
7561 if (u8cc[i] == 0)
7562 break;
7563 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007564 }
7565 if (mbyte_cells == 2)
7566 {
7567 ScreenLines[off + 1] = 0;
7568 ScreenAttrs[off + 1] = attr;
7569 }
7570 screen_char(off, row, col);
7571 }
7572 else if (mbyte_cells == 2)
7573 {
7574 ScreenLines[off + 1] = ptr[1];
7575 ScreenAttrs[off + 1] = attr;
7576 screen_char_2(off, row, col);
7577 }
7578 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7579 {
7580 ScreenLines2[off] = ptr[1];
7581 screen_char(off, row, col);
7582 }
7583 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584 screen_char(off, row, col);
7585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586 if (has_mbyte)
7587 {
7588 off += mbyte_cells;
7589 col += mbyte_cells;
7590 ptr += mbyte_blen;
7591 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007592 {
7593 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007594 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007595 len = -1;
7596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597 }
7598 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599 {
7600 ++off;
7601 ++col;
7602 ++ptr;
7603 }
7604 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007605
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007606 /* If we detected the next character needs to be redrawn, but the text
7607 * doesn't extend up to there, update the character here. */
7608 if (force_redraw_next && col < screen_Columns)
7609 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007610 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7611 screen_char_2(off, row, col);
7612 else
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007613 screen_char(off, row, col);
7614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615}
7616
7617#ifdef FEAT_SEARCH_EXTRA
7618/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007619 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620 */
7621 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007622start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623{
7624 if (p_hls && !no_hlsearch)
7625 {
7626 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007627 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007628# ifdef FEAT_RELTIME
7629 /* Set the time limit to 'redrawtime'. */
7630 profile_setlimit(p_rdt, &search_hl.tm);
7631# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632 }
7633}
7634
7635/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007636 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637 */
7638 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007639end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640{
7641 if (search_hl.rm.regprog != NULL)
7642 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007643 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644 search_hl.rm.regprog = NULL;
7645 }
7646}
7647
7648/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007649 * Init for calling prepare_search_hl().
7650 */
7651 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007652init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007653{
7654 matchitem_T *cur;
7655
7656 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7657 * match */
7658 cur = wp->w_match_head;
7659 while (cur != NULL)
7660 {
7661 cur->hl.rm = cur->match;
7662 if (cur->hlg_id == 0)
7663 cur->hl.attr = 0;
7664 else
7665 cur->hl.attr = syn_id2attr(cur->hlg_id);
7666 cur->hl.buf = wp->w_buffer;
7667 cur->hl.lnum = 0;
7668 cur->hl.first_lnum = 0;
7669# ifdef FEAT_RELTIME
7670 /* Set the time limit to 'redrawtime'. */
7671 profile_setlimit(p_rdt, &(cur->hl.tm));
7672# endif
7673 cur = cur->next;
7674 }
7675 search_hl.buf = wp->w_buffer;
7676 search_hl.lnum = 0;
7677 search_hl.first_lnum = 0;
7678 /* time limit is set at the toplevel, for all windows */
7679}
7680
7681/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682 * Advance to the match in window "wp" line "lnum" or past it.
7683 */
7684 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007685prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007687 matchitem_T *cur; /* points to the match list */
7688 match_T *shl; /* points to search_hl or a match */
7689 int shl_flag; /* flag to indicate whether search_hl
7690 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007691 int pos_inprogress; /* marks that position match search is
7692 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693 int n;
7694
7695 /*
7696 * When using a multi-line pattern, start searching at the top
7697 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007698 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007700 cur = wp->w_match_head;
7701 shl_flag = FALSE;
7702 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007704 if (shl_flag == FALSE)
7705 {
7706 shl = &search_hl;
7707 shl_flag = TRUE;
7708 }
7709 else
7710 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711 if (shl->rm.regprog != NULL
7712 && shl->lnum == 0
7713 && re_multiline(shl->rm.regprog))
7714 {
7715 if (shl->first_lnum == 0)
7716 {
7717# ifdef FEAT_FOLDING
7718 for (shl->first_lnum = lnum;
7719 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7720 if (hasFoldingWin(wp, shl->first_lnum - 1,
7721 NULL, NULL, TRUE, NULL))
7722 break;
7723# else
7724 shl->first_lnum = wp->w_topline;
7725# endif
7726 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007727 if (cur != NULL)
7728 cur->pos.cur = 0;
7729 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007731 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7732 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02007734 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
7735 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007736 pos_inprogress = cur == NULL || cur->pos.cur == 0
7737 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738 if (shl->lnum != 0)
7739 {
7740 shl->first_lnum = shl->lnum
7741 + shl->rm.endpos[0].lnum
7742 - shl->rm.startpos[0].lnum;
7743 n = shl->rm.endpos[0].col;
7744 }
7745 else
7746 {
7747 ++shl->first_lnum;
7748 n = 0;
7749 }
7750 }
7751 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007752 if (shl != &search_hl && cur != NULL)
7753 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754 }
7755}
7756
7757/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007758 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759 * Uses shl->buf.
7760 * Sets shl->lnum and shl->rm contents.
7761 * Note: Assumes a previous match is always before "lnum", unless
7762 * shl->lnum is zero.
7763 * Careful: Any pointers for buffer lines will become invalid.
7764 */
7765 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007766next_search_hl(
7767 win_T *win,
7768 match_T *shl, /* points to search_hl or a match */
7769 linenr_T lnum,
7770 colnr_T mincol, /* minimal column for a match */
7771 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772{
7773 linenr_T l;
7774 colnr_T matchcol;
7775 long nmatched;
Bram Moolenaarbcf94422018-06-23 14:21:42 +02007776 int save_called_emsg = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777
Bram Moolenaarb0acacd2018-08-11 16:40:43 +02007778 // for :{range}s/pat only highlight inside the range
7779 if (lnum < search_first_line || lnum > search_last_line)
7780 {
7781 shl->lnum = 0;
7782 return;
7783 }
7784
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785 if (shl->lnum != 0)
7786 {
7787 /* Check for three situations:
7788 * 1. If the "lnum" is below a previous match, start a new search.
7789 * 2. If the previous match includes "mincol", use it.
7790 * 3. Continue after the previous match.
7791 */
7792 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7793 if (lnum > l)
7794 shl->lnum = 0;
7795 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7796 return;
7797 }
7798
7799 /*
7800 * Repeat searching for a match until one is found that includes "mincol"
7801 * or none is found in this line.
7802 */
7803 called_emsg = FALSE;
7804 for (;;)
7805 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007806#ifdef FEAT_RELTIME
7807 /* Stop searching after passing the time limit. */
7808 if (profile_passed_limit(&(shl->tm)))
7809 {
7810 shl->lnum = 0; /* no match found in time */
7811 break;
7812 }
7813#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814 /* Three situations:
7815 * 1. No useful previous match: search from start of line.
7816 * 2. Not Vi compatible or empty match: continue at next character.
7817 * Break the loop if this is beyond the end of the line.
7818 * 3. Vi compatible searching: continue at end of previous match.
7819 */
7820 if (shl->lnum == 0)
7821 matchcol = 0;
7822 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7823 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007824 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007826 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007827
7828 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007829 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007830 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007832 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833 shl->lnum = 0;
7834 break;
7835 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007836 if (has_mbyte)
7837 matchcol += mb_ptr2len(ml);
7838 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007839 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840 }
7841 else
7842 matchcol = shl->rm.endpos[0].col;
7843
7844 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007845 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007847 /* Remember whether shl->rm is using a copy of the regprog in
7848 * cur->match. */
7849 int regprog_is_copy = (shl != &search_hl && cur != NULL
7850 && shl == &cur->hl
7851 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007852 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007853
Bram Moolenaarb3414592014-06-17 17:48:32 +02007854 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7855 matchcol,
7856#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007857 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02007858#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007859 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02007860#endif
7861 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007862 /* Copy the regprog, in case it got freed and recompiled. */
7863 if (regprog_is_copy)
7864 cur->match.regprog = cur->hl.rm.regprog;
7865
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007866 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007867 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007868 /* Error while handling regexp: stop using this regexp. */
7869 if (shl == &search_hl)
7870 {
7871 /* don't free regprog in the match list, it's a copy */
7872 vim_regfree(shl->rm.regprog);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02007873 set_no_hlsearch(TRUE);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007874 }
7875 shl->rm.regprog = NULL;
7876 shl->lnum = 0;
7877 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
7878 message */
7879 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007880 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007881 }
7882 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007883 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007884 else
7885 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 if (nmatched == 0)
7887 {
7888 shl->lnum = 0; /* no match found */
7889 break;
7890 }
7891 if (shl->rm.startpos[0].lnum > 0
7892 || shl->rm.startpos[0].col >= mincol
7893 || nmatched > 1
7894 || shl->rm.endpos[0].col > mincol)
7895 {
7896 shl->lnum += shl->rm.startpos[0].lnum;
7897 break; /* useful match found */
7898 }
7899 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +02007900
7901 // Restore called_emsg for assert_fails().
7902 called_emsg = save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904
Bram Moolenaar85077472016-10-16 14:35:48 +02007905/*
7906 * If there is a match fill "shl" and return one.
7907 * Return zero otherwise.
7908 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007909 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007910next_search_hl_pos(
7911 match_T *shl, /* points to a match */
7912 linenr_T lnum,
7913 posmatch_T *posmatch, /* match positions */
7914 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007915{
7916 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02007917 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007918
Bram Moolenaarb3414592014-06-17 17:48:32 +02007919 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
7920 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007921 llpos_T *pos = &posmatch->pos[i];
7922
7923 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007924 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02007925 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007926 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007927 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007928 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007929 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007930 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007931 /* if this match comes before the one at "found" then swap
7932 * them */
7933 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007934 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007935 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007936
Bram Moolenaar85077472016-10-16 14:35:48 +02007937 *pos = posmatch->pos[found];
7938 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007939 }
7940 }
7941 else
Bram Moolenaar85077472016-10-16 14:35:48 +02007942 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007943 }
7944 }
7945 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02007946 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007947 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007948 colnr_T start = posmatch->pos[found].col == 0
7949 ? 0 : posmatch->pos[found].col - 1;
7950 colnr_T end = posmatch->pos[found].col == 0
7951 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007952
Bram Moolenaar85077472016-10-16 14:35:48 +02007953 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007954 shl->rm.startpos[0].lnum = 0;
7955 shl->rm.startpos[0].col = start;
7956 shl->rm.endpos[0].lnum = 0;
7957 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02007958 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02007959 posmatch->cur = found + 1;
7960 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007961 }
Bram Moolenaar85077472016-10-16 14:35:48 +02007962 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007963}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02007964#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02007965
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007967screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968{
7969 attrentry_T *aep = NULL;
7970
7971 screen_attr = attr;
7972 if (full_screen
Bram Moolenaar4f974752019-02-17 17:44:42 +01007973#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974 && termcap_active
7975#endif
7976 )
7977 {
7978#ifdef FEAT_GUI
7979 if (gui.in_use)
7980 {
7981 char buf[20];
7982
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007983 /* The GUI handles this internally. */
7984 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985 OUT_STR(buf);
7986 }
7987 else
7988#endif
7989 {
7990 if (attr > HL_ALL) /* special HL attr. */
7991 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007992 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993 aep = syn_cterm_attr2entry(attr);
7994 else
7995 aep = syn_term_attr2entry(attr);
7996 if (aep == NULL) /* did ":syntax clear" */
7997 attr = 0;
7998 else
7999 attr = aep->ae_attr;
8000 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008001 if ((attr & HL_BOLD) && *T_MD != NUL) /* bold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008003 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008004#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008005 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8006 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8007 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008008#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008009 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008010 /* If the Normal FG color has BOLD attribute and the new HL
8011 * has a FG color defined, clear BOLD. */
8012 out_str(T_ME);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008013 if ((attr & HL_STANDOUT) && *T_SO != NUL) /* standout */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014 out_str(T_SO);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008015 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) /* undercurl */
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008016 out_str(T_UCS);
8017 if (((attr & HL_UNDERLINE) /* underline or undercurl */
Bram Moolenaar45a00002017-12-22 21:12:34 +01008018 || ((attr & HL_UNDERCURL) && *T_UCS == NUL))
8019 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008020 out_str(T_US);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008021 if ((attr & HL_ITALIC) && *T_CZH != NUL) /* italic */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022 out_str(T_CZH);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008023 if ((attr & HL_INVERSE) && *T_MR != NUL) /* inverse (reverse) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024 out_str(T_MR);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008025 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) /* strike */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008026 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027
8028 /*
8029 * Output the color or start string after bold etc., in case the
8030 * bold etc. override the color setting.
8031 */
8032 if (aep != NULL)
8033 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008034#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008035 /* When 'termguicolors' is set but fg or bg is unset,
8036 * fall back to the cterm colors. This helps for SpellBad,
8037 * where the GUI uses a red undercurl. */
8038 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008040 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008041 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008042 }
8043 else
8044#endif
8045 if (t_colors > 1)
8046 {
8047 if (aep->ae_u.cterm.fg_color)
8048 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8049 }
8050#ifdef FEAT_TERMGUICOLORS
8051 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
8052 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008053 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008054 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055 }
8056 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008057#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008058 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008060 if (aep->ae_u.cterm.bg_color)
8061 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8062 }
8063
Bram Moolenaarf708ac52018-03-12 21:48:32 +01008064 if (!IS_CTERM)
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008065 {
8066 if (aep->ae_u.term.start != NULL)
8067 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068 }
8069 }
8070 }
8071 }
8072}
8073
8074 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008075screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076{
8077 int do_ME = FALSE; /* output T_ME code */
8078
8079 if (screen_attr != 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01008080#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081 && termcap_active
8082#endif
8083 )
8084 {
8085#ifdef FEAT_GUI
8086 if (gui.in_use)
8087 {
8088 char buf[20];
8089
8090 /* use internal GUI code */
8091 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8092 OUT_STR(buf);
8093 }
8094 else
8095#endif
8096 {
8097 if (screen_attr > HL_ALL) /* special HL attr. */
8098 {
8099 attrentry_T *aep;
8100
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008101 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102 {
8103 /*
8104 * Assume that t_me restores the original colors!
8105 */
8106 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008107 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008108#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008109 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8110 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8111 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008112#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008113 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008114#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008115 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
8116 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
8117 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008118#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008119 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120 do_ME = TRUE;
8121 }
8122 else
8123 {
8124 aep = syn_term_attr2entry(screen_attr);
8125 if (aep != NULL && aep->ae_u.term.stop != NULL)
8126 {
8127 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8128 do_ME = TRUE;
8129 else
8130 out_str(aep->ae_u.term.stop);
8131 }
8132 }
8133 if (aep == NULL) /* did ":syntax clear" */
8134 screen_attr = 0;
8135 else
8136 screen_attr = aep->ae_attr;
8137 }
8138
8139 /*
8140 * Often all ending-codes are equal to T_ME. Avoid outputting the
8141 * same sequence several times.
8142 */
8143 if (screen_attr & HL_STANDOUT)
8144 {
8145 if (STRCMP(T_SE, T_ME) == 0)
8146 do_ME = TRUE;
8147 else
8148 out_str(T_SE);
8149 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008150 if ((screen_attr & HL_UNDERCURL) && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008151 {
8152 if (STRCMP(T_UCE, T_ME) == 0)
8153 do_ME = TRUE;
8154 else
8155 out_str(T_UCE);
8156 }
8157 if ((screen_attr & HL_UNDERLINE)
Bram Moolenaar45a00002017-12-22 21:12:34 +01008158 || ((screen_attr & HL_UNDERCURL) && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159 {
8160 if (STRCMP(T_UE, T_ME) == 0)
8161 do_ME = TRUE;
8162 else
8163 out_str(T_UE);
8164 }
8165 if (screen_attr & HL_ITALIC)
8166 {
8167 if (STRCMP(T_CZR, T_ME) == 0)
8168 do_ME = TRUE;
8169 else
8170 out_str(T_CZR);
8171 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008172 if (screen_attr & HL_STRIKETHROUGH)
8173 {
8174 if (STRCMP(T_STE, T_ME) == 0)
8175 do_ME = TRUE;
8176 else
8177 out_str(T_STE);
8178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8180 out_str(T_ME);
8181
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008182#ifdef FEAT_TERMGUICOLORS
8183 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008185 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008186 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008187 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008188 term_bg_rgb_color(cterm_normal_bg_gui_color);
8189 }
8190 else
8191#endif
8192 {
8193 if (t_colors > 1)
8194 {
8195 /* set Normal cterm colors */
8196 if (cterm_normal_fg_color != 0)
8197 term_fg_color(cterm_normal_fg_color - 1);
8198 if (cterm_normal_bg_color != 0)
8199 term_bg_color(cterm_normal_bg_color - 1);
8200 if (cterm_normal_fg_bold)
8201 out_str(T_MD);
8202 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008203 }
8204 }
8205 }
8206 screen_attr = 0;
8207}
8208
8209/*
8210 * Reset the colors for a cterm. Used when leaving Vim.
8211 * The machine specific code may override this again.
8212 */
8213 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008214reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008216 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217 {
8218 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008219#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008220 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8221 || cterm_normal_bg_gui_color != INVALCOLOR)
8222 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008223#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008225#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226 {
8227 out_str(T_OP);
8228 screen_attr = -1;
8229 }
8230 if (cterm_normal_fg_bold)
8231 {
8232 out_str(T_ME);
8233 screen_attr = -1;
8234 }
8235 }
8236}
8237
8238/*
8239 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8240 * using the attributes from ScreenAttrs["off"].
8241 */
8242 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008243screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244{
8245 int attr;
8246
8247 /* Check for illegal values, just in case (could happen just after
8248 * resizing). */
8249 if (row >= screen_Rows || col >= screen_Columns)
8250 return;
8251
Bram Moolenaarae654382019-01-17 21:09:05 +01008252#ifdef FEAT_INS_EXPAND
8253 if (pum_under_menu(row, col))
8254 return;
8255#endif
Bram Moolenaar494838a2015-02-10 19:20:37 +01008256 /* Outputting a character in the last cell on the screen may scroll the
8257 * screen up. Only do it when the "xn" termcap property is set, otherwise
8258 * mark the character invalid (update it when scrolled up). */
8259 if (*T_XN == NUL
8260 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261#ifdef FEAT_RIGHTLEFT
8262 /* account for first command-line character in rightleft mode */
8263 && !cmdmsg_rl
8264#endif
8265 )
8266 {
8267 ScreenAttrs[off] = (sattr_T)-1;
8268 return;
8269 }
8270
8271 /*
8272 * Stop highlighting first, so it's easier to move the cursor.
8273 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274 if (screen_char_attr != 0)
8275 attr = screen_char_attr;
8276 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277 attr = ScreenAttrs[off];
8278 if (screen_attr != attr)
8279 screen_stop_highlight();
8280
8281 windgoto(row, col);
8282
8283 if (screen_attr != attr)
8284 screen_start_highlight(attr);
8285
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286 if (enc_utf8 && ScreenLinesUC[off] != 0)
8287 {
8288 char_u buf[MB_MAXBYTES + 1];
8289
Bram Moolenaarcb070082016-04-02 22:14:51 +02008290 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008291 {
8292 if (*p_ambw == 'd'
Bram Moolenaara12a1612019-01-24 16:39:02 +01008293#ifdef FEAT_GUI
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008294 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008295#endif
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008296 )
8297 {
8298 /* Clear the two screen cells. If the character is actually
8299 * single width it won't change the second cell. */
8300 out_str((char_u *)" ");
8301 term_windgoto(row, col);
8302 }
8303 /* not sure where the cursor is after drawing the ambiguous width
8304 * character */
Bram Moolenaarcb070082016-04-02 22:14:51 +02008305 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008306 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02008307 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008309
8310 /* Convert the UTF-8 character to bytes and write it. */
8311 buf[utfc_char2bytes(off, buf)] = NUL;
8312 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313 }
8314 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 /* double-byte character in single-width cell */
8319 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8320 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 }
8322
8323 screen_cur_col++;
8324}
8325
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326/*
8327 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8328 * on the screen at position 'row' and 'col'.
8329 * The attributes of the first byte is used for all. This is required to
8330 * output the two bytes of a double-byte character with nothing in between.
8331 */
8332 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008333screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334{
8335 /* Check for illegal values (could be wrong when screen was resized). */
8336 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8337 return;
8338
8339 /* Outputting the last character on the screen may scrollup the screen.
8340 * Don't to it! Mark the character invalid (update it when scrolled up) */
8341 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8342 {
8343 ScreenAttrs[off] = (sattr_T)-1;
8344 return;
8345 }
8346
8347 /* Output the first byte normally (positions the cursor), then write the
8348 * second byte directly. */
8349 screen_char(off, row, col);
8350 out_char(ScreenLines[off + 1]);
8351 ++screen_cur_col;
8352}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353
Bram Moolenaar071d4272004-06-13 20:20:40 +00008354/*
8355 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8356 * This uses the contents of ScreenLines[] and doesn't change it.
8357 */
8358 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008359screen_draw_rectangle(
8360 int row,
8361 int col,
8362 int height,
8363 int width,
8364 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365{
8366 int r, c;
8367 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008368 int max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008370 /* Can't use ScreenLines unless initialized */
8371 if (ScreenLines == NULL)
8372 return;
8373
Bram Moolenaar071d4272004-06-13 20:20:40 +00008374 if (invert)
8375 screen_char_attr = HL_INVERSE;
8376 for (r = row; r < row + height; ++r)
8377 {
8378 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008379 max_off = off + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380 for (c = col; c < col + width; ++c)
8381 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00008382 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383 {
8384 screen_char_2(off + c, r, c);
8385 ++c;
8386 }
8387 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388 {
8389 screen_char(off + c, r, c);
Bram Moolenaar367329b2007-08-30 11:53:22 +00008390 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391 ++c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392 }
8393 }
8394 }
8395 screen_char_attr = 0;
8396}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397
Bram Moolenaar071d4272004-06-13 20:20:40 +00008398/*
8399 * Redraw the characters for a vertically split window.
8400 */
8401 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008402redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403{
8404 int col;
8405 int width;
8406
8407# ifdef FEAT_CLIPBOARD
8408 clip_may_clear_selection(row, end - 1);
8409# endif
8410
8411 if (wp == NULL)
8412 {
8413 col = 0;
8414 width = Columns;
8415 }
8416 else
8417 {
8418 col = wp->w_wincol;
8419 width = wp->w_width;
8420 }
8421 screen_draw_rectangle(row, col, end - row, width, FALSE);
8422}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008424 static void
8425space_to_screenline(int off, int attr)
8426{
8427 ScreenLines[off] = ' ';
8428 ScreenAttrs[off] = attr;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008429 if (enc_utf8)
8430 ScreenLinesUC[off] = 0;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008431}
8432
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433/*
8434 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8435 * with character 'c1' in first column followed by 'c2' in the other columns.
8436 * Use attributes 'attr'.
8437 */
8438 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008439screen_fill(
8440 int start_row,
8441 int end_row,
8442 int start_col,
8443 int end_col,
8444 int c1,
8445 int c2,
8446 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447{
8448 int row;
8449 int col;
8450 int off;
8451 int end_off;
8452 int did_delete;
8453 int c;
8454 int norm_term;
8455#if defined(FEAT_GUI) || defined(UNIX)
8456 int force_next = FALSE;
8457#endif
8458
8459 if (end_row > screen_Rows) /* safety check */
8460 end_row = screen_Rows;
8461 if (end_col > screen_Columns) /* safety check */
8462 end_col = screen_Columns;
8463 if (ScreenLines == NULL
8464 || start_row >= end_row
8465 || start_col >= end_col) /* nothing to do */
8466 return;
8467
8468 /* it's a "normal" terminal when not in a GUI or cterm */
8469 norm_term = (
8470#ifdef FEAT_GUI
8471 !gui.in_use &&
8472#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008473 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008474 for (row = start_row; row < end_row; ++row)
8475 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008476 if (has_mbyte
Bram Moolenaara12a1612019-01-24 16:39:02 +01008477#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00008478 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008479#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00008480 )
8481 {
8482 /* When drawing over the right halve of a double-wide char clear
8483 * out the left halve. When drawing over the left halve of a
8484 * double wide-char clear out the right halve. Only needed in a
8485 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008486 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008487 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008488 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008489 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491 /*
8492 * Try to use delete-line termcap code, when no attributes or in a
8493 * "normal" terminal, where a bold/italic space is just a
8494 * space.
8495 */
8496 did_delete = FALSE;
8497 if (c2 == ' '
8498 && end_col == Columns
8499 && can_clear(T_CE)
8500 && (attr == 0
8501 || (norm_term
8502 && attr <= HL_ALL
8503 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8504 {
8505 /*
8506 * check if we really need to clear something
8507 */
8508 col = start_col;
8509 if (c1 != ' ') /* don't clear first char */
8510 ++col;
8511
8512 off = LineOffset[row] + col;
8513 end_off = LineOffset[row] + end_col;
8514
8515 /* skip blanks (used often, keep it fast!) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516 if (enc_utf8)
8517 while (off < end_off && ScreenLines[off] == ' '
8518 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8519 ++off;
8520 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521 while (off < end_off && ScreenLines[off] == ' '
8522 && ScreenAttrs[off] == 0)
8523 ++off;
8524 if (off < end_off) /* something to be cleared */
8525 {
8526 col = off - LineOffset[row];
8527 screen_stop_highlight();
8528 term_windgoto(row, col);/* clear rest of this screen line */
8529 out_str(T_CE);
8530 screen_start(); /* don't know where cursor is now */
8531 col = end_col - col;
8532 while (col--) /* clear chars in ScreenLines */
8533 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008534 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535 ++off;
8536 }
8537 }
8538 did_delete = TRUE; /* the chars are cleared now */
8539 }
8540
8541 off = LineOffset[row] + start_col;
8542 c = c1;
8543 for (col = start_col; col < end_col; ++col)
8544 {
8545 if (ScreenLines[off] != c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008546 || (enc_utf8 && (int)ScreenLinesUC[off]
8547 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548 || ScreenAttrs[off] != attr
8549#if defined(FEAT_GUI) || defined(UNIX)
8550 || force_next
8551#endif
8552 )
8553 {
8554#if defined(FEAT_GUI) || defined(UNIX)
8555 /* The bold trick may make a single row of pixels appear in
8556 * the next character. When a bold character is removed, the
8557 * next character should be redrawn too. This happens for our
8558 * own GUI and for some xterms. */
8559 if (
8560# ifdef FEAT_GUI
8561 gui.in_use
8562# endif
8563# if defined(FEAT_GUI) && defined(UNIX)
8564 ||
8565# endif
8566# ifdef UNIX
8567 term_is_xterm
8568# endif
8569 )
8570 {
8571 if (ScreenLines[off] != ' '
8572 && (ScreenAttrs[off] > HL_ALL
8573 || ScreenAttrs[off] & HL_BOLD))
8574 force_next = TRUE;
8575 else
8576 force_next = FALSE;
8577 }
8578#endif
8579 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580 if (enc_utf8)
8581 {
8582 if (c >= 0x80)
8583 {
8584 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008585 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586 }
8587 else
8588 ScreenLinesUC[off] = 0;
8589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590 ScreenAttrs[off] = attr;
8591 if (!did_delete || c != ' ')
8592 screen_char(off, row, col);
8593 }
8594 ++off;
8595 if (col == start_col)
8596 {
8597 if (did_delete)
8598 break;
8599 c = c2;
8600 }
8601 }
8602 if (end_col == Columns)
8603 LineWraps[row] = FALSE;
8604 if (row == Rows - 1) /* overwritten the command line */
8605 {
8606 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02008607 if (start_col == 0 && end_col == Columns
8608 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008610 if (start_col == 0)
8611 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612 }
8613 }
8614}
8615
8616/*
8617 * Check if there should be a delay. Used before clearing or redrawing the
8618 * screen or the command line.
8619 */
8620 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008621check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622{
8623 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8624 && !did_wait_return
8625 && emsg_silent == 0)
8626 {
8627 out_flush();
8628 ui_delay(1000L, TRUE);
8629 emsg_on_display = FALSE;
8630 if (check_msg_scroll)
8631 msg_scroll = FALSE;
8632 }
8633}
8634
8635/*
8636 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008637 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638 * Returns TRUE if there is a valid screen to write to.
8639 * Returns FALSE when starting up and screen not initialized yet.
8640 */
8641 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008642screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008644 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645 return (ScreenLines != NULL);
8646}
8647
8648/*
8649 * Resize the shell to Rows and Columns.
8650 * Allocate ScreenLines[] and associated items.
8651 *
8652 * There may be some time between setting Rows and Columns and (re)allocating
8653 * ScreenLines[]. This happens when starting up and when (manually) changing
8654 * the shell size. Always use screen_Rows and screen_Columns to access items
8655 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8656 * final size of the shell is needed.
8657 */
8658 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008659screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660{
8661 int new_row, old_row;
8662#ifdef FEAT_GUI
8663 int old_Rows;
8664#endif
8665 win_T *wp;
8666 int outofmem = FALSE;
8667 int len;
8668 schar_T *new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008669 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008670 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008671 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008672 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008673 sattr_T *new_ScreenAttrs;
8674 unsigned *new_LineOffset;
8675 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008676 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008677 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008679 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008680 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008682retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008683 /*
8684 * Allocation of the screen buffers is done only when the size changes and
8685 * when Rows and Columns have been set and we have started doing full
8686 * screen stuff.
8687 */
8688 if ((ScreenLines != NULL
8689 && Rows == screen_Rows
8690 && Columns == screen_Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691 && enc_utf8 == (ScreenLinesUC != NULL)
8692 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaara12a1612019-01-24 16:39:02 +01008693 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694 || Rows == 0
8695 || Columns == 0
8696 || (!full_screen && ScreenLines == NULL))
8697 return;
8698
8699 /*
8700 * It's possible that we produce an out-of-memory message below, which
8701 * will cause this function to be called again. To break the loop, just
8702 * return here.
8703 */
8704 if (entered)
8705 return;
8706 entered = TRUE;
8707
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008708 /*
8709 * Note that the window sizes are updated before reallocating the arrays,
8710 * thus we must not redraw here!
8711 */
8712 ++RedrawingDisabled;
8713
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714 win_new_shellsize(); /* fit the windows in the new sized shell */
8715
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716 comp_col(); /* recompute columns for shown command and ruler */
8717
8718 /*
8719 * We're changing the size of the screen.
8720 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8721 * - Move lines from the old arrays into the new arrays, clear extra
8722 * lines (unless the screen is going to be cleared).
8723 * - Free the old arrays.
8724 *
8725 * If anything fails, make ScreenLines NULL, so we don't do anything!
8726 * Continuing with the old ScreenLines may result in a crash, because the
8727 * size is wrong.
8728 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008729 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008731 if (aucmd_win != NULL)
8732 win_free_lsize(aucmd_win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733
8734 new_ScreenLines = (schar_T *)lalloc((long_u)(
8735 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
Bram Moolenaar216b7102010-03-23 13:56:59 +01008736 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008737 if (enc_utf8)
8738 {
8739 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8740 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008741 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008742 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8744 }
8745 if (enc_dbcs == DBCS_JPNU)
8746 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8747 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8749 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8750 new_LineOffset = (unsigned *)lalloc((long_u)(
8751 Rows * sizeof(unsigned)), FALSE);
8752 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008753 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008755 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008756 {
8757 if (win_alloc_lines(wp) == FAIL)
8758 {
8759 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008760 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761 }
8762 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008763 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8764 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008765 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008766give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008768 for (i = 0; i < p_mco; ++i)
8769 if (new_ScreenLinesC[i] == NULL)
8770 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771 if (new_ScreenLines == NULL
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008772 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774 || new_ScreenAttrs == NULL
8775 || new_LineOffset == NULL
8776 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008777 || new_TabPageIdxs == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778 || outofmem)
8779 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008780 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008781 {
8782 /* guess the size */
8783 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8784
8785 /* Remember we did this to avoid getting outofmem messages over
8786 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008787 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008788 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01008789 VIM_CLEAR(new_ScreenLines);
Bram Moolenaard23a8232018-02-10 18:45:26 +01008790 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008791 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01008792 VIM_CLEAR(new_ScreenLinesC[i]);
8793 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaard23a8232018-02-10 18:45:26 +01008794 VIM_CLEAR(new_ScreenAttrs);
8795 VIM_CLEAR(new_LineOffset);
8796 VIM_CLEAR(new_LineWraps);
8797 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798 }
8799 else
8800 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008801 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008802
Bram Moolenaar071d4272004-06-13 20:20:40 +00008803 for (new_row = 0; new_row < Rows; ++new_row)
8804 {
8805 new_LineOffset[new_row] = new_row * Columns;
8806 new_LineWraps[new_row] = FALSE;
8807
8808 /*
8809 * If the screen is not going to be cleared, copy as much as
8810 * possible from the old screen to the new one and clear the rest
8811 * (used when resizing the window at the "--more--" prompt or when
8812 * executing an external command, for the GUI).
8813 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008814 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815 {
8816 (void)vim_memset(new_ScreenLines + new_row * Columns,
8817 ' ', (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818 if (enc_utf8)
8819 {
8820 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8821 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008822 for (i = 0; i < p_mco; ++i)
8823 (void)vim_memset(new_ScreenLinesC[i]
8824 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825 0, (size_t)Columns * sizeof(u8char_T));
8826 }
8827 if (enc_dbcs == DBCS_JPNU)
8828 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8829 0, (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8831 0, (size_t)Columns * sizeof(sattr_T));
8832 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008833 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834 {
8835 if (screen_Columns < Columns)
8836 len = screen_Columns;
8837 else
8838 len = Columns;
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008839 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008840 * may be invalid now. Also when p_mco changes. */
8841 if (!(enc_utf8 && ScreenLinesUC == NULL)
8842 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008843 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8844 ScreenLines + LineOffset[old_row],
8845 (size_t)len * sizeof(schar_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008846 if (enc_utf8 && ScreenLinesUC != NULL
8847 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848 {
8849 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8850 ScreenLinesUC + LineOffset[old_row],
8851 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008852 for (i = 0; i < p_mco; ++i)
8853 mch_memmove(new_ScreenLinesC[i]
8854 + new_LineOffset[new_row],
8855 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856 (size_t)len * sizeof(u8char_T));
8857 }
8858 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8859 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8860 ScreenLines2 + LineOffset[old_row],
8861 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8863 ScreenAttrs + LineOffset[old_row],
8864 (size_t)len * sizeof(sattr_T));
8865 }
8866 }
8867 }
8868 /* Use the last line of the screen for the current line. */
8869 current_ScreenLine = new_ScreenLines + Rows * Columns;
8870 }
8871
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008872 free_screenlines();
8873
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874 ScreenLines = new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008875 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008876 for (i = 0; i < p_mco; ++i)
8877 ScreenLinesC[i] = new_ScreenLinesC[i];
8878 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008879 ScreenLines2 = new_ScreenLines2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880 ScreenAttrs = new_ScreenAttrs;
8881 LineOffset = new_LineOffset;
8882 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008883 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884
8885 /* It's important that screen_Rows and screen_Columns reflect the actual
8886 * size of ScreenLines[]. Set them before calling anything. */
8887#ifdef FEAT_GUI
8888 old_Rows = screen_Rows;
8889#endif
8890 screen_Rows = Rows;
8891 screen_Columns = Columns;
8892
8893 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008894 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895 screenclear2();
8896
8897#ifdef FEAT_GUI
8898 else if (gui.in_use
8899 && !gui.starting
8900 && ScreenLines != NULL
8901 && old_Rows != Rows)
8902 {
8903 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8904 /*
8905 * Adjust the position of the cursor, for when executing an external
8906 * command.
8907 */
8908 if (msg_row >= Rows) /* Rows got smaller */
8909 msg_row = Rows - 1; /* put cursor at last row */
8910 else if (Rows > old_Rows) /* Rows got bigger */
8911 msg_row += Rows - old_Rows; /* put cursor in same place */
8912 if (msg_col >= Columns) /* Columns got smaller */
8913 msg_col = Columns - 1; /* put cursor at last column */
8914 }
8915#endif
8916
Bram Moolenaar071d4272004-06-13 20:20:40 +00008917 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008918 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008919
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008920 /*
8921 * Do not apply autocommands more than 3 times to avoid an endless loop
8922 * in case applying autocommands always changes Rows or Columns.
8923 */
8924 if (starting == 0 && ++retry_count <= 3)
8925 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008926 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008927 /* In rare cases, autocommands may have altered Rows or Columns,
8928 * jump back to check if we need to allocate the screen again. */
8929 goto retry;
8930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931}
8932
8933 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008934free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008935{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008936 int i;
8937
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008938 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008939 for (i = 0; i < Screen_mco; ++i)
8940 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008941 vim_free(ScreenLines2);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008942 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008943 vim_free(ScreenAttrs);
8944 vim_free(LineOffset);
8945 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008946 vim_free(TabPageIdxs);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008947}
8948
8949 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008950screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951{
8952 check_for_delay(FALSE);
8953 screenalloc(FALSE); /* allocate screen buffers if size changed */
8954 screenclear2(); /* clear the screen */
8955}
8956
8957 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008958screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959{
8960 int i;
8961
8962 if (starting == NO_SCREEN || ScreenLines == NULL
8963#ifdef FEAT_GUI
8964 || (gui.in_use && gui.starting)
8965#endif
8966 )
8967 return;
8968
8969#ifdef FEAT_GUI
8970 if (!gui.in_use)
8971#endif
8972 screen_attr = -1; /* force setting the Normal colors */
8973 screen_stop_highlight(); /* don't want highlighting here */
8974
8975#ifdef FEAT_CLIPBOARD
8976 /* disable selection without redrawing it */
8977 clip_scroll_selection(9999);
8978#endif
8979
8980 /* blank out ScreenLines */
8981 for (i = 0; i < Rows; ++i)
8982 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02008983 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984 LineWraps[i] = FALSE;
8985 }
8986
8987 if (can_clear(T_CL))
8988 {
8989 out_str(T_CL); /* clear the display */
8990 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008991 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008992 }
8993 else
8994 {
8995 /* can't clear the screen, mark all chars with invalid attributes */
8996 for (i = 0; i < Rows; ++i)
8997 lineinvalid(LineOffset[i], (int)Columns);
8998 clear_cmdline = TRUE;
8999 }
9000
9001 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9002
9003 win_rest_invalid(firstwin);
9004 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009005 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009006 if (must_redraw == CLEAR) /* no need to clear again */
9007 must_redraw = NOT_VALID;
9008 compute_cmdrow();
9009 msg_row = cmdline_row; /* put cursor on last line for messages */
9010 msg_col = 0;
9011 screen_start(); /* don't know where cursor is now */
9012 msg_scrolled = 0; /* can't scroll back */
9013 msg_didany = FALSE;
9014 msg_didout = FALSE;
9015}
9016
9017/*
9018 * Clear one line in ScreenLines.
9019 */
9020 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009021lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009022{
9023 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009024 if (enc_utf8)
9025 (void)vim_memset(ScreenLinesUC + off, 0,
9026 (size_t)width * sizeof(u8char_T));
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009027 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009028}
9029
9030/*
9031 * Mark one line in ScreenLines invalid by setting the attributes to an
9032 * invalid value.
9033 */
9034 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009035lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009036{
9037 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9038}
9039
Bram Moolenaar071d4272004-06-13 20:20:40 +00009040/*
9041 * Copy part of a Screenline for vertically split window "wp".
9042 */
9043 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009044linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009045{
9046 unsigned off_to = LineOffset[to] + wp->w_wincol;
9047 unsigned off_from = LineOffset[from] + wp->w_wincol;
9048
9049 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9050 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009051 if (enc_utf8)
9052 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009053 int i;
9054
Bram Moolenaar071d4272004-06-13 20:20:40 +00009055 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9056 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009057 for (i = 0; i < p_mco; ++i)
9058 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9059 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009060 }
9061 if (enc_dbcs == DBCS_JPNU)
9062 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9063 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9065 wp->w_width * sizeof(sattr_T));
9066}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009067
9068/*
9069 * Return TRUE if clearing with term string "p" would work.
9070 * It can't work when the string is empty or it won't set the right background.
9071 */
9072 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009073can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009074{
9075 return (*p != NUL && (t_colors <= 1
9076#ifdef FEAT_GUI
9077 || gui.in_use
9078#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009079#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009080 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009081 || (!p_tgc && cterm_normal_bg_color == 0)
9082#else
9083 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009084#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009085 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009086}
9087
9088/*
9089 * Reset cursor position. Use whenever cursor was moved because of outputting
9090 * something directly to the screen (shell commands) or a terminal control
9091 * code.
9092 */
9093 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009094screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009095{
9096 screen_cur_row = screen_cur_col = 9999;
9097}
9098
9099/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009100 * Move the cursor to position "row","col" in the screen.
9101 * This tries to find the most efficient way to move, minimizing the number of
9102 * characters sent to the terminal.
9103 */
9104 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009105windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009107 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009108 int i;
9109 int plan;
9110 int cost;
9111 int wouldbe_col;
9112 int noinvcurs;
9113 char_u *bs;
9114 int goto_cost;
9115 int attr;
9116
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009117#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009118#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9119
9120#define PLAN_LE 1
9121#define PLAN_CR 2
9122#define PLAN_NL 3
9123#define PLAN_WRITE 4
9124 /* Can't use ScreenLines unless initialized */
9125 if (ScreenLines == NULL)
9126 return;
9127
9128 if (col != screen_cur_col || row != screen_cur_row)
9129 {
9130 /* Check for valid position. */
9131 if (row < 0) /* window without text lines? */
9132 row = 0;
9133 if (row >= screen_Rows)
9134 row = screen_Rows - 1;
9135 if (col >= screen_Columns)
9136 col = screen_Columns - 1;
9137
9138 /* check if no cursor movement is allowed in highlight mode */
9139 if (screen_attr && *T_MS == NUL)
9140 noinvcurs = HIGHL_COST;
9141 else
9142 noinvcurs = 0;
9143 goto_cost = GOTO_COST + noinvcurs;
9144
9145 /*
9146 * Plan how to do the positioning:
9147 * 1. Use CR to move it to column 0, same row.
9148 * 2. Use T_LE to move it a few columns to the left.
9149 * 3. Use NL to move a few lines down, column 0.
9150 * 4. Move a few columns to the right with T_ND or by writing chars.
9151 *
9152 * Don't do this if the cursor went beyond the last column, the cursor
9153 * position is unknown then (some terminals wrap, some don't )
9154 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009155 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156 * characters to move the cursor to the right.
9157 */
9158 if (row >= screen_cur_row && screen_cur_col < Columns)
9159 {
9160 /*
9161 * If the cursor is in the same row, bigger col, we can use CR
9162 * or T_LE.
9163 */
9164 bs = NULL; /* init for GCC */
9165 attr = screen_attr;
9166 if (row == screen_cur_row && col < screen_cur_col)
9167 {
9168 /* "le" is preferred over "bc", because "bc" is obsolete */
9169 if (*T_LE)
9170 bs = T_LE; /* "cursor left" */
9171 else
9172 bs = T_BC; /* "backspace character (old) */
9173 if (*bs)
9174 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9175 else
9176 cost = 999;
9177 if (col + 1 < cost) /* using CR is less characters */
9178 {
9179 plan = PLAN_CR;
9180 wouldbe_col = 0;
9181 cost = 1; /* CR is just one character */
9182 }
9183 else
9184 {
9185 plan = PLAN_LE;
9186 wouldbe_col = col;
9187 }
9188 if (noinvcurs) /* will stop highlighting */
9189 {
9190 cost += noinvcurs;
9191 attr = 0;
9192 }
9193 }
9194
9195 /*
9196 * If the cursor is above where we want to be, we can use CR LF.
9197 */
9198 else if (row > screen_cur_row)
9199 {
9200 plan = PLAN_NL;
9201 wouldbe_col = 0;
9202 cost = (row - screen_cur_row) * 2; /* CR LF */
9203 if (noinvcurs) /* will stop highlighting */
9204 {
9205 cost += noinvcurs;
9206 attr = 0;
9207 }
9208 }
9209
9210 /*
9211 * If the cursor is in the same row, smaller col, just use write.
9212 */
9213 else
9214 {
9215 plan = PLAN_WRITE;
9216 wouldbe_col = screen_cur_col;
9217 cost = 0;
9218 }
9219
9220 /*
9221 * Check if any characters that need to be written have the
9222 * correct attributes. Also avoid UTF-8 characters.
9223 */
9224 i = col - wouldbe_col;
9225 if (i > 0)
9226 cost += i;
9227 if (cost < goto_cost && i > 0)
9228 {
9229 /*
9230 * Check if the attributes are correct without additionally
9231 * stopping highlighting.
9232 */
9233 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9234 while (i && *p++ == attr)
9235 --i;
9236 if (i != 0)
9237 {
9238 /*
9239 * Try if it works when highlighting is stopped here.
9240 */
9241 if (*--p == 0)
9242 {
9243 cost += noinvcurs;
9244 while (i && *p++ == 0)
9245 --i;
9246 }
9247 if (i != 0)
9248 cost = 999; /* different attributes, don't do it */
9249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009250 if (enc_utf8)
9251 {
9252 /* Don't use an UTF-8 char for positioning, it's slow. */
9253 for (i = wouldbe_col; i < col; ++i)
9254 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9255 {
9256 cost = 999;
9257 break;
9258 }
9259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009260 }
9261
9262 /*
9263 * We can do it without term_windgoto()!
9264 */
9265 if (cost < goto_cost)
9266 {
9267 if (plan == PLAN_LE)
9268 {
9269 if (noinvcurs)
9270 screen_stop_highlight();
9271 while (screen_cur_col > col)
9272 {
9273 out_str(bs);
9274 --screen_cur_col;
9275 }
9276 }
9277 else if (plan == PLAN_CR)
9278 {
9279 if (noinvcurs)
9280 screen_stop_highlight();
9281 out_char('\r');
9282 screen_cur_col = 0;
9283 }
9284 else if (plan == PLAN_NL)
9285 {
9286 if (noinvcurs)
9287 screen_stop_highlight();
9288 while (screen_cur_row < row)
9289 {
9290 out_char('\n');
9291 ++screen_cur_row;
9292 }
9293 screen_cur_col = 0;
9294 }
9295
9296 i = col - screen_cur_col;
9297 if (i > 0)
9298 {
9299 /*
9300 * Use cursor-right if it's one character only. Avoids
9301 * removing a line of pixels from the last bold char, when
9302 * using the bold trick in the GUI.
9303 */
9304 if (T_ND[0] != NUL && T_ND[1] == NUL)
9305 {
9306 while (i-- > 0)
9307 out_char(*T_ND);
9308 }
9309 else
9310 {
9311 int off;
9312
9313 off = LineOffset[row] + screen_cur_col;
9314 while (i-- > 0)
9315 {
9316 if (ScreenAttrs[off] != screen_attr)
9317 screen_stop_highlight();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009318 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009319 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009320 if (enc_dbcs == DBCS_JPNU
9321 && ScreenLines[off] == 0x8e)
9322 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009323 ++off;
9324 }
9325 }
9326 }
9327 }
9328 }
9329 else
9330 cost = 999;
9331
9332 if (cost >= goto_cost)
9333 {
9334 if (noinvcurs)
9335 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009336 if (row == screen_cur_row && (col > screen_cur_col)
9337 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009338 term_cursor_right(col - screen_cur_col);
9339 else
9340 term_windgoto(row, col);
9341 }
9342 screen_cur_row = row;
9343 screen_cur_col = col;
9344 }
9345}
9346
9347/*
9348 * Set cursor to its position in the current window.
9349 */
9350 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009351setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009352{
Bram Moolenaar987723e2018-03-06 11:43:04 +01009353 setcursor_mayforce(FALSE);
9354}
9355
9356/*
9357 * Set cursor to its position in the current window.
9358 * When "force" is TRUE also when not redrawing.
9359 */
9360 void
9361setcursor_mayforce(int force)
9362{
9363 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009364 {
9365 validate_cursor();
9366 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009367 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009368#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009369 /* With 'rightleft' set and the cursor on a double-wide
9370 * character, position it on the leftmost column. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01009371 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol
9372 - ((has_mbyte
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009373 && (*mb_ptr2cells)(ml_get_cursor()) == 2
Bram Moolenaara12a1612019-01-24 16:39:02 +01009374 && vim_isprintc(gchar_cursor())) ? 2 : 1)) :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009375#endif
9376 curwin->w_wcol));
9377 }
9378}
9379
9380
9381/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009382 * Insert 'line_count' lines at 'row' in window 'wp'.
9383 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9384 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009385 * scrolling.
9386 * Returns FAIL if the lines are not inserted, OK for success.
9387 */
9388 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009389win_ins_lines(
9390 win_T *wp,
9391 int row,
9392 int line_count,
9393 int invalid,
9394 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009395{
9396 int did_delete;
9397 int nextrow;
9398 int lastrow;
9399 int retval;
9400
9401 if (invalid)
9402 wp->w_lines_valid = 0;
9403
9404 if (wp->w_height < 5)
9405 return FAIL;
9406
9407 if (line_count > wp->w_height - row)
9408 line_count = wp->w_height - row;
9409
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009410 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009411 if (retval != MAYBE)
9412 return retval;
9413
9414 /*
9415 * If there is a next window or a status line, we first try to delete the
9416 * lines at the bottom to avoid messing what is after the window.
9417 * If this fails and there are following windows, don't do anything to avoid
9418 * messing up those windows, better just redraw.
9419 */
9420 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009421 if (wp->w_next != NULL || wp->w_status_height)
9422 {
9423 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009424 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009425 did_delete = TRUE;
9426 else if (wp->w_next)
9427 return FAIL;
9428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009429 /*
9430 * if no lines deleted, blank the lines that will end up below the window
9431 */
9432 if (!did_delete)
9433 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009434 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009435 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02009436 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009437 lastrow = nextrow + line_count;
9438 if (lastrow > Rows)
9439 lastrow = Rows;
9440 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009441 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009442 ' ', ' ', 0);
9443 }
9444
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009445 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009446 == FAIL)
9447 {
9448 /* deletion will have messed up other windows */
9449 if (did_delete)
9450 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009451 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009452 win_rest_invalid(W_NEXT(wp));
9453 }
9454 return FAIL;
9455 }
9456
9457 return OK;
9458}
9459
9460/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009461 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009462 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9463 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9464 * scrolling
9465 * Return OK for success, FAIL if the lines are not deleted.
9466 */
9467 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009468win_del_lines(
9469 win_T *wp,
9470 int row,
9471 int line_count,
9472 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009473 int mayclear,
9474 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009475{
9476 int retval;
9477
9478 if (invalid)
9479 wp->w_lines_valid = 0;
9480
9481 if (line_count > wp->w_height - row)
9482 line_count = wp->w_height - row;
9483
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009484 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485 if (retval != MAYBE)
9486 return retval;
9487
9488 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009489 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009490 return FAIL;
9491
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492 /*
9493 * If there are windows or status lines below, try to put them at the
9494 * correct place. If we can't do that, they have to be redrawn.
9495 */
9496 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9497 {
9498 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009499 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009500 {
9501 wp->w_redr_status = TRUE;
9502 win_rest_invalid(wp->w_next);
9503 }
9504 }
9505 /*
9506 * If this is the last window and there is no status line, redraw the
9507 * command line later.
9508 */
9509 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009510 redraw_cmdline = TRUE;
9511 return OK;
9512}
9513
9514/*
9515 * Common code for win_ins_lines() and win_del_lines().
9516 * Returns OK or FAIL when the work has been done.
9517 * Returns MAYBE when not finished yet.
9518 */
9519 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009520win_do_lines(
9521 win_T *wp,
9522 int row,
9523 int line_count,
9524 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009525 int del,
9526 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009527{
9528 int retval;
9529
9530 if (!redrawing() || line_count <= 0)
9531 return FAIL;
9532
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009533 /* When inserting lines would result in loss of command output, just redraw
9534 * the lines. */
9535 if (no_win_do_lines_ins && !del)
9536 return FAIL;
9537
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538 /* only a few lines left: redraw is faster */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009539 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009540 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009541 if (!no_win_do_lines_ins)
9542 screenclear(); /* will set wp->w_lines_valid to 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009543 return FAIL;
9544 }
9545
9546 /*
9547 * Delete all remaining lines
9548 */
9549 if (row + line_count >= wp->w_height)
9550 {
9551 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009552 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009553 ' ', ' ', 0);
9554 return OK;
9555 }
9556
9557 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009558 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009559 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009560 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009562 if (!no_win_do_lines_ins)
9563 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009564
9565 /*
9566 * If the terminal can set a scroll region, use that.
9567 * Always do this in a vertically split window. This will redraw from
9568 * ScreenLines[] when t_CV isn't defined. That's faster than using
9569 * win_line().
9570 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009571 * a character in the lower right corner of the scroll region may cause a
9572 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009573 */
Bram Moolenaar02631462017-09-22 15:20:32 +02009574 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009575 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009576 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009577 scroll_region_set(wp, row);
9578 if (del)
9579 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009580 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009581 else
9582 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009583 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009584 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009585 scroll_region_reset();
9586 return retval;
9587 }
9588
Bram Moolenaar071d4272004-06-13 20:20:40 +00009589 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9590 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009591
9592 return MAYBE;
9593}
9594
9595/*
9596 * window 'wp' and everything after it is messed up, mark it for redraw
9597 */
9598 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009599win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009600{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009602 {
9603 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009604 wp->w_redr_status = TRUE;
9605 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009606 }
9607 redraw_cmdline = TRUE;
9608}
9609
9610/*
9611 * The rest of the routines in this file perform screen manipulations. The
9612 * given operation is performed physically on the screen. The corresponding
9613 * change is also made to the internal screen image. In this way, the editor
9614 * anticipates the effect of editing changes on the appearance of the screen.
9615 * That way, when we call screenupdate a complete redraw isn't usually
9616 * necessary. Another advantage is that we can keep adding code to anticipate
9617 * screen changes, and in the meantime, everything still works.
9618 */
9619
9620/*
9621 * types for inserting or deleting lines
9622 */
9623#define USE_T_CAL 1
9624#define USE_T_CDL 2
9625#define USE_T_AL 3
9626#define USE_T_CE 4
9627#define USE_T_DL 5
9628#define USE_T_SR 6
9629#define USE_NL 7
9630#define USE_T_CD 8
9631#define USE_REDRAW 9
9632
9633/*
9634 * insert lines on the screen and update ScreenLines[]
9635 * 'end' is the line after the scrolled part. Normally it is Rows.
9636 * When scrolling region used 'off' is the offset from the top for the region.
9637 * 'row' and 'end' are relative to the start of the region.
9638 *
9639 * return FAIL for failure, OK for success.
9640 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009641 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009642screen_ins_lines(
9643 int off,
9644 int row,
9645 int line_count,
9646 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009647 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01009648 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009649{
9650 int i;
9651 int j;
9652 unsigned temp;
9653 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009654 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009655 int type;
9656 int result_empty;
9657 int can_ce = can_clear(T_CE);
9658
9659 /*
9660 * FAIL if
9661 * - there is no valid screen
9662 * - the screen has to be redrawn completely
9663 * - the line count is less than one
9664 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009665 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009666 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009667 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll
9668#ifdef FEAT_CLIPBOARD
9669 || (clip_star.state != SELECT_CLEARED
9670 && redrawing_for_callback > 0)
9671#endif
9672 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009673 return FAIL;
9674
9675 /*
9676 * There are seven ways to insert lines:
9677 * 0. When in a vertically split window and t_CV isn't set, redraw the
9678 * characters from ScreenLines[].
9679 * 1. Use T_CD (clear to end of display) if it exists and the result of
9680 * the insert is just empty lines
9681 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9682 * present or line_count > 1. It looks better if we do all the inserts
9683 * at once.
9684 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9685 * insert is just empty lines and T_CE is not present or line_count >
9686 * 1.
9687 * 4. Use T_AL (insert line) if it exists.
9688 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9689 * just empty lines.
9690 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9691 * just empty lines.
9692 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9693 * the 'da' flag is not set or we have clear line capability.
9694 * 8. redraw the characters from ScreenLines[].
9695 *
9696 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9697 * the scrollbar for the window. It does have insert line, use that if it
9698 * exists.
9699 */
9700 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9702 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009703 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009704 type = USE_T_CD;
9705 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9706 type = USE_T_CAL;
9707 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9708 type = USE_T_CDL;
9709 else if (*T_AL != NUL)
9710 type = USE_T_AL;
9711 else if (can_ce && result_empty)
9712 type = USE_T_CE;
9713 else if (*T_DL != NUL && result_empty)
9714 type = USE_T_DL;
9715 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9716 type = USE_T_SR;
9717 else
9718 return FAIL;
9719
9720 /*
9721 * For clearing the lines screen_del_lines() is used. This will also take
9722 * care of t_db if necessary.
9723 */
9724 if (type == USE_T_CD || type == USE_T_CDL ||
9725 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009726 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009727
9728 /*
9729 * If text is retained below the screen, first clear or delete as many
9730 * lines at the bottom of the window as are about to be inserted so that
9731 * the deleted lines won't later surface during a screen_del_lines.
9732 */
9733 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009734 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009735
9736#ifdef FEAT_CLIPBOARD
9737 /* Remove a modeless selection when inserting lines halfway the screen
9738 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009739 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009740 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009741 else
9742 clip_scroll_selection(-line_count);
9743#endif
9744
Bram Moolenaar071d4272004-06-13 20:20:40 +00009745#ifdef FEAT_GUI
9746 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9747 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009748 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009749#endif
9750
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009751 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
9752 cursor_col = wp->w_wincol;
9753
Bram Moolenaar071d4272004-06-13 20:20:40 +00009754 if (*T_CCS != NUL) /* cursor relative to region */
9755 cursor_row = row;
9756 else
9757 cursor_row = row + off;
9758
9759 /*
9760 * Shift LineOffset[] line_count down to reflect the inserted lines.
9761 * Clear the inserted lines in ScreenLines[].
9762 */
9763 row += off;
9764 end += off;
9765 for (i = 0; i < line_count; ++i)
9766 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009767 if (wp != NULL && wp->w_width != Columns)
9768 {
9769 /* need to copy part of a line */
9770 j = end - 1 - i;
9771 while ((j -= line_count) >= row)
9772 linecopy(j + line_count, j, wp);
9773 j += line_count;
9774 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009775 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
9776 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009777 else
9778 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9779 LineWraps[j] = FALSE;
9780 }
9781 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009782 {
9783 j = end - 1 - i;
9784 temp = LineOffset[j];
9785 while ((j -= line_count) >= row)
9786 {
9787 LineOffset[j + line_count] = LineOffset[j];
9788 LineWraps[j + line_count] = LineWraps[j];
9789 }
9790 LineOffset[j + line_count] = temp;
9791 LineWraps[j + line_count] = FALSE;
9792 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009793 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009794 else
9795 lineinvalid(temp, (int)Columns);
9796 }
9797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009798
9799 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009800 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009801 if (clear_attr != 0)
9802 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009803
Bram Moolenaar071d4272004-06-13 20:20:40 +00009804 /* redraw the characters */
9805 if (type == USE_REDRAW)
9806 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02009807 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009808 {
9809 term_append_lines(line_count);
9810 screen_start(); /* don't know where cursor is now */
9811 }
9812 else
9813 {
9814 for (i = 0; i < line_count; i++)
9815 {
9816 if (type == USE_T_AL)
9817 {
9818 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009819 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009820 out_str(T_AL);
9821 }
9822 else /* type == USE_T_SR */
9823 out_str(T_SR);
9824 screen_start(); /* don't know where cursor is now */
9825 }
9826 }
9827
9828 /*
9829 * With scroll-reverse and 'da' flag set we need to clear the lines that
9830 * have been scrolled down into the region.
9831 */
9832 if (type == USE_T_SR && *T_DA)
9833 {
9834 for (i = 0; i < line_count; ++i)
9835 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009836 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009837 out_str(T_CE);
9838 screen_start(); /* don't know where cursor is now */
9839 }
9840 }
9841
9842#ifdef FEAT_GUI
9843 gui_can_update_cursor();
9844 if (gui.in_use)
9845 out_flush(); /* always flush after a scroll */
9846#endif
9847 return OK;
9848}
9849
9850/*
Bram Moolenaar107abd22016-08-12 14:08:25 +02009851 * Delete lines on the screen and update ScreenLines[].
9852 * "end" is the line after the scrolled part. Normally it is Rows.
9853 * When scrolling region used "off" is the offset from the top for the region.
9854 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009855 *
9856 * Return OK for success, FAIL if the lines are not deleted.
9857 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009858 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009859screen_del_lines(
9860 int off,
9861 int row,
9862 int line_count,
9863 int end,
9864 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009865 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +01009866 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009867{
9868 int j;
9869 int i;
9870 unsigned temp;
9871 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009872 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009873 int cursor_end;
9874 int result_empty; /* result is empty until end of region */
9875 int can_delete; /* deleting line codes can be used */
9876 int type;
9877
9878 /*
9879 * FAIL if
9880 * - there is no valid screen
9881 * - the screen has to be redrawn completely
9882 * - the line count is less than one
9883 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009884 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009885 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009886 if (!screen_valid(TRUE) || line_count <= 0
9887 || (!force && line_count > p_ttyscroll)
9888#ifdef FEAT_CLIPBOARD
9889 || (clip_star.state != SELECT_CLEARED
9890 && redrawing_for_callback > 0)
9891#endif
9892 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009893 return FAIL;
9894
9895 /*
9896 * Check if the rest of the current region will become empty.
9897 */
9898 result_empty = row + line_count >= end;
9899
9900 /*
9901 * We can delete lines only when 'db' flag not set or when 'ce' option
9902 * available.
9903 */
9904 can_delete = (*T_DB == NUL || can_clear(T_CE));
9905
9906 /*
9907 * There are six ways to delete lines:
9908 * 0. When in a vertically split window and t_CV isn't set, redraw the
9909 * characters from ScreenLines[].
9910 * 1. Use T_CD if it exists and the result is empty.
9911 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9912 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9913 * none of the other ways work.
9914 * 4. Use T_CE (erase line) if the result is empty.
9915 * 5. Use T_DL (delete line) if it exists.
9916 * 6. redraw the characters from ScreenLines[].
9917 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009918 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9919 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009920 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009921 type = USE_T_CD;
9922#if defined(__BEOS__) && defined(BEOS_DR8)
9923 /*
9924 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9925 * its internal termcap... this works okay for tests which test *T_DB !=
9926 * NUL. It has the disadvantage that the user cannot use any :set t_*
9927 * command to get T_DB (back) to empty_option, only :set term=... will do
9928 * the trick...
9929 * Anyway, this hack will hopefully go away with the next OS release.
9930 * (Olaf Seibert)
9931 */
9932 else if (row == 0 && T_DB == empty_option
9933 && (line_count == 1 || *T_CDL == NUL))
9934#else
9935 else if (row == 0 && (
9936#ifndef AMIGA
9937 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9938 * up, so use delete-line command */
9939 line_count == 1 ||
9940#endif
9941 *T_CDL == NUL))
9942#endif
9943 type = USE_NL;
9944 else if (*T_CDL != NUL && line_count > 1 && can_delete)
9945 type = USE_T_CDL;
9946 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +02009947 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009948 type = USE_T_CE;
9949 else if (*T_DL != NUL && can_delete)
9950 type = USE_T_DL;
9951 else if (*T_CDL != NUL && can_delete)
9952 type = USE_T_CDL;
9953 else
9954 return FAIL;
9955
9956#ifdef FEAT_CLIPBOARD
9957 /* Remove a modeless selection when deleting lines halfway the screen or
9958 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009959 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009960 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009961 else
9962 clip_scroll_selection(line_count);
9963#endif
9964
Bram Moolenaar071d4272004-06-13 20:20:40 +00009965#ifdef FEAT_GUI
9966 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9967 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009968 gui_dont_update_cursor(gui.cursor_row >= row + off
9969 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009970#endif
9971
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009972 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
9973 cursor_col = wp->w_wincol;
9974
Bram Moolenaar071d4272004-06-13 20:20:40 +00009975 if (*T_CCS != NUL) /* cursor relative to region */
9976 {
9977 cursor_row = row;
9978 cursor_end = end;
9979 }
9980 else
9981 {
9982 cursor_row = row + off;
9983 cursor_end = end + off;
9984 }
9985
9986 /*
9987 * Now shift LineOffset[] line_count up to reflect the deleted lines.
9988 * Clear the inserted lines in ScreenLines[].
9989 */
9990 row += off;
9991 end += off;
9992 for (i = 0; i < line_count; ++i)
9993 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009994 if (wp != NULL && wp->w_width != Columns)
9995 {
9996 /* need to copy part of a line */
9997 j = row + i;
9998 while ((j += line_count) <= end - 1)
9999 linecopy(j - line_count, j, wp);
10000 j -= line_count;
10001 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010002 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10003 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010004 else
10005 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10006 LineWraps[j] = FALSE;
10007 }
10008 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010009 {
10010 /* whole width, moving the line pointers is faster */
10011 j = row + i;
10012 temp = LineOffset[j];
10013 while ((j += line_count) <= end - 1)
10014 {
10015 LineOffset[j - line_count] = LineOffset[j];
10016 LineWraps[j - line_count] = LineWraps[j];
10017 }
10018 LineOffset[j - line_count] = temp;
10019 LineWraps[j - line_count] = FALSE;
10020 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010021 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010022 else
10023 lineinvalid(temp, (int)Columns);
10024 }
10025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010026
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010027 if (screen_attr != clear_attr)
10028 screen_stop_highlight();
10029 if (clear_attr != 0)
10030 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010031
Bram Moolenaar071d4272004-06-13 20:20:40 +000010032 /* redraw the characters */
10033 if (type == USE_REDRAW)
10034 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010035 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010036 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010037 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010038 out_str(T_CD);
10039 screen_start(); /* don't know where cursor is now */
10040 }
10041 else if (type == USE_T_CDL)
10042 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010043 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010044 term_delete_lines(line_count);
10045 screen_start(); /* don't know where cursor is now */
10046 }
10047 /*
10048 * Deleting lines at top of the screen or scroll region: Just scroll
10049 * the whole screen (scroll region) up by outputting newlines on the
10050 * last line.
10051 */
10052 else if (type == USE_NL)
10053 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010054 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010055 for (i = line_count; --i >= 0; )
10056 out_char('\n'); /* cursor will remain on same line */
10057 }
10058 else
10059 {
10060 for (i = line_count; --i >= 0; )
10061 {
10062 if (type == USE_T_DL)
10063 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010064 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010065 out_str(T_DL); /* delete a line */
10066 }
10067 else /* type == USE_T_CE */
10068 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010069 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010070 out_str(T_CE); /* erase a line */
10071 }
10072 screen_start(); /* don't know where cursor is now */
10073 }
10074 }
10075
10076 /*
10077 * If the 'db' flag is set, we need to clear the lines that have been
10078 * scrolled up at the bottom of the region.
10079 */
10080 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10081 {
10082 for (i = line_count; i > 0; --i)
10083 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010084 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010085 out_str(T_CE); /* erase a line */
10086 screen_start(); /* don't know where cursor is now */
10087 }
10088 }
10089
10090#ifdef FEAT_GUI
10091 gui_can_update_cursor();
10092 if (gui.in_use)
10093 out_flush(); /* always flush after a scroll */
10094#endif
10095
10096 return OK;
10097}
10098
10099/*
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010100 * Return TRUE when postponing displaying the mode message: when not redrawing
10101 * or inside a mapping.
10102 */
10103 int
10104skip_showmode()
10105{
10106 // Call char_avail() only when we are going to show something, because it
10107 // takes a bit of time. redrawing() may also call char_avail_avail().
10108 if (global_busy
10109 || msg_silent != 0
10110 || !redrawing()
10111 || (char_avail() && !KeyTyped))
10112 {
10113 redraw_cmdline = TRUE; // show mode later
10114 return TRUE;
10115 }
10116 return FALSE;
10117}
10118
10119/*
Bram Moolenaar81226e02018-02-20 21:44:45 +010010120 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010121 *
10122 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10123 * If clear_cmdline is FALSE there may be a message there that needs to be
10124 * cleared only if a mode is shown.
10125 * Return the length of the message (0 if no message).
10126 */
10127 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010128showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010129{
10130 int need_clear;
10131 int length = 0;
10132 int do_mode;
10133 int attr;
10134 int nwr_save;
10135#ifdef FEAT_INS_EXPAND
10136 int sub_attr;
10137#endif
10138
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010139 do_mode = ((p_smd && msg_silent == 0)
10140 && ((State & INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +020010141 || restart_edit != NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010142 || VIsual_active));
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010143 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010144 {
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010145 if (skip_showmode())
10146 return 0; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +000010147
10148 nwr_save = need_wait_return;
10149
10150 /* wait a bit before overwriting an important message */
10151 check_for_delay(FALSE);
10152
10153 /* if the cmdline is more than one line high, erase top lines */
10154 need_clear = clear_cmdline;
10155 if (clear_cmdline && cmdline_row < Rows - 1)
10156 msg_clr_cmdline(); /* will reset clear_cmdline */
10157
10158 /* Position on the last line in the window, column 0 */
10159 msg_pos_mode();
10160 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010161 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010162 if (do_mode)
10163 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010164 msg_puts_attr("--", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010165#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010166 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010167# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010168 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010169# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010170 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010171# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010172 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010173# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar32526b32019-01-19 17:43:09 +010010174 msg_puts_attr(" IM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010175# else
Bram Moolenaar32526b32019-01-19 17:43:09 +010010176 msg_puts_attr(" XIM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010177# endif
10178#endif
10179#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10180 if (gui.in_use)
10181 {
10182 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010183 {
10184 /* HANGUL */
10185 if (enc_utf8)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010186 msg_puts_attr(" \355\225\234\352\270\200", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010187 else
Bram Moolenaar32526b32019-01-19 17:43:09 +010010188 msg_puts_attr(" \307\321\261\333", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010190 }
10191#endif
10192#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010193 /* CTRL-X in Insert mode */
10194 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010195 {
10196 /* These messages can get long, avoid a wrap in a narrow
10197 * window. Prefer showing edit_submode_extra. */
10198 length = (Rows - msg_row) * Columns - 3;
10199 if (edit_submode_extra != NULL)
10200 length -= vim_strsize(edit_submode_extra);
10201 if (length > 0)
10202 {
10203 if (edit_submode_pre != NULL)
10204 length -= vim_strsize(edit_submode_pre);
10205 if (length - vim_strsize(edit_submode) > 0)
10206 {
10207 if (edit_submode_pre != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010208 msg_puts_attr((char *)edit_submode_pre, attr);
10209 msg_puts_attr((char *)edit_submode, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010210 }
10211 if (edit_submode_extra != NULL)
10212 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010213 msg_puts_attr(" ", attr); /* add a space in between */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010214 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010215 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010216 else
10217 sub_attr = attr;
Bram Moolenaar32526b32019-01-19 17:43:09 +010010218 msg_puts_attr((char *)edit_submode_extra, sub_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010219 }
10220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010221 }
10222 else
10223#endif
10224 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010225 if (State & VREPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010226 msg_puts_attr(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010227 else if (State & REPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010228 msg_puts_attr(_(" REPLACE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010229 else if (State & INSERT)
10230 {
10231#ifdef FEAT_RIGHTLEFT
10232 if (p_ri)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010233 msg_puts_attr(_(" REVERSE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010234#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +010010235 msg_puts_attr(_(" INSERT"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010236 }
Bram Moolenaar942b4542018-06-17 16:23:34 +020010237 else if (restart_edit == 'I' || restart_edit == 'A')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010238 msg_puts_attr(_(" (insert)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010239 else if (restart_edit == 'R')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010240 msg_puts_attr(_(" (replace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010241 else if (restart_edit == 'V')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010242 msg_puts_attr(_(" (vreplace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010243#ifdef FEAT_RIGHTLEFT
10244 if (p_hkmap)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010245 msg_puts_attr(_(" Hebrew"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010246#endif
10247#ifdef FEAT_KEYMAP
10248 if (State & LANGMAP)
10249 {
10250# ifdef FEAT_ARABIC
10251 if (curwin->w_p_arab)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010252 msg_puts_attr(_(" Arabic"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010253 else
10254# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010255 if (get_keymap_str(curwin, (char_u *)" (%s)",
10256 NameBuff, MAXPATHL))
Bram Moolenaar32526b32019-01-19 17:43:09 +010010257 msg_puts_attr((char *)NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010258 }
10259#endif
10260 if ((State & INSERT) && p_paste)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010261 msg_puts_attr(_(" (paste)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010262
Bram Moolenaar071d4272004-06-13 20:20:40 +000010263 if (VIsual_active)
10264 {
10265 char *p;
10266
10267 /* Don't concatenate separate words to avoid translation
10268 * problems. */
10269 switch ((VIsual_select ? 4 : 0)
10270 + (VIsual_mode == Ctrl_V) * 2
10271 + (VIsual_mode == 'V'))
10272 {
10273 case 0: p = N_(" VISUAL"); break;
10274 case 1: p = N_(" VISUAL LINE"); break;
10275 case 2: p = N_(" VISUAL BLOCK"); break;
10276 case 4: p = N_(" SELECT"); break;
10277 case 5: p = N_(" SELECT LINE"); break;
10278 default: p = N_(" SELECT BLOCK"); break;
10279 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010280 msg_puts_attr(_(p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010281 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010282 msg_puts_attr(" --", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010283 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010284
Bram Moolenaar071d4272004-06-13 20:20:40 +000010285 need_clear = TRUE;
10286 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010287 if (reg_recording != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010288#ifdef FEAT_INS_EXPAND
10289 && edit_submode == NULL /* otherwise it gets too long */
10290#endif
10291 )
10292 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010293 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010294 need_clear = TRUE;
10295 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010296
10297 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010298 if (need_clear || clear_cmdline)
10299 msg_clr_eos();
10300 msg_didout = FALSE; /* overwrite this message */
10301 length = msg_col;
10302 msg_col = 0;
10303 need_wait_return = nwr_save; /* never ask for hit-return for this */
10304 }
10305 else if (clear_cmdline && msg_silent == 0)
10306 /* Clear the whole command line. Will reset "clear_cmdline". */
10307 msg_clr_cmdline();
10308
10309#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010310 /* In Visual mode the size of the selected area must be redrawn. */
10311 if (VIsual_active)
10312 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010313
10314 /* If the last window has no status line, the ruler is after the mode
10315 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010316 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +020010317 win_redr_ruler(lastwin, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010318#endif
10319 redraw_cmdline = FALSE;
10320 clear_cmdline = FALSE;
10321
10322 return length;
10323}
10324
10325/*
10326 * Position for a mode message.
10327 */
10328 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010329msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010330{
10331 msg_col = 0;
10332 msg_row = Rows - 1;
10333}
10334
10335/*
10336 * Delete mode message. Used when ESC is typed which is expected to end
10337 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010338 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010339 */
10340 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010341unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010342{
10343 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010344 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010345 */
10346 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10347 redraw_cmdline = TRUE; /* delete mode later */
10348 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010349 clearmode();
10350}
10351
10352/*
10353 * Clear the mode message.
10354 */
10355 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010356clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010357{
Bram Moolenaar2abad542018-05-19 14:43:45 +020010358 int save_msg_row = msg_row;
10359 int save_msg_col = msg_col;
10360
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010361 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010362 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010363 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010364 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +020010365
10366 msg_col = save_msg_col;
10367 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010368}
10369
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010370 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010371recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010372{
Bram Moolenaar32526b32019-01-19 17:43:09 +010010373 msg_puts_attr(_("recording"), attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010374 if (!shortmess(SHM_RECORDING))
10375 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010376 char s[4];
10377
10378 sprintf(s, " @%c", reg_recording);
10379 msg_puts_attr(s, attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010380 }
10381}
10382
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010383/*
10384 * Draw the tab pages line at the top of the Vim window.
10385 */
Bram Moolenaare12bab32019-01-08 22:02:56 +010010386 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010387draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010388{
10389 int tabcount = 0;
10390 tabpage_T *tp;
10391 int tabwidth;
10392 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010393 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010394 int attr;
10395 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010396 win_T *cwp;
10397 int wincount;
10398 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010399 int c;
10400 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010401 int attr_sel = HL_ATTR(HLF_TPS);
10402 int attr_nosel = HL_ATTR(HLF_TP);
10403 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010404 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010405 int room;
10406 int use_sep_chars = (t_colors < 8
10407#ifdef FEAT_GUI
10408 && !gui.in_use
10409#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010410#ifdef FEAT_TERMGUICOLORS
10411 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010412#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010413 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010414
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010415 if (ScreenLines == NULL)
10416 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010417 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010418
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010419#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010420 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010421 if (gui_use_tabline())
10422 {
10423 gui_update_tabline();
10424 return;
10425 }
10426#endif
10427
10428 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010429 return;
10430
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010431#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010432
10433 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10434 for (scol = 0; scol < Columns; ++scol)
10435 TabPageIdxs[scol] = 0;
10436
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010437 /* Use the 'tabline' option if it's set. */
10438 if (*p_tal != NUL)
10439 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010440 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010441
10442 /* Check for an error. If there is one we would loop in redrawing the
10443 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010444 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010445 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010446 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010447 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010448 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010449 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010450 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010451 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010452#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010453 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010454 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010455 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010456
Bram Moolenaar238a5642006-02-21 22:12:05 +000010457 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10458 if (tabwidth < 6)
10459 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010460
Bram Moolenaar238a5642006-02-21 22:12:05 +000010461 attr = attr_nosel;
10462 tabcount = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010463 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10464 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010465 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010466 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010467
Bram Moolenaar238a5642006-02-21 22:12:05 +000010468 if (tp->tp_topframe == topframe)
10469 attr = attr_sel;
10470 if (use_sep_chars && col > 0)
10471 screen_putchar('|', 0, col++, attr);
10472
10473 if (tp->tp_topframe != topframe)
10474 attr = attr_nosel;
10475
10476 screen_putchar(' ', 0, col++, attr);
10477
10478 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010479 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010480 cwp = curwin;
10481 wp = firstwin;
10482 }
10483 else
10484 {
10485 cwp = tp->tp_curwin;
10486 wp = tp->tp_firstwin;
10487 }
10488
10489 modified = FALSE;
10490 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10491 if (bufIsChanged(wp->w_buffer))
10492 modified = TRUE;
10493 if (modified || wincount > 1)
10494 {
10495 if (wincount > 1)
10496 {
10497 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010498 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010499 if (col + len >= Columns - 3)
10500 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010501 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010502#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010503 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010504#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010505 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010506#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010507 );
10508 col += len;
10509 }
10510 if (modified)
10511 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10512 screen_putchar(' ', 0, col++, attr);
10513 }
10514
10515 room = scol - col + tabwidth - 1;
10516 if (room > 0)
10517 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010518 /* Get buffer name in NameBuff[] */
10519 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010520 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010521 len = vim_strsize(NameBuff);
10522 p = NameBuff;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010523 if (has_mbyte)
10524 while (len > room)
10525 {
10526 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010527 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010528 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010010529 else if (len > room)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010530 {
10531 p += len - room;
10532 len = room;
10533 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010534 if (len > Columns - col - 1)
10535 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010536
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010537 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010538 col += len;
10539 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010540 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010541
10542 /* Store the tab page number in TabPageIdxs[], so that
10543 * jump_to_mouse() knows where each one is. */
10544 ++tabcount;
10545 while (scol < col)
10546 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010547 }
10548
Bram Moolenaar238a5642006-02-21 22:12:05 +000010549 if (use_sep_chars)
10550 c = '_';
10551 else
10552 c = ' ';
10553 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010554
10555 /* Put an "X" for closing the current tab if there are several. */
10556 if (first_tabpage->tp_next != NULL)
10557 {
10558 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10559 TabPageIdxs[Columns - 1] = -999;
10560 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010561 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010562
10563 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10564 * set. */
10565 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010566}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010567
10568/*
10569 * Get buffer name for "buf" into NameBuff[].
10570 * Takes care of special buffer names and translates special characters.
10571 */
10572 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010573get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010574{
10575 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010576 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010577 else
10578 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10579 trans_characters(NameBuff, MAXPATHL);
10580}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010581
Bram Moolenaar071d4272004-06-13 20:20:40 +000010582/*
10583 * Get the character to use in a status line. Get its attributes in "*attr".
10584 */
10585 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010586fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010587{
10588 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010589
10590#ifdef FEAT_TERMINAL
10591 if (bt_terminal(wp->w_buffer))
10592 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010593 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010594 {
10595 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010596 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010597 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010598 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010599 {
10600 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010601 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010602 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010603 }
10604 else
10605#endif
10606 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010607 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010608 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010609 fill = fill_stl;
10610 }
10611 else
10612 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010613 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010614 fill = fill_stlnc;
10615 }
10616 /* Use fill when there is highlighting, and highlighting of current
10617 * window differs, or the fillchars differ, or this is not the
10618 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010619 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010620 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010621 || (fill_stl != fill_stlnc)))
10622 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010623 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010624 return '^';
10625 return '=';
10626}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010627
Bram Moolenaar071d4272004-06-13 20:20:40 +000010628/*
10629 * Get the character to use in a separator between vertically split windows.
10630 * Get its attributes in "*attr".
10631 */
10632 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010633fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010634{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010635 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010636 if (*attr == 0 && fill_vert == ' ')
10637 return '|';
10638 else
10639 return fill_vert;
10640}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010641
10642/*
10643 * Return TRUE if redrawing should currently be done.
10644 */
10645 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010646redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010647{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010648#ifdef FEAT_EVAL
10649 if (disable_redraw_for_testing)
10650 return 0;
10651 else
10652#endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +020010653 return ((!RedrawingDisabled
10654#ifdef FEAT_EVAL
10655 || ignore_redraw_flag_for_testing
10656#endif
10657 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010658}
10659
10660/*
10661 * Return TRUE if printing messages should currently be done.
10662 */
10663 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010664messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010665{
10666 return (!(p_lz && char_avail() && !KeyTyped));
10667}
10668
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010669#ifdef FEAT_MENU
10670/*
10671 * Draw the window toolbar.
10672 */
10673 static void
10674redraw_win_toolbar(win_T *wp)
10675{
10676 vimmenu_T *menu;
10677 int item_idx = 0;
10678 int item_count = 0;
10679 int col = 0;
10680 int next_col;
10681 int off = (int)(current_ScreenLine - ScreenLines);
10682 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
10683 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
10684
10685 vim_free(wp->w_winbar_items);
10686 for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
10687 ++item_count;
10688 wp->w_winbar_items = (winbar_item_T *)alloc_clear(
10689 (unsigned)sizeof(winbar_item_T) * (item_count + 1));
10690
10691 /* TODO: use fewer spaces if there is not enough room */
10692 for (menu = wp->w_winbar->children;
Bram Moolenaar02631462017-09-22 15:20:32 +020010693 menu != NULL && col < wp->w_width; menu = menu->next)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010694 {
10695 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010696 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010697 break;
10698 if (col > 1)
10699 {
10700 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010701 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010702 break;
10703 }
10704
10705 wp->w_winbar_items[item_idx].wb_startcol = col;
10706 space_to_screenline(off + col, button_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010707 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010708 break;
10709
10710 next_col = text_to_screenline(wp, menu->name, col);
10711 while (col < next_col)
10712 {
10713 ScreenAttrs[off + col] = button_attr;
10714 ++col;
10715 }
10716 wp->w_winbar_items[item_idx].wb_endcol = col;
10717 wp->w_winbar_items[item_idx].wb_menu = menu;
10718 ++item_idx;
10719
Bram Moolenaar02631462017-09-22 15:20:32 +020010720 if (col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010721 break;
10722 space_to_screenline(off + col, button_attr);
10723 ++col;
10724 }
Bram Moolenaar02631462017-09-22 15:20:32 +020010725 while (col < wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010726 {
10727 space_to_screenline(off + col, fill_attr);
10728 ++col;
10729 }
10730 wp->w_winbar_items[item_idx].wb_menu = NULL; /* end marker */
10731
Bram Moolenaar02631462017-09-22 15:20:32 +020010732 screen_line(wp->w_winrow, wp->w_wincol, (int)wp->w_width,
10733 (int)wp->w_width, FALSE);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010734}
10735#endif
Bram Moolenaar491ac282018-06-17 14:47:55 +020010736
Bram Moolenaar071d4272004-06-13 20:20:40 +000010737/*
10738 * Show current status info in ruler and various other places
10739 * If always is FALSE, only show ruler if position has changed.
10740 */
10741 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010742showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010743{
10744 if (!always && !redrawing())
10745 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010746#ifdef FEAT_INS_EXPAND
10747 if (pum_visible())
10748 {
10749 /* Don't redraw right now, do it later. */
10750 curwin->w_redr_status = TRUE;
10751 return;
10752 }
10753#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020010754#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010755 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010756 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010757 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010759 else
10760#endif
10761#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +020010762 win_redr_ruler(curwin, always, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010763#endif
10764
10765#ifdef FEAT_TITLE
10766 if (need_maketitle
10767# ifdef FEAT_STL_OPT
10768 || (p_icon && (stl_syntax & STL_IN_ICON))
10769 || (p_title && (stl_syntax & STL_IN_TITLE))
10770# endif
10771 )
10772 maketitle();
10773#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010774 /* Redraw the tab pages line if needed. */
10775 if (redraw_tabline)
10776 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010777}
10778
10779#ifdef FEAT_CMDL_INFO
10780 static void
Bram Moolenaar491ac282018-06-17 14:47:55 +020010781win_redr_ruler(win_T *wp, int always, int ignore_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010782{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010783#define RULER_BUF_LEN 70
10784 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010785 int row;
10786 int fillchar;
10787 int attr;
10788 int empty_line = FALSE;
10789 colnr_T virtcol;
10790 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010791 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010792 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010793 int this_ru_col;
10794 int off = 0;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +010010795 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010796
10797 /* If 'ruler' off or redrawing disabled, don't do anything */
10798 if (!p_ru)
10799 return;
10800
10801 /*
10802 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10803 * after deleting lines, before cursor.lnum is corrected.
10804 */
10805 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10806 return;
10807
10808#ifdef FEAT_INS_EXPAND
10809 /* Don't draw the ruler while doing insert-completion, it might overwrite
10810 * the (long) mode message. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010811 if (wp == lastwin && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010812 if (edit_submode != NULL)
10813 return;
Bram Moolenaar491ac282018-06-17 14:47:55 +020010814 // Don't draw the ruler when the popup menu is visible, it may overlap.
10815 // Except when the popup menu will be redrawn anyway.
10816 if (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010817 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010818#endif
10819
10820#ifdef FEAT_STL_OPT
10821 if (*p_ruf)
10822 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010823 int save_called_emsg = called_emsg;
10824
10825 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010826 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010827 if (called_emsg)
10828 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010829 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010830 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010831 return;
10832 }
10833#endif
10834
10835 /*
10836 * Check if not in Insert mode and the line is empty (will show "0-1").
10837 */
10838 if (!(State & INSERT)
10839 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10840 empty_line = TRUE;
10841
10842 /*
10843 * Only draw the ruler when something changed.
10844 */
10845 validate_virtcol_win(wp);
10846 if ( redraw_cmdline
10847 || always
10848 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10849 || wp->w_cursor.col != wp->w_ru_cursor.col
10850 || wp->w_virtcol != wp->w_ru_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +000010851 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
Bram Moolenaar071d4272004-06-13 20:20:40 +000010852 || wp->w_topline != wp->w_ru_topline
10853 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10854#ifdef FEAT_DIFF
10855 || wp->w_topfill != wp->w_ru_topfill
10856#endif
10857 || empty_line != wp->w_ru_empty)
10858 {
10859 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010860 if (wp->w_status_height)
10861 {
10862 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010863 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar53f81742017-09-22 14:35:51 +020010864 off = wp->w_wincol;
Bram Moolenaar02631462017-09-22 15:20:32 +020010865 width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010866 }
10867 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010868 {
10869 row = Rows - 1;
10870 fillchar = ' ';
10871 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010872 width = Columns;
10873 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010874 }
10875
10876 /* In list mode virtcol needs to be recomputed */
10877 virtcol = wp->w_virtcol;
10878 if (wp->w_p_list && lcs_tab1 == NUL)
10879 {
10880 wp->w_p_list = FALSE;
10881 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10882 wp->w_p_list = TRUE;
10883 }
10884
10885 /*
10886 * Some sprintfs return the length, some return a pointer.
10887 * To avoid portability problems we use strlen() here.
10888 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010889 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010890 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10891 ? 0L
10892 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010893 len = STRLEN(buffer);
10894 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010895 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10896 (int)virtcol + 1);
10897
10898 /*
10899 * Add a "50%" if there is room for it.
10900 * On the last line, don't print in the last column (scrolls the
10901 * screen up on some terminals).
10902 */
10903 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010904 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010905 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010906 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010907 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010908 this_ru_col = ru_col - (Columns - width);
10909 if (this_ru_col < 0)
10910 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010911 /* Never use more than half the window/screen width, leave the other
10912 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010913 if (this_ru_col < (width + 1) / 2)
10914 this_ru_col = (width + 1) / 2;
10915 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010916 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010010917 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010918 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010919 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010920 if (has_mbyte)
10921 i += (*mb_char2bytes)(fillchar, buffer + i);
10922 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010923 buffer[i++] = fillchar;
10924 ++o;
10925 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010926 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010927 }
10928 /* Truncate at window boundary. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010929 if (has_mbyte)
10930 {
10931 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010932 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010933 {
10934 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010935 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010936 {
10937 buffer[i] = NUL;
10938 break;
10939 }
10940 }
10941 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010010942 else if (this_ru_col + (int)STRLEN(buffer) > width)
Bram Moolenaar4033c552017-09-16 20:54:51 +020010943 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010944
Bram Moolenaar4033c552017-09-16 20:54:51 +020010945 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010946 i = redraw_cmdline;
10947 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020010948 this_ru_col + off + (int)STRLEN(buffer),
10949 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010950 fillchar, fillchar, attr);
10951 /* don't redraw the cmdline because of showing the ruler */
10952 redraw_cmdline = i;
10953 wp->w_ru_cursor = wp->w_cursor;
10954 wp->w_ru_virtcol = wp->w_virtcol;
10955 wp->w_ru_empty = empty_line;
10956 wp->w_ru_topline = wp->w_topline;
10957 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10958#ifdef FEAT_DIFF
10959 wp->w_ru_topfill = wp->w_topfill;
10960#endif
10961 }
10962}
10963#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010964
10965#if defined(FEAT_LINEBREAK) || defined(PROTO)
10966/*
Bram Moolenaar64486672010-05-16 15:46:46 +020010967 * Return the width of the 'number' and 'relativenumber' column.
10968 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010969 * Otherwise it depends on 'numberwidth' and the line count.
10970 */
10971 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010972number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010973{
10974 int n;
10975 linenr_T lnum;
10976
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020010977 if (wp->w_p_rnu && !wp->w_p_nu)
10978 /* cursor line shows "0" */
10979 lnum = wp->w_height;
10980 else
10981 /* cursor line shows absolute line number */
10982 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020010983
Bram Moolenaar6b314672015-03-20 15:42:10 +010010984 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010985 return wp->w_nrwidth_width;
10986 wp->w_nrwidth_line_count = lnum;
10987
10988 n = 0;
10989 do
10990 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010991 lnum /= 10;
10992 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010993 } while (lnum > 0);
10994
10995 /* 'numberwidth' gives the minimal width plus one */
10996 if (n < wp->w_p_nuw - 1)
10997 n = wp->w_p_nuw - 1;
10998
10999 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011000 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011001 return n;
11002}
11003#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011004
Bram Moolenaar113e1072019-01-20 15:30:40 +010011005#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011006/*
11007 * Return the current cursor column. This is the actual position on the
11008 * screen. First column is 0.
11009 */
11010 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011011screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011012{
11013 return screen_cur_col;
11014}
11015
11016/*
11017 * Return the current cursor row. This is the actual position on the screen.
11018 * First row is 0.
11019 */
11020 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011021screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011022{
11023 return screen_cur_row;
11024}
Bram Moolenaar113e1072019-01-20 15:30:40 +010011025#endif